Skip to content

Commit dc4e0c1

Browse files
authored
Merge pull request #6 from nesk/matrix-helpers
Add static methods to convert matrices
2 parents 6ad6432 + 7689327 commit dc4e0c1

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

OVRSharp/Math/Matrix.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Valve.VR;
2+
using System.Numerics;
3+
4+
namespace OVRSharp.Math
5+
{
6+
public static class MatrixExtension
7+
{
8+
/// <summary>
9+
/// Converts a <see cref="Matrix4x4"/> to a <see cref="HmdMatrix34_t"/>.
10+
/// <br/>
11+
/// <br/>
12+
/// From: <br/>
13+
/// 11 12 13 14 <br/>
14+
/// 21 22 23 24 <br/>
15+
/// 31 32 33 34 <br/>
16+
/// 41 42 43 44
17+
/// <br/><br/>
18+
/// To: <br/>
19+
/// 11 12 13 41 <br/>
20+
/// 21 22 23 42 <br/>
21+
/// 31 32 33 43
22+
/// </summary>
23+
public static HmdMatrix34_t ToHmdMatrix34_t(this Matrix4x4 matrix)
24+
{
25+
return new HmdMatrix34_t()
26+
{
27+
m0 = matrix.M11,
28+
m1 = matrix.M12,
29+
m2 = matrix.M13,
30+
m3 = matrix.M41,
31+
m4 = matrix.M21,
32+
m5 = matrix.M22,
33+
m6 = matrix.M23,
34+
m7 = matrix.M42,
35+
m8 = matrix.M31,
36+
m9 = matrix.M32,
37+
m10 = matrix.M33,
38+
m11 = matrix.M43,
39+
};
40+
}
41+
42+
/// <summary>
43+
/// Converts a <see cref="HmdMatrix34_t"/> to a <see cref="Matrix4x4"/>.
44+
/// <br/>
45+
/// <br/>
46+
/// From: <br/>
47+
/// 11 12 13 14 <br/>
48+
/// 21 22 23 24 <br/>
49+
/// 31 32 33 34
50+
/// <br/><br/>
51+
/// To: <br/>
52+
/// 11 12 13 XX <br/>
53+
/// 21 22 23 XX <br/>
54+
/// 31 32 33 XX <br/>
55+
/// 14 24 34 XX
56+
/// </summary>
57+
public static Matrix4x4 ToMatrix4x4(this HmdMatrix34_t matrix)
58+
{
59+
return new Matrix4x4(
60+
matrix.m0, matrix.m1, matrix.m2, 0,
61+
matrix.m4, matrix.m5, matrix.m6, 0,
62+
matrix.m8, matrix.m9, matrix.m10, 0,
63+
matrix.m3, matrix.m7, matrix.m11, 1
64+
);
65+
}
66+
}
67+
}

OVRSharp/OVRSharp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@
4646
<PackagePath>\</PackagePath>
4747
</Content>
4848
</ItemGroup>
49+
50+
<ItemGroup>
51+
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
52+
</ItemGroup>
4953
</Project>

0 commit comments

Comments
 (0)