Example #1
0
 public void ApplyOrthographicCentered()
 {
     ViewMatrix = Matrix4.View(Position, LookAtPosition, UpPosition-Position);
     ProjectionMatrix = Matrix4.OrthographicCentered(ViewPort.Size.Width, ViewPort.Size.Height, Near, Far);
     TransformMatrix = ViewMatrix.Multiply(ProjectionMatrix);
     TransformInverseMatrix = TransformMatrix.Invert();
 }
Example #2
0
 public void Apply()
 {
     ViewMatrix = Matrix4.View(Position, LookAtPosition, UpPosition-Position);
     ProjectionMatrix = Matrix4.Perspective(Fov, float.IsNaN(Aspect) ? ViewPort.AspectRatio : Aspect, Near, Far);
     TransformMatrix = ViewMatrix.Multiply(ProjectionMatrix);
     TransformInverseMatrix = TransformMatrix.Invert();
 }
Example #3
0
 public void Set(Matrix4[] values, int count)
 {
     valueArrayOffset = 0;
     valueArrayCount = count;
     valueArrayObject.Target = values;
     Apply = setMatrix4Array;
 }
Example #4
0
 public void Set(Matrix4 value)
 {
     valueObject.Matrix4 = value;
     Apply = setMatrix4;
 }
Example #5
0
 public Vector3 Transform(Matrix4 matrix)
 {
     return new Vector3
     (
         (X * matrix.X.X) + (Y * matrix.Y.X) + (Z * matrix.Z.X) + matrix.X.W,
         (X * matrix.X.Y) + (Y * matrix.Y.Y) + (Z * matrix.Z.Y) + matrix.Y.W,
         (X * matrix.X.Z) + (Y * matrix.Y.Z) + (Z * matrix.Z.Z) + matrix.Z.W
     );
 }
Example #6
0
 public static void FromMatrix4(ref Matrix4 matrix, out Quaternion result)
 {
     float w = (float)Math.Sqrt(1 + matrix.X.X + matrix.Y.Y + matrix.Z.Z) * .5f;
     float delta = 1 / (w * 4);
     result.X = (matrix.Z.Y - matrix.Y.Z) * delta;
     result.Y = (matrix.X.Z - matrix.Z.X) * delta;
     result.Z = (matrix.Y.X - matrix.X.Y) * delta;
     result.W = w;
 }
Example #7
0
 public Matrix4 Multiply(Matrix4 matrix)
 {
     return new Matrix4
     (
         new Vector4((matrix.X.X*X.X) + (matrix.X.Y*Y.X) + (matrix.X.Z*Z.X) + (matrix.X.W*W.X), (matrix.X.X*X.Y) + (matrix.X.Y*Y.Y) + (matrix.X.Z*Z.Y) + (matrix.X.W*W.Y), (matrix.X.X*X.Z) + (matrix.X.Y*Y.Z) + (matrix.X.Z*Z.Z) + (matrix.X.W*W.Z), (matrix.X.X*X.W) + (matrix.X.Y*Y.W) + (matrix.X.Z*Z.W) + (matrix.X.W*W.W)),
         new Vector4((matrix.Y.X*X.X) + (matrix.Y.Y*Y.X) + (matrix.Y.Z*Z.X) + (matrix.Y.W*W.X), (matrix.Y.X*X.Y) + (matrix.Y.Y*Y.Y) + (matrix.Y.Z*Z.Y) + (matrix.Y.W*W.Y), (matrix.Y.X*X.Z) + (matrix.Y.Y*Y.Z) + (matrix.Y.Z*Z.Z) + (matrix.Y.W*W.Z), (matrix.Y.X*X.W) + (matrix.Y.Y*Y.W) + (matrix.Y.Z*Z.W) + (matrix.Y.W*W.W)),
         new Vector4((matrix.Z.X*X.X) + (matrix.Z.Y*Y.X) + (matrix.Z.Z*Z.X) + (matrix.Z.W*W.X), (matrix.Z.X*X.Y) + (matrix.Z.Y*Y.Y) + (matrix.Z.Z*Z.Y) + (matrix.Z.W*W.Y), (matrix.Z.X*X.Z) + (matrix.Z.Y*Y.Z) + (matrix.Z.Z*Z.Z) + (matrix.Z.W*W.Z), (matrix.Z.X*X.W) + (matrix.Z.Y*Y.W) + (matrix.Z.Z*Z.W) + (matrix.Z.W*W.W)),
         new Vector4((matrix.W.X*X.X) + (matrix.W.Y*Y.X) + (matrix.W.Z*Z.X) + (matrix.W.W*W.X), (matrix.W.X*X.Y) + (matrix.W.Y*Y.Y) + (matrix.W.Z*Z.Y) + (matrix.W.W*W.Y), (matrix.W.X*X.Z) + (matrix.W.Y*Y.Z) + (matrix.W.Z*Z.Z) + (matrix.W.W*W.Z), (matrix.W.X*X.W) + (matrix.W.Y*Y.W) + (matrix.W.Z*Z.W) + (matrix.W.W*W.W))
     );
 }
