diff --git a/Engine.Core/Primitives/Matrix4x4.cs b/Engine.Core/Primitives/Matrix4x4.cs index 9bce992..2c2ab80 100644 --- a/Engine.Core/Primitives/Matrix4x4.cs +++ b/Engine.Core/Primitives/Matrix4x4.cs @@ -67,6 +67,11 @@ public readonly struct Matrix4x4( /// public Matrix4x4 Inverse => Invert(this); + /// + /// Represents the transposed version of this . + /// + public Matrix4x4 Transposed => Transpose(this); + public static Matrix4x4 operator *(Matrix4x4 a, Matrix4x4 b) => new( a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41, a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42, @@ -202,6 +207,18 @@ public readonly struct Matrix4x4( ); } + /// + /// Transposes the given . + /// + /// The . + /// The transposed of the given . + public static Matrix4x4 Transpose(Matrix4x4 m) => new( + m.M11, m.M21, m.M31, m.M41, + m.M12, m.M22, m.M32, m.M42, + m.M13, m.M23, m.M33, m.M43, + m.M14, m.M24, m.M34, m.M44 + ); + public static Matrix4x4 CreateTranslation(Vector3D position) => new( 1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, @@ -297,6 +314,30 @@ public readonly struct Matrix4x4( ); } + public static Matrix4x4 CreateOrthographicView(float width, float height, float nearPlane = -1f, float farPlane = 1f) + { + float invDepth = 1f / (farPlane - nearPlane); + + return new Matrix4x4( + 2f / width, 0f, 0f, 0f, + 0f, -2f / height, 0f, 0f, + 0f, 0f, invDepth, 0f, + -1f, 1f, -nearPlane * invDepth, 1f + ); + } + + public static Matrix4x4 CreateOrthographicViewCentered(float width, float height, float nearPlane = -1f, float farPlane = 1f) + { + float invDepth = 1f / (farPlane - nearPlane); + + return new Matrix4x4( + 2f / width, 0f, 0f, 0f, + 0f, 2f / height, 0f, 0f, + 0f, 0f, invDepth, 0f, + 0f, 0f, -nearPlane * invDepth, 1f + ); + } + public static Matrix4x4 CreatePerspectiveFieldOfView(float fieldOfViewInRadians, float aspectRatio, float nearPlane, float farPlane) { float yScale = 1f / Math.Tan(fieldOfViewInRadians / 2f); @@ -344,6 +385,9 @@ public static class Matrix4x4Extensions /// public static Matrix4x4 Invert(this Matrix4x4 matrix) => Matrix4x4.Invert(matrix); + /// + public static Matrix4x4 Transpose(this Matrix4x4 matrix) => Matrix4x4.Transpose(matrix); + /// public static Matrix4x4 ApplyTranslation(this Matrix4x4 matrix, Vector3D translation) => matrix * Matrix4x4.CreateTranslation(translation); @@ -375,6 +419,14 @@ public static class Matrix4x4Extensions public static Matrix4x4 ApplyPerspectiveFieldOfView(this Matrix4x4 matrix, float fieldOfViewInRadians, float aspectRatio, float nearPlane, float farPlane) => matrix * Matrix4x4.CreatePerspectiveFieldOfView(fieldOfViewInRadians, aspectRatio, nearPlane, farPlane); + /// + public static Matrix4x4 ApplyOrthographicView(this Matrix4x4 matrix, float width, float height, float nearPlane = -1f, float farPlane = 1f) + => matrix * Matrix4x4.CreateOrthographicView(width, height, nearPlane, farPlane); + + /// + public static Matrix4x4 ApplyOrthographicViewCentered(this Matrix4x4 matrix, float width, float height, float nearPlane = -1f, float farPlane = 1f) + => matrix * Matrix4x4.CreateOrthographicViewCentered(width, height, nearPlane, farPlane); + ///