示例#1
0
        public static Matrix PointAtMatrix(Vec3D pos, Vec3D target, Vec3D up)
        {
            // ny frammåt
            Vec3D newForward = target - pos;

            newForward.Normalize();

            // ny uppåt
            Vec3D a     = newForward * Vec3D.Dot(up, newForward);
            Vec3D newUp = up - a;

            newUp.Normalize();

            Vec3D  newRight = Vec3D.Cross(newUp, newForward);
            Matrix mat      = new Matrix();

            mat.m[0, 0] = newRight.x;       mat.m[0, 1] = newRight.y;       mat.m[0, 2] = newRight.z;       mat.m[0, 3] = 0.0f;
            mat.m[1, 0] = newUp.x;          mat.m[1, 1] = newUp.y;          mat.m[1, 2] = newUp.z;          mat.m[1, 3] = 0.0f;
            mat.m[2, 0] = newForward.x; mat.m[2, 1] = newForward.y; mat.m[2, 2] = newForward.z; mat.m[2, 3] = 0.0f;
            mat.m[3, 0] = pos.x;            mat.m[2, 1] = pos.y;            mat.m[2, 2] = pos.z;            mat.m[2, 3] = 1.0f;
            return(mat);
        }
示例#2
0
        public override void Update()
        {
            if (Engine.GetKeyDown(ConsoleKey.Y))
            {
                drawWireframe = !drawWireframe;
            }
            if (Engine.GetKeyDown(ConsoleKey.H))
            {
                drawSolid = !drawSolid;
            }

            if (Engine.GetMouseLeft())
            {
                Point delta = Engine.GetMousePos() - lastMousePos;
                yRot += delta.X / 80f;
                xRot += delta.Y / 80f;
            }

            lastMousePos = Engine.GetMousePos();

            // matriserar
            Matrix transformMat, rotationMat;

            rotationMat  = Matrix.RotationMatrixY(yRot);
            rotationMat *= Matrix.RotationMatrixX(xRot);
            transformMat = Matrix.Translation(modelPosition);

            // generera trianglar
            for (int i = 0; i < mesh.Triangles.Length; i++)
            {
                Triangle vertex  = mesh.Triangles[i];
                Triangle rotated = vertex.MatMul(rotationMat);

                Triangle transformed = rotated.MatMul(transformMat);

                // beräknar kryssprodukt av line1 och line2 för att hitta normal.
                Vec3D normal, line1, line2;
                line1 = transformed.p[1] - transformed.p[0];
                line2 = transformed.p[2] - transformed.p[0];

                normal = Vec3D.Cross(line1, line2);
                normal.Normalize();

                // testar ifall vi kan se ytan
                if (Vec3D.Dot(normal, transformed.p[0] - camera) < 0.0f)
                {
                    // beräknar ljus
                    float            l         = Vec3D.Dot(lightDirection, normal);
                    ConsoleCharacter character = ConsoleCharacter.Light;
                    if (l > 0.4)
                    {
                        character = ConsoleCharacter.Medium;
                    }
                    if (l > 0.7)
                    {
                        character = ConsoleCharacter.Dark;
                    }
                    if (l > 1)
                    {
                        character = ConsoleCharacter.Full;
                    }

                    // projekterar från 3D -> 2D
                    Triangle projected = new Triangle(null);
                    projected = transformed.MatMul(projectionMatrix);

                    // transformerar och skalar projektionen
                    Vec3D offsetView = new Vec3D(1, 1, 0);
                    projected.p[0] += offsetView;
                    projected.p[1] += offsetView;
                    projected.p[2] += offsetView;

                    projected.p[0].x *= 0.5f * consoleWidth; projected.p[0].y *= 0.5f * consoleHeight;
                    projected.p[1].x *= 0.5f * consoleWidth; projected.p[1].y *= 0.5f * consoleHeight;
                    projected.p[2].x *= 0.5f * consoleWidth; projected.p[2].y *= 0.5f * consoleHeight;

                    projected.c = character;
                    trianglesToRaster.Add(projected);
                }
            }

            // sortera
            trianglesToRaster.Sort((t1, t2) => ((t2.p[0].z + t2.p[1].z + t2.p[2].z).CompareTo((t1.p[0].z + t1.p[1].z + t1.p[2].z))));
        }