Example #8
0
        public static void View(ref Vector3 position, ref Vector3 lookAt, ref Vector3 upVector, out Matrix4 result)
        {
            var forward = (lookAt - position).Normalize();
            var xVec = forward.Cross(upVector).Normalize();
            upVector = xVec.Cross(forward);

            result.X.X = xVec.X;
            result.X.Y = xVec.Y;
            result.X.Z = xVec.Z;
            result.X.W = position.Dot(-xVec);

            result.Y.X = upVector.X;
            result.Y.Y = upVector.Y;
            result.Y.Z = upVector.Z;
            result.Y.W = position.Dot(-upVector);

            result.Z.X = -forward.X;
            result.Z.Y = -forward.Y;
            result.Z.Z = -forward.Z;
            result.Z.W = position.Dot(forward);

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = 0;
            result.W.W = 1;
        }
Example #9
0
        public static void Frustum(float left, float right, float bottom, float top, float near, float far, out Matrix4 result)
        {
            float width = right - left;
            float height = top - bottom;
            float depth = far - near;
            float n = near * 2;

            result.X.X = n/width;
            result.X.Y = 0;
            result.X.Z = (right+left)/width;
            result.X.W = 0;

            result.Y.X = 0;
            result.Y.Y = n/height;
            result.Y.Z = (top+bottom)/height;
            result.Y.W = 0;

            result.Z.X = 0;
            result.Z.Y = 0;
            result.Z.Z = -(far+near)/depth;
            result.Z.W = -(n*far)/depth;

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = -1;
            result.W.W = 0;
        }
Example #10
0
 public static void FromRotationAxis(ref Vector3 axis, float angle, out Matrix4 result)
 {
     Core.Quaternion quaternion;
     Core.Quaternion.FromRotationAxis(ref axis, angle, out quaternion);
     Matrix4.FromQuaternion(ref quaternion, out result);
 }
Example #11
0
 public static void FromRigidTransform(ref RigidTransform3 transform, out Matrix4 result)
 {
     var qMat = Matrix3.FromQuaternion(transform.Orientation);
     Core.Matrix4.FromAffineTransform(ref qMat, ref transform.Position, out result);
 }
Example #12
0
        public static void FromQuaternion(ref Quaternion quaternion, out Matrix4 result)
        {
            var squared = new Vector4(quaternion.X*quaternion.X, quaternion.Y*quaternion.Y, quaternion.Z*quaternion.Z, quaternion.W*quaternion.W);
            float invSqLength = 1 / (squared.X + squared.Y + squared.Z + squared.W);

            float temp1 = quaternion.X * quaternion.Y;
            float temp2 = quaternion.Z * quaternion.W;
            float temp3 = quaternion.X * quaternion.Z;
            float temp4 = quaternion.Y * quaternion.W;
            float temp5 = quaternion.Y * quaternion.Z;
            float temp6 = quaternion.X * quaternion.W;

            result.X.X = (squared.X-squared.Y-squared.Z+squared.W) * invSqLength;
            result.X.Y = 2*(temp1-temp2) * invSqLength;
            result.X.Z = 2*(temp3+temp4) * invSqLength;
            result.X.W = 0;

            result.Y.X = 2*(temp1+temp2) * invSqLength;
            result.Y.Y = (-squared.X+squared.Y-squared.Z+squared.W) * invSqLength;
            result.Y.Z = 2*(temp5-temp6) * invSqLength;
            result.Y.W = 0;

            result.Z.X = 2*(temp3-temp4) * invSqLength;
            result.Z.Y = 2*(temp5+temp6) * invSqLength;
            result.Z.Z = (-squared.X-squared.Y+squared.Z+squared.W) * invSqLength;
            result.Z.W = 0;

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = 0;
            result.W.W = 1;
        }
