public MainViewModel() { Current = this; Engine = new Engine(900,600); Cube = new Mesh("Cube", 8, 12); Tetra = new Mesh("Tetraedrum", 4, 4); Camera = new Camera(); CreateMeshes(); }
// The main method of the engine that re-compute each vertex projection // during each frame public void Render(Camera camera, Mesh mesh) { //First, we create the camera matrix. var viewMatrix = Matrix.LookAtLH(camera.Position, camera.Target, Vector3.UnitY); //Then, the projection matrix to add some perspective var projectionMatrix = Matrix.PerspectiveFovRH(0.78f, (float)this.Bitmap.PixelWidth / this.Bitmap.PixelHeight, 0.01f, 1.0f); //Then the worldmatrix. Beware to apply rotation before translation //var worldMatrix = Matrix.RotationYawPitchRoll(mesh.Rotation.Y, // mesh.Rotation.X, mesh.Rotation.Z) * // Matrix.Translation(mesh.Position); //Let's play with quaternions! var worldMatrix = QuaternionEngine.rotationMatrix(mesh.Rotation) * Matrix.Translation(mesh.Position); //Final transformMatrix, remember to multiply backwards var transformMatrix = worldMatrix * viewMatrix * projectionMatrix; // This is where magic happens: transfomed vertex are projected on the screen. First we identify each face, then transform its vertex and later we draw them //and the lines that join them. 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); var pixelB = Project(vertexB, transformMatrix); var pixelC = Project(vertexC, transformMatrix); DrawLine(pixelA, pixelB); DrawLine(pixelB, pixelC); DrawLine(pixelC, pixelA); } }