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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion
@ -24,9 +25,11 @@ namespace MoonWorks.Math
{
/// <summary>
/// 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>
[Serializable]
[DebuggerDisplay("{DebugDisplayString,nq}")]
[StructLayout(LayoutKind.Sequential)]
public struct Matrix : IEquatable<Matrix>
{
#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);

View File

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

View File

@ -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
/// </summary>
[Serializable]
[DebuggerDisplay("{DebugDisplayString,nq}")]
[StructLayout(LayoutKind.Explicit)]
public struct Vector3 : IEquatable<Vector3>
{
#region Public Static Properties
@ -192,16 +194,19 @@ namespace MoonWorks.Math
/// <summary>
/// The x coordinate of this <see cref="Vector3"/>.
/// </summary>
[FieldOffset(0)]
public float X;
/// <summary>
/// The y coordinate of this <see cref="Vector3"/>.
/// </summary>
[FieldOffset(4)]
public float Y;
/// <summary>
/// The z coordinate of this <see cref="Vector3"/>.
/// </summary>
[FieldOffset(8)]
public float Z;
#endregion

View File

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