-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.cpp
1189 lines (1149 loc) · 61 KB
/
drive.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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// drive.cpp : Devon McKee
#define _USE_MATH_DEFINES
#include <glad.h>
#define GLFW_INCLUDE_NONE
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <GLFW/glfw3.h>
#pragma clang diagnostic pop
#include <chrono>
#include <vector>
#include <string>
#include <stdexcept>
#include "VecMat.h"
#include "GeomUtils.h"
#include "dCollisions.h"
#include "dCamera.h"
#include "dRenderPass.h"
#include "dMesh.h"
#include "dMisc.h"
#include "dSkybox.h"
#include "dParticles.h"
#include "dTextureDebug.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
#include "imgui/implot.h"
using std::vector;
using std::string;
using std::runtime_error;
using time_p = std::chrono::system_clock::time_point;
using sys_clock = std::chrono::system_clock;
using float_ms = std::chrono::duration<float, std::milli>;
GLFWwindow* window;
int windowed_width = 1000, windowed_height = 800;
int win_width = windowed_width, win_height = windowed_height;
GLFWmonitor* monitor;
const GLFWvidmode* videoModes;
GLFWvidmode currentVidMode;
int videoModesCount;
bool fullscreen = false;
bool frustumCulling = true;
bool showShadowMap = false;
float dt;
GLenum polygonMode = GL_FILL;
const int SHADOW_DIM = 16384;
RenderPass mainPass;
RenderPass mainPassInst;
RenderPass shadowPass;
RenderPass shadowPassInst;
RenderPass phongPass;
RenderPass phongPassInst;
GLuint shadowFramebuffer = 0;
GLuint shadowTexture = 0;
float lightColor[3] = { 1.0f, 1.0f, 1.0f };
float init_time;
const int PERF_MEMORY = 50;
static bool showPerformance = false;
float fps_x[PERF_MEMORY] = { 0.0f };
float fps_y[PERF_MEMORY] = { 0.0f };
float fps_shade[PERF_MEMORY] = { 0.0f };
const char* shadowVert = R"(
#version 410 core
layout(location = 0) in vec3 point;
uniform mat4 depth_vp;
uniform mat4 model;
uniform mat4 transform;
void main() {
gl_Position = depth_vp * transform * model * vec4(point, 1);
}
)";
const char* shadowVertInstanced = R"(
#version 410 core
layout(location = 0) in vec3 point;
layout(location = 3) in mat4 transform;
uniform mat4 depth_vp;
uniform mat4 model;
void main() {
gl_Position = depth_vp * transform * model * vec4(point, 1);
}
)";
const char* shadowFrag = R"(
#version 410 core
void main() {}
)";
const char* mainVert = R"(
#version 410 core
layout(location = 0) in vec3 point;
layout(location = 1) in vec2 uv;
out vec2 vUv;
out vec4 shadowCoord;
uniform mat4 model;
uniform mat4 transform;
uniform mat4 view;
uniform mat4 persp;
uniform mat4 depth_vp;
void main() {
shadowCoord = depth_vp * transform * model * vec4(point, 1);
vUv = uv;
gl_Position = persp * view * transform * model * vec4(point, 1);
}
)";
const char* mainVertInstanced = R"(
#version 410 core
layout (location = 0) in vec3 point;
layout (location = 1) in vec2 uv;
layout (location = 3) in mat4 transform;
out vec2 vUv;
out vec4 shadowCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 persp;
uniform mat4 depth_vp;
void main() {
shadowCoord = depth_vp * transform * model * vec4(point, 1);
vUv = uv;
gl_Position = persp * view * transform * model * vec4(point, 1);
}
)";
const char* mainFrag = R"(
#version 410 core
in vec2 vUv;
in vec4 shadowCoord;
out vec4 pColor;
uniform sampler2D txtr;
uniform sampler2DShadow shadow;
uniform vec4 ambient = vec4(vec3(0.1), 1);
uniform vec3 lightColor;
vec2 uniformSamples[16] = vec2[](vec2(-0.16696604, -0.09312990), vec2(0.53086904, 0.58433708), vec2(0.78145449, -0.86848999), vec2(0.74769790, 0.07194131), vec2(-0.09741326, 0.74185956), vec2(0.32706693, -0.03813042), vec2(0.73635845, 0.86372260), vec2(-0.27280913, 0.54069966), vec2(0.01584532, 0.26754421), vec2(-0.68606618, 0.53619244), vec2(-0.33333386, 0.35455430), vec2(-0.43123940, -0.60522600), vec2(0.03626988, -0.22807865), vec2(-0.53173498, 0.54256439), vec2(-0.65302623, 0.75253209), vec2(0.07282969, 0.19763551));
float random(vec4 seed) {
float dot_product = dot(seed, vec4(12.9898,78.233,45.164,94.673));
return fract(sin(dot_product) * 43758.5453);
}
float calcShadow(vec4 shadow_coord) {
// Perspective division and transforming shadow coord from [-1, 1] to [0, 1]
vec3 coord = shadow_coord.xyz / shadow_coord.w;
coord = coord * 0.5 + 0.5;
// Calculating total texel size and value at shadow
vec2 texelSize = 1.0 / textureSize(shadow, 0);
float shadowVal = texture(shadow, vec3(coord.xy, coord.z)) == 0.0f ? 0.4f : 1.0f;
// Early bailing on extra sampling if nearby values are the same (not on shadow edge)
bool different = false;
for (int x = -1; x <= 1; x += 2) {
for (int y = -1; y <= 1; y += 2) {
float diffVal = texture(shadow, vec3(coord.xy + vec2(x, y) * texelSize, coord.z)) == 0.0f ? 0.4f : 1.0f;
if (diffVal != shadowVal) different = true;
}
}
if (!different) return shadowVal == 1.0f ? 1.0f : shadowVal / 5.0;
// If on shadow edge, sample using nearby precalculated uniform random coordinates
for (int i = 0; i < 16; i++) {
int ind = int(float(16)*random(vec4(gl_FragCoord.xyy, i))) % 16;
shadowVal += texture(shadow, vec3(coord.xy + uniformSamples[ind] * texelSize, coord.z)) == 0.0f ? 0.4f : 1.0f;
}
return shadowVal / 21.0f;
}
void main() {
pColor = ambient + texture(txtr, vUv) * vec4(lightColor, 1) * calcShadow(shadowCoord);
}
)";
const char* phongVert = R"(
#version 410 core
layout (location = 0) in vec3 point;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normal;
out vec3 vPoint;
out vec2 vUv;
out vec3 vNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 persp;
void main() {
vPoint = (view * model * vec4(point, 1)).xyz;
vNormal = (view * model * vec4(normal, 0)).xyz;
vUv = uv;
gl_Position = persp * vec4(vPoint, 1);
}
)";
const char* phongVertInstanced = R"(
#version 410 core
layout (location = 0) in vec3 point;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normal;
layout (location = 3) in mat4 transform;
out vec3 vPoint;
out vec2 vUv;
out vec3 vNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 persp;
void main() {
vPoint = (view * transform * model * vec4(point, 1)).xyz;
vNormal = (view * transform * model * vec4(normal, 0)).xyz;
vUv = uv;
gl_Position = persp * vec4(vPoint, 1);
}
)";
const char* phongFrag = R"(
#version 410 core
in vec3 vPoint;
in vec2 vUv;
in vec3 vNormal;
out vec4 pColor;
uniform vec3 light = vec3(-.2, .1, -3);
uniform sampler2D txtr;
void main() {
vec3 n = normalize(vNormal);
vec3 l = normalize(light - vPoint);
vec3 e = normalize(vPoint);
vec3 r = reflect(l, n);
float d = abs(dot(n, l));
float s = abs(dot(r, e));
float intensity = clamp(d+pow(s, 50), 0, 1);
pColor = vec4(intensity * texture(txtr, vUv).rgb, 1);
}
)";
vector<Skybox> skyboxes;
vector<string> skyboxPaths {
"textures/skybox/humble/",
"textures/skybox/classic-land/",
"textures/skybox/maskonaive/",
"textures/skybox/empty-space/",
"textures/skybox/dusk-ocean/",
};
vector<string> skyboxNames {
"Humble",
"Classic Land",
"Maskonaive",
"Empty Space",
"Dusk Ocean",
};
int cur_skybox = 0;
dParticles particleSystem;
Camera camera(win_width, win_height, 60, 0.5f, 2000.0f);
int camera_type = 1;
// 1 - Third person chase camera
// 2 - Top-down camera of arena
// 3 - Hood camera
// 4 - Frozen free camera, moved right behind car
Mesh floor_mesh;
vector<vec3> floor_points = { {-1, 0, -1}, {1, 0, -1}, {1, 0, 1}, {-1, 0, 1} };
vector<vec3> floor_normals = { {0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {0, 1, 0} };
vector<vec2> floor_uvs = { {0, 0}, {1, 0}, {1, 1}, {0, 1} };
vector<int3> floor_triangles = { {2, 1, 0}, {0, 3, 2} };
Mesh large_tree_mesh;
vector<vec3> large_tree_instance_positions {
{49, 0, 37}, {50, 0, 32}, {29, 0, 15}, {50, 0, 10},
{-10, 0, 32}, {-34, 0, 7}, {15, 0, -13}, {6, 0, -15},
{-0.75f, 0, 0.3f}, {-9.11f, 0, -8.64f}, {-16.16f, 0, -12.58f}, {-27.02f, 0, -15.88f},
{-32.39f, 0, -7.18f}, {-22.69f, 0, -24.82f}, {-15.40f, 0, -31.95f}, {-8.23f, 0, -33.90f},
{-7.48f, 0, -48.32f}, {7.70f, 0, -34.07f}, {49.83f, 0, -22.92f}, {54.68f, 0, -30.48f},
{52.80f, 0, -38.50f}, {46.54f, 0, -49.25f}, {36.14f, 0, -50.60f}, {26.13f, 0, -51.73f},
{10.83f, 0, -51.97f}, {-13.78f, 0, -33.34f}, {-25.02f, 0, -24.78f}, {-31.62f, 0, -16.71f},
{-29.06f, 0, -6.59f}, {7.55f, 0, 10.67f}, {25.84f, 0, 21.15f}, {23.49f, 0, 34.07f},
{7.52f, 0, 46.78f}, {-2.10f, 0, 54.42f}, {-12.51f, 0, 54.03f}, {-21.23f, 0, 54.79f},
{-32.39f, 0, 54.86f}, {-37.58f, 0, 46.49f}, {-36.75f, 0, 35.87f}, {-41.17f, 0, 30.20f},
{-52.15f, 0, 31.90f}, {-57.76f, 0, 14.15f}, {-58.13f, 0, 3.82f}, {-58.32f, 0, -4.81f},
{-58.50f, 0, -15.75f}, {-58.31f, 0, -26.44f}, {-58.05f, 0, -38.86f}, {-57.40f, 0, -49.04f},
{-34.08f, 0, -27.36f}, {-35.94f, 0, -16.43f}, {-31.61f, 0, -12.72f}, {-22.11f, 0, -10.64f},
{-11.82f, 0, -11.64f}, {-10.84f, 0, -21.56f}, {-17.93f, 0, -28.87f}, {-25.40f, 0, -33.24f},
{-34.90f, 0, -30.52f}, {-8.35f, 0, 9.46f}, {-7.34f, 0, 21.43f}, {-7.45f, 0, 35.74f},
{-16.61f, 0, 39.66f}, {7.60f, 0, 16.11f}, {12.85f, 0, 11.31f}, {23.16f, 0, 8.94f},
{23.09f, 0, 17.21f}, {33.01f, 0, 7.78f}, {31.65f, 0, 19.08f}, {30.85f, 0, 25.40f},
{35.05f, 0, 33.10f}, {29.93f, 0, 34.80f}, {22.32f, 0, 41.41f}, {6.87f, 0, 36.39f},
{6.92f, 0, 55.30f}, {12.63f, 0, 57.65f}, {22.51f, 0, 57.53f}, {29.38f, 0, 54.41f},
{41.66f, 0, 56.19f}, {51.12f, 0, 53.26f}, {55.26f, 0, 46.08f}, {50.39f, 0, 42.98f},
{53.33f, 0, 37.18f}, {56.60f, 0, 29.23f}, {50.68f, 0, 23.18f}, {47.43f, 0, 17.28f},
{53.80f, 0, 13.17f}, {57.79f, 0, 2.03f}, {53.03f, 0, -7.96f}, {54.85f, 0, -20.03f},
{53.67f, 0, -44.20f}, {35.25f, 0, -35.85f}, {25.01f, 0, 17.04f}, {27.80f, 0, 18.07f},
{30.01f, 0, 21.32f}, {27.01f, 0, 24.33f}, {-7.86f, 0, -15.65f}, {-14.89f, 0, -17.18f},
{-24.90f, 0, -20.11f}, {-31.91f, 0, -20.91f}, {-57.89f, 0, 9.50f}, {-59.31f, 0, 19.05f},
{-58.51f, 0, 25.29f}, {-56.70f, 0, 35.26f}, {-50.98f, 0, 38.97f}, {-46.18f, 0, 33.34f},
{-38.07f, 0, 26.96f}, {-34.56f, 0, 31.28f}, {-34.16f, 0, 37.87f}, {-36.81f, 0, 42.84f},
{-41.31f, 0, 42.38f}, {-32.40f, 0, 49.67f}, {-23.46f, 0, 57.46f}, {-14.06f, 0, 56.53f},
{-2.28f, 0, 53.18f}, {6.61f, 0, 50.97f}, {14.48f, 0, 53.34f}, {19.61f, 0, 54.60f},
{27.19f, 0, 56.32f}, {45.39f, 0, 57.37f}, {51.48f, 0, 48.51f}, {53.57f, 0, 42.74f},
{55.55f, 0, 36.85f}, {54.38f, 0, 26.52f}, {55.33f, 0, 19.61f}, {54.74f, 0, 6.12f},
{49.12f, 0, -5.46f}, {46.48f, 0, -14.67f}, {56.58f, 0, -27.82f}, {54.64f, 0, -40.71f},
{49.18f, 0, -45.38f}, {40.86f, 0, -49.48f}, {25.39f, 0, -49.89f}, {13.77f, 0, -49.16f},
{-2.02f, 0, -51.96f}, {11.01f, 0, 7.26f}, {13.33f, 0, 8.87f}, {16.48f, 0, 10.46f},
{19.92f, 0, 11.65f}, {26.48f, 0, 12.77f}, {32.18f, 0, 10.76f}, {35.11f, 0, 6.94f},
{31.87f, 0, 16.33f}, {7.90f, 0, 38.96f}, {8.29f, 0, 42.33f}, {2.28f, 0, 51.69f},
{-6.42f, 0, 56.59f}, {-28.42f, 0, 53.62f}, {-38.48f, 0, 52.90f}, {-41.16f, 0, 52.04f},
{-45.38f, 0, 48.90f}, {-46.25f, 0, 45.71f}, {-45.19f, 0, 42.25f}, {-43.87f, 0, 36.51f},
{-44.07f, 0, 31.30f}, {-47.49f, 0, 27.07f}, {-51.11f, 0, 27.18f}, {-54.14f, 0, 31.55f},
{-53.87f, 0, 37.11f}, {-53.30f, 0, 41.10f}, {-57.40f, 0, 18.26f}, {-56.87f, 0, 11.89f},
{-57.14f, 0, 7.42f}, {-56.71f, 0, 0.37f}, {-57.52f, 0, -19.77f}, {-57.82f, 0, -30.18f},
{-57.25f, 0, -42.02f}, {-42.21f, 0, -58.22f}, {-31.70f, 0, -57.72f}, {-13.13f, 0, -57.26f},
{-5.69f, 0, -57.26f}, {4.19f, 0, -57.32f}, {12.61f, 0, -57.31f}, {20.29f, 0, -56.96f},
{25.05f, 0, -56.66f}, {30.72f, 0, -56.69f}, {34.94f, 0, -56.89f}, {39.91f, 0, -56.25f},
{42.73f, 0, -54.73f}, {45.96f, 0, -52.28f}, {49.24f, 0, -50.56f}, {52.45f, 0, -50.05f},
{52.41f, 0, -56.66f}, {46.12f, 0, -53.55f}, {42.78f, 0, -52.05f}, {34.30f, 0, -51.96f},
{29.32f, 0, -46.49f}, {47.28f, 0, -39.51f}, {49.31f, 0, -35.55f}, {50.26f, 0, -31.58f},
{51.32f, 0, -27.77f}, {52.52f, 0, -22.80f}, {53.24f, 0, -17.62f}, {55.14f, 0, -11.74f},
{56.41f, 0, -6.88f}, {51.25f, 0, 12.24f}, {49.11f, 0, 19.59f}, {48.08f, 0, 24.16f},
{49.38f, 0, 29.80f}, {53.92f, 0, 33.04f}, {57.16f, 0, 36.24f}, {56.22f, 0, 48.83f},
{47.80f, 0, 53.40f}, {37.92f, 0, 54.39f}, {20.33f, 0, 55.91f}, {10.11f, 0, 54.36f},
{-26.37f, 0, 54.01f}, {34.43f, 0, 54.40f}, {31.47f, 0, 57.06f}, {53.41f, 0, 54.71f},
{55.33f, 0, 41.25f}, {50.85f, 0, 35.64f}, {49.49f, 0, 25.79f}, {51.63f, 0, 18.89f},
{53.68f, 0, 1.17f}, {54.13f, 0, -3.98f}, {57.81f, 0, -15.81f}, {58.67f, 0, -19.96f},
{55.51f, 0, -35.65f}, {48.99f, 0, -40.59f}, {46.78f, 0, -44.15f}, {43.45f, 0, -47.26f},
{34.74f, 0, -48.75f}, {30.60f, 0, -49.21f}, {22.94f, 0, -48.71f}, {18.03f, 0, -49.42f},
{11.44f, 0, -53.66f}, {7.85f, 0, -54.97f}, {2.40f, 0, -54.92f}, {-4.58f, 0, -54.76f},
{-18.88f, 0, -57.27f}, {-24.03f, 0, -58.32f}, {-36.24f, 0, -58.71f}, {-46.70f, 0, -57.97f},
{-53.89f, 0, -57.15f}, {-58.09f, 0, -45.90f}, {-56.46f, 0, -36.90f}, {-56.69f, 0, -26.61f},
{-56.60f, 0, -14.89f}, {-56.29f, 0, 22.82f}, {-51.58f, 0, 45.38f}, {-43.59f, 0, 53.44f},
{-38.02f, 0, 56.29f}, {-18.65f, 0, 55.90f}, {-1.49f, 0, 57.18f}, {5.27f, 0, 58.12f},
{32.96f, 0, 56.77f}, {46.31f, 0, 51.64f}, {47.90f, 0, 41.29f}, {35.89f, 0, 38.23f},
{30.27f, 0, 39.04f}, {26.93f, 0, 38.70f}, {23.19f, 0, 31.18f}, {27.90f, 0, 28.60f},
{32.41f, 0, 27.79f}, {33.90f, 0, 14.39f}, {27.47f, 0, 9.37f}, {8.09f, 0, -6.20f},
{8.81f, 0, -13.83f}, {6.51f, 0, -22.14f}, {6.91f, 0, -24.57f}, {7.72f, 0, -27.59f},
{16.58f, 0, -5.08f}, {27.26f, 0, -5.23f},
};
vector<mat4> large_tree_instance_transforms;
int num_culled_large_trees = 0;
Mesh grass_mesh;
vector<vec3> grass_instance_positions {
{7.79f, 0, -5.38f}, {5.27f, 0, -8.41f}, {-6.32f, 0, -8.58f}, {-9.40f, 0, -5.62f},
{-9.49f, 0, 5.47f}, {-6.46f, 0, 8.56f}, {4.59f, 0, 9.61f}, {7.51f, 0, 6.12f},
{12.07f, 0, 5.49f}, {15.67f, 0, 5.87f}, {20.84f, 0, 5.78f}, {23.66f, 0, 10.43f},
{16.71f, 0, 11.87f}, {-7.01f, 0, 17.79f}, {-7.03f, 0, 27.66f}, {-7.15f, 0, 37.66f},
{-18.52f, 0, 41.08f}, {-21.48f, 0, 34.07f}, {-20.76f, 0, 22.88f}, {-30.34f, 0, 15.04f},
{-36.73f, 0, 14.27f}, {-43.83f, 0, 14.90f}, {-44.92f, 0, 9.24f}, {-35.86f, 0, -6.22f},
{-25.08f, 0, -8.68f}, {-14.91f, 0, -15.59f}, {-10.73f, 0, -23.47f}, {-11.57f, 0, -30.97f},
{-20.33f, 0, -33.76f}, {-31.8f, 0, -28.42f}, {13.06f, 0, -5.47f}, {20.72f, 0, -5.45f},
{31.65f, 0, -4.70f}, {35.97f, 0, -33.82f}, {25.40f, 0, -36.43f}, {9.04f, 0, 8.93f},
{7.82f, 0, -36.36f}, {5.39f, 0, -23.72f}, {-9.14f, 0, -11.88f}, {-20.57f, 0, -12.46f},
{-32.20f, 0, -20.17f}, {-20.66f, 0, -26.84f}, {4.43f, 0, -47.64f}, {3.93f, 0, -53.39f},
{0.33f, 0, -57.04f}, {-7.27f, 0, -51.45f}, {-10.98f, 0, -48.05f}, {16.12f, 0, -50.30f},
{30.95f, 0, -49.04f}, {45.22f, 0, -46.89f}, {49.36f, 0, -38.55f}, {50.14f, 0, -25.47f},
{51.44f, 0, -17.93f}, {48.32f, 0, -10.58f}, {52.20f, 0, -5.34f}, {55.19f, 0, 2.71f},
{53.23f, 0, 10.13f}, {48.65f, 0, 19.89f}, {53.20f, 0, 29.25f}, {52.94f, 0, 39.64f},
{40.97f, 0, 53.80f}, {33.35f, 0, 55.97f}, {22.45f, 0, 55.02f}, {12.37f, 0, 52.92f},
{4.89f, 0, 51.58f}, {7.12f, 0, 44.37f}, {8.70f, 0, 35.15f}, {8.79f, 0, 15.86f},
{8.65f, 0, -8.66f}, {5.71f, 0, -19.49f}, {-7.94f, 0, -18.39f}, {-12.41f, 0, -13.37f},
{-18.98f, 0, -6.11f}, {-23.53f, 0, -13.22f}, {-18.43f, 0, -21.01f}, {-12.61f, 0, -26.95f},
{-20.56f, 0, -31.51f}, {-25.20f, 0, -27.63f}, {-26.66f, 0, -18.66f}, {-28.89f, 0, -11.67f},
{-35.18f, 0, -10.46f}, {-35.20f, 0, -24.29f}, {-42.69f, 0, 6.82f}, {-33.15f, 0, 27.00f},
{-33.92f, 0, 33.73f}, {-33.91f, 0, 43.06f}, {-33.89f, 0, 49.01f}, {-37.87f, 0, 51.80f},
{-41.86f, 0, 46.81f}, {-39.06f, 0, 37.08f}, {-37.24f, 0, 30.39f}, {-45.18f, 0, 26.06f},
{-52.10f, 0, 28.20f}, {-51.13f, 0, 35.53f}, {-48.92f, 0, 42.02f}, {-44.19f, 0, 49.47f},
{-48.15f, 0, 51.39f}, {-52.47f, 0, 45.00f}, {-19.89f, 0, 37.80f}, {-8.92f, 0, 40.31f},
{6.92f, 0, 40.09f}, {21.51f, 0, 40.12f}, {31.11f, 0, 42.29f}, {34.82f, 0, 36.45f},
{35.20f, 0, 30.16f}, {34.46f, 0, 21.91f}, {33.01f, 0, 14.88f}, {31.73f, 0, 7.26f},
{18.89f, 0, 8.64f}, {11.94f, 0, 10.72f}, {6.14f, 0, 13.45f}, {19.11f, 0, 15.25f},
{25.39f, 0, 18.05f}, {27.88f, 0, 24.53f}, {24.49f, 0, 30.18f}, {-15.33f, 0, -9.27f},
{-17.74f, 0, -15.42f}, {-21.91f, 0, -19.81f}, {-28.09f, 0, -25.55f}, {-35.06f, 0, -18.19f},
{-31.77f, 0, -11.53f}, {-17.96f, 0, -8.67f}, {-7.88f, 0, -29.60f}, {-14.56f, 0, -29.49f},
{-16.12f, 0, -34.21f}, {-21.71f, 0, 28.06f}, {6.18f, 0, 37.26f}, {7.01f, 0, 49.03f},
{10.97f, 0, 55.62f}, {17.51f, 0, 56.45f}, {49.75f, 0, 49.19f}, {53.50f, 0, 33.92f},
{49.00f, 0, 25.35f}, {53.92f, 0, 22.33f}, {49.92f, 0, 17.05f}, {53.79f, 0, 16.88f},
{56.55f, 0, 13.86f}, {56.15f, 0, 8.20f}, {50.05f, 0, 6.49f}, {33.86f, 0, 10.72f},
{26.73f, 0, 12.74f}, {23.75f, 0, 14.55f}, {29.74f, 0, 18.33f}, {28.37f, 0, 7.24f},
{34.98f, 0, -7.36f}, {26.29f, 0, -5.06f}, {48.05f, 0, -21.12f}, {52.87f, 0, -23.11f},
{54.21f, 0, -27.11f}, {49.96f, 0, -33.09f}, {49.25f, 0, -43.09f}, {49.27f, 0, -49.34f},
{43.54f, 0, -53.94f}, {34.86f, 0, -55.65f}, {31.18f, 0, -54.53f}, {23.49f, 0, -47.76f},
{19.93f, 0, -52.79f}, {16.68f, 0, -57.96f}, {11.80f, 0, -57.26f}, {9.42f, 0, -53.46f},
{10.55f, 0, -47.26f}, {16.66f, 0, -47.86f}, {7.30f, 0, -56.81f}, {0.79f, 0, -55.33f},
{-6.02f, 0, -54.50f}, {-11.82f, 0, -57.15f}, {-22.68f, 0, -58.74f}, {-30.38f, 0, -57.70f},
{-40.51f, 0, -57.49f}, {-53.80f, 0, -57.32f}, {-57.84f, 0, -53.29f}, {-58.09f, 0, -44.97f},
{-58.10f, 0, -32.31f}, {-57.77f, 0, -20.81f}, {-57.48f, 0, -11.24f}, {-57.76f, 0, -0.54f},
{-58.23f, 0, 8.00f}, {-57.65f, 0, 20.25f}, {-56.34f, 0, 27.91f}, {-12.13f, 0, -7.25f},
{-10.14f, 0, -17.27f}, {-13.09f, 0, -21.82f}, {-17.47f, 0, -25.97f}, {-23.17f, 0, -31.02f},
{-31.75f, 0, -32.83f}, {-31.41f, 0, -24.12f}, {-25.43f, 0, -21.01f}, {-19.74f, 0, -23.85f},
{-16.54f, 0, -25.51f}, {-7.47f, 0, -25.94f}, {-9.09f, 0, -20.63f}, {-20.19f, 0, -17.30f},
{-30.55f, 0, -8.13f}, {-24.77f, 0, -6.63f}, {-13.41f, 0, -7.41f}, {12.13f, 0, -7.70f},
{48.80f, 0, -29.86f}, {54.54f, 0, -13.45f}, {55.77f, 0, -6.18f}, {57.66f, 0, -15.36f},
{54.96f, 0, -34.84f}, {52.84f, 0, -46.37f}, {50.84f, 0, -54.69f}, {37.50f, 0, -53.79f},
{30.17f, 0, -50.93f}, {25.60f, 0, -55.13f}, {20.31f, 0, -56.20f}, {15.10f, 0, -53.24f},
{10.99f, 0, -50.25f}, {-0.23f, 0, -53.39f}, {-4.14f, 0, -56.11f}, {-11.35f, 0, -52.83f},
{-33.86f, 0, -35.34f}, {-36.90f, 0, -22.12f}, {-37.35f, 0, -10.74f}, {-21.66f, 0, 24.40f},
{-21.72f, 0, 39.58f}, {-31.84f, 0, 51.97f}, {-36.49f, 0, 54.49f}, {-34.69f, 0, 56.44f},
{-27.99f, 0, 54.64f}, {-20.92f, 0, 53.10f}, {-16.84f, 0, 56.33f}, {-12.06f, 0, 57.21f},
{-9.39f, 0, 53.56f}, {-4.92f, 0, 54.34f}, {2.20f, 0, 56.54f}, {8.00f, 0, 52.92f},
{9.49f, 0, 57.30f}, {15.74f, 0, 54.92f}, {24.05f, 0, 41.10f}, {25.94f, 0, 36.21f},
{27.32f, 0, 32.39f}, {29.15f, 0, 27.36f}, {32.31f, 0, 21.46f}, {35.16f, 0, 24.48f},
{32.21f, 0, 32.18f}, {32.75f, 0, 36.64f}, {30.44f, 0, 40.23f}, {23.81f, 0, 41.49f},
{19.42f, 0, 37.20f}, {-33.55f, 0, 14.57f}, {-27.67f, 0, 14.98f}, {-29.79f, 0, 14.04f},
{-32.18f, 0, 13.55f}, {-35.26f, 0, 13.33f}, {-38.98f, 0, 13.40f}, {-44.59f, 0, 12.91f},
{-46.66f, 0, 7.57f}, {-44.61f, 0, 5.95f}, {-40.08f, 0, 5.57f}, {-27.39f, 0, -5.66f},
{-26.85f, 0, -9.52f}, {-26.07f, 0, -12.88f}, {-24.40, 0, -16.34f}, {-23.27f, 0, -20.96f},
{-27.79f, 0, -23.86f}, {-29.43f, 0, -21.98f}, {-30.33f, 0, -18.65f}, {-30.18f, 0, -15.58f},
{-31.41f, 0, -13.76f}, {-34.75f, 0, -12.85f}, {-36.49f, 0, -14.15f}, {-36.92f, 0, -18.37f},
{-36.22f, 0, -20.76f}, {-34.27f, 0, -22.29f}, {-32.51f, 0, -24.84f}, {-31.13f, 0, -26.86f},
{-29.61f, 0, -29.13f}, {-28.76f, 0, -32.15f}, {-28.27f, 0, -34.72f}, {-26.58f, 0, -36.07f},
{-22.98f, 0, -35.24f}, {-22.26f, 0, -33.07f}, {-21.23f, 0, -29.38f}, {-18.45f, 0, -26.65f},
{-13.72f, 0, -25.51f}, {-10.28f, 0, -25.43f}, {-5.84f, 0, -27.94f}, {-6.24f, 0, -32.47f},
{-6.26f, 0, -35.59f}, {-10.39f, 0, -36.67f}, {-14.25f, 0, -36.43f}, {-17.40f, 0, -36.38f},
{-22.20f, 0, -36.52f}, {-27.87f, 0, -36.81f}, {-36.01f, 0, -36.67f}, {-17.48f, 0, -18.48f},
{-21.01f, 0, -21.98f}, {-25.72f, 0, -23.66f}, {-28.87f, 0, -19.30f}, {-28.49f, 0, -13.54f},
{-26.28f, 0, -10.59f}, {-22.39f, 0, -8.57f}, {-16.60f, 0, -5.67f}, {-13.35f, 0, -4.84f},
{-7.64f, 0, -6.07f}, {-6.41f, 0, -11.05f}, {-9.05f, 0, -14.32f}, {-12.80f, 0, -16.71f},
{-15.39f, 0, -19.39f}, {-16.52f, 0, -22.11f}, {-15.72f, 0, -27.53f}, {-18.79f, 0, -34.71f},
{-16.88f, 0, -23.59f}, {-14.52f, 0, -20.90f}, {-12.08f, 0, -16.92f}, {-7.35f, 0, -12.63f},
{-5.75f, 0, -17.78f}, {-5.84f, 0, -21.74f}, {6.76f, 0, -47.18f}, {7.78f, 0, -50.94f},
{4.30f, 0, -54.88f}, {-0.90f, 0, -56.83f}, {-6.28f, 0, -58.10f}, {-10.86f, 0, -54.56f},
{-8.83f, 0, -50.10f}, {-19.19f, 0, -56.78f}, {-16.15f, 0, -58.57f}, {-12.47f, 0, -49.55f},
{4.79f, 0, -12.65f}, {9.41f, 0, -10.75f}, {10.83f, 0, -6.25f}, {4.76f, 0, -11.12f},
{3.96f, 0, -20.00f}, {4.69f, 0, -27.87f}, {5.04f, 0, -33.21f}, {10.44f, 0, -36.83f},
{27.54f, 0, -35.99f}, {36.46f, 0, -21.07f}, {36.40f, 0, -17.21f}, {35.67f, 0, -11.70f},
{35.96f, 0, -4.80f}, {29.95f, 0, -6.10f}, {24.39f, 0, -5.66f}, {18.90f, 0, -5.31f},
{11.17f, 0, -5.14f}, {-7.37f, 0, 12.00f}, {-6.71f, 0, 15.04f}, {-5.78f, 0, 19.91f},
{-5.51f, 0, 24.88f}, {-5.64f, 0, 30.31f}, {-5.64f, 0, 35.28f}, {-5.81f, 0, 39.78f},
{-10.05f, 0, 42.11f}, {-13.67f, 0, 41.83f}, {-20.10f, 0, 42.18f}, {-22.22f, 0, 37.72f},
{-20.33f, 0, 34.77f}, {-21.37f, 0, 31.31f}, {-20.09f, 0, 26.05f}, {-20.46f, 0, 20.12f},
{-39.62f, 0, 15.22f}, {-42.69f, 0, 12.81f}, {-42.90f, 0, 9.50f}, {-56.69f, 0, 21.79f},
{-56.43f, 0, 24.90f}, {-52.67f, 0, 26.47f}, {-48.11f, 0, 26.70f}, {-44.68f, 0, 28.87f},
{-41.58f, 0, 26.45f}, {-38.73f, 0, 24.67f}, {-34.97f, 0, 29.57f}, {-34.64f, 0, 37.02f},
{-33.14f, 0, 46.21f}, {-25.28f, 0, 56.25f}, {-9.07f, 0, 56.45f}, {-2.69f, 0, 57.27f},
{3.69f, 0, 58.65f}, {16.90f, 0, 58.91f}, {27.23f, 0, 58.25f}, {36.18f, 0, 56.12f},
{43.06f, 0, 52.91f}, {48.84f, 0, 48.37f}, {52.10f, 0, 44.23f}, {55.49f, 0, 39.55f},
{56.85f, 0, 34.40f}, {54.39f, 0, 28.08f}, {52.06f, 0, 21.57f}, {51.39f, 0, 15.64f},
{52.38f, 0, 6.39f}, {53.44f, 0, 0.50f}, {55.45f, 0, -9.32f}, {56.91f, 0, -18.42f},
{57.29f, 0, -25.93f}, {54.85f, 0, -37.79f}, {51.45f, 0, -44.24f}, {43.59f, 0, -49.36f},
{36.16f, 0, -52.86f}, {28.66f, 0, -56.44f}, {22.47f, 0, -57.88f}, {17.48f, 0, -56.27f},
{18.20f, 0, -48.93f}, {26.47f, 0, -47.31f}, {34.46f, 0, -47.86f}, {42.41f, 0, -51.44f},
{49.29f, 0, -36.01f}, {47.09f, 0, -27.21f}, {49.26f, 0, -18.03f}, {56.10f, 0, -4.74f},
{56.14f, 0, -4.00f}, {53.54f, 0, 1.52f}, {47.41f, 0, 7.95f}, {46.24f, 0, 21.65f},
{38.94f, 0, -47.43f}, {33.79f, 0, -49.83f}, {30.55f, 0, -52.44f}, {27.22f, 0, -54.78f},
{22.52f, 0, -55.69f}, {17.95f, 0, -55.24f}, {13.81f, 0, -55.44f}, {9.36f, 0, -56.34f},
{2.66f, 0, -56.98f}, {-2.99f, 0, -57.04f}, {-9.30f, 0, -57.38f}, {-15.63f, 0, -57.94f},
{-20.38f, 0, -58.25f}, {-25.96f, 0, -58.29f}, {-34.27f, 0, -58.04f}, {-39.78f, 0, -58.01f},
{-45.41f, 0, -58.31f}, {-52.74f, 0, -57.31f}, {-56.38f, 0, -55.73f}, {-58.71f, 0, -46.41f},
{-56.91f, 0, -40.70f}, {-56.34f, 0, -35.57f}, {-56.91f, 0, -26.94f}, {-56.76f, 0, -23.05f},
{-56.58f, 0, -16.76f}, {-56.61f, 0, -12.46f}, {-56.59f, 0, -6.18f}, {-56.64f, 0, -1.49f},
{-56.39f, 0, 8.65f}, {-56.33f, 0, 12.85f}, {-56.32f, 0, 17.78f}, {-54.17f, 0, 28.98f},
{-48.91f, 0, 29.00f}, {-45.86f, 0, 27.89f}, {-34.40f, 0, 26.29f}, {-36.32f, 0, 28.00f},
{-39.20f, 0, 31.56f}, {-41.69f, 0, 34.52f}, {-44.07f, 0, 37.33f}, {-45.98f, 0, 40.27f},
{-47.25f, 0, 43.02f}, {-47.96f, 0, 46.18f}, {-47.64f, 0, 48.80f}, {-45.17f, 0, 51.62f},
{-42.54f, 0, 52.05f}, {-38.97f, 0, 50.31f}, {-36.42f, 0, 47.81f}, {-34.10f, 0, 40.85f},
{-30.29f, 0, 57.02f}, {-20.73f, 0, 56.91f}, {-7.47f, 0, 57.99f}, {-4.24f, 0, 57.32f},
{1.91f, 0, 54.61f}, {13.65f, 0, 54.87f}, {30.86f, 0, 57.23f}, {37.79f, 0, 54.56f},
{46.25f, 0, 52.50f}, {51.37f, 0, 50.67f}, {56.74f, 0, 35.82f}, {53.26f, 0, 31.24f},
{50.68f, 0, 28.23f}, {47.81f, 0, 22.54f}, {50.64f, 0, 13.15f}, {51.93f, 0, 9.73f},
{54.83f, 0, -1.94f}, {55.71f, 0, -13.07f}, {56.62f, 0, -21.19f}, {52.35f, 0, -30.55f},
{48.46f, 0, -40.50f}, {53.61f, 0, -48.78f}, {5.09f, 0, 16.62f}, {9.34f, 0, 12.47f},
{12.81f, 0, 13.42f}, {15.58f, 0, 14.36f}, {19.47f, 0, 14.14f}, {22.79f, 0, 13.22f},
{28.75f, 0, 11.69f}, {33.18f, 0, 12.77f}, {33.85f, 0, 17.45f}, {33.40f, 0, 24.04f},
{32.91f, 0, 27.34f}, {32.58f, 0, 34.53f}, {32.69f, 0, 39.06f}, {34.38f, 0, 41.43f},
{35.74f, 0, 39.22f}, {35.91f, 0, 37.12f}, {36.90f, 0, 32.56f}, {37.51f, 0, 29.22f},
{32.64f, 0, 26.35f}, {27.75f, 0, 34.76f}, {27.28f, 0, 36.89f}, {26.97f, 0, 38.37f},
{24.39f, 0, 38.93f}, {22.82f, 0, 36.59f}, {21.97f, 0, 34.31f}, {22.24f, 0, 30.07f},
{23.61f, 0, 25.81f}, {23.95f, 0, 22.37f}, {16.42f, 0, 15.65f}, {14.28f, 0, 15.42f},
{12.38f, 0, 15.56f}, {6.59f, 0, 19.50f}, {4.14f, 0, 23.44f}, {4.71f, 0, 31.14f},
{5.15f, 0, 34.47f}, {5.30f, 0, 38.29f}, {5.30f, 0, 41.84f}, {5.20f, 0, 45.10f},
{4.66f, 0, 48.77f}, {-0.55f, 0, 55.32f}, {-9.01f, 0, 57.66f}, {-17.88f, 0, 56.68f},
{-24.87f, 0, 52.94f}, {-27.82f, 0, 51.87f}, {-34.80f, 0, 51.60f}, {-41.61f, 0, 51.37f},
{-50.01f, 0, 46.54f}, {-49.57f, 0, 43.92f}, {-48.88f, 0, 38.37f}, {-48.94f, 0, 35.12f},
{-49.97f, 0, 31.50f}, {-51.18f, 0, 29.02f}, {-55.23f, 0, 24.71f}, {-46.65f, 0, 14.44f},
{-45.64f, 0, 10.87f}, {-45.77f, 0, 9.32f}, {-46.22f, 0, 4.41f}, {-35.55f, 0, -9.57f},
{-32.86f, 0, -9.59f}, {-29.92f, 0, -10.06f}, {-23.26f, 0, -14.88f}, {-22.00f, 0, -17.16f},
{-19.81f, 0, -21.49f}, {-19.55f, 0, -29.35f}, {-20.39f, 0, -35.36f}, {-17.33f, 0, -32.07f},
{-15.32f, 0, -28.43f}, {-11.14f, 0, -25.83f}, {5.46f, 0, -29.87f}, {7.75f, 0, -30.83f},
{23.47f, 0, -36.36f}, {47.78f, 0, -28.33f}, {50.18f, 0, -27.19f}, {52.96f, 0, -25.91f},
{57.32f, 0, -23.95f}, {56.58f, 0, -31.89f}, {54.45f, 0, -33.92f}, {52.47f, 0, -36.03f},
{50.95f, 0, -38.36f}, {51.49f, 0, -46.06f}, {52.44f, 0, -48.50f}, {53.00f, 0, -51.11f},
{52.91f, 0, -53.58f}, {50.07f, 0, -57.54f}, {45.45f, 0, -57.41f}, {43.36f, 0, -56.52f},
{40.55f, 0, -55.41f}, {37.62f, 0, -54.58f}, {34.99f, 0, -54.38f}, {32.68f, 0, -54.87f},
{25.77f, 0, -58.26f}, {19.76f, 0, -58.25f}, {13.74f, 0, -56.77f}, {5.09f, 0, -56.43f},
{-7.84f, 0, -56.52f}, {-18.55f, 0, -57.89f}, {-27.67f, 0, -57.99f}, {-36.49f, 0, -58.12f},
{-41.07f, 0, -58.32f}, { 21.57f, 0, 38.42f }, { 26.00f, 0, 41.18f }, { 28.12f, 0, 41.85f },
{ 33.05f, 0, 40.88f }, { 33.18f, 0, 36.80f }, { 30.38f, 0, 31.58f }, { 31.19f, 0, 30.01f },
{ 36.97f, 0, 27.68f }, { 34.90f, 0, 26.15f }, { 31.79f, 0, 23.68f }, { 27.49f, 0, 22.34f },
{ 24.90f, 0, 23.55f }, { 23.57f, 0, 19.80f }, { 21.60f, 0, 17.56f }, { 21.48f, 0, 15.50f },
{ 21.68f, 0, 11.71f }, { 21.55f, 0, 9.13f }, { 18.56f, 0, 5.81f }, { 15.87f, 0, 8.03f },
{ -5.63f, 0, 22.07f }, { -6.87f, 0, 23.27f }, { -7.47f, 0, 25.76f }, { -6.12f, 0, 31.83f },
{ -6.99f, 0, 40.06f }, { -15.49f, 0, 42.69f }, { -22.04f, 0, 20.43f }, { -36.08f, 0, 25.88f },
{ -38.62f, 0, 32.12f }, { -39.92f, 0, 35.25f }, { -41.74f, 0, 38.26f }, { -46.64f, 0, 41.17f },
{ -53.02f, 0, 35.14f }, { -53.89f, 0, 33.56f }, { -56.80f, 0, 31.98f }, { -54.82f, 0, 27.13f },
{ -44.25f, 0, 45.49f }, { -41.47f, 0, 45.26f }, { -38.61f, 0, 44.70f }, { -35.11f, 0, 44.46f },
{ -23.97f, 0, 54.63f }, { -0.30f, 0, 58.74f }, { 9.11f, 0, 59.28f }, { 36.81f, 0, 57.64f },
{ 50.27f, 0, 55.22f }, { 46.93f, 0, 55.28f }, { 44.07f, 0, 54.52f }, { 47.36f, 0, 45.87f },
{ 54.86f, 0, 43.48f }, { 49.55f, 0, 38.87f }, { 47.18f, 0, 35.37f }, { 50.34f, 0, 33.63f },
{ 56.19f, 0, 27.30f }, { 58.14f, 0, 22.87f }, { 58.18f, 0, 17.00f }, { 56.67f, 0, 11.44f },
{ 56.97f, 0, -1.50f }, { 56.09f, 0, 0.20f }, { 46.54f, 0, 10.94f }, { 47.81f, 0, 13.39f },
{ 58.06f, 0, -11.35f }, { 55.72f, 0, -22.92f }, { 52.50f, 0, -32.51f }, { 51.65f, 0, -38.87f },
{ 41.64f, 0, -57.81f }, { 33.92f, 0, -58.19f }, { 30.10f, 0, -58.24f }, { 9.47f, 0, -58.33f },
{ 1.21f, 0, -58.92f }, { -3.25f, 0, -58.90f }, { -10.07f, 0, -59.04f }, { -48.94f, 0, -57.36f },
{ -57.86f, 0, -57.26f }, { -57.49f, 0, -55.68f }, { -36.20f, 0, -34.43f }, { -33.50f, 0, -32.37f },
{ -30.82f, 0, -30.75f }, { -27.72f, 0, -29.72f }, { -25.40f, 0, -29.57f }, { -12.61f, 0, -28.41f },
{ -9.25f, 0, -28.02f }, { -6.21f, 0, -20.05f }, { -10.26f, 0, -19.39f }, { -19.01f, 0, -19.78f },
{ -23.80f, 0, -18.77f }, { -24.60f, 0, -9.96f }, { 24.10f, 0, 5.98f }, { 24.52f, 0, 9.41f },
{ 27.50f, 0, 16.84f }, { 29.73f, 0, 17.18f }, { 34.77f, 0, 19.41f }, { 48.27f, 0, 27.44f },
{ 53.14f, 0, 25.60f }, { 54.67f, 0, 24.06f }, { 57.14f, 0, 21.41f }, { 58.43f, 0, 11.90f },
{ 57.19f, 0, 6.75f }, { 58.28f, 0, -8.24f }, { 53.38f, 0, -10.45f }, { 50.74f, 0, -11.99f },
{ 48.67f, 0, -14.99f }, { 47.27f, 0, -18.25f }, { 46.71f, 0, -23.69f }, { 6.69f, 0, -53.31f },
{ 9.37f, 0, -49.49f }, { 13.61f, 0, -46.99f }, { 19.53f, 0, -47.11f }, { 22.56f, 0, -47.09f },
{ 27.84f, 0, -49.37f }, { 28.49f, 0, -52.33f }, { -0.60f, 0, -59.11f }, { -6.40f, 0, -59.06f },
{ -9.50f, 0, -55.26f }, { -9.50f, 0, -52.49f }, { -8.71f, 0, -47.28f }, { -6.71f, 0, -46.99f },
{ -5.70f, 0, -50.21f }, { -11.83f, 0, -55.45f }, { -12.20f, 0, -36.02f }, { -12.06f, 0, -34.09f },
{ -9.92f, 0, -31.57f }, { -6.29f, 0, -30.69f }, { -9.29f, 0, -30.05f }, { -10.89f, 0, -28.22f },
{ -13.06f, 0, -24.90f }, { -13.96f, 0, -23.61f }, { -16.30f, 0, -21.04f }, { -19.99f, 0, -15.99f },
{ -21.62f, 0, -13.29f }, { -24.45f, 0, -11.49f }, { -27.34f, 0, -14.43f }, { -28.68f, 0, -16.38f },
{ -29.51f, 0, -24.00f }, { -29.50f, 0, -26.34f }, { -27.76f, 0, -28.10f }, { -23.80f, 0, -27.57f },
{ -22.84f, 0, -28.34f }, { -23.85f, 0, -32.58f }, { -29.74f, 0, -36.28f }, { -32.49f, 0, -36.44f },
{ -33.06f, 0, -34.40f }, { -30.85f, 0, -34.00f }, { -29.69f, 0, -33.01f }, { -31.37f, 0, -30.65f },
{ -35.18f, 0, -28.76f }, { -36.59f, 0, -26.76f }, { -34.22f, 0, -25.66f }, { -36.51f, 0, -24.10f },
{ -34.78f, 0, -20.10f }, { -33.92f, 0, -18.53f }, { -33.30f, 0, -17.01f }, { -34.29f, 0, -15.94f },
{ -33.61f, 0, -14.39f }, { -32.67f, 0, -12.39f }, { -33.86f, 0, -9.07f }, { -32.61f, 0, -5.10f },
{ -30.48f, 0, -5.47f }, { -25.51f, 0, -5.15f }, { -23.80f, 0, -5.28f }, { -21.11f, 0, -7.14f },
{ -19.89f, 0, -9.21f }, { -18.85f, 0, -11.52f }, { -19.68f, 0, -14.33f }, { -21.05f, 0, -14.46f },
{ -20.85f, 0, -15.22f }, { -13.94f, 0, -18.57f }, { -12.40f, 0, -19.74f }, { -8.43f, 0, -19.82f },
{ -6.78f, 0, -22.18f }, { -8.36f, 0, -23.42f }, { -6.57f, 0, -25.14f }, { -5.74f, 0, -14.35f },
{ -8.87f, 0, 7.16f }, { -6.19f, 0, 10.64f }, { -5.92f, 0, 16.25f }, { -6.00f, 0, 26.85f },
{ -7.51f, 0, 33.26f }, { -10.92f, 0, 40.87f }, { -15.60f, 0, 41.41f }, { -19.17f, 0, 39.74f },
{ -21.22f, 0, 37.41f }, { -40.43f, 0, 14.08f }, { -42.77f, 0, 14.27f }, { -44.33f, 0, 10.97f },
{ -44.04f, 0, 8.85f }, { -56.59f, 0, 2.85f }, { -56.82f, 0, 5.58f }, { -58.84f, 0, 11.99f },
{ -58.89f, 0, 15.95f }, { -47.66f, 0, 30.59f }, { -48.46f, 0, 32.53f }, { -50.73f, 0, 33.47f },
{ -54.97f, 0, 35.88f }, { -56.08f, 0, 37.66f }, { -54.80f, 0, 40.04f }, { -51.64f, 0, 40.17f },
{ -47.68f, 0, 40.67f }, { -46.91f, 0, 38.58f }, { -45.76f, 0, 36.36f }, { -44.51f, 0, 34.22f },
{ -40.92f, 0, 32.01f }, { -38.09f, 0, 36.33f }, { -36.86f, 0, 38.44f }, { -35.50f, 0, 40.80f },
{ -34.54f, 0, 46.37f }, { -35.31f, 0, 49.37f }, { -36.30f, 0, 52.49f }, { -32.52f, 0, 56.78f },
{ -36.33f, 0, 55.96f }, { -38.11f, 0, 54.97f }, { -41.63f, 0, 52.96f }, { -47.01f, 0, 50.07f },
{ -41.92f, 0, 56.68f }, { -15.52f, 0, 55.47f }, { -16.90f, 0, 54.07f }, { -18.84f, 0, 52.81f },
{ -16.76f, 0, 52.59f }, { -13.06f, 0, 52.57f }, { -7.97f, 0, 52.48f }, { -1.40f, 0, 52.19f },
{ 0.91f, 0, 55.66f }, { -8.65f, 0, 41.74f }, { -7.01f, 0, 41.51f }, { -5.86f, 0, 37.95f },
{ -5.91f, 0, 28.07f }, { -21.75f, 0, 21.93f }, { -21.93f, 0, 26.61f }, { -21.62f, 0, 29.60f },
{ -20.77f, 0, 32.44f }, { -20.33f, 0, 39.84f }, { -17.11f, 0, 41.30f }, { 5.03f, 0, 33.06f },
{ 7.35f, 0, 33.21f }, { 4.93f, 0, 28.86f }, { 4.49f, 0, 26.60f }, { 4.60f, 0, 21.51f },
{ 4.76f, 0, 19.99f }, { 4.75f, 0, 12.98f }, { 5.34f, 0, 11.78f }, { 7.05f, 0, 8.93f },
{ 9.13f, 0, 5.75f }, { 13.54f, 0, 6.54f }, { 18.43f, 0, 10.55f }, { 17.59f, 0, 13.44f },
{ 20.31f, 0, 16.43f }, { 24.22f, 0, 12.55f }, { 25.11f, 0, 11.31f }, { 26.04f, 0, 8.61f },
{ 23.95f, 0, 5.56f }, { 25.10f, 0, 5.50f }, { 26.88f, 0, 5.97f }, { 29.38f, 0, 6.16f },
{ 30.16f, 0, 8.09f }, { 29.41f, 0, 9.73f }, { 30.19f, 0, 11.18f }, { 30.21f, 0, 13.47f },
{ 30.34f, 0, 15.44f }, { 28.39f, 0, 20.34f }, { 26.62f, 0, 19.77f }, { 29.97f, 0, 26.10f },
{ 31.98f, 0, 28.70f }, { 34.17f, 0, 28.84f }, { 36.90f, 0, 25.82f }, { 36.84f, 0, 22.56f },
{ 35.70f, 0, 18.73f }, { 35.54f, 0, 15.63f }, { 35.85f, 0, 12.77f }, { 36.23f, 0, 5.75f },
{ 34.19f, 0, 28.17f }, { 33.70f, 0, 30.17f }, { 33.91f, 0, 34.17f }, { 36.54f, 0, 35.31f },
{ 31.01f, 0, 38.03f }, { 29.56f, 0, 36.46f }, { 28.66f, 0, 39.02f }, { 19.72f, 0, 34.29f },
{ 19.93f, 0, 35.84f }, { 19.82f, 0, 38.98f }, { 19.91f, 0, 42.03f }, { 24.99f, 0, 52.75f },
{ 24.88f, 0, 55.82f }, { 29.94f, 0, 53.25f }, { 31.96f, 0, 53.60f }, { 39.10f, 0, 56.54f },
{ 41.77f, 0, 55.14f }, { 55.22f, 0, 53.93f }, { 54.72f, 0, 51.76f }, { 53.76f, 0, 48.69f },
{ 50.74f, 0, 45.52f }, { 47.75f, 0, 47.66f }, { 47.29f, 0, 50.14f }, { 51.25f, 0, 46.04f },
{ 52.20f, 0, 41.81f }, { 51.48f, 0, 38.85f }, { 51.70f, 0, 36.46f }, { 55.17f, 0, 33.59f },
{ 57.15f, 0, 31.54f }, { 57.56f, 0, 25.08f }, { 53.03f, 0, 24.36f }, { 52.28f, 0, 26.02f },
{ 52.07f, 0, 23.54f }, { 53.11f, 0, 19.77f }, { 54.47f, 0, 17.86f }, { 57.95f, 0, 14.91f },
{ 52.79f, 0, 14.50f }, { 49.39f, 0, 14.72f }, { 46.92f, 0, 15.40f }, { 46.08f, 0, 13.51f },
{ 47.86f, 0, 9.79f }, { 49.19f, 0, 8.19f }, { 53.99f, 0, 7.96f }, { 56.56f, 0, 6.32f },
{ 55.41f, 0, 4.19f }, { 52.98f, 0, 3.31f }, { 51.72f, 0, -0.48f }, { 52.44f, 0, -2.27f },
{ 51.55f, 0, -4.91f }, { 50.24f, 0, -7.82f }, { 46.91f, 0, -4.91f }, { 47.36f, 0, -6.17f },
{ 48.62f, 0, -7.71f }, { 50.16f, 0, -8.61f }, { 52.26f, 0, -10.21f }, { 52.02f, 0, -13.28f },
{ 49.74f, 0, -12.87f }, { 47.97f, 0, -12.54f }, { 48.53f, 0, -14.84f }, { 51.40f, 0, -15.51f },
{ 56.80f, 0, -13.79f }, { 51.05f, 0, -19.37f }, { 48.85f, 0, -19.29f }, { 47.44f, 0, -24.19f },
{ 51.56f, 0, -24.81f }, { 54.78f, 0, -24.72f }, { 56.16f, 0, -26.48f }, { 54.13f, 0, -28.42f },
{ 47.71f, 0, -31.59f }, { 46.88f, 0, -33.94f }, { 52.13f, 0, -40.43f }, { 53.54f, 0, -41.79f },
{ 51.50f, 0, -47.52f }, { 48.87f, 0, -47.76f }, { 44.77f, 0, -49.47f }, { 44.47f, 0, -51.67f },
{ 39.40f, 0, -57.76f }, { 37.63f, 0, -57.13f }, { 38.38f, 0, -51.33f }, { 39.79f, 0, -48.78f },
{ 37.03f, 0, -47.33f }, { 32.75f, 0, -47.12f }, { 32.36f, 0, -49.75f }, { 32.36f, 0, -51.72f },
{ 29.59f, 0, -53.98f }, { 26.03f, 0, -53.59f }, { 23.92f, 0, -53.44f }, { 21.65f, 0, -53.80f },
{ 17.41f, 0, -52.93f }, { 20.82f, 0, -48.53f }, { 19.81f, 0, -47.62f }, { 15.23f, 0, -47.65f },
{ 8.99f, 0, -48.89f }, { 6.32f, 0, -50.02f }, { 3.40f, 0, -51.99f }, { 0.38f, 0, -54.51f },
{ -3.05f, 0, -54.93f }, { -5.65f, 0, -49.58f }, { -8.72f, 0, -51.62f }, { -17.70f, 0, -58.69f },
{ -28.50f, 0, -59.50f }, { -38.44f, 0, -57.30f }, { -36.20f, 0, -57.10f }, { -29.49f, 0, -56.79f },
{ -25.90f, 0, -56.94f }, { -10.58f, 0, -50.28f }, { -5.12f, 0, -52.24f }, { 6.50f, 0, -58.81f },
{ 12.25f, 0, -54.32f }, { 13.39f, 0, -52.25f }, { 21.76f, 0, -50.69f }, { 38.94f, 0, -52.72f },
{ 40.29f, 0, -52.28f }, { 47.24f, 0, -46.72f }, { 50.45f, 0, -48.56f }, { 52.32f, 0, -34.34f },
{ 52.76f, 0, -28.48f }, { 49.01f, 0, -26.49f }, { 46.48f, 0, -21.59f }, { 48.39f, 0, -17.15f },
{ 50.04f, 0, -14.48f }, { 51.22f, 0, -8.46f }, { 51.86f, 0, 1.80f }, { 50.82f, 0, 7.50f },
{ 48.13f, 0, 11.58f }, { 50.76f, 0, 19.59f }, { 54.93f, 0, 24.79f }, { 57.12f, 0, 38.98f },
{ 56.23f, 0, 42.44f }, { 56.68f, 0, 44.42f }, { 55.11f, 0, 48.04f }, { 53.98f, 0, 50.57f },
{ 51.54f, 0, 54.29f }, { 41.96f, 0, 57.94f }, { 29.96f, 0, 55.75f }, { 26.19f, 0, 55.08f },
{ 14.04f, 0, 56.66f }, { 15.12f, 0, 57.98f }, { 17.89f, 0, 54.18f }, { 10.50f, 0, 51.90f },
{ 9.12f, 0, 51.09f }, { 5.33f, 0, 40.32f }, { 7.32f, 0, 37.91f }, { 6.15f, 0, 31.37f },
{ 6.93f, 0, 14.92f }, { 9.36f, 0, 14.49f }, { 10.78f, 0, 14.57f }, { 13.41f, 0, 13.83f },
{ 19.98f, 0, 10.25f }, { 22.57f, 0, 7.55f }, { 19.59f, 0, 6.85f }, { 17.74f, 0, 7.23f },
{ 23.52f, 0, -5.53f }, { 22.39f, 0, -6.01f }, { 18.95f, 0, -6.49f }, { 15.78f, 0, -6.89f },
{ 11.36f, 0, -9.35f }, { 7.91f, 0, -11.52f }, { 6.14f, 0, -9.82f }, { 8.23f, 0, -7.11f },
{ 31.15f, 0, -7.49f }, { 33.75f, 0, -5.98f }, { 34.57f, 0, 8.31f }, { 29.83f, 0, 29.31f },
{ 27.75f, 0, 29.61f }, { 25.29f, 0, 31.32f }, { 25.19f, 0, 33.15f }, { 24.76f, 0, 35.16f },
{ 21.09f, 0, 52.20f }, { 9.03f, 0, 56.11f }, { 1.79f, 0, 58.45f }, { -6.74f, 0, 54.05f },
{ -10.44f, 0, 54.99f }, { -27.07f, 0, 56.72f }, { -41.20f, 0, 55.51f }, { -43.14f, 0, 48.33f },
{ -43.73f, 0, 43.56f }, { -43.53f, 0, 41.66f }, { -45.00f, 0, 38.08f }, { -47.48f, 0, 36.47f },
{ -57.45f, 0, 30.35f }, { -57.81f, 0, 23.64f }, { -57.70f, 0, 15.34f }, { -58.16f, 0, -2.38f },
{ -57.30f, 0, -7.40f }, { -57.05f, 0, -9.18f }, { -58.11f, 0, -13.11f }, { -58.43f, 0, -18.31f },
{ -57.89f, 0, -22.26f }, { -57.42f, 0, -24.69f }, { -57.31f, 0, -33.43f }, { -57.55f, 0, -35.70f },
{ -56.67f, 0, -43.26f }, { -56.50f, 0, -46.19f }, { -56.47f, 0, -50.34f }, { -56.05f, 0, -57.30f },
{ -50.94f, 0, -58.01f }, { -43.34f, 0, -57.15f }, { -34.19f, 0, -56.94f }, { -29.01f, 0, -57.93f },
{ -13.11f, 0, -58.57f }, { -8.10f, 0, -54.39f }, { -15.78f, 0, -36.10f }, { -17.96f, 0, -33.51f },
{ -25.76f, 0, -31.07f }, { -28.83f, 0, -27.96f }, { -28.04f, 0, -22.31f }, { -27.38f, 0, -21.02f },
{ -25.56f, 0, -17.26f }, { -25.44f, 0, -15.38f }, { -17.95f, 0, -11.92f }, { -14.27f, 0, -11.04f },
{ -11.70f, 0, -15.28f }, { -8.10f, 0, -13.89f }, { -8.11f, 0, -10.88f }, { -10.42f, 0, -10.11f },
{ -13.10f, 0, -9.87f }, { -32.87f, 0, -15.44f }, { -31.98f, 0, -18.43f }, { -26.25f, 0, -24.69f },
{ -24.35f, 0, -29.04f }, { -18.31f, 0, -30.59f }, { -15.05f, 0, -30.33f }, { -8.05f, 0, -26.74f },
{ -35.61f, 0, -7.96f }, { -42.00f, 0, 5.40f },
};
vector<mat4> grass_instance_transforms;
int num_culled_grass = 0;
Mesh cloud_mesh;
vector<mat4> cloud_instance_transforms;
Mesh campfire_mesh;
Mesh sleeping_bag_mesh;
struct Car {
Mesh mesh;
float mass = 0.0; // mass of car
float engine = 0.0; // engine force
float roll = 0.0; // rolling resistance
float drag = 0.0; // aerodynamic drag constant
vec3 pos = vec3(0, 0, 0); // position
vec3 dir = vec3(1, 0, 0); // direction
vec3 vel = vec3(0, 0, 0); // velocity
Car() { };
Car(Mesh _mesh, float _mass, float _engine, float _roll, float _drag) {
mesh = _mesh;
mass = _mass;
engine = _engine;
roll = _roll;
drag = _drag;
}
mat4 transform() {
return Translate(pos) * Orientation(dir, vec3(0, 1, 0));
}
void collide() {
if (pos.y < 0) pos.y = 0;
if (pos.x > 59.5) { pos.x = 59.5; }
else if (pos.x < -59.5) { pos.x = -59.5; }
else if (pos.z > 59.5) { pos.z = 59.5; }
else if (pos.z < -59.5) { pos.z = -59.5; }
/*for (vec3 tree_pos : large_tree_positions) {
float d = dist(pos, tree_pos);
if (d < 1.5) vel *= 0.5;
//if (d < 1.0) vel = (pos - tree_pos); hard collision with tree
}*/
}
void update(float dt) {
// Weight update by time delta for consistent effect of updates
// regardless of framerate
static float last_pt = 0.0f;
last_pt += dt;
// Check if space key pressed ("drifting")
float drift = (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) ? 0.5f : 1.0f;
// Check for car turning with A/D
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
float deg = dt * -1.5f * ((drift == 0.5f) ? 1.25f : 1.0f);
vec4 dirw = RotateY(deg) * dir;
dir = vec3(dirw.x, dirw.y, dirw.z);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
float deg = dt * 1.5f * ((drift == 0.5f) ? 1.25f : 1.0f);
vec4 dirw = RotateY(deg) * dir;
dir = vec3(dirw.x, dirw.y, dirw.z);
}
vec3 force = vec3(0.0);
// Apply engine force if pressed (F = u{vel} * engine)
float turbo = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) ? 1.5f : 1.0f;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
force += turbo * drift * dir * engine;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
force -= turbo * drift * dir * engine;
// Apply aerodynamic drag (F = - C{drag} * vel * |vel|)
force -= drag * vel * length(vel);
// Apply rolling resistance (F = - C{roll} * vel)
// Weigh rolling resistance by up to 2x normal based on angle difference between direction and velocity
float roll_wt = 1 - (dot(dir, vel) / length(dir) / length(vel));
if (roll_wt > 1) roll_wt = 1 - (roll_wt - 1);
roll_wt = roll_wt * 4 + 1;
if (length(vel) < 0.001) roll_wt = 1.0;
force -= drift * roll_wt * roll * vel;
vec3 acc = force / mass;
// Using last_pt to limit particles by dt
if (last_pt > 0.5f) {
last_pt = 0.0f;
// Spawn drifting particles if roll wt and speed higher than threshold
if (length(vel) > 0.16f && roll_wt > 1.6) {
particleSystem.createParticle(pos - 0.3f * cross(dir, vec3(0, 1, 0)) - 0.5f * dir, vec3(1, 0.1f, 0.1f));
particleSystem.createParticle(pos + 0.3f * cross(dir, vec3(0, 1, 0)) - 0.5f * dir, vec3(1, 0.1f, 0.1f));
}
}
// Move velocity according to acceleration
vel += dt * acc;
// Move position according to velocity
pos += dt * vel;
}
} car;
void WindowResized(GLFWwindow* window, int _width, int _height) {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
camera.width = win_width = width;
camera.height = win_height = height;
glViewport(0, 0, win_width, win_height);
}
void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_Q && glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
if (key == GLFW_KEY_R && action == GLFW_PRESS) {
car.pos = vec3(2, 0, 0);
car.vel = vec3(0, 0, 0);
}
if (key == GLFW_KEY_P && action == GLFW_PRESS) {
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) {
showPerformance = !showPerformance;
} else {
printf("{%.2ff, %.0f, %.2ff}, ", car.pos.x, car.pos.y, car.pos.z);
}
}
if (key == GLFW_KEY_1 && action == GLFW_PRESS)
camera_type = 1;
if (key == GLFW_KEY_2 && action == GLFW_PRESS)
camera_type = 2;
if (key == GLFW_KEY_3 && action == GLFW_PRESS)
camera_type = 3;
if (key == GLFW_KEY_4 && action == GLFW_PRESS) {
camera_type = 4;
camera.loc = car.pos + vec3(0, 1.5, 0) + -3 * car.dir;
camera.look = car.pos + 2.5 * car.dir;
camera.fov = 70;
camera.up = vec3(0, 1, 0);
}
if (key == GLFW_KEY_BACKSLASH && action == GLFW_PRESS) {
cur_skybox++;
if (cur_skybox >= skyboxes.size()) cur_skybox = 0;
}
if (key == GLFW_KEY_F && action == GLFW_PRESS) {
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) {
frustumCulling = !frustumCulling;
if (!frustumCulling) {
large_tree_mesh.loadInstances(large_tree_instance_transforms);
num_culled_large_trees = (int)large_tree_instance_transforms.size();
grass_mesh.loadInstances(grass_instance_transforms);
num_culled_grass = (int)grass_instance_transforms.size();
}
} else {
if (fullscreen) {
win_width = windowed_width;
win_height = windowed_height;
glfwSetWindowMonitor(window, NULL, 100, 100, win_width, win_height, 0);
fullscreen = false;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
} else {
glfwSetWindowMonitor(window, monitor, 0, 0, currentVidMode.width, currentVidMode.height, currentVidMode.refreshRate);
fullscreen = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
}
}
if (key == GLFW_KEY_M && action == GLFW_PRESS)
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) showShadowMap = !showShadowMap;
}
void load_icon() {
GLFWimage icon[2];
icon[0].pixels = stbi_load("textures/icon/drive.png", &icon[0].width, &icon[0].height, 0, 4);
icon[1].pixels = stbi_load("textures/icon/drive_small.png", &icon[1].width, &icon[1].height, 0, 4);
glfwSetWindowIcon(window, 2, icon);
stbi_image_free(icon[0].pixels);
stbi_image_free(icon[1].pixels);
}
void update_title(time_p cur) {
static time_p lastTitleUpdate = sys_clock::now();
float_ms sinceTitleUpdate = cur - lastTitleUpdate;
if (sinceTitleUpdate.count() < 200.0f) return;
char title[30];
snprintf(title, 30, "Drive: %.0f fps", (1.0f / dt) * 60.0f);
glfwSetWindowTitle(window, title);
lastTitleUpdate = cur;
}
void init_perf() {
for (int i = 0; i < PERF_MEMORY; i++)
fps_x[i] = (float)i;
}
void collect_perf(time_p cur) {
static time_p lastPerfCollect = sys_clock::now();
float_ms sincePerfCollect = cur - lastPerfCollect;
if (sincePerfCollect.count() < 500.0f) return;
ImGuiIO& io = ImGui::GetIO();
for (int i = 1; i < PERF_MEMORY; i++) {
fps_y[i - 1] = fps_y[i];
}
fps_y[PERF_MEMORY - 1] = io.Framerate;
lastPerfCollect = cur;
}
void show_performance_window() {
ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings;
window_flags |= ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs;
window_flags |= ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse;
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImVec2 work_pos = viewport->WorkPos;
ImVec2 work_size = viewport->WorkSize;
ImVec2 window_pos, window_pos_pivot;
window_pos.x = work_pos.x + work_size.x - 10.0f;
window_pos.y = work_pos.y + 10.0f;
window_pos_pivot.x = 1.0f;
window_pos_pivot.y = 0.0f;
ImGuiIO& io = ImGui::GetIO();
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot);
ImGui::SetNextWindowSize(ImVec2(work_size.x / 4.0f, 0.0f));
ImGui::SetNextWindowBgAlpha(0.35f);
if (ImGui::Begin("Performance", NULL, window_flags)) {
ImGui::Text("Initialization time: %.2f ms", init_time);
ImGui::Text("FPS: %.0f fps", io.Framerate);
ImVec2 displaySize = io.DisplaySize;
ImGui::Text("Display Size: %.0f x %.0f", displaySize.x, displaySize.y);
ImGui::Text("Trees in view: %d / %d", num_culled_large_trees, (int)large_tree_instance_transforms.size());
ImGui::Text("Grass in view: %d / %d", num_culled_grass, (int)grass_instance_transforms.size());
ImGui::Separator();
static ImPlotFlags plot_flags = ImPlotFlags_NoBoxSelect | ImPlotFlags_NoMouseText;
ImPlot::PushStyleColor(ImPlotCol_FrameBg, {0.0f, 0.0f, 0.0f, 0.3f});
ImPlot::PushStyleColor(ImPlotCol_PlotBg, {0.0f, 0.0f, 0.0f, 0.0f});
if (ImPlot::BeginPlot("Frames Per Second", ImVec2(work_size.x / 5.0f, work_size.y / 10.0f), plot_flags)) {
static ImPlotAxisFlags plot_x_flags = ImPlotAxisFlags_AutoFit | ImPlotAxisFlags_RangeFit | ImPlotAxisFlags_NoTickMarks | ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoGridLines;
static ImPlotAxisFlags plot_y_flags = ImPlotAxisFlags_AutoFit | ImPlotAxisFlags_RangeFit;
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.35f);
ImPlot::SetupAxes("", "", plot_x_flags, plot_y_flags);
ImPlot::PlotShaded("", fps_x, fps_shade, fps_y, PERF_MEMORY);
ImPlot::PlotLine("", fps_x, fps_y, PERF_MEMORY);
ImPlot::PopStyleVar();
ImPlot::EndPlot();
}
ImPlot::PopStyleColor(2);
}
ImGui::End();
}
void render_imgui() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("Drive")) {
if (ImGui::MenuItem("Performance Window", "CTRL + P", showPerformance)) showPerformance = !showPerformance;
if (ImGui::MenuItem("Quit", "CTRL + Q", false)) glfwSetWindowShouldClose(window, GLFW_TRUE);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Renderer")) {
ImGui::ColorEdit3("Directional Light Color", lightColor);
if (ImGui::BeginCombo("Skybox", skyboxNames[cur_skybox].c_str())) {
for (int i = 0; i < skyboxes.size(); i++) {
if (ImGui::Selectable(skyboxNames[i].c_str(), cur_skybox == i)) cur_skybox = i;
}
ImGui::EndCombo();
}
const char* curPolygonMode = (polygonMode == GL_FILL) ? "GL_FILL" : (polygonMode == GL_LINE ? "GL_LINE" : "GL_POINT");
if (ImGui::BeginCombo("Polygon Mode", curPolygonMode)) {
if (ImGui::Selectable("GL_FILL", polygonMode == GL_FILL)) { polygonMode = GL_FILL; glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }
if (ImGui::Selectable("GL_LINE", polygonMode == GL_LINE)) { polygonMode = GL_LINE; glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); }
if (ImGui::Selectable("GL_POINT", polygonMode == GL_POINT)) { polygonMode = GL_POINT; glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); }
ImGui::EndCombo();
}
if (ImGui::MenuItem("Frustum Culling", "CTRL + F", frustumCulling)) {
large_tree_mesh.loadInstances(large_tree_instance_transforms);
num_culled_large_trees = (int)large_tree_instance_transforms.size();
grass_mesh.loadInstances(grass_instance_transforms);
num_culled_grass = (int)grass_instance_transforms.size();
}
if (ImGui::MenuItem("Shadow Map Display", "CTRL + M", showShadowMap)) showShadowMap = !showShadowMap;
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Settings")) {
char videoModeComboLabel[20];
snprintf(videoModeComboLabel, 20, "%dx%d %dHz", currentVidMode.width, currentVidMode.height, currentVidMode.refreshRate);
if (ImGui::BeginCombo("Fullscreen Mode", videoModeComboLabel)) {
for (size_t i = 0; i < videoModesCount; i++) {
snprintf(videoModeComboLabel, 20, "%dx%d %dHz", videoModes[i].width, videoModes[i].height, videoModes[i].refreshRate);
if (ImGui::Selectable(videoModeComboLabel, videoModes[i] == currentVidMode)) {
currentVidMode = videoModes[i];
if (fullscreen) glfwSetWindowMonitor(window, monitor, 0, 0, currentVidMode.width, currentVidMode.width, currentVidMode.refreshRate);
}
}
ImGui::EndCombo();
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
if (showPerformance) show_performance_window();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void setup() {
// Initialize GLFW callbacks
glfwSetKeyCallback(window, Keyboard);
glfwSetWindowSizeCallback(window, WindowResized);
// Load shaders into render passes
mainPass.loadShaders(&mainVert, &mainFrag);
mainPassInst.loadShaders(&mainVertInstanced, &mainFrag);
shadowPass.loadShaders(&shadowVert, &shadowFrag);
shadowPassInst.loadShaders(&shadowVertInstanced, &shadowFrag);
phongPass.loadShaders(&phongVert, &phongFrag);
phongPassInst.loadShaders(&phongVertInstanced, &phongFrag);
// Set up shadowmap resources
glGenFramebuffers(1, &shadowFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, shadowFramebuffer);
glGenTextures(1, &shadowTexture);
glBindTexture(GL_TEXTURE_2D, shadowTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, SHADOW_DIM, SHADOW_DIM, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowTexture, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw runtime_error("Failed to set up shadow framebuffer!");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Setup meshes
mat4 car_transform = Scale(0.75f) * RotateY(-90);
Mesh car_mesh = Mesh("objects/car.obj", "textures/car.png", car_transform);
// Mesh, mass, engine force, rolling resistance, air drag
car = Car(car_mesh, 500.0, 3, 10.0, 10.0);
car.pos = vec3(2, 0, 0);
floor_mesh = Mesh(floor_points, floor_uvs, floor_normals, floor_triangles, "textures/racetrack.png");
mat4 large_tree_transform = Scale(2.0);
large_tree_mesh = Mesh("objects/largetree.obj", "textures/largetree.png", large_tree_transform);
grass_mesh = Mesh("objects/grass.obj", "textures/grass.png", mat4());
cloud_mesh = Mesh("objects/cloud.obj", "textures/cloud.png", Scale(4.0f));
campfire_mesh = Mesh("objects/campfire.obj", "textures/campfire.png", Scale(0.5f));
sleeping_bag_mesh = Mesh("objects/sleeping_bag.obj", "textures/sleeping_bag.png", Translate(0.0f, 0.05f, 0.0f));
// Setup instance render buffers
for (vec3 pos : large_tree_instance_positions)
large_tree_instance_transforms.push_back(Translate(pos) * RotateY(rand_float(-180.0f, 180.0f)));
large_tree_mesh.setupInstanceBuffer((GLsizei)large_tree_instance_transforms.size());
large_tree_mesh.loadInstances(large_tree_instance_transforms);
for (vec3 pos : grass_instance_positions)
grass_instance_transforms.push_back(Translate(pos) * RotateY(rand_float(-180.0f, 180.0f)));
grass_mesh.setupInstanceBuffer((GLsizei)grass_instance_transforms.size());
grass_mesh.loadInstances(grass_instance_transforms);
for (int i = 0; i < 500; i++) {
mat4 m;
m = Scale(rand_float(3, 6)) * m;
m = RotateY(rand_float(-180.0f, 180.0f)) * m;
m = Translate(rand_float(-1000.0, 1000.0), rand_float(150.0, 160.0), rand_float(-1000.0, 1000.0)) * m;
cloud_instance_transforms.push_back(m);
}
cloud_mesh.setupInstanceBuffer((GLsizei)cloud_instance_transforms.size());
cloud_mesh.loadInstances(cloud_instance_transforms);
// Setup colliders
large_tree_mesh.createCollider<Sphere>();
grass_mesh.createCollider<Sphere>();
// Setup skyboxes
for (string path : skyboxPaths) {
Skybox skybox;
skybox.setup();
skybox.loadCubemap(path);
skyboxes.push_back(skybox);
}
// Setup particle system
particleSystem.setup();
// Set some GL drawing context settings
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
}
void cleanup() {
// Cleanup meshes
car.mesh.cleanup();
floor_mesh.cleanup();
large_tree_mesh.cleanup();
grass_mesh.cleanup();
campfire_mesh.cleanup();
sleeping_bag_mesh.cleanup();
cloud_mesh.cleanup();
// Cleanup skyboxes / particle system
for (Skybox skybox : skyboxes)
skybox.cleanup();
particleSystem.cleanup();
// Cleanup shadow map resources
glDeleteFramebuffers(1, &shadowFramebuffer);
glDeleteTextures(1, &shadowTexture);
// Cleanup render passes