Example #13
0
        public static void FromMatrix3(ref Matrix3 matrix, out Matrix4 result)
        {
            result.X.X = matrix.X.X;
            result.X.Y = matrix.X.Y;
            result.X.Z = matrix.X.Z;
            result.X.W = 0;

            result.Y.X = matrix.Y.X;
            result.Y.Y = matrix.Y.Y;
            result.Y.Z = matrix.Y.Z;
            result.Y.W = 0;

            result.Z.X = matrix.Z.X;
            result.Z.Y = matrix.Z.Y;
            result.Z.Z = matrix.Z.Z;
            result.Z.W = 0;

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = 0;
            result.W.W = 1;
        }
Example #14
0
        public static void FromAffineTransform(ref Matrix3 transform, ref Vector3 scale, ref Vector3 position, out Matrix4 result)
        {
            result.X.X = transform.X.X * scale.X;
            result.X.Y = transform.X.Y * scale.X;
            result.X.Z = transform.X.Z * scale.X;
            result.X.W = position.X;

            result.Y.X = transform.Y.X * scale.Y;
            result.Y.Y = transform.Y.Y * scale.Y;
            result.Y.Z = transform.Y.Z * scale.Y;
            result.Y.W = position.Y;

            result.Z.X = transform.Z.X * scale.Z;
            result.Z.Y = transform.Z.Y * scale.Z;
            result.Z.Z = transform.Z.Z * scale.Z;
            result.Z.W = position.Z;

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = 0;
            result.W.W = 1;
        }
Example #15
0
        public static void FromAffineTransform(ref AffineTransform3 transform, out Matrix4 result)
        {
            result.X.X = transform.Transform.X.X;
            result.X.Y = transform.Transform.X.Y;
            result.X.Z = transform.Transform.X.Z;
            result.X.W = transform.Translation.X;

            result.Y.X = transform.Transform.Y.X;
            result.Y.Y = transform.Transform.Y.Y;
            result.Y.Z = transform.Transform.Y.Z;
            result.Y.W = transform.Translation.Y;

            result.Z.X = transform.Transform.Z.X;
            result.Z.Y = transform.Transform.Z.Y;
            result.Z.Z = transform.Transform.Z.Z;
            result.Z.W = transform.Translation.Z;

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = 0;
            result.W.W = 1;
        }
Example #16
0
        public static void Perspective(float fov, float aspect, float near, float far, out Matrix4 result)
        {
            float top = near * (float)Math.Tan(fov * .5f);
            float bottom = -top;
            float right = top * aspect;
            float left = -right;

            Frustum(left, right, bottom, top, near, far, out result);
        }
Example #17
0
        public static void Transpose(Matrix4 matrix, out Matrix4 result)
        {
            result.X.X = matrix.X.X;
            result.X.Y = matrix.Y.X;
            result.X.Z = matrix.Z.X;
            result.X.W = matrix.W.X;

            result.Y.X = matrix.X.Y;
            result.Y.Y = matrix.Y.Y;
            result.Y.Z = matrix.Z.Y;
            result.Y.W = matrix.W.Y;

            result.Z.X = matrix.X.Z;
            result.Z.Y = matrix.Y.Z;
            result.Z.Z = matrix.Z.Z;
            result.Z.W = matrix.W.Z;

            result.W.X = matrix.X.W;
            result.W.Y = matrix.Y.W;
            result.W.Z = matrix.Z.W;
            result.W.W = matrix.W.W;
        }
Example #18
0
 public static void GetTranslation(ref Matrix4 matrix, out Vector3 result)
 {
     result.X = matrix.X.W;
     result.Y = matrix.Y.W;
     result.Z = matrix.Z.W;
 }
