|
| 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 | +} |
0 commit comments