Skip to content

Commit ed793a9

Browse files
committed
Fix OpenXR flags being 32-bit not 64-bit, "almost" working OpenXR demo
1 parent b21cefd commit ed793a9

File tree

70 files changed

+2410
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2410
-90
lines changed

build/cache/gles2.json.gz

0 Bytes
Binary file not shown.

build/cache/openxr.json.gz

735 Bytes
Binary file not shown.

build/cache/vulkan.json.gz

1.62 KB
Binary file not shown.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using Silk.NET.OpenGL;
3+
4+
namespace OpenGL_VR_Demo
5+
{
6+
public class BufferObject<TDataType> : IDisposable
7+
where TDataType : unmanaged
8+
{
9+
private uint _handle;
10+
private BufferTargetARB _bufferType;
11+
private GL _gl;
12+
13+
public unsafe BufferObject(GL gl, Span<TDataType> data, BufferTargetARB bufferType)
14+
{
15+
_gl = gl;
16+
_bufferType = bufferType;
17+
18+
_handle = _gl.GenBuffer();
19+
Bind();
20+
fixed (void* d = data)
21+
{
22+
_gl.BufferData(bufferType, (nuint) (data.Length * sizeof(TDataType)), d, BufferUsageARB.StaticDraw);
23+
}
24+
}
25+
26+
public void Bind()
27+
{
28+
_gl.BindBuffer(_bufferType, _handle);
29+
}
30+
31+
public void Dispose()
32+
{
33+
_gl.DeleteBuffer(_handle);
34+
}
35+
}
36+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Numerics;
3+
using OpenGL_VR_Demo.OpenXR;
4+
5+
namespace OpenGL_VR_Demo
6+
{
7+
public class Camera
8+
{
9+
private Quaternion _quat;
10+
11+
public Vector3 Position { get; set; }
12+
public Vector3 Front { get; set; }
13+
14+
public Vector3 Up { get; private set; }
15+
public float AspectRatio { get; set; }
16+
17+
public float Yaw { get; set; } = -90f;
18+
public float Pitch { get; set; }
19+
20+
private float Zoom = 45f;
21+
22+
public Camera(Vector3 position, Vector3 front, Vector3 up, float aspectRatio)
23+
{
24+
Position = position;
25+
AspectRatio = aspectRatio;
26+
Front = front;
27+
Up = up;
28+
}
29+
30+
public void ModifyZoom(float zoomAmount)
31+
{
32+
//We don't want to be able to zoom in too close or too far away so clamp to these values
33+
Zoom = Math.Clamp(Zoom - zoomAmount, 1.0f, 45f);
34+
}
35+
36+
//public void ModifyDirection(float xOffset, float yOffset)
37+
public void ModifyDirection(Quaternion orientation)
38+
{
39+
// Yaw += xOffset;
40+
// Pitch -= yOffset;
41+
42+
////We don't want to be able to look behind us by going over our head or under our feet so make sure it stays within these bounds
43+
//Pitch = Math.Clamp(Pitch, -89f, 89f);
44+
45+
//var cameraDirection = Vector3.Zero;
46+
var cameraDirection = Vector3.Zero;
47+
cameraDirection.X = MathF.Cos(MathHelper.DegreesToRadians(Yaw)) * MathF.Cos(MathHelper.DegreesToRadians(Pitch));
48+
cameraDirection.Y = MathF.Sin(MathHelper.DegreesToRadians(Pitch));
49+
cameraDirection.Z = MathF.Sin(MathHelper.DegreesToRadians(Yaw)) * MathF.Cos(MathHelper.DegreesToRadians(Pitch));
50+
cameraDirection = Vector3.Transform(cameraDirection, orientation);
51+
Front = Vector3.Normalize(cameraDirection);
52+
53+
_quat = orientation;
54+
}
55+
56+
public Matrix4x4 GetViewMatrix()
57+
{
58+
//return Matrix4x4.CreateLookAt(Position, Position + Front, Up);
59+
var s = Matrix4x4.Invert
60+
(Matrix4x4.CreateFromQuaternion(_quat) * Matrix4x4.CreateTranslation(Position), out var ret);
61+
if (!s)
62+
{
63+
throw new();
64+
}
65+
66+
return ret;
67+
}
68+
69+
public Matrix4x4 GetProjectionMatrix()
70+
{
71+
return Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(Zoom), AspectRatio, 0.1f, 100.0f);
72+
}
73+
}
74+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Numerics;
5+
using System.Runtime.CompilerServices;
6+
using OpenGL_VR_Demo.OpenXR;
7+
using Silk.NET.OpenGL;
8+
using Silk.NET.OpenXR;
9+
using Shader = Silk.NET.OpenGL.Shader;
10+
11+
namespace OpenGL_VR_Demo
12+
{
13+
public class Game : OpenGLXRGame
14+
{
15+
private static GL Gl;
16+
17+
private static BufferObject<float> Vbo;
18+
private static BufferObject<uint> Ebo;
19+
private static VertexArrayObject<float, uint> VaoCube;
20+
private static Shader LightingShader;
21+
private static Shader LampShader;
22+
private static Vector3 LampPosition = new(1.2f, 1.0f, 2.0f);
23+
24+
private static Camera Camera;
25+
private static Vector2 LastOrientation;
26+
27+
private static readonly float[] Vertices =
28+
{
29+
//X Y Z Normals
30+
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
31+
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
32+
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
33+
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
34+
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
35+
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
36+
37+
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
38+
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
39+
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
40+
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
41+
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
42+
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
43+
44+
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
45+
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
46+
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
47+
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
48+
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
49+
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
50+
51+
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
52+
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
53+
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
54+
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
55+
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
56+
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
57+
58+
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
59+
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
60+
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
61+
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
62+
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
63+
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
64+
65+
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
66+
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
67+
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
68+
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
69+
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
70+
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
71+
};
72+
73+
private static readonly uint[] Indices =
74+
{
75+
0, 1, 3,
76+
1, 2, 3
77+
};
78+
79+
80+
public Game(bool forceNoDebug = false, bool useMinimumVersion = false)
81+
: base("OpenGL VR Demo", forceNoDebug, useMinimumVersion)
82+
{
83+
}
84+
85+
protected override void Load()
86+
{
87+
Gl = Renderer.Gl;
88+
89+
Ebo = new(Gl, Indices, BufferTargetARB.ElementArrayBuffer);
90+
Vbo = new(Gl, Vertices, BufferTargetARB.ArrayBuffer);
91+
VaoCube = new(Gl, Vbo, Ebo);
92+
93+
VaoCube.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, 6, 0);
94+
VaoCube.VertexAttributePointer(1, 3, VertexAttribPointerType.Float, 6, 3);
95+
96+
//The lighting shader will give our main cube it's colour multiplied by the light's intensity
97+
LightingShader = new(Gl, "shader.vert", "lighting.frag");
98+
//The Lamp shader uses a fragment shader that just colours it solid white so that we know it is the light source
99+
LampShader = new(Gl, "shader.vert", "shader.frag");
100+
101+
//Start a camera at position 3 on the Z axis, looking at position -1 on the Z axis
102+
Camera = new(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, (float)EyeWidth / EyeHeight);
103+
}
104+
105+
protected override void Update(double delta)
106+
{
107+
var moveSpeed = 2.5f * (float) delta;
108+
// TODO add OpenXR controller input handling
109+
}
110+
111+
protected override void Render(int eye, double delta)
112+
{
113+
var position = Unsafe.As<Vector3f, Vector3>(ref Renderer.ViewStates[0].Pose.Position);
114+
var orientation = Unsafe.As<Quaternionf, Quaternion>(ref Renderer.ViewStates[0].Pose.Orientation);
115+
Camera.ModifyDirection(orientation);
116+
117+
var cameraPosBefore = Camera.Position;
118+
Camera.Position += position * Camera.Front;
119+
120+
Gl.Enable(EnableCap.DepthTest);
121+
122+
//Gl.ClearColor(eye, 0, 1 - eye, 0);
123+
var projection = Camera.GetViewMatrix();
124+
var xrprojection = Renderer.Projections[eye];
125+
126+
Gl.Clear((uint) (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));
127+
128+
VaoCube.Bind();
129+
LightingShader.Use();
130+
131+
//Slightly rotate the cube to give it an angled face to look at
132+
LightingShader.SetUniform("uModel", Matrix4x4.CreateRotationY(MathHelper.DegreesToRadians(25f)));
133+
LightingShader.SetUniform("uView", Camera.GetViewMatrix());
134+
//LightingShader.SetUniform("uProjection", Camera.GetProjectionMatrix());
135+
LightingShader.SetUniform("uProjection", xrprojection);
136+
LightingShader.SetUniform("objectColor", new Vector3(1.0f, 0.5f, 0.31f));
137+
LightingShader.SetUniform("lightColor", Vector3.One);
138+
LightingShader.SetUniform("lightPos", LampPosition);
139+
LightingShader.SetUniform("viewPos", Camera.Position);
140+
141+
//We're drawing with just vertices and no indicies, and it takes 36 verticies to have a six-sided textured cube
142+
Gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
143+
144+
LampShader.Use();
145+
146+
//The Lamp cube is going to be a scaled down version of the normal cubes verticies moved to a different screen location
147+
var lampMatrix = Matrix4x4.Identity;
148+
lampMatrix *= Matrix4x4.CreateScale(0.2f);
149+
lampMatrix *= Matrix4x4.CreateTranslation(LampPosition);
150+
151+
LampShader.SetUniform("uModel", lampMatrix);
152+
LampShader.SetUniform("uView", Camera.GetViewMatrix());
153+
LampShader.SetUniform("uProjection", xrprojection);
154+
155+
Gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
156+
157+
Camera.Position = cameraPosBefore;
158+
}
159+
160+
protected override void Unload()
161+
{
162+
Vbo.Dispose();
163+
Ebo.Dispose();
164+
VaoCube.Dispose();
165+
LightingShader.Dispose();
166+
}
167+
}
168+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace OpenGL_VR_Demo
4+
{
5+
public static class MathHelper
6+
{
7+
public static float DegreesToRadians(float degrees)
8+
{
9+
return MathF.PI / 180f * degrees;
10+
}
11+
}
12+
}

examples/CSharp/OpenGL Demos/OpenGL VR Demo/OpenGL VR Demo.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,20 @@
1616
<ProjectReference Include="..\..\..\..\src\Windowing\Silk.NET.Windowing\Silk.NET.Windowing.csproj" />
1717
</ItemGroup>
1818

19+
<ItemGroup>
20+
<None Update="shader.frag">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
<None Update="lighting.frag">
24+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
25+
</None>
26+
<None Update="shader.vert">
27+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
28+
</None>
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.1" />
33+
</ItemGroup>
34+
1935
</Project>

0 commit comments

Comments
 (0)