Example #19
0
        public Matrix4 Invert()
        {
            float determinant = 1 / Determinant();

            var mat = new Matrix4
            (
                new Vector4
                (
                    ((Y.Y * Z.Z * W.W) + (Y.Z * Z.W * W.Y) + (Y.W * Z.Y * W.Z) - (Y.Y * Z.W * W.Z) - (Y.Z * Z.Y * W.W) - (Y.W * Z.Z * W.Y)) * determinant,
                    ((X.Y * Z.W * W.Z) + (X.Z * Z.Y * W.W) + (X.W * Z.Z * W.Y) - (X.Y * Z.Z * W.W) - (X.Z * Z.W * W.Y) - (X.W * Z.Y * W.Z)) * determinant,
                    ((X.Y * Y.Z * W.W) + (X.Z * Y.W * W.Y) + (X.W * Y.Y * W.Z) - (X.Y * Y.W * W.Z) - (X.Z * Y.Y * W.W) - (X.W * Y.Z * W.Y)) * determinant,
                    ((X.Y * Y.W * Z.Z) + (X.Z * Y.Y * Z.W) + (X.W * Y.Z * Z.Y) - (X.Y * Y.Z * Z.W) - (X.Z * Y.W * Z.Y) - (X.W * Y.Y * Z.Z)) * determinant
                ),
                new Vector4
                (
                    ((Y.X * Z.W * W.Z) + (Y.Z * Z.X * W.W) + (Y.W * Z.Z * W.X) - (Y.X * Z.Z * W.W) - (Y.Z * Z.W * W.X) - (Y.W * Z.X * W.Z)) * determinant,
                    ((X.X * Z.Z * W.W) + (X.Z * Z.W * W.X) + (X.W * Z.X * W.Z) - (X.X * Z.W * W.Z) - (X.Z * Z.X * W.W) - (X.W * Z.Z * W.X)) * determinant,
                    ((X.X * Y.W * W.Z) + (X.Z * Y.X * W.W) + (X.W * Y.Z * W.X) - (X.X * Y.Z * W.W) - (X.Z * Y.W * W.X) - (X.W * Y.X * W.Z)) * determinant,
                    ((X.X * Y.Z * Z.W) + (X.Z * Y.W * Z.X) + (X.W * Y.X * Z.Z) - (X.X * Y.W * Z.Z) - (X.Z * Y.X * Z.W) - (X.W * Y.Z * Z.X)) * determinant
                ),
                new Vector4
                (
                    ((Y.X * Z.Y * W.W) + (Y.Y * Z.W * W.X) + (Y.W * Z.X * W.Y) - (Y.X * Z.W * W.Y) - (Y.Y * Z.X * W.W) - (Y.W * Z.Y * W.X)) * determinant,
                    ((X.X * Z.W * W.Y) + (X.Y * Z.X * W.W) + (X.W * Z.Y * W.X) - (X.X * Z.Y * W.W) - (X.Y * Z.W * W.X) - (X.W * Z.X * W.Y)) * determinant,
                    ((X.X * Y.Y * W.W) + (X.Y * Y.W * W.X) + (X.W * Y.X * W.Y) - (X.X * Y.W * W.Y) - (X.Y * Y.X * W.W) - (X.W * Y.Y * W.X)) * determinant,
                    ((X.X * Y.W * Z.Y) + (X.Y * Y.X * Z.W) + (X.W * Y.Y * Z.X) - (X.X * Y.Y * Z.W) - (X.Y * Y.W * Z.X) - (X.W * Y.X * Z.Y)) * determinant
                ),
                new Vector4
                (
                    ((Y.X * Z.Z * W.Y) + (Y.Y * Z.X * W.Z) + (Y.Z * Z.Y * W.X) - (Y.X * Z.Y * W.Z) - (Y.Y * Z.Z * W.X) - (Y.Z * Z.X * W.Y)) * determinant,
                    ((X.X * Z.Y * W.Z) + (X.Y * Z.Z * W.X) + (X.Z * Z.X * W.Y) - (X.X * Z.Z * W.Y) - (X.Y * Z.X * W.Z) - (X.Z * Z.Y * W.X)) * determinant,
                    ((X.X * Y.Z * W.Y) + (X.Y * Y.X * W.Z) + (X.Z * Y.Y * W.X) - (X.X * Y.Y * W.Z) - (X.Y * Y.Z * W.X) - (X.Z * Y.X * W.Y)) * determinant,
                    ((X.X * Y.Y * Z.Z) + (X.Y * Y.Z * Z.X) + (X.Z * Y.X * Z.Y) - (X.X * Y.Z * Z.Y) - (X.Y * Y.X * Z.Z) - (X.Z * Y.Y * Z.X)) * determinant
                )
            );

            Transpose(mat, out mat);
            return mat;
        }
