Ejemplo n.º 1
0
        private void Render(object sender, EventArgs e)
        {
            PainterHelper.ClearScreen(bm);

            camera.vForward = Vec3D.VectorMul(camera.vLookDir, 0.05F);

            List <Triangle> trianglesToRaster = new List <Triangle>();

            Matrix matView = camera.RotateCamera();

            foreach (var tri in scene.triangles)
            {
                Triangle triProjected, triTransformed, triViewed;

                triViewed = new Triangle(3);

                triTransformed = Triangle.TransformTriangle(tri, transformations.matWorld);

                Vec3D vCameraRay = Vec3D.VectorSub(triTransformed.points[0], camera.vCamera);

                if (Vec3D.VectorDotProduct(triTransformed.Normal, vCameraRay) < 0.0F)
                {
                    float lightIntensity = light.GetLightIntensityForSurface(triTransformed.Normal);

                    Color color = PainterHelper.ChangeColorBrightness(Color.Black, lightIntensity);

                    triViewed = Triangle.TransformTriangle(triTransformed, matView);

                    triProjected = Triangle.ProjectTriangle(transformations.matProj, triViewed);

                    triProjected.InvertXAndY();

                    //// Scale into view
                    Vec3D vOffsetView = new Vec3D {
                        x = 1, y = 1, z = 0
                    };

                    triProjected.Offset(vOffsetView, bm);

                    triProjected.color = color;

                    triProjected.zTotal = (triProjected.points[0].z + triProjected.points[1].z + triProjected.points[2].z) / 3.0F;

                    trianglesToRaster.Add(triProjected);
                }
            }

            trianglesToRaster.Sort((p, q) => q.zTotal.CompareTo(p.zTotal));

            foreach (var triangle in trianglesToRaster)
            {
                PainterHelper.DrawTriangle(triangle, bm);

                pictureBoxCanvas.Image = bm;
            }
        }
Ejemplo n.º 2
0
        public void MoveCamera(Keys key)
        {
            if (key == Keys.Up)
            {
                vCamera.y += 0.05F;
            }

            if (key == Keys.Down)
            {
                vCamera.y -= 0.05F;
            }

            if (key == Keys.Left)
            {
                vCamera.x += 0.05F;
            }

            if (key == Keys.Right)
            {
                vCamera.x -= 0.05F;
            }

            if (key == Keys.W)
            {
                vCamera = Vec3D.VectorAdd(vCamera, vForward);
            }

            if (key == Keys.S)
            {
                vCamera = Vec3D.VectorSub(vCamera, vForward);
            }

            if (key == Keys.A)
            {
                fYaw -= 0.05F;
            }

            if (key == Keys.D)
            {
                fYaw += 0.05F;
            }
        }
Ejemplo n.º 3
0
        public static Matrix Matrix_PointAt(Vec3D pos, Vec3D target, Vec3D up)
        {
            Vec3D newForward = Vec3D.VectorSub(target, pos);

            newForward = Vec3D.VectorNormalise(newForward);

            Vec3D a     = Vec3D.VectorMul(newForward, Vec3D.VectorDotProduct(up, newForward));
            Vec3D newUp = Vec3D.VectorSub(up, a);

            newUp = Vec3D.VectorNormalise(newUp);

            Vec3D newRight = Vec3D.VectorCrossProduct(newUp, newForward);

            Matrix matrix = new Matrix(4);

            matrix.matrix[0, 0] = newRight.x; matrix.matrix[0, 1] = newRight.y; matrix.matrix[0, 2] = newRight.z; matrix.matrix[0, 3] = 0.0f;
            matrix.matrix[1, 0] = newUp.x; matrix.matrix[1, 1] = newUp.y; matrix.matrix[1, 2] = newUp.z; matrix.matrix[1, 3] = 0.0f;
            matrix.matrix[2, 0] = newForward.x; matrix.matrix[2, 1] = newForward.y; matrix.matrix[2, 2] = newForward.z; matrix.matrix[2, 3] = 0.0f;
            matrix.matrix[3, 0] = pos.x; matrix.matrix[3, 1] = pos.y; matrix.matrix[3, 2] = pos.z; matrix.matrix[3, 3] = 1.0f;

            return(matrix);
        }