-
Notifications
You must be signed in to change notification settings - Fork 5
/
Client.cs
316 lines (234 loc) · 8.55 KB
/
Client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using Silk.NET.Input;
using Silk.NET.Windowing;
namespace glvertexid;
public unsafe partial class Client
{
public Client()
{
// Create a Silk.NET window
var options = WindowOptions.Default;
options.API = new GraphicsAPI(ContextAPI.OpenGL, new APIVersion(3, 3));
options.Position = new(200, 200);
options.PreferredDepthBufferBits = 32;
options.Title = "gl_VertexID";
window = Window.Create(options);
// Callback when the window is created
window.Load += () =>
{
// Create an OpenGL Context
Gl = window.CreateOpenGL();
SilkOnDidCreateOpenGLContext();
// Precalculate input stuff
inputContext = window.CreateInput();
keyboard = inputContext.Keyboards[0];
mouse = inputContext.Mice[0];
mouse.DoubleClickTime = 1;
keyboard.KeyDown += SilkOnKeyDown;
};
window.Render += (_) => Render();
window.Size = new(1920, 1080);
window.FramesPerSecond = 144;
window.UpdatesPerSecond = 144;
window.VSync = false;
window.FocusChanged += SilkOnFocusChanged;
// Initialise OpenGL and input context
window.Initialize();
}
public void Run()
{
// Run forever
window.Run();
}
void SilkOnDidCreateOpenGLContext()
{
var major = Gl.GetInteger(GetPName.MajorVersion);
var minor = Gl.GetInteger(GetPName.MinorVersion);
var version = major * 10 + minor;
Console.WriteLine($"OpenGL Version: {version}");
buffer = new();
var bytes_vertexData = Marshal.SizeOf<HeightmapVertex>() * Constants.VERTICES_PER_CHUNK;
var vertexData = (HeightmapVertex*)Allocator.Alloc(bytes_vertexData);
GenerateBuffer(vertexData);
buffer.BufferData(Constants.VERTICES_PER_CHUNK, vertexData);
Allocator.Free(ref vertexData, ref bytes_vertexData);
#if DEBUG
// Set up the OpenGL debug message callback (NVIDIA only)
debugDelegate = DebugCallback;
Gl.Enable(EnableCap.DebugOutput);
Gl.Enable(EnableCap.DebugOutputSynchronous);
Gl.DebugMessageCallback(debugDelegate, null);
#endif
}
void SilkOnFocusChanged(bool focused)
{
if (!focused)
captureMouse = false;
else
captureMouse = true;
lastMouse = mouse.Position;
}
// Heightmap helpers
float GetHeight(int x, int z) => MathF.Sin(x * 0.5f) + MathF.Cos(z * 0.25f) * 2;
void GenerateBuffer(HeightmapVertex* write)
{
// Generate 32 triangle strips
for (int z = 0; z < Constants.HEIGHTMAP_SIZE; z++)
{
int x = 0;
var altitude0 = GetHeight(x, z);
var altitude1 = GetHeight(x, z + 1);
var altitude2 = GetHeight(x + 1, z);
// First vertex is a degenerate
write++->Reset(altitude0);
// Create the first triangle
write++->Reset(altitude0);
write++->Reset(altitude1);
write++->Reset(altitude2);
// Rest of the strip
x += 1;
var altitude = GetHeight(x, z + 1);
write++->Reset(altitude);
x += 1;
for (; x <= Constants.HEIGHTMAP_SIZE; x++)
{
altitude = GetHeight(x, z);
write++->Reset(altitude);
altitude = GetHeight(x, z + 1);
write++->Reset(altitude);
}
// Degenerate
altitude = GetHeight(x - 1, z + 1);
write++->Reset(altitude);
}
}
// Rendering
void Render()
{
UpdateCamera();
// Prepare OpenGL
PreRenderSetup();
// Prepare the shader
HeightmapShader.UseProgram();
HeightmapShader.mvp.Set(GetViewProjection());
HeightmapShader.showWireframe.Set(keyboard.IsKeyPressed(Key.Space));
Gl.FrontFace(FrontFaceDirection.Ccw);
buffer.primitiveType = PrimitiveType.TriangleStrip;
buffer.BindAndDraw();
}
void PreRenderSetup()
{
// Prepare rendering
Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
Gl.Enable(EnableCap.DepthTest);
Gl.Disable(EnableCap.Blend);
Gl.Disable(EnableCap.StencilTest);
Gl.Enable(EnableCap.CullFace);
Gl.FrontFace(FrontFaceDirection.CW);
// Clear everything
Gl.ClearDepth(1.0f);
Gl.DepthFunc(DepthFunction.Less);
Gl.ColorMask(true, true, true, true);
Gl.DepthMask(true);
Gl.ClearColor(0, 0, 0, 0);
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
// Set the viewport to the window size
Gl.Viewport(0, 0, (uint)window.Size.X, (uint)window.Size.Y);
}
Matrix4x4 GetViewProjection()
{
var view = Helper.CreateFPSView(cameraPos, cameraPitch, cameraYaw);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(FieldOfView, Aspect, NearPlane, FarPlane);
return view * proj;
}
// Input
void SilkOnKeyDown(IKeyboard keyboard, Key key, int something)
{
if (key == Key.Escape)
{
captureMouse = !captureMouse;
// Don't snip the camera when capturing the mouse
lastMouse = mouse.Position;
}
}
void UpdateCamera()
{
if (firstRender)
{
cameraPos = new Vector3(0, 18, 0) - Helper.FromPitchYaw(cameraPitch, cameraYaw) * 24;
lastMouse = mouse.Position;
firstRender = false;
}
// Mouse movement
if (captureMouse)
{
var diff = lastMouse - mouse.Position;
cameraYaw -= diff.X * 0.003f;
cameraPitch += diff.Y * 0.003f;
mouse.Position = new Vector2(window.Size.X / 2, window.Size.Y / 2);
lastMouse = mouse.Position;
mouse.Cursor.CursorMode = CursorMode.Hidden;
}
else
mouse.Cursor.CursorMode = CursorMode.Normal;
// Fly camera movement
float movementSpeed = 0.15f;
if (keyboard.IsKeyPressed(Key.W))
cameraPos += Helper.FromPitchYaw(cameraPitch, cameraYaw) * movementSpeed;
else if (keyboard.IsKeyPressed(Key.S))
cameraPos -= Helper.FromPitchYaw(cameraPitch, cameraYaw) * movementSpeed;
if (keyboard.IsKeyPressed(Key.A))
cameraPos += Helper.FromPitchYaw(0, cameraYaw - MathF.PI / 2) * movementSpeed;
else if (keyboard.IsKeyPressed(Key.D))
cameraPos += Helper.FromPitchYaw(0, cameraYaw + MathF.PI / 2) * movementSpeed;
if (keyboard.IsKeyPressed(Key.E))
cameraPos += Helper.FromPitchYaw(MathF.PI / 2, 0) * movementSpeed;
else if (keyboard.IsKeyPressed(Key.Q))
cameraPos += Helper.FromPitchYaw(-MathF.PI / 2, 0) * movementSpeed;
}
#if DEBUG
// Debug OpenGL callbacks (Works on NVIDIA, not sure about AMD/Intel)
DebugProc debugDelegate;
unsafe void DebugCallback(GLEnum source, GLEnum type, int id, GLEnum severity, int length, nint messageInt, nint userParam)
{
// TODO
var message = Marshal.PtrToStringAnsi(messageInt);
if (message == "Pixel-path performance warning: Pixel transfer is synchronized with 3D rendering.")
{
// TODO: Use proper id for this
return;
}
// Skip our own notifications
if (severity == GLEnum.DebugSeverityNotification)
return;
// Buffer detailed info
if (id == 131185)
return;
// "Program/shader state performance warning: Vertex shader in program 69 is being recompiled based on GL state."
if (id == 131218)
return;
// "Buffer performance warning: Buffer object 15 (bound to NONE, usage hint is GL_DYNAMIC_DRAW) is being copied/moved from VIDEO memory to HOST memory."
if (id == 131186)
return;
AssertFalse();
Console.WriteLine(message);
}
#endif
// Silk
IWindow window;
IMouse mouse;
IKeyboard keyboard;
IInputContext inputContext;
// Camera
Vector2 lastMouse;
Vector3 cameraPos;
float cameraPitch = -MathF.PI / 4;
float cameraYaw = MathF.PI / 4;
bool captureMouse = true;
// Rendering
bool firstRender = true;
float FieldOfView = 50.0f / 180.0f * MathF.PI;
float Aspect => window.Size.X / (float)window.Size.Y;
float NearPlane = 1.0f;
float FarPlane = 256.0f;
HeightmapVertexBuffer buffer;
}