Example #20
0
        public static void Invert(ref Matrix4 matrix, out Matrix4 result)
        {
            float determinant = 1 / matrix.Determinant();

            result.X.X = ((matrix.Y.Y * matrix.Z.Z * matrix.W.W) + (matrix.Y.Z * matrix.Z.W * matrix.W.Y) + (matrix.Y.W * matrix.Z.Y * matrix.W.Z) - (matrix.Y.Y * matrix.Z.W * matrix.W.Z) - (matrix.Y.Z * matrix.Z.Y * matrix.W.W) - (matrix.Y.W * matrix.Z.Z * matrix.W.Y)) * determinant;
            result.X.Y = ((matrix.X.Y * matrix.Z.W * matrix.W.Z) + (matrix.X.Z * matrix.Z.Y * matrix.W.W) + (matrix.X.W * matrix.Z.Z * matrix.W.Y) - (matrix.X.Y * matrix.Z.Z * matrix.W.W) - (matrix.X.Z * matrix.Z.W * matrix.W.Y) - (matrix.X.W * matrix.Z.Y * matrix.W.Z)) * determinant;
            result.X.Z = ((matrix.X.Y * matrix.Y.Z * matrix.W.W) + (matrix.X.Z * matrix.Y.W * matrix.W.Y) + (matrix.X.W * matrix.Y.Y * matrix.W.Z) - (matrix.X.Y * matrix.Y.W * matrix.W.Z) - (matrix.X.Z * matrix.Y.Y * matrix.W.W) - (matrix.X.W * matrix.Y.Z * matrix.W.Y)) * determinant;
            result.X.W = ((matrix.X.Y * matrix.Y.W * matrix.Z.Z) + (matrix.X.Z * matrix.Y.Y * matrix.Z.W) + (matrix.X.W * matrix.Y.Z * matrix.Z.Y) - (matrix.X.Y * matrix.Y.Z * matrix.Z.W) - (matrix.X.Z * matrix.Y.W * matrix.Z.Y) - (matrix.X.W * matrix.Y.Y * matrix.Z.Z)) * determinant;

            result.Y.X = ((matrix.Y.X * matrix.Z.W * matrix.W.Z) + (matrix.Y.Z * matrix.Z.X * matrix.W.W) + (matrix.Y.W * matrix.Z.Z * matrix.W.X) - (matrix.Y.X * matrix.Z.Z * matrix.W.W) - (matrix.Y.Z * matrix.Z.W * matrix.W.X) - (matrix.Y.W * matrix.Z.X * matrix.W.Z)) * determinant;
            result.Y.Y = ((matrix.X.X * matrix.Z.Z * matrix.W.W) + (matrix.X.Z * matrix.Z.W * matrix.W.X) + (matrix.X.W * matrix.Z.X * matrix.W.Z) - (matrix.X.X * matrix.Z.W * matrix.W.Z) - (matrix.X.Z * matrix.Z.X * matrix.W.W) - (matrix.X.W * matrix.Z.Z * matrix.W.X)) * determinant;
            result.Y.Z = ((matrix.X.X * matrix.Y.W * matrix.W.Z) + (matrix.X.Z * matrix.Y.X * matrix.W.W) + (matrix.X.W * matrix.Y.Z * matrix.W.X) - (matrix.X.X * matrix.Y.Z * matrix.W.W) - (matrix.X.Z * matrix.Y.W * matrix.W.X) - (matrix.X.W * matrix.Y.X * matrix.W.Z)) * determinant;
            result.Y.W = ((matrix.X.X * matrix.Y.Z * matrix.Z.W) + (matrix.X.Z * matrix.Y.W * matrix.Z.X) + (matrix.X.W * matrix.Y.X * matrix.Z.Z) - (matrix.X.X * matrix.Y.W * matrix.Z.Z) - (matrix.X.Z * matrix.Y.X * matrix.Z.W) - (matrix.X.W * matrix.Y.Z * matrix.Z.X)) * determinant;

            result.Z.X = ((matrix.Y.X * matrix.Z.Y * matrix.W.W) + (matrix.Y.Y * matrix.Z.W * matrix.W.X) + (matrix.Y.W * matrix.Z.X * matrix.W.Y) - (matrix.Y.X * matrix.Z.W * matrix.W.Y) - (matrix.Y.Y * matrix.Z.X * matrix.W.W) - (matrix.Y.W * matrix.Z.Y * matrix.W.X)) * determinant;
            result.Z.Y = ((matrix.X.X * matrix.Z.W * matrix.W.Y) + (matrix.X.Y * matrix.Z.X * matrix.W.W) + (matrix.X.W * matrix.Z.Y * matrix.W.X) - (matrix.X.X * matrix.Z.Y * matrix.W.W) - (matrix.X.Y * matrix.Z.W * matrix.W.X) - (matrix.X.W * matrix.Z.X * matrix.W.Y)) * determinant;
            result.Z.Z = ((matrix.X.X * matrix.Y.Y * matrix.W.W) + (matrix.X.Y * matrix.Y.W * matrix.W.X) + (matrix.X.W * matrix.Y.X * matrix.W.Y) - (matrix.X.X * matrix.Y.W * matrix.W.Y) - (matrix.X.Y * matrix.Y.X * matrix.W.W) - (matrix.X.W * matrix.Y.Y * matrix.W.X)) * determinant;
            result.Z.W = ((matrix.X.X * matrix.Y.W * matrix.Z.Y) + (matrix.X.Y * matrix.Y.X * matrix.Z.W) + (matrix.X.W * matrix.Y.Y * matrix.Z.X) - (matrix.X.X * matrix.Y.Y * matrix.Z.W) - (matrix.X.Y * matrix.Y.W * matrix.Z.X) - (matrix.X.W * matrix.Y.X * matrix.Z.Y)) * determinant;

            result.W.X = ((matrix.Y.X * matrix.Z.Z * matrix.W.Y) + (matrix.Y.Y * matrix.Z.X * matrix.W.Z) + (matrix.Y.Z * matrix.Z.Y * matrix.W.X) - (matrix.Y.X * matrix.Z.Y * matrix.W.Z) - (matrix.Y.Y * matrix.Z.Z * matrix.W.X) - (matrix.Y.Z * matrix.Z.X * matrix.W.Y)) * determinant;
            result.W.Y = ((matrix.X.X * matrix.Z.Y * matrix.W.Z) + (matrix.X.Y * matrix.Z.Z * matrix.W.X) + (matrix.X.Z * matrix.Z.X * matrix.W.Y) - (matrix.X.X * matrix.Z.Z * matrix.W.Y) - (matrix.X.Y * matrix.Z.X * matrix.W.Z) - (matrix.X.Z * matrix.Z.Y * matrix.W.X)) * determinant;
            result.W.Z = ((matrix.X.X * matrix.Y.Z * matrix.W.Y) + (matrix.X.Y * matrix.Y.X * matrix.W.Z) + (matrix.X.Z * matrix.Y.Y * matrix.W.X) - (matrix.X.X * matrix.Y.Y * matrix.W.Z) - (matrix.X.Y * matrix.Y.Z * matrix.W.X) - (matrix.X.Z * matrix.Y.X * matrix.W.Y)) * determinant;
            result.W.W = ((matrix.X.X * matrix.Y.Y * matrix.Z.Z) + (matrix.X.Y * matrix.Y.Z * matrix.Z.X) + (matrix.X.Z * matrix.Y.X * matrix.Z.Y) - (matrix.X.X * matrix.Y.Z * matrix.Z.Y) - (matrix.X.Y * matrix.Y.X * matrix.Z.Z) - (matrix.X.Z * matrix.Y.Y * matrix.Z.X)) * determinant;

            Transpose(result, out result);
        }
