Skip to content

Commit

Permalink
Merge pull request #9 from JerryImMouse/camera-compatibility
Browse files Browse the repository at this point in the history
Compatibility methods
  • Loading branch information
Tornado-Technology committed Jul 11, 2024
2 parents 27c9d95 + 5f1c041 commit ff734cb
Show file tree
Hide file tree
Showing 18 changed files with 578 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private GlfwWindowRegistration WindowSetup(Window* window, WindowCreateSettings
Pointer = window,
Id = new WindowId(_nextWindowId++),

Ratio = framebufferSize.Ratio,
Ratio = framebufferSize.AspectRatio,
Size = size,
FramebufferSize = framebufferSize
};
Expand Down
1 change: 1 addition & 0 deletions Hypercube.Shared.Math/Hypercube.Shared.Math.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="OpenTK.Mathematics" Version="5.0.0-pre.10" />
<PackageReference Include="OpenToolkit.Graphics" Version="4.0.0-pre9.3" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions Hypercube.Shared.Math/Matrix/Matrix3X3.Compatibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Runtime.CompilerServices;
using OpenTK.Mathematics;

namespace Hypercube.Shared.Math.Matrix;

public partial struct Matrix3X3
{
/*
* OpenTK Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Matrix3(Matrix3X3 matrix3)
{
return new Matrix3(matrix3.Row0, matrix3.Row1, matrix3.Row2);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Matrix3X3(Matrix3 matrix3)
{
return new Matrix3X3(matrix3.Row0, matrix3.Row1, matrix3.Row2);
}

/*
* OpenToolkit Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator OpenToolkit.Mathematics.Matrix3(Matrix3X3 matrix3)
{
return new OpenToolkit.Mathematics.Matrix3(matrix3.Row0, matrix3.Row1, matrix3.Row2);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Matrix3X3(OpenToolkit.Mathematics.Matrix3 matrix3)
{
return new Matrix3X3(matrix3.Row0, matrix3.Row1, matrix3.Row2);
}
}
89 changes: 56 additions & 33 deletions Hypercube.Shared.Math/Matrix/Matrix3X3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@

namespace Hypercube.Shared.Math.Matrix;

// TODO: May be it's can be immutable, and also layout broken
[StructLayout(LayoutKind.Sequential)]
public struct Matrix3X3(Vector3 x, Vector3 y, Vector3 z)
public partial struct Matrix3X3
{
private const int IndexRaw0 = 0;
private const int IndexRaw1 = 1;
private const int IndexRaw2 = 2;
private const int IndexRow0 = 0;
private const int IndexRow1 = 1;
private const int IndexRow2 = 2;

private const int IndexColumn0 = 0;
private const int IndexColumn1 = 1;
private const int IndexColumn2 = 2;

public static Matrix3X3 Zero => new(Vector3.Zero);
public static Matrix3X3 One => new(Vector3.One);
public static Matrix3X3 Identity => new(Vector3.Right, Vector3.Up, Vector3.Forward);
public static Matrix3X3 Identity => new(Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ);

public Vector3 Raw0 = x;
public Vector3 Raw1 = y;
public Vector3 Raw2 = z;
public Vector3 Row0;
public Vector3 Row1;
public Vector3 Row2;

/// <summary>
/// Matrix x: 0, y: 0 element.
/// </summary>
public float M00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw0.X;
get => Row0.X;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw0 = Raw0.WithX(value);
set => Row0 = Row0.WithX(value);
}

/// <summary>
Expand All @@ -40,9 +41,9 @@ public float M00
public float M01
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw0.Y;
get => Row0.Y;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw0 = Raw0.WithY(value);
set => Row0 = Row0.WithY(value);
}

/// <summary>
Expand All @@ -51,9 +52,9 @@ public float M01
public float M02
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw0.Z;
get => Row0.Z;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw0 = Raw0.WithZ(value);
set => Row0 = Row0.WithZ(value);
}

/// <summary>
Expand All @@ -62,9 +63,9 @@ public float M02
public float M10
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw1.X;
get => Row1.X;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw1 = Raw1.WithX(value);
set => Row1 = Row1.WithX(value);
}

/// <summary>
Expand All @@ -73,9 +74,9 @@ public float M10
public float M11
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw1.Y;
get => Row1.Y;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw1 = Raw1.WithY(value);
set => Row1 = Row1.WithY(value);
}

/// <summary>
Expand All @@ -84,9 +85,9 @@ public float M11
public float M12
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw1.Z;
get => Row1.Z;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw1 = Raw1.WithZ(value);
set => Row1 = Row1.WithZ(value);
}

/// <summary>
Expand All @@ -95,9 +96,9 @@ public float M12
public float M20
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw2.X;
get => Row2.X;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw2 = Raw2.WithX(value);
set => Row2 = Row2.WithX(value);
}

/// <summary>
Expand All @@ -106,9 +107,9 @@ public float M20
public float M21
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw2.Y;
get => Row2.Y;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw2 = Raw2.WithY(value);
set => Row2 = Row2.WithY(value);
}

/// <summary>
Expand All @@ -117,30 +118,30 @@ public float M21
public float M22
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Raw2.Z;
get => Row2.Z;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Raw2 = Raw2.WithZ(value);
set => Row2 = Row2.WithZ(value);
}

public float this[int raw, int colum]
{
get => raw switch
{
IndexRaw0 => colum switch
IndexRow0 => colum switch
{
IndexColumn0 => M00,
IndexColumn1 => M01,
IndexColumn2 => M02,
_ => throw new ArgumentOutOfRangeException(nameof(colum), colum, null)
},
IndexRaw1 => colum switch
IndexRow1 => colum switch
{
IndexColumn0 => M10,
IndexColumn1 => M11,
IndexColumn2 => M12,
_ => throw new ArgumentOutOfRangeException(nameof(colum), colum, null)
},
IndexRaw2 => colum switch
IndexRow2 => colum switch
{
IndexColumn0 => M20,
IndexColumn1 => M21,
Expand All @@ -153,7 +154,7 @@ public float M22
{
switch (raw)
{
case IndexRaw0:
case IndexRow0:
switch (colum)
{
case IndexColumn0:
Expand All @@ -173,7 +174,7 @@ public float M22
}
break;

case IndexRaw1:
case IndexRow1:
switch (colum)
{
case IndexColumn0:
Expand All @@ -193,7 +194,7 @@ public float M22
}
break;

case IndexRaw2:
case IndexRow2:
switch (colum)
{
case IndexColumn0:
Expand All @@ -218,11 +219,33 @@ public float M22
}
}
}
/// <summary>
/// Creates 3x3 matrix
/// <code>
/// x.X | x.Y | x.Z
/// y.X | y.Y | y.Z
/// z.X | z.Y | z.Z
/// </code>
/// </summary>
public Matrix3X3(Vector3 x, Vector3 y, Vector3 z)
{
Row0 = x;
Row1 = y;
Row2 = z;
}

public Matrix3X3(Vector3 value) : this(value, value, value)
{
}

/// <summary>
/// Creates 3x3 matrix
/// <code>
/// m00 | m01 | m02
/// m10 | m11 | m12
/// m20 | m21 | m22
/// </code>
/// </summary>
public Matrix3X3(float m00, float m01, float m02,
float m10, float m11, float m12,
float m20, float m21, float m22) : this(new Vector3(m00, m01, m02),
Expand Down Expand Up @@ -379,6 +402,6 @@ public static Matrix3X3 CreateTransform(Vector2 position, Angle angle, Vector2 s

public static Vector3 operator *(Matrix3X3 a, Vector3 b)
{
return a.Raw0 * b.X + a.Raw1 * b.Y + a.Raw2 * b.Z;
return a.Row0 * b.X + a.Row1 * b.Y + a.Row2 * b.Z;
}
}
12 changes: 0 additions & 12 deletions Hypercube.Shared.Math/Matrix/Matrix4X4.Compability.cs

This file was deleted.

38 changes: 38 additions & 0 deletions Hypercube.Shared.Math/Matrix/Matrix4X4.Compatibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Runtime.CompilerServices;
using OpenTK.Mathematics;

namespace Hypercube.Shared.Math.Matrix;

public partial struct Matrix4X4
{
/*
* OpenTK Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator OpenTK.Mathematics.Matrix4(Matrix4X4 matrix4X4)
{
return new OpenTK.Mathematics.Matrix4(matrix4X4.Row0, matrix4X4.Row1, matrix4X4.Row2, matrix4X4.Row3);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Matrix4X4(Matrix4 matrix4)
{
return new Matrix4X4(matrix4.Row0, matrix4.Row1, matrix4.Row2, matrix4.Row3);
}

/*
* Open Toolkit Compatibility
*/

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator OpenToolkit.Mathematics.Matrix4(Matrix4X4 matrix4X4)
{
return new OpenToolkit.Mathematics.Matrix4(matrix4X4.Row0, matrix4X4.Row1, matrix4X4.Row2, matrix4X4.Row3);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Matrix4X4(OpenToolkit.Mathematics.Matrix4 matrix4)
{
return new Matrix4X4(matrix4.Row0, matrix4.Row1, matrix4.Row2, matrix4.Row3);
}
}
28 changes: 18 additions & 10 deletions Hypercube.Shared.Math/Matrix/Matrix4X4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@ namespace Hypercube.Shared.Math.Matrix;
[StructLayout(LayoutKind.Sequential)]
public partial struct Matrix4X4 : IEquatable<Matrix4X4>
{
public const int Size = 4 * Vector4.Size;

public static Matrix4X4 Zero => new(Vector4.Zero);
public static Matrix4X4 One => new(Vector4.One);

public static Matrix4X4 Identity => new(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
public static Matrix4X4 Identity => new(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW);

public Vector4 Row0;
public Vector4 Row1;
Expand Down Expand Up @@ -204,15 +197,30 @@ public float M33
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Row3 = Row3.WithW(value);
}

/// <summary>
/// Creates new matrix 4x4
/// <code>
/// Row0.X | Row0.Y | Row0.Z | Row0.W
/// Row1.X | Row1.Y | Row1.Z | Row1.W
/// Row2.X | Row2.Y | Row2.Z | Row2.W
/// Row2.X | Row2.Y | Row2.Z | Row2.W
/// </code>
/// </summary>
/// <param name="x">Row 0</param>
/// <param name="y">Row 1</param>
/// <param name="z">Row 2</param>
/// <param name="w">Row 3</param>
public Matrix4X4(Vector4 x, Vector4 y, Vector4 z, Vector4 w)
{
Row0 = x;
Row1 = y;
Row2 = z;
Row3 = w;
}

/// <summary>
/// Creates new matrix with all rows is "<paramref name="value"/>"
/// </summary>
/// <param name="value">Vector4 to make rows out of</param>
public Matrix4X4(Vector4 value) : this(value, value, value, value)
{
}
Expand Down
Loading

0 comments on commit ff734cb

Please sign in to comment.