void Init() { device = new Device(this.panel2.Width, this.panel2.Height); mesh = new Mesh("Cube", 8,12); mesh.Vertices[0] = new SharpDX.Vector3(-1, 1, 1); mesh.Vertices[1] = new SharpDX.Vector3(1, 1, 1); mesh.Vertices[2] = new SharpDX.Vector3(-1, -1, 1); mesh.Vertices[3] = new SharpDX.Vector3(1, -1, 1); mesh.Vertices[4] = new SharpDX.Vector3(-1, 1, -1); mesh.Vertices[5] = new SharpDX.Vector3(1, 1, -1); mesh.Vertices[6] = new SharpDX.Vector3(1, -1, -1); mesh.Vertices[7] = new SharpDX.Vector3(-1, -1, -1); mesh.Faces[0] = new Face { A = 0, B = 1, C = 2 }; mesh.Faces[1] = new Face { A = 1, B = 2, C = 3 }; mesh.Faces[2] = new Face { A = 1, B = 3, C = 6 }; mesh.Faces[3] = new Face { A = 1, B = 5, C = 6 }; mesh.Faces[4] = new Face { A = 0, B = 1, C = 4 }; mesh.Faces[5] = new Face { A = 1, B = 4, C = 5 }; mesh.Faces[6] = new Face { A = 2, B = 3, C = 7 }; mesh.Faces[7] = new Face { A = 3, B = 6, C = 7 }; mesh.Faces[8] = new Face { A = 0, B = 2, C = 7 }; mesh.Faces[9] = new Face { A = 0, B = 4, C = 7 }; mesh.Faces[10] = new Face { A = 4, B = 5, C = 6 }; mesh.Faces[11] = new Face { A = 4, B = 6, C = 7 }; camera = new Camera(); camera.Postion = new SharpDX.Vector3(0, 0, 10f); camera.Target = SharpDX.Vector3.Zero; }
// The main method of the engine that re-compute each vertex projection // during each frame public void Render(Camera camera, params Mesh[] meshes) { // To understand this part, please read the prerequisites resources var viewMatrix = Matrix.LookAtLH(camera.Position, camera.Target, Vector3.UnitY); var projectionMatrix = Matrix.PerspectiveFovRH(0.78f, (float)bmp.PixelWidth / bmp.PixelHeight, 0.01f, 1.0f); foreach (Mesh mesh in meshes) { // Beware to apply rotation before translation var worldMatrix = Matrix.RotationYawPitchRoll(mesh.Rotation.Y, mesh.Rotation.X, mesh.Rotation.Z) * Matrix.Translation(mesh.Position); var transformMatrix = worldMatrix * viewMatrix * projectionMatrix; /* foreach (var vertex in mesh.Vertices) { // First, we project the 3D coordinates into the 2D space var point = Project(vertex, transformMatrix); // Then we can draw on screen DrawPoint(point); for (var i = 0; i < mesh.Vertices.Length - 1; i++) { var point0 = Project(mesh.Vertices[i], transformMatrix); var point1 = Project(mesh.Vertices[i + 1], transformMatrix); DrawLine(point0, point1); } } */ var faceIndex = 0; foreach (var face in mesh.Faces) { var vertexA = mesh.Vertices[face.A]; var vertexB = mesh.Vertices[face.B]; var vertexC = mesh.Vertices[face.C]; var pixelA = Project(vertexA, transformMatrix, worldMatrix); var pixelB = Project(vertexB, transformMatrix, worldMatrix); var pixelC = Project(vertexC, transformMatrix, worldMatrix); var color = 0.25f + (faceIndex % mesh.Faces.Length) * 0.75f / mesh.Faces.Length; DrawTriangle(pixelA, pixelB, pixelC, new Color4(color, color, color, 1)); faceIndex++; } } }
public void Render(Camera camera,params Mesh[] meshes) { var viewMatrix = Matrix.LookAtLH(camera.Postion, camera.Target, Vector3.UnitY); var projectionMatrx = Matrix.PerspectiveFovLH(0.78f, (float)m_backBuffer.Width / m_backBuffer.Height, 0.01f, 1.0f); foreach(Mesh mesh in meshes) { var worldMatrix = Matrix.RotationYawPitchRoll(mesh.Rotation.Y, mesh.Rotation.X, mesh.Rotation.Z) * Matrix.Translation(mesh.Postion); var transMat = worldMatrix * viewMatrix * projectionMatrx; foreach(var face in mesh.Faces) { var vertexA = mesh.Vertices[face.A]; var vertexB = mesh.Vertices[face.B]; var vertexC = mesh.Vertices[face.C]; var pixelA = Project(vertexA, transMat); var pixelB = Project(vertexB, transMat); var pixelC = Project(vertexC, transMat); BresenhamDrawLine(pixelA, pixelB); BresenhamDrawLine(pixelB, pixelC); BresenhamDrawLine(pixelC, pixelA); } } }