Example #21
0
 public static Quaternion FromMatrix4(Matrix4 matrix)
 {
     float w = (float)Math.Sqrt(1 + matrix.X.X + matrix.Y.Y + matrix.Z.Z) * .5f;
     float delta = 1 / (w * 4);
     return new Quaternion
     (
         (matrix.Z.Y - matrix.Y.Z) * delta,
         (matrix.X.Z - matrix.Z.X) * delta,
         (matrix.Y.X - matrix.X.Y) * delta,
         w
     );
 }
Example #22
0
        public static void Multiply(ref Matrix4 matrix1, ref Matrix2 matrix2, out Matrix4 result)
        {
            result.X.X = (matrix1.X.X*matrix2.X.X) + (matrix1.X.Y*matrix2.Y.X);
            result.X.Y = (matrix1.X.X*matrix2.X.Y) + (matrix1.X.Y*matrix2.Y.Y);
            result.X.Z = matrix1.X.Z;
            result.X.W = matrix1.X.W;

            result.Y.X = (matrix1.Y.X*matrix2.X.X) + (matrix1.Y.Y*matrix2.Y.X);
            result.Y.Y = (matrix1.Y.X*matrix2.X.Y) + (matrix1.Y.Y*matrix2.Y.Y);
            result.Y.Z = matrix1.Y.Z;
            result.Y.W = matrix1.Y.W;

            result.Z = matrix1.Z;
            result.W = matrix1.W;
        }
