public MainForm() { InitForm(); screen = new Bitmap(Defaults.WIDTH, Defaults.HEIGHT); renderer = new Renderer(screen, BackgroundColor); mesh = WaveObjHelper.ReadMeshFromFile(appPath + @"3dModels\bear.obj"); //mesh.RecalculateNormals(); Bitmap texture = Paloma.TargaImage.LoadTargaImage(appPath + @"3dModels\bear.tga"); SceneObject sObject = new SceneObject { mesh = mesh, material = new Material(texture, RenderType.RegularWithWireframe), uniformScale = 20f, rotation = new Float3(40, 150, 0), localPosition = new Float3(0, -210, 550) }; renderer.Render(sObject, viewDirection, lightDirection, true); }
public void Render(SceneObject sObject, Float3 viewDirection, Float3 lightDirection, bool useProjection = true) { Mesh mesh = sObject.mesh; Color wireFrameColor = Color.LightGreen; RenderType renderType = sObject.material.renderType; // Vertex uniforms // scale matrix Float3x3 S = Float3x3.identity * sObject.uniformScale; // rotation matrix Float3x3 R = Float3x3.getRotationMatrix(sObject.rotation); Float3x3 CombinedLinear = S * R; // translation Float4x4 Tr = Float4x4.identity; Tr.setTranslation(sObject.localPosition); // projection Float4x4 Pr = useProjection ? Float4x4.getProjectionMatrix(10f, 1300f, 1f, 1f) : Float4x4.identity; // BACK FACE CULLING if (backFaceCulling) { for (int i = mesh.Triangles.Count - 1; i >= 0; i--) { Triangle t = mesh.Triangles[i]; Float3 v1 = mesh.Vertices[t[0] - 1].position; Float3 v2 = mesh.Vertices[t[1] - 1].position; Float3 v3 = mesh.Vertices[t[2] - 1].position; Float3 normal = Utils.getTriangleNormalR(v1, v2, v3); // remove faced back triangles if (viewDirection.dot(normal) >= 0) mesh.Triangles.Remove(t); } } // VERTEX SHADER for (int i = 0; i < mesh.Vertices.Count; i++) { Vertex v = mesh.Vertices[i]; // scale var p = v.position.mul(S); // rotate p = p.mul(R); // translate p = Tr.transformPoint(p); // project if(useProjection) p = Pr.transformPoint(p); // TODO: Transforming normals while NON UNIFORM TRANSFORMS v.normal = v.normal.mul(R); // TODO: place to center of screen if(useProjection) v.position = new Float3(p.x * Defaults.WIDTH + Defaults.WIDTH / 2f, p.y * Defaults.HEIGHT + Defaults.HEIGHT / 2f, p.z); else v.position = new Float3(p.x + Defaults.WIDTH / 2f, p.y + Defaults.HEIGHT / 2f, p.z); } if((renderType & RenderType.Regular) != 0) RenderRegular(mesh, sObject.material, lightDirection); if ((renderType & RenderType.Wireframe) != 0) RenderWireframe(mesh, wireFrameColor); if ((renderType & RenderType.Normals) != 0) DrawVertexNormals(mesh, Color.Red); }