Ejemplo n.º 1
0
        public void Render(params Mesh[] meshes)
        {
            float FoV = 0.8f;
            float aspectRatio = (float)width / height;
            float nearPlane = 0.01f, farPlane = 1.0f;

            var viewMatrix       = SharpDX.Matrix.LookAtLH(camera.Position, camera.Target, Vector3.UnitY);
            var projectionMatrix = SharpDX.Matrix.PerspectiveFovLH(FoV, aspectRatio, nearPlane, farPlane);

            foreach (Mesh mesh in meshes)
            {
                var worldMatrix =
                    SharpDX.Matrix.Translation(mesh.Position) *
                    SharpDX.Matrix.RotationYawPitchRoll(mesh.Rotation.Y, mesh.Rotation.X, mesh.Rotation.Z);
                var worldView       = worldMatrix * viewMatrix;
                var transformMatrix = worldView * projectionMatrix;

                Parallel.For(0, mesh.Faces.Length, faceIndex =>
                {
                    // Face-back culling
                    //var transformedNormal = Vector3.TransformNormal(mesh.Faces[faceIndex].Normal, worldView);
                    //if (transformedNormal.Z > 0.0)
                    //{
                    //    return;
                    //}

                    var vertexA = mesh.Vertices[mesh.Faces[faceIndex].A];
                    var vertexB = mesh.Vertices[mesh.Faces[faceIndex].B];
                    var vertexC = mesh.Vertices[mesh.Faces[faceIndex].C];

                    var pixelA = Project(vertexA, transformMatrix, worldMatrix);
                    var pixelB = Project(vertexB, transformMatrix, worldMatrix);
                    var pixelC = Project(vertexC, transformMatrix, worldMatrix);

                    visualizer.RenderTriangle(pixelA, pixelB, pixelC, mesh.Color, mesh.Texture);
                });
            }
        }