Example #1
0
 public static Vec3 Transform(Vec3 position, Mat4x4 matrix)
 {
     return(new Vec3(
                position.X * matrix.M11 + position.Y * matrix.M21 + position.Z * matrix.M31 + matrix.M41,
                position.X * matrix.M12 + position.Y * matrix.M22 + position.Z * matrix.M32 + matrix.M42,
                position.X * matrix.M13 + position.Y * matrix.M23 + position.Z * matrix.M33 + matrix.M43));
 }
Example #2
0
        public static Mat4x4 CalculateTransformRelativeToParent(this Kn5Node child, Kn5Node root)
        {
            var mat = new Mat4x4();

            if (!TraverseDown(root, Mat4x4.Identity, ref mat))
            {
                throw new Exception("Failed to traverse down");
            }
            return(mat);

            bool TraverseDown(Kn5Node node, Mat4x4 m, ref Mat4x4 r)
            {
                foreach (var c in node.Children)
                {
                    if (c == child)
                    {
                        r = m;
                        return(true);
                    }
                    if (c.NodeClass == Kn5NodeClass.Base)
                    {
                        if (TraverseDown(c, c.Transform * m, ref r))
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
        }
        public LEDTerrainModule(ModuleConfiguration moduleConfiguration) : base(moduleConfiguration, 2.5f)
        {
            perlin  = new Perlin(-1);
            matProj = Mat4x4.MakeProjection(45.0f, 1.5f, 0.1f, 1000.0f);

            // Set up "World Tranmsform" though not updating theta
            // makes this a bit redundant
            Mat4x4 matRotZ, matRotX;

            matRotZ = Mat4x4.MakeRotationZ(0f);
            matRotX = Mat4x4.MakeRotationX(1.1f);

            Mat4x4 matTrans = Mat4x4.MakeTranslation(0.0f, 0.0f, 5.0f);

            matWorld = Mat4x4.MakeIdentity(); // Form World Matrix
            matWorld = matRotZ * matRotX;     // Transform by rotation
            matWorld = matWorld * matTrans;   // Transform by translation

            // Create "Point At" Matrix for camera
            Vector3D vUp          = new Vector3D(0, 2, 0);
            Vector3D vTarget      = new Vector3D(0, 0, 1);
            Mat4x4   matCameraRot = Mat4x4.MakeRotationY(fYaw);

            vLookDir = matCameraRot * vTarget;
            vTarget  = vCamera + vLookDir;
            Mat4x4 matCamera = Mat4x4.PointAt(vCamera, vTarget, vUp);

            // Make view matrix from camera
            matView = matCamera.QuickInverse();
        }
Example #4
0
 public static Vec3 TransformNormal(Vec3 normal, Mat4x4 matrix)
 {
     return(new Vec3(
                normal.X * matrix.M11 + normal.Y * matrix.M21 + normal.Z * matrix.M31,
                normal.X * matrix.M12 + normal.Y * matrix.M22 + normal.Z * matrix.M32,
                normal.X * matrix.M13 + normal.Y * matrix.M23 + normal.Z * matrix.M33));
 }
Example #5
0
 public static Vec4 Transform(Vec2 position, Mat4x4 matrix)
 {
     return(new Vec4(
                position.X * matrix.M11 + position.Y * matrix.M21 + matrix.M41,
                position.X * matrix.M12 + position.Y * matrix.M22 + matrix.M42,
                position.X * matrix.M13 + position.Y * matrix.M23 + matrix.M43,
                position.X * matrix.M14 + position.Y * matrix.M24 + matrix.M44));
 }
Example #6
0
 public static Vec4 Transform(Vec4 vector, Mat4x4 matrix)
 {
     return(new Vec4(
                vector.X * matrix.M11 + vector.Y * matrix.M21 + vector.Z * matrix.M31 + vector.W * matrix.M41,
                vector.X * matrix.M12 + vector.Y * matrix.M22 + vector.Z * matrix.M32 + vector.W * matrix.M42,
                vector.X * matrix.M13 + vector.Y * matrix.M23 + vector.Z * matrix.M33 + vector.W * matrix.M43,
                vector.X * matrix.M14 + vector.Y * matrix.M24 + vector.Z * matrix.M34 + vector.W * matrix.M44));
 }
 private void Form1_Load(object sender, EventArgs e)
 {
     vaszon.BackColor   = Color.White;
     matVetulet         = new Mat4x4();
     matForgatasY       = new Mat4x4();
     matForgatasZOraEll = new Mat4x4();
     matForgatasX       = new Mat4x4();
     elokeszuletek();
 }
Example #8
0
        public static Plane Transform(Plane plane, Mat4x4 matrix)
        {
            Mat4x4.Invert(matrix, out var m);
            float x = plane.Normal.X, y = plane.Normal.Y, z = plane.Normal.Z, w = plane.D;

            return(new Plane(
                       x * m.M11 + y * m.M12 + z * m.M13 + w * m.M14,
                       x * m.M21 + y * m.M22 + z * m.M23 + w * m.M24,
                       x * m.M31 + y * m.M32 + z * m.M33 + w * m.M34,
                       x * m.M41 + y * m.M42 + z * m.M43 + w * m.M44));
        }
Example #9
0
        public static Kn5Node.Vertex Transform(this Kn5Node.Vertex v, Mat4x4 m, double offsetAlongNormal = 0d)
        {
            var posVec    = Vec3.Transform(v.Position, m);
            var normalVec = Vec3.TransformNormal(v.Normal, m);

            if (offsetAlongNormal != 0d)
            {
                posVec += Vec3.Normalize(normalVec) * (float)offsetAlongNormal;
            }
            v.Position = posVec;
            v.Normal   = normalVec;
            v.Tangent  = Vec3.TransformNormal(v.Tangent, m);
            return(v);
        }
Example #10
0
        void CreateMediaResources(FFmpegStreamDesc ffmpegStream)
        {
            Console.WriteLine($"Hardware Decoding : {ffmpegStream.IsHardwareDecoded}");
            Console.WriteLine($"FPS : {ffmpegStream.FramesPerSecond}");

            mediaFrameRect = new Vec4(0, 0, ffmpegStream.Width, ffmpegStream.Height);

            if (ffmpegStream.IsHardwareDecoded)
            {
                mediaFrameTexture = new D3DTexture2D(renderDevice,
                                                     ffmpegStream.Width,
                                                     ffmpegStream.Height,
                                                     DxgiFormat.NV12,
                                                     D3DBind.ShaderResource,
                                                     D3DUsage.Default,
                                                     D3DAccess.None,
                                                     D3DMisc.Shared);
            }
            else
            {
                mediaFrameTexture = new D3DTexture2D(renderDevice,
                                                     ffmpegStream.Width,
                                                     ffmpegStream.Height,
                                                     DxgiFormat.NV12,
                                                     D3DBind.ShaderResource,
                                                     D3DUsage.Dynamic,
                                                     D3DAccess.Write);
            }

            mediaFrameTextureYView  = new D3DShaderResourceView(renderDevice, mediaFrameTexture, DxgiFormat.R8_UNORM);
            mediaFrameTextureUVView = new D3DShaderResourceView(renderDevice, mediaFrameTexture, DxgiFormat.R8G8_UNORM);

            mediaTransform = Mat4x4.Ortho(0, mediaFrameRect.Right, mediaFrameRect.Bottom, 0);
            mediaVertices  = new MediaVertex[]
            {
                new MediaVertex(new Vec3(0, 0, 0), new Vec2(0, 0)),
                new MediaVertex(new Vec3(ffmpegStream.Width, 0, 0), new Vec2(1, 0)),
                new MediaVertex(new Vec3(0, ffmpegStream.Height, 0), new Vec2(0, 1)),

                new MediaVertex(new Vec3(ffmpegStream.Width, 0, 0), new Vec2(1, 0)),
                new MediaVertex(new Vec3(ffmpegStream.Width, ffmpegStream.Height, 0), new Vec2(1, 1)),
                new MediaVertex(new Vec3(0, ffmpegStream.Height, 0), new Vec2(0, 1)),
            };

            // update GPU resources
            var newMediaConstantBuffer = new MediaConstantBuffer(Mat4x4.Transpose(mediaTransform));

            renderDeviceContext.UpdateGPUResource(mediaConstantBuffer, newMediaConstantBuffer);
            renderDeviceContext.UpdateGPUResource(mediaVertexBuffer, mediaVertices, D3DMap.WriteDiscard);
        }
Example #11
0
        public static Quat CreateFromRotationMatrix(Mat4x4 matrix)
        {
            var trace = matrix.M11 + matrix.M22 + matrix.M33;

            var q = new Quat();

            if (trace > 0f)
            {
                var s = (float)Math.Sqrt(trace + 1f);
                q.W = s * 0.5f;
                s   = 0.5f / s;
                q.X = (matrix.M23 - matrix.M32) * s;
                q.Y = (matrix.M31 - matrix.M13) * s;
                q.Z = (matrix.M12 - matrix.M21) * s;
            }
            else
            {
                if (matrix.M11 >= matrix.M22 && matrix.M11 >= matrix.M33)
                {
                    var s    = (float)Math.Sqrt(1f + matrix.M11 - matrix.M22 - matrix.M33);
                    var invS = 0.5f / s;
                    q.X = 0.5f * s;
                    q.Y = (matrix.M12 + matrix.M21) * invS;
                    q.Z = (matrix.M13 + matrix.M31) * invS;
                    q.W = (matrix.M23 - matrix.M32) * invS;
                }
                else if (matrix.M22 > matrix.M33)
                {
                    var s    = (float)Math.Sqrt(1f + matrix.M22 - matrix.M11 - matrix.M33);
                    var invS = 0.5f / s;
                    q.X = (matrix.M21 + matrix.M12) * invS;
                    q.Y = 0.5f * s;
                    q.Z = (matrix.M32 + matrix.M23) * invS;
                    q.W = (matrix.M31 - matrix.M13) * invS;
                }
                else
                {
                    var s    = (float)Math.Sqrt(1f + matrix.M33 - matrix.M11 - matrix.M22);
                    var invS = 0.5f / s;
                    q.X = (matrix.M31 + matrix.M13) * invS;
                    q.Y = (matrix.M32 + matrix.M23) * invS;
                    q.Z = 0.5f * s;
                    q.W = (matrix.M12 - matrix.M21) * invS;
                }
            }

            return(q);
        }
Example #12
0
        public KsAnimEntryV1 ReadEntryV1()
        {
            var entry = new KsAnimEntryV1 {
                NodeName = ReadString()
            };

            var keyFramesCount = ReadInt32();
            var matrices       = new Mat4x4[keyFramesCount];

            for (var i = 0; i < keyFramesCount; i++)
            {
                matrices[i] = ReadMatrix();
            }

            entry.Matrices = matrices;
            return(entry);
        }
        Pont3D szorzasMatrixPonttal(Pont3D be, Mat4x4 m)
        {
            Pont3D ki = new Pont3D(0, 0, 0);

            ki.setX(be.getX() * m.M[0, 0] + be.getY() * m.M[1, 0] + be.getZ() * m.M[2, 0] + m.M[3, 0]);
            ki.setY(be.getX() * m.M[0, 1] + be.getY() * m.M[1, 1] + be.getZ() * m.M[2, 1] + m.M[3, 1]);
            ki.setZ(be.getX() * m.M[0, 2] + be.getY() * m.M[1, 2] + be.getZ() * m.M[2, 2] + m.M[3, 2]);
            float w = be.getX() * m.M[0, 3] + be.getY() * m.M[1, 3] + be.getZ() * m.M[2, 3] + m.M[3, 3];

            //if (w != 0)
            //{
            //    ki.setX(ki.getX() / w);
            //    ki.setY(ki.getY() / w);
            //    ki.setZ(ki.getZ() / w);
            //}

            return(ki);
        }
Example #14
0
 public void Write(Mat4x4 v)
 {
     Write(v.M11);
     Write(v.M12);
     Write(v.M13);
     Write(v.M14);
     Write(v.M21);
     Write(v.M22);
     Write(v.M23);
     Write(v.M24);
     Write(v.M31);
     Write(v.M32);
     Write(v.M33);
     Write(v.M34);
     Write(v.M41);
     Write(v.M42);
     Write(v.M43);
     Write(v.M44);
 }
Example #15
0
 private static bool IsFrameSame(Mat4x4 a, Mat4x4 b)
 {
     return(Math.Abs(a.M11 - b.M11) < 0.0001f &&
            Math.Abs(a.M12 - b.M12) < 0.0001f &&
            Math.Abs(a.M13 - b.M13) < 0.0001f &&
            Math.Abs(a.M14 - b.M14) < 0.0001f &&
            Math.Abs(a.M21 - b.M21) < 0.0001f &&
            Math.Abs(a.M22 - b.M22) < 0.0001f &&
            Math.Abs(a.M23 - b.M23) < 0.0001f &&
            Math.Abs(a.M24 - b.M24) < 0.0001f &&
            Math.Abs(a.M31 - b.M31) < 0.0001f &&
            Math.Abs(a.M32 - b.M32) < 0.0001f &&
            Math.Abs(a.M33 - b.M33) < 0.0001f &&
            Math.Abs(a.M34 - b.M34) < 0.0001f &&
            Math.Abs(a.M41 - b.M41) < 0.0001f &&
            Math.Abs(a.M42 - b.M42) < 0.0001f &&
            Math.Abs(a.M43 - b.M43) < 0.0001f &&
            Math.Abs(a.M44 - b.M44) < 0.0001f);
 }
Example #16
0
        private static Mat4x4[] ConvertFramesV1(Matrix[] matrices, int?fillLength)
        {
            var v = new Mat4x4[fillLength ?? matrices.Length];
            var l = Matrix.Identity.ToMat4x4();

            var i = 0;

            for (; i < matrices.Length; i++)
            {
                v[i] = l = matrices[i].ToMat4x4();
            }

            for (; i < v.Length; i++)
            {
                v[i] = l;
            }

            return(v);
        }
Example #17
0
        private static void MergeMeshes(Kn5Node root, List <Tuple <Kn5Node, double, Mat4x4> > children, CarLodGeneratorMergeRules mergeRules,
                                        CarLodGeneratorStageParams stage)
        {
            if (children.Count == 0)
            {
                return;
            }

            var mesh            = children[0].Item1;
            var priority        = children[0].Item2;
            var considerDetails = mesh.MaterialId != uint.MaxValue;
            var builder         = new Kn5MeshBuilder(considerDetails, considerDetails);
            // AcToolsLogging.Write($"Merging together: {children.Select(x => $"{x.Item1.Name} [{x.Item2}]").JoinToString(", ")}");

            var extraCounter = 0;

            foreach (var child in children)
            {
                var transform = child.Item3 * Mat4x4.CreateScale(new Vec3((float)priority)) * Mat4x4.CreateTranslation(MoveAsideDistance(priority, stage));
                var offset    = mergeRules.GetOffsetAlongNormal(child.Item1);
                for (var i = 0; i < child.Item1.Indices.Length; ++i)
                {
                    builder.AddVertex(child.Item1.Vertices[child.Item1.Indices[i]].Transform(transform, offset));
                    if (i % 3 == 2 && builder.IsCloseToLimit)
                    {
                        builder.SetTo(mesh);
                        root.Children.Add(mesh);
                        mesh.Tag = priority;

                        builder.Clear();
                        mesh = Kn5MeshUtils.Create(children[0].Item1.Name + $"___$extra:{extraCounter}", children[0].Item1.MaterialId);
                        ++extraCounter;
                    }
                }
            }

            if (builder.Count > 0)
            {
                builder.SetTo(mesh);
                root.Children.Add(mesh);
                mesh.Tag = priority;
            }
        }
Example #18
0
        protected override Image <Rgba32> Run()
        {
            fElapsedTime = stopwatch.ElapsedMilliseconds / 1000f;
            stopwatch.Restart();
            image           = new Image <Rgba32>(renderWidth, renderHeight);
            engine3D.Canvas = image;

            // Set up "World Tranmsform" though not updating theta
            // makes this a bit redundant
            Mat4x4 matRotZ, matRotX;

            fTheta += 1.0f * fElapsedTime;             // Uncomment to spin me right round baby right round
            matRotZ = Mat4x4.MakeRotationZ(fTheta * 0.5f);
            matRotX = Mat4x4.MakeRotationX(fTheta);

            Mat4x4 matTrans = Mat4x4.MakeTranslation(0.0f, 0.0f, 5.0f);

            Mat4x4 matWorld = Mat4x4.MakeIdentity();  // Form World Matrix

            matWorld = matRotZ * matRotX;             // Transform by rotation
            matWorld = matWorld * matTrans;           // Transform by translation

            // Create "Point At" Matrix for camera
            Vector3D vUp          = new Vector3D(0, 1, 0);
            Vector3D vTarget      = new Vector3D(0, 0, 1);
            Mat4x4   matCameraRot = Mat4x4.MakeRotationY(fYaw);

            vLookDir = matCameraRot * vTarget;
            vTarget  = vCamera + vLookDir;
            Mat4x4 matCamera = Mat4x4.PointAt(vCamera, vTarget, vUp);

            // Make view matrix from camera
            Mat4x4 matView = matCamera.QuickInverse();

            // Store triagles for rastering later
            // Store triagles for rastering later
            drawTriangles(meshCube, matWorld, matView, matProj, vCamera, light_direction, false);

            image.Mutate(c => c.Resize(LEDWidth, LEDHeight));
            return(image);
        }
Example #19
0
        public static Matrix ToMatrix(this Mat4x4 v)
        {
            Matrix ret;

            ret.M11 = v.M11;
            ret.M12 = v.M12;
            ret.M13 = v.M13;
            ret.M14 = v.M14;
            ret.M21 = v.M21;
            ret.M22 = v.M22;
            ret.M23 = v.M23;
            ret.M24 = v.M24;
            ret.M31 = v.M31;
            ret.M32 = v.M32;
            ret.M33 = v.M33;
            ret.M34 = v.M34;
            ret.M41 = v.M41;
            ret.M42 = v.M42;
            ret.M43 = v.M43;
            ret.M44 = v.M44;
            return(ret);
        }
Example #20
0
        public static string MatrixToCollada(Mat4x4 matrix)
        {
            var sb = new StringBuilder();

            sb.Append(matrix.M11);
            sb.Append(' ');
            sb.Append(matrix.M21);
            sb.Append(' ');
            sb.Append(matrix.M31);
            sb.Append(' ');
            sb.Append(matrix.M41);
            sb.Append(' ');
            sb.Append(matrix.M12);
            sb.Append(' ');
            sb.Append(matrix.M22);
            sb.Append(' ');
            sb.Append(matrix.M32);
            sb.Append(' ');
            sb.Append(matrix.M42);
            sb.Append(' ');
            sb.Append(matrix.M13);
            sb.Append(' ');
            sb.Append(matrix.M23);
            sb.Append(' ');
            sb.Append(matrix.M33);
            sb.Append(' ');
            sb.Append(matrix.M43);
            sb.Append(' ');
            sb.Append(matrix.M14);
            sb.Append(' ');
            sb.Append(matrix.M24);
            sb.Append(' ');
            sb.Append(matrix.M34);
            sb.Append(' ');
            sb.Append(matrix.M44);
            return(sb.ToString());
        }
        protected void drawTriangles(Mesh meshCube, Mat4x4 matWorld, Mat4x4 matView, Mat4x4 matProj, Vector3D vCamera, Vector3D light_direction, bool wire)
        {
            Stack <Triangle> vecTrianglesToRaster = new Stack <Triangle>();

            // Draw Triangles
            foreach (Triangle tri in meshCube.Tris)
            {
                // World Matrix Transform
                Triangle triTransformed = new Triangle(new List <Vector3D>()
                {
                    matWorld *tri.P[0],
                    matWorld *tri.P[1],
                    matWorld *tri.P[2],
                }, new List <Vector2D>()
                {
                    tri.T[0], tri.T[1], tri.T[2]
                });


                Triangle triProjected = new Triangle(new List <Vector3D>(), new List <Vector2D>());
                Triangle triViewed;

                // Calculate Triangle Normal
                Vector3D normal, line1, line2;

                // Get lines either side of Triangle
                line1 = triTransformed.P[1] - triTransformed.P[0];
                line2 = triTransformed.P[2] - triTransformed.P[0];

                // Take cross product of lines to get normal to Triangle surface
                normal = line1.CrossProduct(line2);

                // You normally need to normalise a normal!
                normal = normal.Normalise();

                // Get Ray from Triangle to camera
                Vector3D vCameraRay = triTransformed.P[0] - vCamera;

                // If ray is aligned with normal, then Triangle is visible
                if (normal * vCameraRay < 0.0f)
                {
                    // Illumination
                    light_direction = light_direction.Normalise();

                    // How "aligned" are light direction and Triangle surface normal?
                    float dp = Math.Max(0.1f, light_direction * normal);

                    // Choose console colours as required (much easier with RGB)
                    triTransformed.color = engine3D.GetColour(dp);

                    // Convert World Space --> View Space
                    triViewed = new Triangle(
                        new List <Vector3D>()
                    {
                        matView *triTransformed.P[0],
                        matView *triTransformed.P[1],
                        matView *triTransformed.P[2]
                    }, new List <Vector2D>()
                    {
                        triTransformed.T[0], triTransformed.T[1], triTransformed.T[2]
                    });

                    triViewed.color = triTransformed.color;

                    // Clip Viewed Triangle against near plane, this could form two additional
                    // additional Triangles.
                    int             nClippedTriangles = 0;
                    List <Triangle> clipped           = new List <Triangle>();

                    nClippedTriangles = engine3D.Triangle_ClipAgainstPlane(new Vector3D(0.0f, 0.0f, 0.1f),
                                                                           new Vector3D(0.0f, 0.0f, 1.0f), triViewed, ref clipped);

                    // We may end up with multiple Triangles form the clip, so project as
                    // required
                    for (int n = 0; n < nClippedTriangles; n++)
                    {
                        // Project Triangles from 3D --> 2D
                        triProjected.P.Add(matProj * clipped[n].P[0]);
                        triProjected.P.Add(matProj * clipped[n].P[1]);
                        triProjected.P.Add(matProj * clipped[n].P[2]);
                        triProjected.color = clipped[n].color;
                        triProjected.T.Add(clipped[n].T[0]);
                        triProjected.T.Add(clipped[n].T[1]);
                        triProjected.T.Add(clipped[n].T[2]);

                        triProjected.T.Add(new Vector2D(triProjected.T[0].X / triProjected.P[0].W,
                                                        triProjected.T[0].Y / triProjected.P[0].W, 1.0f / triProjected.P[0].W));
                        triProjected.T.Add(new Vector2D(triProjected.T[1].X / triProjected.P[1].W,
                                                        triProjected.T[1].Y / triProjected.P[1].W, 1.0f / triProjected.P[1].W));
                        triProjected.T.Add(new Vector2D(triProjected.T[2].X / triProjected.P[2].W,
                                                        triProjected.T[2].Y / triProjected.P[2].W, 1.0f / triProjected.P[2].W));

                        // Scale into view, we moved the normalising into cartesian space
                        // out of the matrix.vector function from the previous videos, so
                        // do this manually
                        triProjected.P[0] = triProjected.P[0] / triProjected.P[0].W;
                        triProjected.P[1] = triProjected.P[1] / triProjected.P[1].W;
                        triProjected.P[2] = triProjected.P[2] / triProjected.P[2].W;

                        var test0 = triProjected.P[0];
                        var test1 = triProjected.P[1];
                        var test2 = triProjected.P[2];

                        // X/Y are inverted so put them back
                        test0.vector.X *= -1.0f;
                        test1.vector.X *= -1.0f;
                        test2.vector.X *= -1.0f;
                        test0.vector.Y *= -1.0f;
                        test1.vector.Y *= -1.0f;
                        test2.vector.Y *= -1.0f;

                        // Offset verts into visible normalised space
                        Vector3D vOffsetView = new Vector3D(1, 1, 0);
                        test0          += vOffsetView;
                        test1          += vOffsetView;
                        test2          += vOffsetView;
                        test0.vector.X *= 0.5f * (float)renderWidth;
                        test0.vector.Y *= 0.5f * (float)renderHeight;
                        test1.vector.X *= 0.5f * (float)renderWidth;
                        test1.vector.Y *= 0.5f * (float)renderHeight;
                        test2.vector.X *= 0.5f * (float)renderWidth;
                        test2.vector.Y *= 0.5f * (float)renderHeight;

                        triProjected.P[0] = test0;
                        triProjected.P[1] = test1;
                        triProjected.P[2] = test2;

                        // Store Triangle for sorting
                        vecTrianglesToRaster.Push(triProjected);
                    }
                }
            }

            // Clear Screen
            SetBackgroundColor(image);

            Stack <Triangle> listTriangles = new Stack <Triangle>();

            // Loop through all transformed, viewed, projected, and sorted Triangles
            foreach (Triangle triToRaster in vecTrianglesToRaster)
            {
                // Clip Triangles against all four screen edges, this could yield
                // a bunch of Triangles, so create a queue that we traverse to
                //  ensure we only test new Triangles generated against planes

                // Add initial Triangle
                listTriangles.Push(triToRaster);
                int nNewTriangles = 1;

                for (int p = 0; p < 4; p++)
                {
                    int             nTrisToAdd = 0;
                    List <Triangle> clipped    = new List <Triangle>();
                    while (nNewTriangles > 0)
                    {
                        // Take Triangle from front of queue
                        Triangle test = listTriangles.Pop();
                        nNewTriangles--;

                        // Clip it against a plane. We only need to test each
                        // subsequent plane, against subsequent new Triangles
                        // as all Triangles after a plane clip are guaranteed
                        // to lie on the inside of the plane. I like how this
                        // comment is almost completely and utterly justified
                        switch (p)
                        {
                        case 0: nTrisToAdd = engine3D.Triangle_ClipAgainstPlane(new Vector3D(0.0f, 0.0f, 0.0f), new Vector3D(0.0f, 1.0f, 0.0f), test, ref clipped); break;

                        case 1: nTrisToAdd = engine3D.Triangle_ClipAgainstPlane(new Vector3D(0.0f, (float)renderHeight - 1, 0.0f), new Vector3D(0.0f, -1.0f, 0.0f), test, ref clipped); break;

                        case 2: nTrisToAdd = engine3D.Triangle_ClipAgainstPlane(new Vector3D(0.0f, 0.0f, 0.0f), new Vector3D(1.0f, 0.0f, 0.0f), test, ref clipped); break;

                        case 3: nTrisToAdd = engine3D.Triangle_ClipAgainstPlane(new Vector3D((float)renderWidth - 1, 0.0f, 0.0f), new Vector3D(-1.0f, 0.0f, 0.0f), test, ref clipped); break;
                        }

                        // Clipping may yield a variable number of Triangles, so
                        // add these new ones to the back of the queue for subsequent
                        // clipping against next planes
                        for (int w = 0; w < nTrisToAdd; w++)
                        {
                            listTriangles.Push(clipped[w]);
                        }
                    }
                    nNewTriangles = listTriangles.Count();
                }
            }

            // Draw the transformed, viewed, clipped, projected, sorted, clipped Triangles
            foreach (Triangle t in listTriangles)
            {
                if (!wire)
                {
                    engine3D.FillTriangle(t.P[0].vector.X, t.P[0].vector.Y, t.P[1].vector.X, t.P[1].vector.Y, t.P[2].vector.X, t.P[2].vector.Y, t.color);
                }
                else
                {
                    engine3D.DrawTriangle(t.P[0].vector.X, t.P[0].vector.Y, t.P[1].vector.X, t.P[1].vector.Y, t.P[2].vector.X, t.P[2].vector.Y, Color.White);
                }
            }
        }
Example #22
0
 public static TMatrix FromMat4(Mat4x4 mat4)
 {
     return(new TMatrix(mat4.m));
 }
Example #23
0
 public KnhEntry(string name, Mat4x4 transformation, KnhEntry[] children)
 {
     Name           = name;
     Transformation = transformation;
     Children       = children;
 }
Example #24
0
 public MediaConstantBuffer(Mat4x4 wvp)
 {
     WorldViewProjection = wvp;
 }
Example #25
0
        public LEDCubeModule(ModuleConfiguration moduleConfiguration) : base(moduleConfiguration, 3f)
        {
            meshCube.Tris = new List <Triangle>()
            {
                // SOUTH
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(0.0f, 0.0f, 0.0f, 1.0f), new Vector3D(0.0f, 1.0f, 0.0f, 1.0f), new Vector3D(1.0f, 1.0f, 0.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(0.0f, 0.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f)
                }),
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(0.0f, 0.0f, 0.0f, 1.0f), new Vector3D(1.0f, 1.0f, 0.0f, 1.0f), new Vector3D(1.0f, 0.0f, 0.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f), new Vector2D(1.0f, 1.0f, 1.0f)
                }),

                // EAST
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(1.0f, 0.0f, 0.0f, 1.0f), new Vector3D(1.0f, 1.0f, 0.0f, 1.0f), new Vector3D(1.0f, 1.0f, 1.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(0.0f, 0.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f)
                }),
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(1.0f, 0.0f, 0.0f, 1.0f), new Vector3D(1.0f, 1.0f, 1.0f, 1.0f), new Vector3D(1.0f, 0.0f, 1.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f), new Vector2D(1.0f, 1.0f, 1.0f)
                }),

                // NORTH
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(1.0f, 0.0f, 1.0f, 1.0f), new Vector3D(1.0f, 1.0f, 1.0f, 1.0f), new Vector3D(0.0f, 1.0f, 1.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(0.0f, 0.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f)
                }),
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(1.0f, 0.0f, 1.0f, 1.0f), new Vector3D(0.0f, 1.0f, 1.0f, 1.0f), new Vector3D(0.0f, 0.0f, 1.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f), new Vector2D(1.0f, 1.0f, 1.0f)
                }),

                // WEST
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(0.0f, 0.0f, 1.0f, 1.0f), new Vector3D(0.0f, 1.0f, 1.0f, 1.0f), new Vector3D(0.0f, 1.0f, 0.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(0.0f, 0.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f)
                }),
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(0.0f, 0.0f, 1.0f, 1.0f), new Vector3D(0.0f, 1.0f, 0.0f, 1.0f), new Vector3D(0.0f, 0.0f, 0.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f), new Vector2D(1.0f, 1.0f, 1.0f)
                }),

                // TOP
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(0.0f, 1.0f, 0.0f, 1.0f), new Vector3D(0.0f, 1.0f, 1.0f, 1.0f), new Vector3D(1.0f, 1.0f, 1.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(0.0f, 0.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f)
                }),
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(0.0f, 1.0f, 0.0f, 1.0f), new Vector3D(1.0f, 1.0f, 1.0f, 1.0f), new Vector3D(1.0f, 1.0f, 0.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f), new Vector2D(1.0f, 1.0f, 1.0f)
                }),

                // BOTTOM
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(1.0f, 0.0f, 1.0f, 1.0f), new Vector3D(0.0f, 0.0f, 1.0f, 1.0f), new Vector3D(0.0f, 0.0f, 0.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(0.0f, 0.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f)
                }),
                new Triangle(new List <Vector3D>()
                {
                    new Vector3D(1.0f, 0.0f, 1.0f, 1.0f), new Vector3D(0.0f, 0.0f, 0.0f, 1.0f), new Vector3D(1.0f, 0.0f, 0.0f, 1.0f)
                }, new List <Vector2D>()
                {
                    new Vector2D(0.0f, 1.0f, 1.0f), new Vector2D(1.0f, 0.0f, 1.0f), new Vector2D(1.0f, 1.0f, 1.0f)
                }),
            };

            matProj = Mat4x4.MakeProjection(90.0f, (float)renderHeight / (float)renderWidth, 0.1f, 1000.0f);
        }
Example #26
0
 public static Vec2 TransformNormal(Vec2 normal, Mat4x4 matrix)
 {
     return(new Vec2(
                normal.X * matrix.M11 + normal.Y * matrix.M21,
                normal.X * matrix.M12 + normal.Y * matrix.M22));
 }
Example #27
0
 public static Vec2 Transform(Vec2 position, Mat4x4 matrix)
 {
     return(new Vec2(
                position.X * matrix.M11 + position.Y * matrix.M21 + matrix.M41,
                position.X * matrix.M12 + position.Y * matrix.M22 + matrix.M42));
 }
Example #28
0
 public static TMatrix FromMat4(Mat4x4 mat4)
 {
     return new TMatrix(mat4.m);
 }