From 45861e122dda7948f73441527a3d5813acbaa486 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 22 Jan 2021 00:46:22 -0800 Subject: [PATCH] math struct layouts + change Matrix projections to use right handed NDC space --- src/Math/Matrix.cs | 16 +++++++++------- src/Math/Vector2.cs | 4 ++++ src/Math/Vector3.cs | 5 +++++ src/Math/Vector4.cs | 6 ++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Math/Matrix.cs b/src/Math/Matrix.cs index 29aca4ac..65d1a2cc 100644 --- a/src/Math/Matrix.cs +++ b/src/Math/Matrix.cs @@ -17,6 +17,7 @@ #region Using Statements using System; using System.Diagnostics; +using System.Runtime.InteropServices; #endregion @@ -24,9 +25,11 @@ namespace MoonWorks.Math { /// /// Represents the right-handed 4x4 floating point matrix, which can store translation, scale and rotation information. + /// This differs from XNA in one major way: projections are modified to give right handed NDC space. /// [Serializable] [DebuggerDisplay("{DebugDisplayString,nq}")] + [StructLayout(LayoutKind.Sequential)] public struct Matrix : IEquatable { #region Public Properties @@ -973,7 +976,7 @@ namespace MoonWorks.Math ) { result.M11 = 2f / width; result.M12 = result.M13 = result.M14 = 0f; - result.M22 = 2f / height; + result.M22 = -2f / height; result.M21 = result.M23 = result.M24 = 0f; result.M33 = 1f / (zNearPlane - zFarPlane); result.M31 = result.M32 = result.M34 = 0f; @@ -1037,7 +1040,7 @@ namespace MoonWorks.Math result.M13 = 0.0f; result.M14 = 0.0f; result.M21 = 0.0f; - result.M22 = (float) (2.0 / ((double) top - (double) bottom)); + result.M22 = -(float) (2.0 / ((double) top - (double) bottom)); result.M23 = 0.0f; result.M24 = 0.0f; result.M31 = 0.0f; @@ -1107,7 +1110,7 @@ namespace MoonWorks.Math } result.M11 = (2f * nearPlaneDistance) / width; result.M12 = result.M13 = result.M14 = 0f; - result.M22 = (2f * nearPlaneDistance) / height; + result.M22 = -(2f * nearPlaneDistance) / height; result.M21 = result.M23 = result.M24 = 0f; result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance); result.M31 = result.M32 = 0f; @@ -1176,10 +1179,9 @@ namespace MoonWorks.Math throw new ArgumentException("nearPlaneDistance >= farPlaneDistance"); } float num = 1f / ((float) System.Math.Tan((double) (fieldOfView * 0.5f))); - float num9 = num / aspectRatio; - result.M11 = num9; + result.M11 = num / aspectRatio; result.M12 = result.M13 = result.M14 = 0; - result.M22 = num; + result.M22 = -num; result.M21 = result.M23 = result.M24 = 0; result.M31 = result.M32 = 0f; result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance); @@ -1255,7 +1257,7 @@ namespace MoonWorks.Math } result.M11 = (2f * nearPlaneDistance) / (right - left); result.M12 = result.M13 = result.M14 = 0; - result.M22 = (2f * nearPlaneDistance) / (top - bottom); + result.M22 = -(2f * nearPlaneDistance) / (top - bottom); result.M21 = result.M23 = result.M24 = 0; result.M31 = (left + right) / (right - left); result.M32 = (top + bottom) / (top - bottom); diff --git a/src/Math/Vector2.cs b/src/Math/Vector2.cs index 7e33e54f..f7e6c8b8 100644 --- a/src/Math/Vector2.cs +++ b/src/Math/Vector2.cs @@ -17,6 +17,7 @@ #region Using Statements using System; using System.Diagnostics; +using System.Runtime.InteropServices; #endregion @@ -27,6 +28,7 @@ namespace MoonWorks.Math /// [Serializable] [DebuggerDisplay("{DebugDisplayString,nq}")] + [StructLayout(LayoutKind.Explicit)] public struct Vector2 : IEquatable { #region Public Static Properties @@ -97,11 +99,13 @@ namespace MoonWorks.Math /// /// The x coordinate of this . /// + [FieldOffset(0)] public float X; /// /// The y coordinate of this . /// + [FieldOffset(4)] public float Y; #endregion diff --git a/src/Math/Vector3.cs b/src/Math/Vector3.cs index 2c372c40..eadaf77d 100644 --- a/src/Math/Vector3.cs +++ b/src/Math/Vector3.cs @@ -17,6 +17,7 @@ #region Using Statements using System; using System.Diagnostics; +using System.Runtime.InteropServices; using System.Text; #endregion @@ -28,6 +29,7 @@ namespace MoonWorks.Math /// [Serializable] [DebuggerDisplay("{DebugDisplayString,nq}")] + [StructLayout(LayoutKind.Explicit)] public struct Vector3 : IEquatable { #region Public Static Properties @@ -192,16 +194,19 @@ namespace MoonWorks.Math /// /// The x coordinate of this . /// + [FieldOffset(0)] public float X; /// /// The y coordinate of this . /// + [FieldOffset(4)] public float Y; /// /// The z coordinate of this . /// + [FieldOffset(8)] public float Z; #endregion diff --git a/src/Math/Vector4.cs b/src/Math/Vector4.cs index ce869f87..a78f3d27 100644 --- a/src/Math/Vector4.cs +++ b/src/Math/Vector4.cs @@ -17,6 +17,7 @@ #region Using Statements using System; using System.Diagnostics; +using System.Runtime.InteropServices; #endregion @@ -27,6 +28,7 @@ namespace MoonWorks.Math /// [Serializable] [DebuggerDisplay("{DebugDisplayString,nq}")] + [StructLayout(LayoutKind.Explicit)] public struct Vector4 : IEquatable { #region Public Static Properties @@ -121,21 +123,25 @@ namespace MoonWorks.Math /// /// The x coordinate of this . /// + [FieldOffset(0)] public float X; /// /// The y coordinate of this . /// + [FieldOffset(4)] public float Y; /// /// The z coordinate of this . /// + [FieldOffset(8)] public float Z; /// /// The w coordinate of this . /// + [FieldOffset(12)] public float W; #endregion