-
Notifications
You must be signed in to change notification settings - Fork 1
/
mineclone.cpp
4400 lines (3694 loc) · 132 KB
/
mineclone.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
// TODO:
//
// * load .obj files (http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading)
//
// * mipmap the texture atlas (http://download.nvidia.com/developer/NVTextureSuite/Atlas_Tools/Texture_Atlas_Whitepaper.pdf)
//
// * reduce number of vertices by merging blocks of the same type
//
// * Fix jittering shadows by moving light in texel-sized increments (https://msdn.microsoft.com/en-us/library/ee416324(v=vs.85).aspx)
//
// * Improve block representation:
// - persist block changes to disk
// - Compress blocktype cache (RLE would probably work really well)
// - Keep a table-style cache temporarily for speed when loading blocks
// - divide block mesh vertices into multiple buffer objects
// - good datastructure for storing block changes
//
// * fix perlin noise at negative coordinates
//
// * give the shadowmap close to player higher precision (like a fishbowl kind of thing?)
//
// * bloom (https://learnopengl.com/Advanced-Lighting/Bloom)
//
// * ambient occlusion (https://learnopengl.com/Advanced-Lighting/SSAO)
//
// * antialiasing (https://learnopengl.com/Advanced-OpenGL/Anti-Aliasing)
//
// * more ui (menus, buttons, etc..)
//
// * dynamic skybox texture, and rotate skybox depending on sun position
//
// * transparent blocks (leaves etc)
//
// * complex transparent blocks (arbitrary meshes)
//
// * crafting ui
//
// * inventory ui
//
// * fix smoothing out sunlight as it goes behind horizon, right now it just clips off directly
//
// * view distance in water?
//
// * fix position precision limitations (player position and vertex position).
// - if possible, we would like vertices to be relative to player position so we can keep
// vertex size down. but that would mean we need to update all vertices each time the player moves
// is there a way to keep vertex sizes down by modding them somehow?
//
// * torches - calculate lighting per block (voxel lighting)
//
// * reflect and refract lighting from skybox? or if skybox is not interesting enough, maybe we can do something cool using the surrounding blocks?
//
////
#ifdef _MSC_VER
#define OS_WINDOWS 1
#else
#define OS_LINUX 1
#endif
#ifdef OS_WINDOWS
// windows.h defines max and min as macros, destroying all our code. remove it with this define :)
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN 1
#endif
// glEnable(GL_FRAMEBUFFER_SRGB) doesn't work on ubuntu intel drivers,
// so this flag is if we need to do it manually or not :(
#ifndef OS_WINDOWS
#define MANUAL_GAMMA
#endif
#include <stdarg.h>
#include "GL/gl3w.h"
#include "SDL2/SDL.h"
#include <math.h>
#include "array.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "GL/gl3w.c"
#include <stdint.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
// uncomment to enable vr (only on Windows)
#if defined(OS_WINDOWS) && defined(VR_ENABLED)
#include "OpenVR/openvr.h"
#endif
typedef unsigned int uint;
typedef uint8_t u8;
typedef uint16_t u16;
typedef int16_t i16;
typedef int32_t i32;
typedef uint32_t u32;
typedef uint64_t u64;
// bools that are word size can be faster
typedef u32 b32;
// just a tag to say that it's okay if argument is null
#define OPTIONAL
// @logging
static void _die(const char *file, int line, const char *fmt, ...) {
printf("%s:%i: ", file, line);
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
fflush(stderr);
abort();
}
#define die(fmt, ...) _die(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define DEBUG 1
#ifdef DEBUG
#define debug(stmt) stmt
#else
#define debug(stmt) 0
#endif
#define VERBOSE_DEBUG 1
#ifdef VERBOSE_DEBUG
#define debug_verbose(stmt) stmt
#else
#define debug_verbose(stmt)
#endif
static void _sdl_die(const char *file, int line, const char *fmt, ...) {
printf("%s:%i: ", file, line);
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
printf(": %s\n", SDL_GetError());
fflush(stdout);
abort();
}
#define sdl_die(fmt, ...) _sdl_die(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
#define sdl_try(stmt) ((stmt) && (die("%s\n", SDL_GetError()),0))
#define gl_ok_or_die _gl_ok_or_die(__FILE__, __LINE__)
static void _gl_ok_or_die(const char* file, int line) {
GLenum error_code;
const char* error;
error_code = glGetError();
if (error_code == GL_NO_ERROR) return;
switch (error_code) {
case GL_INVALID_ENUM: error = "INVALID_ENUM"; break;
case GL_INVALID_VALUE: error = "INVALID_VALUE"; break;
case GL_INVALID_OPERATION: error = "INVALID_OPERATION"; break;
case GL_STACK_OVERFLOW: error = "STACK_OVERFLOW"; break;
case GL_STACK_UNDERFLOW: error = "STACK_UNDERFLOW"; break;
case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break;
default: error = "unknown error";
};
die("GL error at %s:%u: (%u) %s\n", file, line, error_code, error);
}
// @utils
template<class T>
struct Vec {
typedef T* Iterator;
T *items;
int size;
T& operator[](int i) {return items[i];}
const T& operator[](int i) const {return items[i];}
};
template<class T, int N>
Vec<T> vec(T (&t)[N]) {
return {t, N};
}
template<class T>
struct VecIter {
T *t, *end;
};
template<class T>
static VecIter<T> iter(const Vec<T> &v) {
return {v.items, v.items+v.size};
}
template<class T>
static T* next(VecIter<T> &i) {
if (i.t == i.end) return 0;
return i.t++;
}
#define STATIC_ASSERT(expr, name) typedef char static_assert_##name[expr?1:-1]
static FILE* mine_fopen(const char *filename, const char *mode) {
#ifdef OS_WINDOWS
FILE *f;
if (fopen_s(&f, filename, mode))
return 0;
return f;
#else
return fopen(filename, mode);
#endif
}
// @math
#define sign(x) ((x) < 0.0f ? -1.0f : 1.0f)
static bool is_power_of_2(int x) {
return (x & (x-1)) == 0;
}
#define ARRAY_LEN(a) (sizeof(a)/sizeof(*a))
#define ARRAY_LAST(a) ((a)[ARRAY_LEN(a)-1])
template<class T>
static T clamp(T x, T a, T b) {
if (x < a) return a;
if (x > b) return b;
return x;
}
static const float PI = 3.141592651f;
static const float SQRT2 = 1.4142135623f;
struct v3i {
int x,y,z;
};
static v3i operator-(v3i a, v3i b) {
return {a.x-b.x, a.y-b.y, a.z-b.z};
}
static v3i operator+(v3i a, v3i b) {
return {a.x+b.x, a.y+b.y, a.z+b.z};
}
typedef v3i Block;
struct BlockIndex {
int x;
int y;
int z;
};
static bool is_invalid(Block b) {
return b.x == INT_MIN;
}
struct v2 {
static const int DIMENSION = 2;
float x,y;
};
union v3 {
static const int DIMENSION = 3;
struct {
float x,y,z;
};
struct {
v2 xy;
float _z;
};
};
struct v4 {
static const int DIMENSION = 4;
float x,y,z,w;
};
static float operator*(v3 a, v3 b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}
static v3 cross(v3 a, v3 b) {
v3 r = {
a.y*b.z - a.z*b.y,
a.z*b.x - a.x*b.z,
a.x*b.y - a.y*b.x
};
return r;
}
static v3 operator/(v3 v, float f) {
return {v.x/f, v.y/f, v.z/f};
}
static v3 operator*(v3 v, float f) {
return {v.x*f, v.y*f, v.z*f};
}
static v3 operator*(float x, v3 v) {
return v*x;
}
static v3 operator+(v3 a, v3 b) {
return {a.x+b.x, a.y+b.y, a.z+b.z};
}
static v3 operator-(v3 a, v3 b) {
return {a.x-b.x, a.y-b.y, a.z-b.z};
}
static void operator+=(v3& v, v3 x) {
v = {v.x+x.x, v.y+x.y, v.z+x.z};
}
static v3 operator-(v3 v) {
return {-v.x, -v.y, -v.z};
}
static void operator-=(v3& v, v3 x) {
v = {v.x-x.x, v.y-x.y, v.z-x.z};
}
static v3 normalize(v3 v) {
float len = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
if (len == 0.0f) return v;
return v/len;
}
struct v2i {
int x,y;
};
static void operator+=(v2& v, v2 x) {
v = {v.x+x.x, v.y+x.y};
}
static v2 operator*(v2 v, float f) {
return {v.x*f, v.y*f};
}
static v2 operator/(v2 v, float f) {
return {v.x/f, v.y/f};
}
static v2 normalize(v2 v) {
float len = sqrtf(v.x*v.x + v.y*v.y);
return v/len;
}
template <class T>
static void swap(T &a, T &b) {
T tmp = a;
a = b;
b = tmp;
}
template<class T>
static T max(T a, T b) {
return a < b ? b : a;
}
template <class T>
static T min(T a, T b) {
return b < a ? b : a;
}
static v3 min(v3 a, v3 b) {
return {min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)};
}
static v3 max(v3 a, v3 b) {
return {max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)};
}
static v3i min(v3i a, v3i b) {
return {min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)};
}
static v3i max(v3i a, v3i b) {
return {max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)};
}
#define at_most min
#define at_least max
// @perlin
// good explanation of perlin noise: http://flafla2.github.io/2014/08/09/perlinnoise.html
static float perlin__grad(int hash, float x, float y, float z) {
switch (hash&0xF) {
case 0x0: return x + y;
case 0x1: return -x + y;
case 0x2: return x - y;
case 0x3: return -x - y;
case 0x4: return x + z;
case 0x5: return -x + z;
case 0x6: return x - z;
case 0x7: return -x - z;
case 0x8: return y + z;
case 0x9: return -y + z;
case 0xA: return y - z;
case 0xB: return -y - z;
case 0xC: return y + x;
case 0xD: return -y + z;
case 0xE: return y - x;
case 0xF: return -y - z;
default: return 0; // never happens
}
}
static float lerp(float t, float a, float b) {
return a + t * (b - a);
}
static float perlin(float x, float y, float z) {
#define FADE(t) (t * t * t * (t * (t * 6 - 15) + 10))
static int p[] = { 151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
};
int X = ((int)x) & 255, // FIND UNIT CUBE THAT
Y = ((int)y) & 255, // CONTAINS POINT.
Z = ((int)z) & 255;
x -= (int)x; // FIND RELATIVE X,Y,Z
y -= (int)y; // OF POINT IN CUBE.
z -= (int)z;
float u = FADE(x), // COMPUTE FADE CURVES
v = FADE(y), // FOR EACH OF X,Y,Z.
w = FADE(z);
int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF
B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS,
return (lerp(w, lerp(v, lerp(u, perlin__grad(p[AA ], x , y , z ), // AND ADD
perlin__grad(p[BA ], x-1, y , z )), // BLENDED
lerp(u, perlin__grad(p[AB ], x , y-1, z ), // RESULTS
perlin__grad(p[BB ], x-1, y-1, z ))),// FROM 8
lerp(v, lerp(u, perlin__grad(p[AA+1], x , y , z-1 ), // CORNERS
perlin__grad(p[BA+1], x-1, y , z-1 )), // OF CUBE
lerp(u, perlin__grad(p[AB+1], x , y-1, z-1 ),
perlin__grad(p[BB+1], x-1, y-1, z-1 )))) + 1.0f )/2.0f;
}
#define GENERATE_VECTOR_TYPE_1(type) \
struct v1_##type { \
static const int DIMENSION = 1; \
type x; \
}
#define GENERATE_VECTOR_TYPE_2(type) \
struct v2_##type { \
static const int DIMENSION = 2; \
type x,y; \
}
#define GENERATE_VECTOR_TYPE_3(type) \
struct v3_##type { \
static const int DIMENSION = 3; \
type x,y,z; \
}
#define GENERATE_VECTOR_TYPE_4(type) \
struct v4_##type { \
static const int DIMENSION = 4; \
type x,y,z,w; \
}
GENERATE_VECTOR_TYPE_1(u32);
GENERATE_VECTOR_TYPE_2(u32);
GENERATE_VECTOR_TYPE_3(u32);
GENERATE_VECTOR_TYPE_4(u32);
GENERATE_VECTOR_TYPE_1(i16);
GENERATE_VECTOR_TYPE_2(i16);
GENERATE_VECTOR_TYPE_3(i16);
GENERATE_VECTOR_TYPE_4(i16);
GENERATE_VECTOR_TYPE_1(u16);
GENERATE_VECTOR_TYPE_2(u16);
GENERATE_VECTOR_TYPE_3(u16);
GENERATE_VECTOR_TYPE_4(u16);
GENERATE_VECTOR_TYPE_1(u8);
GENERATE_VECTOR_TYPE_2(u8);
GENERATE_VECTOR_TYPE_3(u8);
GENERATE_VECTOR_TYPE_4(u8);
struct r2i {
int x0,y0,x1,y1;
};
struct r2 {
float x0,y0,x1,y1;
};
struct QuadVertex {
v2 pos;
v2 tex;
};
struct m4 {
float d[16];
// 0 1 2 3
// 4 5 6 7
// 8 9 10 11
// 12 13 14 15
};
static m4 m4_iden() {
return m4{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
}
static m4 m4_invert(const m4& m) {
m4 inv;
inv.d[0] = m.d[5] * m.d[10] * m.d[15] -
m.d[5] * m.d[11] * m.d[14] -
m.d[9] * m.d[6] * m.d[15] +
m.d[9] * m.d[7] * m.d[14] +
m.d[13] * m.d[6] * m.d[11] -
m.d[13] * m.d[7] * m.d[10];
inv.d[4] = -m.d[4] * m.d[10] * m.d[15] +
m.d[4] * m.d[11] * m.d[14] +
m.d[8] * m.d[6] * m.d[15] -
m.d[8] * m.d[7] * m.d[14] -
m.d[12] * m.d[6] * m.d[11] +
m.d[12] * m.d[7] * m.d[10];
inv.d[8] = m.d[4] * m.d[9] * m.d[15] -
m.d[4] * m.d[11] * m.d[13] -
m.d[8] * m.d[5] * m.d[15] +
m.d[8] * m.d[7] * m.d[13] +
m.d[12] * m.d[5] * m.d[11] -
m.d[12] * m.d[7] * m.d[9];
inv.d[12] = -m.d[4] * m.d[9] * m.d[14] +
m.d[4] * m.d[10] * m.d[13] +
m.d[8] * m.d[5] * m.d[14] -
m.d[8] * m.d[6] * m.d[13] -
m.d[12] * m.d[5] * m.d[10] +
m.d[12] * m.d[6] * m.d[9];
inv.d[1] = -m.d[1] * m.d[10] * m.d[15] +
m.d[1] * m.d[11] * m.d[14] +
m.d[9] * m.d[2] * m.d[15] -
m.d[9] * m.d[3] * m.d[14] -
m.d[13] * m.d[2] * m.d[11] +
m.d[13] * m.d[3] * m.d[10];
inv.d[5] = m.d[0] * m.d[10] * m.d[15] -
m.d[0] * m.d[11] * m.d[14] -
m.d[8] * m.d[2] * m.d[15] +
m.d[8] * m.d[3] * m.d[14] +
m.d[12] * m.d[2] * m.d[11] -
m.d[12] * m.d[3] * m.d[10];
inv.d[9] = -m.d[0] * m.d[9] * m.d[15] +
m.d[0] * m.d[11] * m.d[13] +
m.d[8] * m.d[1] * m.d[15] -
m.d[8] * m.d[3] * m.d[13] -
m.d[12] * m.d[1] * m.d[11] +
m.d[12] * m.d[3] * m.d[9];
inv.d[13] = m.d[0] * m.d[9] * m.d[14] -
m.d[0] * m.d[10] * m.d[13] -
m.d[8] * m.d[1] * m.d[14] +
m.d[8] * m.d[2] * m.d[13] +
m.d[12] * m.d[1] * m.d[10] -
m.d[12] * m.d[2] * m.d[9];
inv.d[2] = m.d[1] * m.d[6] * m.d[15] -
m.d[1] * m.d[7] * m.d[14] -
m.d[5] * m.d[2] * m.d[15] +
m.d[5] * m.d[3] * m.d[14] +
m.d[13] * m.d[2] * m.d[7] -
m.d[13] * m.d[3] * m.d[6];
inv.d[6] = -m.d[0] * m.d[6] * m.d[15] +
m.d[0] * m.d[7] * m.d[14] +
m.d[4] * m.d[2] * m.d[15] -
m.d[4] * m.d[3] * m.d[14] -
m.d[12] * m.d[2] * m.d[7] +
m.d[12] * m.d[3] * m.d[6];
inv.d[10] = m.d[0] * m.d[5] * m.d[15] -
m.d[0] * m.d[7] * m.d[13] -
m.d[4] * m.d[1] * m.d[15] +
m.d[4] * m.d[3] * m.d[13] +
m.d[12] * m.d[1] * m.d[7] -
m.d[12] * m.d[3] * m.d[5];
inv.d[14] = -m.d[0] * m.d[5] * m.d[14] +
m.d[0] * m.d[6] * m.d[13] +
m.d[4] * m.d[1] * m.d[14] -
m.d[4] * m.d[2] * m.d[13] -
m.d[12] * m.d[1] * m.d[6] +
m.d[12] * m.d[2] * m.d[5];
inv.d[3] = -m.d[1] * m.d[6] * m.d[11] +
m.d[1] * m.d[7] * m.d[10] +
m.d[5] * m.d[2] * m.d[11] -
m.d[5] * m.d[3] * m.d[10] -
m.d[9] * m.d[2] * m.d[7] +
m.d[9] * m.d[3] * m.d[6];
inv.d[7] = m.d[0] * m.d[6] * m.d[11] -
m.d[0] * m.d[7] * m.d[10] -
m.d[4] * m.d[2] * m.d[11] +
m.d[4] * m.d[3] * m.d[10] +
m.d[8] * m.d[2] * m.d[7] -
m.d[8] * m.d[3] * m.d[6];
inv.d[11] = -m.d[0] * m.d[5] * m.d[11] +
m.d[0] * m.d[7] * m.d[9] +
m.d[4] * m.d[1] * m.d[11] -
m.d[4] * m.d[3] * m.d[9] -
m.d[8] * m.d[1] * m.d[7] +
m.d[8] * m.d[3] * m.d[5];
inv.d[15] = m.d[0] * m.d[5] * m.d[10] -
m.d[0] * m.d[6] * m.d[9] -
m.d[4] * m.d[1] * m.d[10] +
m.d[4] * m.d[2] * m.d[9] +
m.d[8] * m.d[1] * m.d[6] -
m.d[8] * m.d[2] * m.d[5];
float det = m.d[0] * inv.d[0] + m.d[1] * inv.d[4] + m.d[2] * inv.d[8] + m.d[3] * inv.d[12];
if (det == 0)
return inv;
det = 1.0f / det;
for (int i = 0; i < 16; i++)
inv.d[i] *= det;
return inv;
}
static void m4_print(m4 m) {
printf("(\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n)\n", m.d[0], m.d[1], m.d[2], m.d[3], m.d[4], m.d[5], m.d[6], m.d[7], m.d[8], m.d[9], m.d[10], m.d[11], m.d[12], m.d[13], m.d[14], m.d[15]);
}
static m4 operator*(m4 a, m4 b) {
return
{
a.d[0]*b.d[0] + a.d[1]*b.d[4] + a.d[2]*b.d[8] + a.d[3]*b.d[12],
a.d[0]*b.d[1] + a.d[1]*b.d[5] + a.d[2]*b.d[9] + a.d[3]*b.d[13],
a.d[0]*b.d[2] + a.d[1]*b.d[6] + a.d[2]*b.d[10] + a.d[3]*b.d[14],
a.d[0]*b.d[3] + a.d[1]*b.d[7] + a.d[2]*b.d[11] + a.d[3]*b.d[15],
a.d[4]*b.d[0] + a.d[5]*b.d[4] + a.d[6]*b.d[8] + a.d[7]*b.d[12],
a.d[4]*b.d[1] + a.d[5]*b.d[5] + a.d[6]*b.d[9] + a.d[7]*b.d[13],
a.d[4]*b.d[2] + a.d[5]*b.d[6] + a.d[6]*b.d[10] + a.d[7]*b.d[14],
a.d[4]*b.d[3] + a.d[5]*b.d[7] + a.d[6]*b.d[11] + a.d[7]*b.d[15],
a.d[8]*b.d[0] + a.d[9]*b.d[4] + a.d[10]*b.d[8] + a.d[11]*b.d[12],
a.d[8]*b.d[1] + a.d[9]*b.d[5] + a.d[10]*b.d[9] + a.d[11]*b.d[13],
a.d[8]*b.d[2] + a.d[9]*b.d[6] + a.d[10]*b.d[10] + a.d[11]*b.d[14],
a.d[8]*b.d[3] + a.d[9]*b.d[7] + a.d[10]*b.d[11] + a.d[11]*b.d[15],
a.d[12]*b.d[0] + a.d[13]*b.d[4] + a.d[14]*b.d[8] + a.d[15]*b.d[12],
a.d[12]*b.d[1] + a.d[13]*b.d[5] + a.d[14]*b.d[9] + a.d[15]*b.d[13],
a.d[12]*b.d[2] + a.d[13]*b.d[6] + a.d[14]*b.d[10] + a.d[15]*b.d[14],
a.d[12]*b.d[3] + a.d[13]*b.d[7] + a.d[14]*b.d[11] + a.d[15]*b.d[15],
};
}
static m4 m4_transpose(m4 m) {
m4 r;
r.d[0] = m.d[0];
r.d[1] = m.d[4];
r.d[2] = m.d[8];
r.d[3] = m.d[12];
r.d[4] = m.d[1];
r.d[5] = m.d[5];
r.d[6] = m.d[9];
r.d[7] = m.d[13];
r.d[8] = m.d[2];
r.d[9] = m.d[6];
r.d[10] = m.d[10];
r.d[11] = m.d[14];
r.d[12] = m.d[3];
r.d[13] = m.d[7];
r.d[14] = m.d[11];
r.d[15] = m.d[15];
return r;
}
static v3 operator*(m4 m, v3 v) {
v3 r;
r.x = m.d[0]*v.x + m.d[1]*v.y + m.d[2]*v.z;
r.y = m.d[4]*v.x + m.d[5]*v.y + m.d[6]*v.z;
r.z = m.d[8]*v.x + m.d[9]*v.y + m.d[10]*v.z;
return r;
}
static float len(v3 v) {
return sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
}
static float lensq(v3 v) {
return v.x*v.x + v.y*v.y + v.z*v.z;
}
// camera
struct Camera {
v2 look;
float up; // how much up we are looking, in radians
};
// the camera_* functions transforms camera movement to (x,y,z) coordinates, but do not modify the camera position
static v3 camera_move(const Camera *camera, float forward, float right, float up) {
return v3{
camera->look.x*forward + camera->look.y*right,
camera->look.y*forward + -camera->look.x*right,
up
};
};
static v3 camera_forward(const Camera *camera, float speed) {
return {camera->look.x*speed, camera->look.y*speed, 0.0f};
}
static v3 camera_forward_fly(const Camera *camera, float speed) {
float cu = cosf(camera->up);
float su = sinf(camera->up);
return {camera->look.x*speed*cu, camera->look.y*speed*cu, su*speed};
}
static v3 camera_backward(const Camera *camera, float speed) {
return camera_forward(camera, -speed);
}
static v3 camera_backward_fly(const Camera *camera, float speed) {
return camera_forward_fly(camera, -speed);
}
static v3 camera_up(const Camera *, float speed) {
return v3{0.0f, 0.0f, speed};
}
static v3 camera_down(const Camera *, float speed) {
return v3{0.0f, 0.0f, -speed};
}
static v3 camera_strafe_right(const Camera *camera, float speed) {
return {camera->look.y*speed, -camera->look.x*speed, 0.0f};
}
static v3 camera_strafe_left(const Camera *camera, float speed) {
return camera_strafe_right(camera, -speed);
}
static void camera_turn(Camera *camera, float angle) {
float a = atan2f(camera->look.y, camera->look.x);
a -= angle;
camera->look = {cosf(a), sinf(a)};
}
static void camera_pitch(Camera *camera, float angle) {
camera->up = clamp(camera->up + angle, -PI/2.0f, PI/2.0f);
}
static m4 camera_rotation_matrix(const Camera *camera) {
// rotation
float cu = cosf(camera->up);
float su = sinf(camera->up);
// rotation
m4 r = {};
// x is right = look * (0,0,1)
r.d[0] = camera->look.y;
r.d[1] = -camera->look.x;
r.d[2] = 0.0f;
// y is up
r.d[4] = -camera->look.x*su;
r.d[5] = -camera->look.y*su;
r.d[6] = cu;
// z is out of screen
r.d[8] = -camera->look.x*cu;
r.d[9] = -camera->look.y*cu;
r.d[10] = -su;
r.d[15] = 1.0f;
return r;
}
static m4 camera_view_matrix(const Camera *camera, v3 pos) {
// translation
m4 t = m4_iden();
t.d[3] = -pos.x;
t.d[7] = -pos.y;
t.d[11] = -pos.z;
// rotation
m4 r = camera_rotation_matrix(camera);
return r * t;
}
static m4 camera_projection_matrix(const Camera *, float fov, float nearz, float farz, float screen_ratio) {
// projection (http://www.songho.ca/opengl/gl_projectionmatrix.html)
const float n = nearz;
const float f = farz;
const float r = n * tanf(fov/2.0f);
const float t = r * screen_ratio;
m4 p = {};
p.d[0] = n/r;
p.d[5] = n/t;
p.d[10] = -(f+n)/(f-n);
p.d[11] = -2.0f*f*n/(f-n);
p.d[14] = -1.0f;
return p;
}
static m4 camera_viewprojection_matrix(const Camera *camera, v3 pos, float fov, float nearz, float farz, float screen_ratio) {
m4 v = camera_view_matrix(camera, pos);
m4 p = camera_projection_matrix(camera, fov, nearz, farz, screen_ratio);
// printf("t,r,p:\n");
// m4_print(t);
// m4_print(r);
// m4_print(p);
m4 result = p * v;
// m4_print(result);
return (result);
}
static void camera_lookat(Camera *camera, v3 from, v3 to) {
v3 d = to - from;
camera->look = normalize(d.xy);
d = normalize(d);
camera->up = asinf(d.z);
}
static m4 camera_ortho_matrix(const Camera *, float width, float height, float nearz, float farz) {
m4 o = {};
o.d[0] = 1.0f/width;
o.d[5] = 1.0f/height;
o.d[10] = -2.0f/(farz - nearz);
o.d[11] = -(farz + nearz)/(farz - nearz);
o.d[15] = 1.0f;
return o;
}
static m4 camera_viewortho_matrix(const Camera *camera, v3 pos, float width, float height, float nearz, float farz) {
m4 v = camera_view_matrix(camera, pos);
m4 o = camera_ortho_matrix(camera, width, height, nearz, farz);
return o * v;
}
// @world_object_vertex_shader
static const char *world_object_vertex_shader = R"VSHADER(
#version 330 core
// in
layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 tpos;
layout(location = 2) in vec3 normal;
// out
out vec2 f_tpos;
out vec3 f_position;
out vec3 f_normal;
out vec3 f_diffuse;
out vec3 f_ambient;
out vec4 f_shadowmap_pos;
out vec4 f_fog;
// uniform
uniform vec3 u_camerapos;
uniform float u_fog_near;
uniform float u_fog_far;
uniform mat4 u_viewprojection;
uniform vec3 u_ambient;
uniform vec3 u_skylight_dir;
uniform vec3 u_skylight_color;
uniform mat4 u_shadowmap_viewprojection;
uniform samplerCube u_skybox; // so we know what color the fog should be!
void main() {
// calculate where the distance lies between fog_near and fog_far
vec3 dp = pos - u_camerapos;
// convert to openGL xyz coordinates
dp = vec3(dp.x, -dp.z, dp.y);
float fog = clamp((length(dp) - u_fog_near) / (u_fog_far - u_fog_near), 0, 1);
if (fog > 0.0) {
f_fog = vec4(texture(u_skybox, dp).xyz * u_ambient, fog);
} else {
f_fog = vec4(0);
}
// calculate lighting
f_ambient = vec3(u_ambient);
f_diffuse = vec3(0.0f);
f_diffuse += u_skylight_color * max(dot(-u_skylight_dir, normal), 0.0f);
gl_Position = u_viewprojection * vec4(pos, 1.0f);
f_shadowmap_pos = u_shadowmap_viewprojection * vec4(pos, 1.0f);
f_tpos = tpos;
f_normal = normal;
f_position = pos - u_camerapos;
}
)VSHADER";
// @world_object_fragment_shader
static const char *world_object_fragment_shader = R"FSHADER(
#version 330 core
// in
in vec2 f_tpos;
in vec3 f_position;
in vec3 f_normal;
in vec3 f_diffuse;
in vec3 f_ambient;
in vec4 f_shadowmap_pos;
in vec4 f_fog;
// out
layout(location = 0) out vec4 g_color;
layout(location = 1) out vec4 g_normal;
layout(location = 2) out vec4 g_position;
// uniform
uniform sampler2D u_texture;
uniform sampler2D u_shadowmap;
float calc_shadow(vec4 pos) {
// perspective divide
vec3 p = pos.xyz / pos.w;
// normalize to [0,1]
p = p*0.5 + 0.5;
float depth = texture(u_shadowmap, p.xy).r;
vec2 texelSize = 1.0 / textureSize(u_shadowmap, 0);
float bias = 0.0f;
// change to 1 to enable (very rudamentary) pcf, see https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping
#if 0
float shadow = 0.0;
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
float pcfDepth = texture(u_shadowmap, p.xy + vec2(x, y) * texelSize).r;
shadow += p.z - bias > pcfDepth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
return 1.0 - shadow;
#else
return depth < p.z - bias ? 0.0f : 1.0f;
#endif
}
void main() {
vec3 light = vec3(0.0f);
float shadow = calc_shadow(f_shadowmap_pos);
light += f_ambient;