math struct layouts + change Matrix projections to use right handed NDC space

pull/14/head
cosmonaut 2021-01-22 00:46:22 -08:00
parent f2487cd71b
commit 45861e122d
4 changed files with 24 additions and 7 deletions

View File

@ -17,6 +17,7 @@
#region Using Statements #region Using Statements
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion #endregion
@ -24,9 +25,11 @@ namespace MoonWorks.Math
{ {
/// <summary> /// <summary>
/// Represents the right-handed 4x4 floating point matrix, which can store translation, scale and rotation information. /// 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.
/// </summary> /// </summary>
[Serializable] [Serializable]
[DebuggerDisplay("{DebugDisplayString,nq}")] [DebuggerDisplay("{DebugDisplayString,nq}")]
[StructLayout(LayoutKind.Sequential)]
public struct Matrix : IEquatable<Matrix> public struct Matrix : IEquatable<Matrix>
{ {
#region Public Properties #region Public Properties
@ -973,7 +976,7 @@ namespace MoonWorks.Math
) { ) {
result.M11 = 2f / width; result.M11 = 2f / width;
result.M12 = result.M13 = result.M14 = 0f; result.M12 = result.M13 = result.M14 = 0f;
result.M22 = 2f / height; result.M22 = -2f / height;
result.M21 = result.M23 = result.M24 = 0f; result.M21 = result.M23 = result.M24 = 0f;
result.M33 = 1f / (zNearPlane - zFarPlane); result.M33 = 1f / (zNearPlane - zFarPlane);
result.M31 = result.M32 = result.M34 = 0f; result.M31 = result.M32 = result.M34 = 0f;
@ -1037,7 +1040,7 @@ namespace MoonWorks.Math
result.M13 = 0.0f; result.M13 = 0.0f;
result.M14 = 0.0f; result.M14 = 0.0f;
result.M21 = 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.M23 = 0.0f;
result.M24 = 0.0f; result.M24 = 0.0f;
result.M31 = 0.0f; result.M31 = 0.0f;
@ -1107,7 +1110,7 @@ namespace MoonWorks.Math
} }
result.M11 = (2f * nearPlaneDistance) / width; result.M11 = (2f * nearPlaneDistance) / width;
result.M12 = result.M13 = result.M14 = 0f; 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.M21 = result.M23 = result.M24 = 0f;
result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance); result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M31 = result.M32 = 0f; result.M31 = result.M32 = 0f;
@ -1176,10 +1179,9 @@ namespace MoonWorks.Math
throw new ArgumentException("nearPlaneDistance >= farPlaneDistance"); throw new ArgumentException("nearPlaneDistance >= farPlaneDistance");
} }
float num = 1f / ((float) System.Math.Tan((double) (fieldOfView * 0.5f))); float num = 1f / ((float) System.Math.Tan((double) (fieldOfView * 0.5f)));
float num9 = num / aspectRatio; result.M11 = num / aspectRatio;
result.M11 = num9;
result.M12 = result.M13 = result.M14 = 0; result.M12 = result.M13 = result.M14 = 0;
result.M22 = num; result.M22 = -num;
result.M21 = result.M23 = result.M24 = 0; result.M21 = result.M23 = result.M24 = 0;
result.M31 = result.M32 = 0f; result.M31 = result.M32 = 0f;
result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance); result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
@ -1255,7 +1257,7 @@ namespace MoonWorks.Math
} }
result.M11 = (2f * nearPlaneDistance) / (right - left); result.M11 = (2f * nearPlaneDistance) / (right - left);
result.M12 = result.M13 = result.M14 = 0; 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.M21 = result.M23 = result.M24 = 0;
result.M31 = (left + right) / (right - left); result.M31 = (left + right) / (right - left);
result.M32 = (top + bottom) / (top - bottom); result.M32 = (top + bottom) / (top - bottom);

View File