Example #23
0
 public static void TransformNormal(ref Vector3 vector, ref Matrix4 matrix, out Vector3 result)
 {
     result.X = (vector.X * matrix.X.X) + (vector.Y * matrix.Y.X) + (vector.Z * matrix.Z.X);
     result.Y = (vector.X * matrix.X.Y) + (vector.Y * matrix.Y.Y) + (vector.Z * matrix.Z.Y);
     result.Z = (vector.X * matrix.X.Z) + (vector.Y * matrix.Y.Z) + (vector.Z * matrix.Z.Z);
 }
Example #24
0
        public static void Multiply(ref Matrix4 matrix1, ref Matrix4 matrix2, out Matrix4 result)
        {
            result.X.X = (matrix1.X.X*matrix2.X.X) + (matrix1.X.Y*matrix2.Y.X) + (matrix1.X.Z*matrix2.Z.X) + (matrix1.X.W*matrix2.W.X);
            result.X.Y = (matrix1.X.X*matrix2.X.Y) + (matrix1.X.Y*matrix2.Y.Y) + (matrix1.X.Z*matrix2.Z.Y) + (matrix1.X.W*matrix2.W.Y);
            result.X.Z = (matrix1.X.X*matrix2.X.Z) + (matrix1.X.Y*matrix2.Y.Z) + (matrix1.X.Z*matrix2.Z.Z) + (matrix1.X.W*matrix2.W.Z);
            result.X.W = (matrix1.X.X*matrix2.X.W) + (matrix1.X.Y*matrix2.Y.W) + (matrix1.X.Z*matrix2.Z.W) + (matrix1.X.W*matrix2.W.W);

            result.Y.X = (matrix1.Y.X*matrix2.X.X) + (matrix1.Y.Y*matrix2.Y.X) + (matrix1.Y.Z*matrix2.Z.X) + (matrix1.Y.W*matrix2.W.X);
            result.Y.Y = (matrix1.Y.X*matrix2.X.Y) + (matrix1.Y.Y*matrix2.Y.Y) + (matrix1.Y.Z*matrix2.Z.Y) + (matrix1.Y.W*matrix2.W.Y);
            result.Y.Z = (matrix1.Y.X*matrix2.X.Z) + (matrix1.Y.Y*matrix2.Y.Z) + (matrix1.Y.Z*matrix2.Z.Z) + (matrix1.Y.W*matrix2.W.Z);
            result.Y.W = (matrix1.Y.X*matrix2.X.W) + (matrix1.Y.Y*matrix2.Y.W) + (matrix1.Y.Z*matrix2.Z.W) + (matrix1.Y.W*matrix2.W.W);

            result.Z.X = (matrix1.Z.X*matrix2.X.X) + (matrix1.Z.Y*matrix2.Y.X) + (matrix1.Z.Z*matrix2.Z.X) + (matrix1.Z.W*matrix2.W.X);
            result.Z.Y = (matrix1.Z.X*matrix2.X.Y) + (matrix1.Z.Y*matrix2.Y.Y) + (matrix1.Z.Z*matrix2.Z.Y) + (matrix1.Z.W*matrix2.W.Y);
            result.Z.Z = (matrix1.Z.X*matrix2.X.Z) + (matrix1.Z.Y*matrix2.Y.Z) + (matrix1.Z.Z*matrix2.Z.Z) + (matrix1.Z.W*matrix2.W.Z);
            result.Z.W = (matrix1.Z.X*matrix2.X.W) + (matrix1.Z.Y*matrix2.Y.W) + (matrix1.Z.Z*matrix2.Z.W) + (matrix1.Z.W*matrix2.W.W);

            result.W.X = (matrix1.W.X*matrix2.X.X) + (matrix1.W.Y*matrix2.Y.X) + (matrix1.W.Z*matrix2.Z.X) + (matrix1.W.W*matrix2.W.X);
            result.W.Y = (matrix1.W.X*matrix2.X.Y) + (matrix1.W.Y*matrix2.Y.Y) + (matrix1.W.Z*matrix2.Z.Y) + (matrix1.W.W*matrix2.W.Y);
            result.W.Z = (matrix1.W.X*matrix2.X.Z) + (matrix1.W.Y*matrix2.Y.Z) + (matrix1.W.Z*matrix2.Z.Z) + (matrix1.W.W*matrix2.W.Z);
            result.W.W = (matrix1.W.X*matrix2.X.W) + (matrix1.W.Y*matrix2.Y.W) + (matrix1.W.Z*matrix2.Z.W) + (matrix1.W.W*matrix2.W.W);
        }
