예제 #1
0
 public PointDrawingAlgorithm(
     Bitmap workingBitmap,
     ZBuffer zBuffer)
 {
     this.workingBitmap = workingBitmap;
     this.zBuffer       = zBuffer;
 }
예제 #2
0
        public void Render(Camera camera, Bitmap bmp, IEnumerable <LightSource> lightSources, params Mesh[] meshes)
        {
            workingBitmap = bmp;

            zBuffer = new ZBuffer(workingBitmap.Size.Width, workingBitmap.Size.Height);

            pointDrawingAlgorithm = new PointDrawingAlgorithm(workingBitmap, zBuffer);

            var viewMatrix       = MatrixPrefabs.LookAtLeftHanded(camera.Position, camera.Target, Vector3Prefabs.UnitY);
            var projectionMatrix = MatrixPrefabs.PerspectiveFieldOfViewRightHanded(
                0.78f,
                (float)workingBitmap.Size.Width / workingBitmap.Size.Height,
                0.01f,
                1.0f);

            foreach (Mesh mesh in meshes)
            {
                var worldMatrix = MatrixPrefabs.RotationMatrixYawPitchRoll(
                    mesh.Rotation.Y,
                    mesh.Rotation.X,
                    mesh.Rotation.Z) *
                                  MatrixPrefabs.TranslationMatrix(mesh.Position);

                var transformMatrix = worldMatrix * viewMatrix * projectionMatrix;

                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 vertexATransformed = Project(vertexA, transformMatrix, worldMatrix);
                    var vertexBTransformed = Project(vertexB, transformMatrix, worldMatrix);
                    var vertexCTransformed = Project(vertexC, transformMatrix, worldMatrix);

                    //DrawLine(pixelA, pixelB, vertexA.Color, vertexB.Color);
                    //DrawLine(pixelB, pixelC, vertexB.Color, vertexC.Color);
                    //DrawLine(pixelC, pixelA, vertexC.Color, vertexA.Color);

                    //DrawLineBresenham(pixelA, pixelB, vertexA.Color, vertexB.Color);
                    //DrawLineBresenham(pixelB, pixelC, vertexB.Color, vertexC.Color);
                    //DrawLineBresenham(pixelC, pixelA, vertexC.Color, vertexA.Color);

                    DrawTriangle(vertexATransformed, vertexBTransformed, vertexCTransformed, lightSources);
                }
            }
        }