Пример #1
0
        public Mat4(Vec4 row1, Vec4 row2, Vec4 row3, Vec4 row4)
        {
            data = new Vec4[4];

            data[0] = row1;
            data[1] = row2;
            data[2] = row3;
            data[3] = row4;
        }
Пример #2
0
        public Mat4()
        {
            data = new Vec4[4];

            data[0] = new Vec4(1.0f, 0.0f, 0.0f, 0.0f);
            data[1] = new Vec4(0.0f, 1.0f, 0.0f, 0.0f);
            data[2] = new Vec4(0.0f, 0.0f, 1.0f, 0.0f);
            data[3] = new Vec4(0.0f, 0.0f, 0.0f, 1.0f);
        }
Пример #3
0
        public static Mat4 ProjectionMatrix4(float angleOfView, float near, float far)
        {
            float scale = 1.0f / (float)(Math.Tan(angleOfView * 0.5f * (float)(Math.PI) / 180.0f));
            Vec4  row1  = new Vec4(scale, 0.0f, 0.0f, 0.0f);
            Vec4  row2  = new Vec4(0.0f, scale, 0.0f, 0.0f);
            Vec4  row3  = new Vec4(0.0f, 0.0f, -far / (far - near), -1.0f);
            Vec4  row4  = new Vec4(0.0f, 0.0f, -far * near / (far - near), 0.0f);

            return(new Mat4(row1, row2, row3, row4));
        }
Пример #4
0
        public static Mat4 RotationXMatrix(float angle)
        {
            float cosTheta = (float)Math.Cos(angle);
            float sinTheta = (float)Math.Sin(angle);

            Vec4 row1 = new Vec4(1.0f, 0.0f, 0.0f, 0.0f);
            Vec4 row2 = new Vec4(0.0f, cosTheta, -sinTheta, 0.0f);
            Vec4 row3 = new Vec4(0.0f, sinTheta, cosTheta, 0.0f);
            Vec4 row4 = new Vec4(0.0f, 0.0f, 0.0f, 1.0f);

            return(new Mat4(row1, row2, row3, row4));
        }
Пример #5
0
        public static Vec4 operator *(Mat4 m1, Vec4 v1)
        {
            Vec4 outRes = new Vec4();

            outRes.x = v1.x * m1.data[0].x + v1.y * m1.data[1].x + v1.z * m1.data[2].x + m1.data[3].x;
            outRes.y = v1.x * m1.data[0].y + v1.y * m1.data[1].y + v1.z * m1.data[2].y + m1.data[3].y;
            outRes.z = v1.x * m1.data[0].z + v1.y * m1.data[1].z + v1.z * m1.data[2].z + m1.data[3].z;
            float w = v1.x * m1.data[0].w + v1.y * m1.data[1].w + v1.z * m1.data[2].w + m1.data[3].w;

            if (w != 1.0f)
            {
                outRes.x /= w;
                outRes.y /= w;
                outRes.z /= w;
                outRes.w /= w;
            }

            return(outRes);
        }
Пример #6
0
        public static Mat4 operator *(Mat4 m1, Mat4 m2)
        {
            Vec4 ap1 = m1.data[0];
            Vec4 ap2 = m1.data[1];
            Vec4 ap3 = m1.data[2];
            Vec4 ap4 = m1.data[3];

            Vec4 bp1 = m2.data[0];
            Vec4 bp2 = m2.data[1];
            Vec4 bp3 = m2.data[2];
            Vec4 bp4 = m2.data[3];

            Vec4 cp1 = new Vec4();
            Vec4 cp2 = new Vec4();
            Vec4 cp3 = new Vec4();
            Vec4 cp4 = new Vec4();

            float a0, a1, a2, a3;

            a0 = ap1.x;
            a1 = ap1.y;
            a2 = ap1.z;
            a3 = ap1.w;

            cp1.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x;
            cp1.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y;
            cp1.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z;
            cp1.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w;

            a0 = ap2.x;
            a1 = ap2.y;
            a2 = ap2.z;
            a3 = ap2.w;

            cp2.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x;
            cp2.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y;
            cp2.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z;
            cp2.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w;

            a0 = ap3.x;
            a1 = ap3.y;
            a2 = ap3.z;
            a3 = ap3.w;

            cp3.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x;
            cp3.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y;
            cp3.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z;
            cp3.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w;

            a0 = ap4.x;
            a1 = ap4.y;
            a2 = ap4.z;
            a3 = ap4.w;

            cp4.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x;
            cp4.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y;
            cp4.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z;
            cp4.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w;

            return(new Mat4(cp1, cp2, cp3, cp4));
        }