Esempio n. 1
0
        public void RenderWireFrame(Camera camera, params Mesh[] meshes)
        {
            // To understand this part, please read the prerequisites resources
            Matrix4d viewMatrix = Matrix.LookAtLHd(camera.Position, camera.Target, Vector3d.UnitY);
            //Matrix4d projectionMatrix = Matrix.PerspectiveFovLHd(0.78f,
            //                                    (double)bmp.PixelWidth / (double)bmp.PixelHeight,
            //                                    0.01, 1.0);

            //test orthogonal view
            Matrix4d projectionMatrix;

            Matrix.OrthoOffCenterLHd(-2, 2, -2, 2, 0.01f, 1, out projectionMatrix);

            foreach (Mesh mesh in meshes)
            {
                // Beware to apply rotation before translation
                Vector3d pos         = mesh.Position;
                Matrix4d worldMatrix = Matrix4d.RotationYawPitchRoll(mesh.Rotation.Y, mesh.Rotation.X, mesh.Rotation.Z) *
                                       Matrix4d.CreateTranslation(pos.X, pos.Y, pos.Z);
                Matrix4d worldView       = worldMatrix * viewMatrix;
                Matrix4d transformMatrix = worldMatrix * viewMatrix * projectionMatrix;


                foreach (Face face in mesh.Faces)
                {
                    // Face-back culling
                    //var transformedNormal = Vector3.TransformNormals(face.Normal, worldView);

                    //if (transformedNormal.Z >= 0)
                    //{
                    //    continue;
                    //}

                    Vertex vertexA = mesh.Vertices[face.A];
                    Vertex vertexB = mesh.Vertices[face.B];
                    Vertex vertexC = mesh.Vertices[face.C];

                    Vertex pixelA = this.Project(vertexA, transformMatrix, worldMatrix);
                    Vertex pixelB = this.Project(vertexB, transformMatrix, worldMatrix);
                    Vertex pixelC = this.Project(vertexC, transformMatrix, worldMatrix);


                    DrawBline(pixelA.Coordinates, pixelB.Coordinates);
                    DrawBline(pixelB.Coordinates, pixelC.Coordinates);
                    DrawBline(pixelC.Coordinates, pixelA.Coordinates);

                    //float color = 1.0f;
                    //this.DrawTriangle(pixelA, pixelB, pixelC, new Color4(color, color, color, 1), mesh.Texture);
                }
            }
        }