Example #25
0
 public Vector3 TransformNormal(Matrix4 matrix)
 {
     return new Vector3
     (
         (X * matrix.X.X) + (Y * matrix.Y.X) + (Z * matrix.Z.X),
         (X * matrix.X.Y) + (Y * matrix.Y.Y) + (Z * matrix.Z.Y),
         (X * matrix.X.Z) + (Y * matrix.Y.Z) + (Z * matrix.Z.Z)
     );
 }
Example #26
0
        public static void Orthographic(float left, float right, float bottom, float top, float near, float far, out Matrix4 result)
        {
            float width = right - left;
            float height = top - bottom;
            float depth = far - near;

            result.X.X = 2/width;
            result.X.Y = 0;
            result.X.Z = 0;
            result.X.W = -(right+left)/width;

            result.Y.X = 0;
            result.Y.Y = 2/height;
            result.Y.Z = 0;
            result.Y.W = -(top+bottom)/height;

            result.Z.X = 0;
            result.Z.Y = 0;
            result.Z.Z = -2/depth;
            result.Z.W = -(far+near)/depth;

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = 0;
            result.W.W = 1;
        }
Example #27
0
 public void Set(Matrix4[] values)
 {
     throw new NotImplementedException();
 }
Example #28
0
 public static void OrthographicCentered(float width, float height, float near, float far, out Matrix4 result)
 {
     OrthographicCentered(0, width, 0, height, near, far, out result);
 }
Example #29
0
 public void Set(Matrix4[] values, int offset, int count)
 {
     throw new NotImplementedException();
 }
Example #30
0
        public static void OrthographicCentered(float left, float right, float bottom, float top, float near, float far, out Matrix4 result)
        {
            float width = right - left;
            float height = top - bottom;
            float depth = far - near;

            result.X.X = (2/width);
            result.X.Y = 0;
            result.X.Z = 0;
            result.X.W = 0;

            result.Y.X = 0;
            result.Y.Y = (2/height);
            result.Y.Z = 0;
            result.Y.W = 0;

            result.Z.X = 0;
            result.Z.Y = 0;
            result.Z.Z = (-2)/depth;
            result.Z.W = -((far+near)/depth);

            result.W.X = 0;
            result.W.Y = 0;
            result.W.Z = 0;
            result.W.W = 1;
        }