@ -17,6 +17,7 @@
#region Using Statements #region Using Statements
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion #endregion
@ -27,6 +28,7 @@ namespace MoonWorks.Math
/// </summary> /// </summary>
[Serializable] [Serializable]
[DebuggerDisplay("{DebugDisplayString,nq}")] [DebuggerDisplay("{DebugDisplayString,nq}")]
[StructLayout(LayoutKind.Explicit)]
public struct Vector2 : IEquatable<Vector2> public struct Vector2 : IEquatable<Vector2>
{ {
#region Public Static Properties #region Public Static Properties
@ -97,11 +99,13 @@ namespace MoonWorks.Math
/// <summary> /// <summary>
/// The x coordinate of this <see cref="Vector2"/>. /// The x coordinate of this <see cref="Vector2"/>.
/// </summary> /// </summary>
[FieldOffset(0)]
public float X; public float X;
/// <summary> /// <summary>
/// The y coordinate of this <see cref="Vector2"/>. /// The y coordinate of this <see cref="Vector2"/>.
/// </summary> /// </summary>
[FieldOffset(4)]
public float Y; public float Y;
#endregion #endregion

View File

@ -17,6 +17,7 @@
#region Using Statements #region Using Statements
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
#endregion #endregion
@ -28,6 +29,7 @@ namespace MoonWorks.Math
/// </summary> /// </summary>
[Serializable] [Serializable]
[DebuggerDisplay("{DebugDisplayString,nq}")] [DebuggerDisplay("{DebugDisplayString,nq}")]
[StructLayout(LayoutKind.Explicit)]
public struct Vector3 : IEquatable<Vector3> public struct Vector3 : IEquatable<Vector3>
{ {
#region Public Static Properties #region Public Static Properties
@ -192,16 +194,19 @@ namespace MoonWorks.Math
/// <summary> /// <summary>
/// The x coordinate of this <see cref="Vector3"/>. /// The x coordinate of this <see cref="Vector3"/>.
/// </summary> /// </summary>
[FieldOffset(0)]
public float X; public float X;
/// <summary> /// <summary>
/// The y coordinate of this <see cref="Vector3"/>. /// The y coordinate of this <see cref="Vector3"/>.
/// </summary> /// </summary>
[FieldOffset(4)]
public float Y; public float Y;
/// <summary> /// <summary>
/// The z coordinate of this <see cref="Vector3"/>. /// The z coordinate of this <see cref="Vector3"/>.
/// </summary> /// </summary>
[FieldOffset(8)]
public float Z; public float Z;
#endregion #endregion

View File

@ -17,6 +17,7 @@
#region Using Statements #region Using Statements
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion #endregion
@ -27,6 +28,7 @@ namespace MoonWorks.Math
/// </summary> /// </summary>
[Serializable] [Serializable]
[DebuggerDisplay("{DebugDisplayString,nq}")] [DebuggerDisplay("{DebugDisplayString,nq}")]
[StructLayout(LayoutKind.Explicit)]
public struct Vector4 : IEquatable<Vector4> public struct Vector4 : IEquatable<Vector4>
{ {
#region Public Static Properties #region Public Static Properties
@ -121,21 +123,25 @@ namespace MoonWorks.Math
/// <summary> /// <summary>
/// The x coordinate of this <see cref="Vector4"/>. /// The x coordinate of this <see cref="Vector4"/>.
/// </summary> /// </summary>
[FieldOffset(0)]
public float X; public float X;
/// <summary> /// <summary>
/// The y coordinate of this <see cref="Vector4"/>. /// The y coordinate of this <see cref="Vector4"/>.
/// </summary> /// </summary>
[FieldOffset(4)]
public float Y; public float Y;
/// <summary> /// <summary>
/// The z coordinate of this <see cref="Vector4"/>. /// The z coordinate of this <see cref="Vector4"/>.
/// </summary> /// </summary>
[FieldOffset(8)]
public float Z; public float Z;
/// <summary> /// <summary>
/// The w coordinate of this <see cref="Vector4"/>. /// The w coordinate of this <see cref="Vector4"/>.
/// </summary> /// </summary>
[FieldOffset(12)]
public float W; public float W;
#endregion #endregion