-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
955 lines (781 loc) · 28 KB
/
main.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/*
MessyBsp. BSP collision and loading example code.
Copyright (C) 2014 Richard Maxwell <[email protected]>
This file is part of MessyBsp
MessyBsp is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "Bsp.hpp"
#include "BspBrushToMesh.hpp"
#include "TraceTest.hpp"
#include "VectorMaths3.hpp"
#include "Matrix4x4Maths.hpp"
#include "GLDebug.hpp"
#include "third-party/getopt/getopt.h"
// SDL + OpenGL
#include <SDL.h>
// Need ifdef for different platforms.
#include <GL/glew.h>
#include <vector>
#include <memory>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#ifdef UNDEF_MAIN
#undef main
#endif
static const float Pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f;
static const float DegToRad = (2.0f * Pi / 360.0f);
struct Globals
{
// 50Hz for now.
unsigned tickClientPeriodInMicroseconds = 20 * 1000;
unsigned ticksPerSecond = 1000000 / tickClientPeriodInMicroseconds;
// Need to convert all this stuff to SI units.
// For now, lets assume 1 game unit = 1m
float viewAngleSpeedPerTick = 2 * Pi / ticksPerSecond * 2.0f;
float moveDeltaPerTick = 200.0f / ticksPerSecond;
float runMultiplier = 5.0f;
float viewAnglePerMouseMoveUnit = 0.01f;
// For testing lighting, rotates once every 5 seconds
float modelRotationsPerTick = ((2 * Pi) / 5.0f) / ticksPerSecond;
// got the deadzone value from SDL somewhere.
int joystickDeadZone = 6000;
// Deduced this one emperically.
float joystickToMouseMultiplier = 0.001f;
};
const Globals globals = {};
enum ActionMap
{
Forward = 0,
Backward,
StrafeLeft,
StrafeRight,
Up,
Down,
Fast,
Quit,
Count
};
struct PlayerActions
{
bool actions[ActionMap::Count];
int mouseX;
int mouseY;
};
struct ControllerIdToPointer
{
int id;
SDL_GameController* pad;
SDL_Joystick* padAsJoystick;
int instance;
};
static const uint8_t keymap[ActionMap::Count] =
{
SDL_SCANCODE_W,
SDL_SCANCODE_S,
SDL_SCANCODE_A,
SDL_SCANCODE_D,
SDL_SCANCODE_SPACE,
SDL_SCANCODE_Q,
SDL_SCANCODE_LSHIFT,
SDL_SCANCODE_ESCAPE,
};
static const SDL_GameControllerButton padmap[ActionMap::Count] =
{
SDL_CONTROLLER_BUTTON_DPAD_UP,
SDL_CONTROLLER_BUTTON_DPAD_DOWN,
SDL_CONTROLLER_BUTTON_DPAD_LEFT,
SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
SDL_CONTROLLER_BUTTON_X,
SDL_CONTROLLER_BUTTON_START,
};
// Globals
Matrix4x4 g_projection;
void DoGraphics(const Bsp::CollisionBsp& bsp);
void PrintHelp()
{
printf("\n");
printf("MessyBsp - By Richard Maxwell\n\n");
printf(" Renders or does a collsion detection benchmark on a quake3 bsp.\n\n");
printf(" MessyBsp [-b] [-h] [-f <path to quake3 bsp>]\n\n");
printf(" -b: Benchmark 100,000 random collision tests\n");
printf(" Prints the cost in Microseconds. Otherwise\n");
printf(" Renders all the solid brushes using opengl.\n\n");
printf(" -f: Quake3 bsp file to use. Defaults to 'final.bsp'.\n\n");
printf(" -h: This help text.\n");
printf("\n");
}
int main(int argc, char *argv[])
{
bool benchmark = false;
char fileName[1024];
strcpy(fileName, "final.bsp");
// Parse options
while (auto ch = getopt(argc, argv, "hbf:"))
{
if (ch < 0)
{
break;
}
if (ch == 'h')
{
PrintHelp();
return 0;
}
if (ch == 'f')
{
if (optarg)
{
strncpy(fileName, optarg, sizeof(fileName) - 1);
}
}
if (ch == 'b')
{
benchmark = true;
}
}
{
auto fileHandleExists = fopen(fileName, "r");
if (!fileHandleExists)
{
PrintHelp();
printf("---------------------------\n");
printf("File '%s' cannot be opened.\n", fileName);
printf("---------------------------\n");
return -1;
}
fclose(fileHandleExists);
}
Bsp::CollisionBsp bsp;
Bsp::GetCollisionBsp(fileName, bsp);
if (benchmark)
{
auto result = TimeBspCollision(bsp, 100000);
printf("Trace Took %ld microseconds\n", result.count());
return 0;
}
DoGraphics(bsp);
return 0;
}
void AddController(
int id,
std::vector<ControllerIdToPointer>& controllers)
{
if (!SDL_IsGameController(id))
{
return;
}
for (const auto& pad : controllers)
{
if (pad.id == id)
{
return;
}
}
ControllerIdToPointer result;
result.id = id;
result.pad = SDL_GameControllerOpen(id);
if (result.pad)
{
result.padAsJoystick = SDL_GameControllerGetJoystick(result.pad);
if (result.padAsJoystick)
{
result.instance = SDL_JoystickInstanceID(result.padAsJoystick);
controllers.push_back(result);
return;
}
}
if (result.pad != nullptr)
{
SDL_GameControllerClose(result.pad);
}
}
void RemoveController(
int instance,
std::vector<ControllerIdToPointer>& controllers)
{
for (const auto& pad : controllers)
{
if (pad.instance == instance)
{
SDL_GameControllerClose(pad.pad);
return;
}
}
}
void OpenAllControllers(std::vector<ControllerIdToPointer>& controllers)
{
auto count = SDL_NumJoysticks();
for (int i = 0; i < count; ++i)
{
AddController(i, controllers);
}
}
PlayerActions GetActions(std::vector<ControllerIdToPointer>& controllers)
{
PlayerActions result = {{},0,0};
// Keyboard
const auto* keys = SDL_GetKeyboardState(nullptr);
for (unsigned i = 0; i < ActionMap::Count; ++i)
{
if (keys[keymap[i]])
{
result.actions[i] = true;
}
}
// Mouse
SDL_GetRelativeMouseState(&result.mouseX, &result.mouseY);
// Gamepad
for (const auto& controller : controllers)
{
// buttons
for (unsigned i = 0; i < ActionMap::Count; ++i)
{
if (SDL_GameControllerGetButton(controller.pad, padmap[i]))
{
result.actions[i] = true;
}
}
// Gamepad axis
if (!result.mouseX && !result.mouseY)
{
auto x = SDL_GameControllerGetAxis(
controller.pad,
SDL_CONTROLLER_AXIS_RIGHTX);
auto y = SDL_GameControllerGetAxis(
controller.pad,
SDL_CONTROLLER_AXIS_RIGHTY);
if (x > globals.joystickDeadZone)
{
result.mouseX = x - globals.joystickDeadZone;
}
if (x < -globals.joystickDeadZone)
{
result.mouseX = x + globals.joystickDeadZone;
}
if (y > globals.joystickDeadZone)
{
result.mouseY = y - globals.joystickDeadZone;
}
if (y < -globals.joystickDeadZone)
{
result.mouseY = y + globals.joystickDeadZone;
}
result.mouseX = static_cast<int>(result.mouseX * globals.joystickToMouseMultiplier);
result.mouseY = static_cast<int>(result.mouseY * globals.joystickToMouseMultiplier);
}
}
return result;
}
// This class should be full of platform voodoo if done properly.
// good thing I'm not doing it properly.
uint64_t Microseconds()
{
return static_cast<uint64_t>(SDL_GetTicks()) * 1000l;
}
std::vector<float> MakeTriangles(const Bsp::CollisionBsp& bsp)
{
static bool useBsp = true;
static unsigned maxCount = 10009;
if (useBsp)
{
return Bsp::BrushMeshesAsTriangleListWithNormals(
bsp,
maxCount);
}
else
{
return std::vector<float>
{
// front (x,y,z,nx,ny,nz)
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 1.0f,
10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 1.0f,
// left
0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 10.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, -10.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, -10.0f, -1.0f, 0.0f, 0.0f,
0.0f, 10.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 10.0f, -10.0f, -1.0f, 0.0f, 0.0f,
// right
10.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
10.0f, 0.0f, -10.0f, 1.0f, 0.0f, 0.0f,
10.0f, 10.0f, 0.0f, 1.0f, 0.0f, 0.0f,
10.0f, 0.0f, -10.0f, 1.0f, 0.0f, 0.0f,
10.0f, 10.0f, -10.0f, 1.0f, 0.0f, 0.0f,
10.0f, 10.0f, 0.0f, 1.0f, 0.0f, 0.0f,
// back
0.0f, 0.0f, -10.0f, 0.0f, 0.0f, -1.0f,
0.0f, 10.0f, -10.0f, 0.0f, 0.0f, -1.0f,
10.0f, 0.0f, -10.0f, 0.0f, 0.0f, -1.0f,
10.0f, 0.0f, -10.0f, 0.0f, 0.0f, -1.0f,
0.0f, 10.0f, -10.0f, 0.0f, 0.0f, -1.0f,
10.0f, 10.0f, -10.0f, 0.0f, 0.0f, -1.0f,
// top
0.0f, 10.0f, 0.0f, 0.0f, 1.0f, 0.0f,
10.0f, 10.0f, -10.0f, 0.0f, 1.0f, 0.0f,
0.0f, 10.0f, -10.0f, 0.0f, 1.0f, 0.0f,
0.0f, 10.0f, 0.0f, 0.0f, 1.0f, 0.0f,
10.0f, 10.0f, 0.0f, 0.0f, 1.0f, 0.0f,
10.0f, 10.0f, -10.0f, 0.0f, 1.0f, 0.0f,
// bottom
0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
0.0f, 0.0f, -10.0f, 0.0f, -1.0f, 0.0f,
10.0f, 0.0f, -10.0f, 0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
10.0f, 0.0f, -10.0f, 0.0f, -1.0f, 0.0f,
10.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
};
}
}
void DoGraphics(const Bsp::CollisionBsp& bsp)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
// Window mode MUST include SDL_WINDOW_OPENGL for use with OpenGL.
// (btw, when will C++ get scoped_exit?)
SDL_Window *window = SDL_CreateWindow(
"SDL2/OpenGL Demo",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_OPENGL|
SDL_WINDOW_RESIZABLE|
SDL_WINDOW_INPUT_GRABBED);
// Create an OpenGL context associated with the window.
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
if (!glcontext)
{
printf("Create context failed: %s\n", SDL_GetError());
return;
}
SDL_GL_MakeCurrent(window, glcontext);
// capture the mouse so no-one else can use it. Means
// when the app starts the mouse doesn't leave the window
// http://gamedev.stackexchange.com/questions/33519/trap-mouse-in-sdl
// NOTE: for a real game, have an option to turn this on or off, so the
// user can escape the window if running in a window.
SDL_SetRelativeMouseMode(SDL_TRUE);
// Now I can init glew.
{
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
}
// RAM: debug for the debug extension
bool noDebug = true;
if (noDebug && glewIsExtensionSupported("GL_KHR_debug"))
{
noDebug = false;
glDebugMessageCallback((GLDEBUGPROC) DebugCallback, nullptr);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
GLuint unusedIds = 0;
glDebugMessageControl(GL_DONT_CARE,
GL_DONT_CARE,
GL_DONT_CARE,
0,
&unusedIds,
true);
printf("GL_KHR_debug\n");
}
if (noDebug && glewIsExtensionSupported("GL_ARB_debug_output"))
{
//noDebug = false;
// RAM: TODO
printf("GL_ARB_debug_output\n");
}
if (noDebug && glewIsExtensionSupported("GL_AMD_debug_output"))
{
//noDebug = false;
// RAM: TODO
printf("GL_AMD_debug_output\n");
}
if (!noDebug)
{
glEnable(GL_DEBUG_OUTPUT);
}
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// Loading vertex data (1 == number of buffers)
// http://en.wikipedia.org/wiki/Vertex_Buffer_Object
GLuint triangleVboHandle;
glGenBuffers(1, &triangleVboHandle);
glBindBuffer(GL_ARRAY_BUFFER, triangleVboHandle);
auto triangles = MakeTriangles(bsp);
glBufferData(
GL_ARRAY_BUFFER,
triangles.size() * sizeof(float),
triangles.data(),
GL_STATIC_DRAW);
// try https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/loading.php
// http://classes.soe.ucsc.edu/cmps162/Spring12/s12/labnotes/shaders.html
// FFS I cannot find a simple tutorial!
// try https://www.khronos.org/webgl/wiki/Tutorial
// normals are transformed differently, ugh.
// http://www.songho.ca/opengl/gl_normaltransform.html
// Using demo code from:
// http://www.lighthouse3d.com/cg-topics/code-samples/opengl-3-3-glsl-1-5-sample/
// Even though I pass in a vec3 for the verticies, opengl will
// expand them to a vec4 filling in the missing values with the
// template <0,0,0,1>
// http://stackoverflow.com/questions/8551935/opengl-es-2-0-specifiying-position-attribute-vec3-or-vec4
static const GLchar* vs[] =
{
"//#version 330 core \n"
"uniform mat4 projMatrix; \n"
"uniform mat4 modelViewMatrix; \n"
"uniform mat4 normalMatrix; \n"
"uniform vec3 lightDir; \n"
" \n"
"attribute vec4 vPosition; \n"
"attribute vec3 vNormal; \n"
"varying vec4 eyeNormal; \n"
"varying vec4 eyePosition; \n"
" \n"
"void main() \n"
"{ \n"
" eyePosition = modelViewMatrix * vPosition;\n"
" eyeNormal = normalize(normalMatrix * vec4(vNormal, 0.0));\n"
" gl_Position = projMatrix * eyePosition;\n"
"}"
};
static const GLchar* ps[] =
{
"//#version 330 core \n"
"varying vec4 eyePosition; \n"
"varying vec4 eyeNormal; \n"
"uniform vec4 lightPos; \n"
"void main() \n"
"{ \n"
" vec4 c = vec4(0.1, 0.1, 1.0, 1.0); \n"
" vec3 s = vec3(normalize(lightPos - eyePosition));\n"
" vec3 eyeN = vec3(eyeNormal);\n"
" float mydot = dot(eyeN, s);\n"
" float diff = max(mydot, 0.0);\n"
" gl_FragColor = c * diff; \n"
"}"
};
auto vsO = glCreateShader(GL_VERTEX_SHADER);
auto psO = glCreateShader(GL_FRAGMENT_SHADER);
auto pO = glCreateProgram();
glShaderSource(vsO, 1, vs, nullptr);
glShaderSource(psO, 1, ps, nullptr);
glCompileShader(vsO);
glCompileShader(psO);
PrintShaderLog(vsO, Log::Shader);
PrintShaderLog(psO, Log::Shader);
glAttachShader(pO, vsO);
glAttachShader(pO, psO);
glLinkProgram(pO);
GLint gotLinked;
glGetProgramiv(pO, GL_LINK_STATUS, &gotLinked);
if (gotLinked != GL_TRUE)
{
PrintShaderLog(pO, Log::Program);
}
// Get the attribute addresses so we can setup the state
// for the vertex buffer correctly.
// RAM: TODO: Swap to glBindAttribLocation()
// See http://stackoverflow.com/questions/4635913/explicit-vs-automatic-attribute-location-binding-for-opengl-shaders
auto lvPosition = glGetAttribLocation(pO, "vPosition");
auto lvNormal = glGetAttribLocation(pO, "vNormal");
// Get the ids for the uniforms as well
auto lmodelViewMatrix = glGetUniformLocation(pO, "modelViewMatrix");
auto lprojMatrix = glGetUniformLocation(pO, "projMatrix");
auto lnormalMatrix = glGetUniformLocation(pO, "normalMatrix");
auto llightDir = glGetUniformLocation(pO, "lightdir");
auto llightPos = glGetUniformLocation(pO, "lightPos");
// Right, enable the normal and position attribes in the vertex buffer
// and set what offset they are using.
// The last item is meant to be a pointer to the data
// but it's actually an offset. yay.
glEnableVertexAttribArray(lvPosition);
glVertexAttribPointer(
lvPosition,
3,
GL_FLOAT,
GL_FALSE,
3*2*sizeof(float),
reinterpret_cast<const void*>(0));
if (lvNormal >= 0)
{
glEnableVertexAttribArray(lvNormal);
glVertexAttribPointer(
lvNormal,
3,
GL_FLOAT,
GL_FALSE,
3*2*sizeof(float),
reinterpret_cast<const void*>(3*sizeof(float)));
}
// On my linux, my wireless xbox360 controller has an unrecognised GUID
// so add it explicitly. I copied the linux x360 wireless controller
// but had to use the hat isntead of the buttons for the dpad to work.
// Also I had to adjust the right joystick axis to 2 and 3
// https://hg.libsdl.org/SDL/file/b577c4753421/include/SDL_gamecontroller.h#l95
// https://hg.libsdl.org/SDL/file/tip/src/joystick/SDL_gamecontrollerdb.h#l58
auto mapAddResult = SDL_GameControllerAddMapping(
"0000000058626f782047616d65706100,X360 Wireless Controller,a:b0,b:b1,back:b6,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a2,righty:a3,start:b7,x:b2,y:b3,");
std::vector<ControllerIdToPointer> controllers;
if (mapAddResult >= 0)
{
// Add any existing controllers.
OpenAllControllers(controllers);
}
// MAIN SDL LOOP
bool running = true;
bool visible = true;
bool resized = true;
bool rotatingModel = false;
Vec3 cameraPosition = {0,0,30};
Radians yaw = {-90.0f * DegToRad};
Radians pitch = {0.0f};
Radians modelRotation = {0.0f};
auto then = Microseconds();
while (running)
{
SDL_Event e;
if (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
running = false;
}
if (e.type == SDL_KEYUP)
{
if (e.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
}
if (e.type == SDL_CONTROLLERDEVICEADDED)
{
AddController(e.cdevice.which, controllers);
}
if (e.type == SDL_CONTROLLERDEVICEREMOVED)
{
RemoveController(e.cdevice.which, controllers);
}
if (e.type == SDL_WINDOWEVENT)
{
switch (e.window.event)
{
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_EXPOSED:
case SDL_WINDOWEVENT_MAXIMIZED:
case SDL_WINDOWEVENT_RESTORED:
{
visible = true;
resized = true;
break;
}
case SDL_WINDOWEVENT_HIDDEN:
case SDL_WINDOWEVENT_MINIMIZED:
{
visible = false;
break;
}
case SDL_WINDOWEVENT_RESIZED:
{
resized = true;
break;
}
default:
{
break;
}
}
}
}
// Poll for user input.
auto now = Microseconds();
if (now < then)
{
then = now;
}
auto delta = now - then;
if (delta > globals.tickClientPeriodInMicroseconds)
{
PlayerActions actions = GetActions(controllers);
if (actions.actions[Quit])
{
running = false;
}
// deal with where we are looking.
float mouseDelta =
globals.viewAnglePerMouseMoveUnit *
globals.viewAngleSpeedPerTick;
yaw.data += -actions.mouseX * mouseDelta;
pitch.data += -actions.mouseY * mouseDelta;
// clamp to +- 90 degrees up and down
// +- Pi for hrizontal
if (yaw.data < -(2.0f*Pi)) yaw.data += Pi * 2.0f;
if (yaw.data > (2.0f*Pi)) yaw.data -= Pi * 2.0f;
if (pitch.data < -((Pi / 2.0f) - 0.01f)) pitch.data = -((Pi / 2.0f) - 0.01f);
if (pitch.data > ((Pi / 2.0f) - 0.01f)) pitch.data = (Pi / 2.0f) - 0.01f;
// Ok, forward movement is tied only to the yaw angle, then
// treat up as up, and left as normal to forward.
Vec3 forward =
{
std::sin(yaw.data),
// Set this to 0 to prevent "flying".
-std::sin(pitch.data),
std::cos(yaw.data),
};
forward = Normalise(forward);
// Fun fact, "forward" is along the +ve z axis.
// for openGL that's out of the monitor, ie backwards.
forward = - forward;
Vec3 left =
{
forward.data[2],
0,
-forward.data[0],
};
Vec3 up =
{
0,
1.0f,
0,
};
Vec3 movement = {0.0f};
if (actions.actions[Forward])
{
movement += forward;
}
if (actions.actions[Backward])
{
movement -= forward;
}
if (actions.actions[StrafeLeft])
{
movement += left;
}
if (actions.actions[StrafeRight])
{
movement -= left;
}
if (actions.actions[Up])
{
movement += up;
}
if (actions.actions[Down])
{
movement -= up;
}
float speed = actions.actions[Fast] ? globals.runMultiplier : 1.0f;
if (SquareF(movement) > 0.0f)
{
cameraPosition +=
Normalise(movement) * globals.moveDeltaPerTick * speed;
}
then = now;
// light rotation
if (rotatingModel)
{
modelRotation.data += globals.modelRotationsPerTick;
}
}
// now you can make GL calls.
if (visible)
{
if (resized)
{
int width;
int height;
SDL_GetWindowSize(window, &width, &height);
glViewport(0, 0, width, height);
float ratio = 1.0f * width / height;
// Note: I got marching stairs at the intersection of planes
// due to loss of precision on my depth buffer.
// I need log2(far/near) bits before I start getting z-fighting.
g_projection = ProjectionMatrix(
Radians{90 * DegToRad},
ratio,
0.5f,
4096.0f);
resized = false;
}
glClearColor(0.1f,0.2f,0.1f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auto model = Ry(modelRotation) * Rx({modelRotation.data*2.0f});
// Get Matricies
auto view = LookAtRH(cameraPosition, yaw, pitch) * model;
// Note: I should be able to avoid an inverse
// calcuation of the view matrix due to the fact
// that the rotation part is orthonormal and therefore
// M^T = M^(-1). But for now, I'll just use inverse.
auto normalXform = Transpose(Inverse(view));
auto lightPosition = Vec4
{
20.0f,
100.0f,
20.0f,
0.0f,
};
auto lightDir = Normalise(-lightPosition);
// zero out the w row? Yes!
normalXform.data[3] = {0.0f, 0.0f, 0.0f, 0.0f};
// Matrix stuff was hard due to confusing documentation on the net.
// So I figured it out from first principles and wrote my own guide.
// https://gist.github.com/JodiTheTigger/477c776b30da21a6a123
auto viewMatrix = Transpose(view);
auto projMatrix = Transpose(g_projection);
auto normalMatrix = Transpose(normalXform);
glUseProgram(pO);
glUniformMatrix4fv(
lmodelViewMatrix,
1,
false,
&viewMatrix.data[0].data[0]);
glUniformMatrix4fv(
lprojMatrix,
1,
false,
&projMatrix.data[0].data[0]);
glUniformMatrix4fv(
lnormalMatrix,
1,
false,
&normalMatrix.data[0].data[0]);
glUniform3fv(
llightDir,
1,
&lightDir.data[0]);
glUniform4fv(
llightPos,
1,
&lightPosition.data[0]);
glDrawArrays(GL_TRIANGLES, 0, triangles.size() / 3);
SDL_GL_SwapWindow(window);
}
}
// Once finished with OpenGL functions, the SDL_GLContext can be deleted.
SDL_GL_DeleteContext(glcontext);
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
}