-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenGL.h
1492 lines (1171 loc) · 51.2 KB
/
OpenGL.h
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
#ifndef RR_OPENGL
#define RR_OPENGL
// GL Version 1.5
#define GL_ARRAY_BUFFER 0x8892
#define GL_ELEMENT_ARRAY_BUFFER 0x8893
// GL Version 1.2
#define GL_CLAMP_TO_EDGE 0x812F
// GL Version 1.3
#define GL_CLAMP_TO_BORDER 0x812D
#define GL_MULTISAMPLE 0x809D
#define GL_SAMPLE_ALPHA_TO_COVERAGE 0x809E
#define GL_SAMPLE_ALPHA_TO_ONE 0x809F
#define GL_SAMPLE_COVERAGE 0x80A0
#define GL_SAMPLE_BUFFERS 0x80A8
#define GL_SAMPLES 0x80A9
#define GL_SAMPLE_COVERAGE_VALUE 0x80AA
#define GL_SAMPLE_COVERAGE_INVERT 0x80AB
#define GL_TEXTURE0 0x84C0
#define GL_TEXTURE1 0x84C1
#define GL_TEXTURE2 0x84C2
#define GL_TEXTURE3 0x84C3
#define GL_TEXTURE4 0x84C4
#define GL_TEXTURE5 0x84C5
#define GL_TEXTURE6 0x84C6
#define GL_TEXTURE7 0x84C7
#define GL_TEXTURE8 0x84C8
#define GL_TEXTURE9 0x84C9
// GL Version 1.4
#define GL_DEPTH_COMPONENT16 0x81A5
#define GL_DEPTH_COMPONENT24 0x81A6
#define GL_DEPTH_COMPONENT32 0x81A7
#define GL_FUNC_ADD 0x8006
// GL Version 1.5
#define GL_STREAM_DRAW 0x88E0
#define GL_STREAM_READ 0x88E1
#define GL_STREAM_COPY 0x88E2
// GL Version 2.0
#define GL_SHADING_LANGUAGE_VERSION 0x8B8C
#define GL_VERTEX_SHADER 0x8B31
#define GL_FRAGMENT_SHADER 0x8B30
#define GL_VALIDATE_STATUS 0x8B83
#define GL_COMPILE_STATUS 0x8B81
#define GL_LINK_STATUS 0x8B82
// GL Version 2.1
#define GL_SRGB 0x8C40
#define GL_SRGB8 0x8C41
#define GL_SRGB_ALPHA 0x8C42
#define GL_SRGB8_ALPHA8 0x8C43
// GL Version 3.0
#define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 0x8CD6
#define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT 0x8CD7
#define GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER 0x8CDB
#define GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER 0x8CDC
#define GL_FRAMEBUFFER_UNSUPPORTED 0x8CDD
#define GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE 0x8D56
#define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506
#define GL_FRAMEBUFFER_UNDEFINED 0x8219
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5
#define GL_READ_FRAMEBUFFER 0x8CA8
#define GL_DRAW_FRAMEBUFFER 0x8CA9
#define GL_FRAMEBUFFER 0x8D40
#define GL_FRAMEBUFFER_SRGB 0x8DB9
#define GL_MAJOR_VERSION 0x821B // use these?
#define GL_MINOR_VERSION 0x821C // use these?
#define GL_NUM_EXTENSIONS 0x821D
#define GL_MAX_SAMPLES 0x8D57
#define GL_DEPTH_ATTACHMENT 0x8D00
#define GL_COLOR_ATTACHMENT0 0x8CE0
#define GL_DEPTH_COMPONENT32F 0x8CAC
#define GL_TEXTURE_2D_ARRAY 0x8C1A
// GL Version 3.1
#define GL_PRIMITIVE_RESTART_FIXED_INDEX 0x8D69
// GL Version 3.2
#define GL_TEXTURE_2D_MULTISAMPLE 0x9100
#define GL_GEOMETRY_SHADER 0x8DD9
// GL Version 4.3
#define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242
#define GL_DEBUG_SEVERITY_HIGH 0x9146
#define GL_DEBUG_SEVERITY_MEDIUM 0x9147
#define GL_DEBUG_SEVERITY_LOW 0x9148
// windows
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
#define WGL_CONTEXT_FLAGS_ARB 0x2094
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
#define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
#define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
typedef char GLchar;
typedef ptrdiff_t GLsizeiptr;
typedef void (APIENTRY *GLDEBUGPROC)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, void *userParam);
// GL Version 1.1
typedef void WINAPI glTexSubImage3D_(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid * data);
typedef void WINAPI glTexImage3D_(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid * data);
// GL Version 1.3
typedef void WINAPI glActiveTexture_(GLenum texture);
typedef void WINAPI glBlendEquation_(GLenum mode);
// GL Version 1.5
typedef void WINAPI glGenBuffers_(GLsizei n, GLuint *buffers);
typedef void WINAPI glBindBuffer_(GLenum target, GLuint buffer);
typedef void WINAPI glBufferData_(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
// GL Version 2.0
typedef GLuint WINAPI glCreateShader_(GLenum type);
typedef void WINAPI glShaderSource_(GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length);
typedef void WINAPI glCompileShader_(GLuint shader);
typedef void WINAPI glLinkProgram_(GLuint program);
typedef GLuint WINAPI glCreateProgram_(void);
typedef void WINAPI glAttachShader_(GLuint program, GLuint shader);
typedef void WINAPI glValidateProgram_(GLuint program);
typedef void WINAPI glGetProgramInfoLog_(GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
typedef void WINAPI glGetProgramiv_(GLuint program, GLenum pname, GLint *params);
typedef void WINAPI glGetShaderInfoLog_(GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
typedef GLint WINAPI glGetUniformLocation_(GLuint program, const GLchar *name);
typedef void WINAPI glUseProgram_(GLuint program);
typedef void WINAPI glVertexAttribPointer_(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
typedef void WINAPI glEnableVertexAttribArray_(GLuint index);
typedef void WINAPI glDisableVertexAttribArray_(GLuint index);
typedef GLint WINAPI glGetAttribLocation_(GLuint program, const GLchar *name);
typedef void WINAPI glVertexAttrib3f_(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2);
typedef void WINAPI glUniform1i_(GLint location, GLint v0);
typedef void WINAPI glBindAttribLocation_(GLuint program, GLuint index, const GLchar *name);
typedef void WINAPI glUniform4fv_(GLint location, GLsizei count, const GLfloat *value);
typedef void WINAPI glUniform1iv_(GLint location, GLsizei count, const GLint *value);
typedef void WINAPI glUniformMatrix4fv_(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
typedef void WINAPI glUniform1f_(GLint location, GLfloat v0);
typedef void WINAPI glUniform2f_(GLint location, GLfloat v0, GLfloat v1);
typedef void WINAPI glUniform3f_(GLint location, GLfloat v0, GLfloat v1, GLfloat v2);
typedef void WINAPI glUniform4f_(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
// GL Version 3.0
typedef const GLubyte* WINAPI glGetStringi_(GLenum name, GLuint index);
typedef void WINAPI glVertexAttribIPointer_(GLuint index, GLint size, GLenum type,GLsizei stride,const GLvoid *pointer);
typedef void WINAPI glGenFramebuffers_(GLsizei n, GLuint *ids);
typedef void WINAPI glBindFramebuffer_(GLenum target, GLuint framebuffer);
typedef GLenum WINAPI glCheckFramebufferStatus_(GLenum target);
typedef void WINAPI glFramebufferTexture2D_(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
typedef void WINAPI glBlitFramebuffer_(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter);
typedef void WINAPI glGenVertexArrays_(GLsizei n, GLuint *arrays);
typedef void WINAPI glBindVertexArray_(GLuint arrayName);
// GL Version 3.2
typedef void WINAPI glTexImage2DMultisample_(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height, GLboolean fixedsamplelocations);
typedef void WINAPI glFramebufferTexture_(GLenum target, GLenum attachment, GLuint texture, GLint level);
// GL Version 4.3
typedef void WINAPI glDebugMessageCallback_(GLDEBUGPROC callback, void * userParam);
static glBlendEquation_ *glBlendEquation;
static glTexImage3D_ *glTexImage3D;
static glTexSubImage3D_ *glTexSubImage3D;
static glGetStringi_ *glGetStringi;
static glTexImage2DMultisample_ *glTexImage2DMultisample;
static glUniform1f_ *glUniform1f;
static glUniform2f_ *glUniform2f;
static glUniform3f_ *glUniform3f;
static glUniform4f_ *glUniform4f;
static glBufferData_ *glBufferData;
static glBindBuffer_ *glBindBuffer;
static glGenBuffers_ *glGenBuffers;
static glBindVertexArray_ *glBindVertexArray;
static glGenVertexArrays_ *glGenVertexArrays;
static glBindAttribLocation_ *glBindAttribLocation;
static glUniform1i_ *glUniform1i;
static glActiveTexture_ *glActiveTexture;
static glDebugMessageCallback_ *glDebugMessageCallback;
static glBlitFramebuffer_ *glBlitFramebuffer;
static glFramebufferTexture2D_ *glFramebufferTexture2D;
static glFramebufferTexture_ *glFramebufferTexture;
static glCheckFramebufferStatus_ *glCheckFramebufferStatus;
static glVertexAttrib3f_ *glVertexAttrib3f;
static glBindFramebuffer_ *glBindFramebuffer;
static glGenFramebuffers_ *glGenFramebuffers;
static glCreateShader_ *glCreateShader;
static glShaderSource_ *glShaderSource;
static glCompileShader_ *glCompileShader;
static glLinkProgram_ *glLinkProgram;
static glCreateProgram_ *glCreateProgram;
static glAttachShader_ *glAttachShader;
static glValidateProgram_ *glValidateProgram;
static glGetProgramInfoLog_ *glGetProgramInfoLog;
static glGetProgramiv_ *glGetProgramiv;
static glGetShaderInfoLog_ *glGetShaderInfoLog;
static glUniformMatrix4fv_ *glUniformMatrix4fv;
static glUniform4fv_ *glUniform4fv;
static glUniform1iv_ *glUniform1iv;
static glGetUniformLocation_ *glGetUniformLocation;
static glUseProgram_ *glUseProgram;
static glVertexAttribIPointer_ *glVertexAttribIPointer;
static glVertexAttribPointer_ *glVertexAttribPointer;
static glEnableVertexAttribArray_ *glEnableVertexAttribArray;
static glDisableVertexAttribArray_ *glDisableVertexAttribArray;
static glGetAttribLocation_ *glGetAttribLocation;
struct OpenGLInfo
{
bool modernContext;
bool GL_EXT_texture_sRGB;
//char *extensions; this crap is now an array?
char *renderer;
char *version;
char *shadingLanguageVersion;
char *vendor;
GLint amountOfMultiSamples;
};
enum SamplerLocations
{
Sampler_Texture,
Sampler_Depth,
};
struct OpenGLProgram
{
b32 compiled;
GLuint program;
u32 flags;
//uniforms
GLuint projection;
GLuint cameraTransform;
GLuint objectTransform;
GLuint scaleColor;
GLuint shadowTransform;
GLuint boneStates;
GLuint ka;
GLuint kd;
GLuint ks;
GLuint lightP;
GLuint cameraP;
GLuint specularExponent;
GLuint depthSampler;
GLuint textureSampler;
// this is either a uniform or an attribute, depending on 'MulitTextured'
GLuint textureIndex;
//attributes
GLuint vertP;
GLuint vertC;
GLuint vertUV;
GLuint vertN;
GLuint boneIndices;
GLuint boneWeights;
};
struct FrameBuffer
{
GLuint id;
GLuint texture;
GLuint depth;
u32 width;
u32 height;
};
struct BlitProgram
{
GLuint program;
GLuint vertP;
GLuint vertUV;
GLuint textureSampler;
};
struct OpenGLContext
{
OpenGLInfo info;
GLuint vertexBuffer;
GLuint indexBuffer;
GLuint defaultInternalTextureFormat;
File shaderFile;
FrameBuffer renderBuffer;
FrameBuffer shadowBuffer;
FrameBuffer orthogonalBuffer;
BlitProgram blitProgram;
OpenGLProgram shaders[ShaderArray_Size];
};
// globals
static GLuint globalTextureArray;
static bool GLCStringEquals(char *first, u64 firstSize, char* second)
{
char* at = second;
for (u64 i = 0; i < firstSize; i++, at++)
{
if (*at == '\0' || first[i] != *at)
{
return false;
}
}
return true;
}
// not sure how to handle this whole situation todo
static OpenGLInfo OpenGLGetInfo(bool modernContext)
{
OpenGLInfo info = {};
info.modernContext = modernContext;
info.renderer = (char *)glGetString(GL_RENDERER);
if (modernContext)
{
info.shadingLanguageVersion = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
}
info.vendor = (char *)glGetString(GL_VENDOR);
info.version = (char *)glGetString(GL_VERSION);
glGetIntegerv(GL_MAX_SAMPLES, &(info.amountOfMultiSamples));
GLint amountOfExtensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &amountOfExtensions);
if(amountOfExtensions < 0) amountOfExtensions = 0;
for(u32 i = 0; i < (u32)amountOfExtensions; i++)
{
char *extension = (char *)glGetStringi(GL_EXTENSIONS, i);
if (CStringsAreEqual(extension, "GL_EXT_texture_sRGB"))
{
info.GL_EXT_texture_sRGB = true;
}
}
{
GLuint err = glGetError();
Assert(err == GL_NO_ERROR);
}
return info;
}
static void WINAPI OpenGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, void *userParam)
{
if (1 || severity == GL_DEBUG_SEVERITY_HIGH)
{
char *ErrorMessage = (char *)message;
#if 1
OutputDebugStringA("OPENGL: ");
OutputDebugStringA(ErrorMessage);
OutputDebugStringA("\n");
#endif
//Assert(!"OpenGL Error encountered");
fprintf(stderr, "OPENGL:\n");
fprintf(stderr, ErrorMessage);
fprintf(stderr, "\n");
fflush(stderr);
ExitProcess(0);
//running = false;
}
}
static BlitProgram CompileBlitProgram()
{
BlitProgram ret;
File file = LoadFile("src/simple.glsl", frameArena);
char *shaderCode = (char *)file.data;
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLchar *vertexShaderCode[] =
{
"#version 150\n", "#define VertexCode \n", shaderCode
};
glShaderSource(vertexShaderID, ArrayCount(vertexShaderCode), vertexShaderCode, 0);
glCompileShader(vertexShaderID);
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
GLchar *fragmentShaderCode[] =
{
"#version 150\n", "#define FragmentCode \n", shaderCode
};
glShaderSource(fragmentShaderID, ArrayCount(fragmentShaderCode), fragmentShaderCode, 0);
glCompileShader(fragmentShaderID);
GLuint programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
glValidateProgram(programID);
GLint linkValidated = false;
glGetProgramiv(programID, GL_LINK_STATUS, &linkValidated);
{
GLuint err = glGetError();
Assert(err == GL_NO_ERROR);
}
glUseProgram(programID);
ret.program = programID;
ret.vertP = glGetAttribLocation(ret.program, "vertP");
ret.vertUV = glGetAttribLocation(ret.program, "vertUV");
ret.textureSampler = glGetUniformLocation(programID, "textureSampler");
glUniform1i(ret.textureSampler, Sampler_Texture);
glUseProgram(0);
return ret;
}
static OpenGLProgram OpenGLMakeProgram(char *shaderCode, u32 flags)
{
String defines = PushArray(frameArena, Char, 0);
{
GLuint err = glGetError();
Assert(err == GL_NO_ERROR);
}
S("#version 150 \n", frameArena);
if(flags & ShaderFlags_Phong)
{
S("#define Phong \n", frameArena);
}
if(flags & ShaderFlags_Textured)
{
S("#define Textured \n", frameArena);
}
if(flags & ShaderFlags_ShadowMapping)
{
S("#define ShadowMapping \n", frameArena);
}
if(flags & ShaderFlags_Animated)
{
S("#define Animated \n", frameArena);
}
if(flags & ShaderFlags_ZBias)
{
S("#define ZBias \n", frameArena);
}
if(flags & ShaderFlags_MultiTextured)
{
S("#define MultiTextured \n", frameArena);
}
*PushStruct(frameArena, Char) = '\0';
EndArray(frameArena, Char, defines);
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLchar *vertexShaderCode[] =
{
defines.cstr, "#define VertexCode \n", shaderCode
};
glShaderSource(vertexShaderID, ArrayCount(vertexShaderCode), vertexShaderCode, 0);
glCompileShader(vertexShaderID);
#define GEOM 1
#if GEOM
GLuint geometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);
GLchar *geometryShaderCode[] =
{
defines.cstr, "#define GeometryCode \n", shaderCode
};
glShaderSource(geometryShaderID, ArrayCount(geometryShaderCode), geometryShaderCode, 0);
glCompileShader(geometryShaderID);
#endif
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
GLchar *fragmentShaderCode[] =
{
defines.cstr, "#define FragmentCode \n", shaderCode
};
glShaderSource(fragmentShaderID, ArrayCount(fragmentShaderCode), fragmentShaderCode, 0);
glCompileShader(fragmentShaderID);
GLuint programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
#if GEOM
glAttachShader(programID, geometryShaderID);
#endif
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
glValidateProgram(programID);
GLint linkValidated = false;
glGetProgramiv(programID, GL_LINK_STATUS, &linkValidated);
//glGetProgramiv(programID, GL_COMPILE_STATUS, &compileValidated);
{
GLuint err = glGetError();
Assert(err == GL_NO_ERROR);
}
GLint validated = linkValidated;
if (!validated)
{
GLsizei length;
char programError[4096];
char vertexError[4096];
#if GEOM
char geomError[4096];
#endif
char fragmentError[4096];
glGetProgramInfoLog(programID, sizeof(programError), &length, programError);
glGetShaderInfoLog(vertexShaderID, sizeof(vertexError), &length, vertexError);
#if GEOM
glGetShaderInfoLog(geometryShaderID, sizeof(geomError), &length, geomError);
#endif
glGetShaderInfoLog(fragmentShaderID, sizeof(fragmentError), &length, fragmentError);
fprintf(stderr, "OpenGL shader error:\n");
fprintf(stderr, "Program error:\n");
fprintf(stderr, programError);
fprintf(stderr, "Vertex error:\n");
fprintf(stderr, vertexError);
// todo here goem error
fprintf(stderr, "Fragment error:\n");
fprintf(stderr, fragmentError);
fprintf(stderr, "\n");
fflush(stderr);
ExitProcess(0);
Assert(!"ShaderError");
}
Assert(programID);
OpenGLProgram ret;
ret.compiled = true;
ret.flags = flags;
ret.program = programID;
glUseProgram(programID);
ret.vertP = glGetAttribLocation(ret.program, "vertP");
ret.vertC = glGetAttribLocation(ret.program, "vertC");
ret.vertUV = glGetAttribLocation(ret.program, "vertUV");
ret.vertN = glGetAttribLocation(ret.program, "vertN");
ret.boneIndices = glGetAttribLocation(ret.program, "boneIndices");
ret.boneWeights = glGetAttribLocation(ret.program, "boneWeights");
ret.projection = glGetUniformLocation(ret.program, "projection");
ret.cameraTransform = glGetUniformLocation(ret.program, "cameraTransform");
ret.objectTransform = glGetUniformLocation(ret.program, "objectTransform");
ret.depthSampler = glGetUniformLocation(ret.program, "depthTexture");
ret.textureSampler = glGetUniformLocation(ret.program, "textureSampler");
ret.shadowTransform = glGetUniformLocation(ret.program, "shadowTransform");
ret.lightP = glGetUniformLocation(ret.program, "lightPos");
ret.specularExponent = glGetUniformLocation(ret.program, "specularExponent");
ret.ka = glGetUniformLocation(ret.program, "ka");
ret.kd = glGetUniformLocation(ret.program, "kd");
ret.ks = glGetUniformLocation(ret.program, "ks");
ret.cameraP = glGetUniformLocation(ret.program, "cameraPos");
ret.textureIndex = (flags & ShaderFlags_MultiTextured) ? glGetAttribLocation(ret.program, "textureIndex") : glGetUniformLocation(ret.program, "textureIndex");
ret.scaleColor = glGetUniformLocation(ret.program, "scaleColor");
ret.boneStates = glGetUniformLocation(ret.program, "boneStates");
glUniform1i(ret.textureSampler, Sampler_Texture);
glUniform1i(ret.depthSampler, Sampler_Depth);
{
GLuint err = glGetError();
Assert(err == GL_NO_ERROR);
}
return ret;
}
static void RegisterTriangleMesh(TriangleMesh *mesh)
{
void *data = NULL;
u32 stride = 0;
Arena *arena = globalDebugState.arena;
// todo maybe make flags
if(mesh->skeleton.bones.amount)
{
stride = sizeof(VertexFormatPCUNBD);
v3Array colors = PushArray(frameArena, v3, mesh->skeleton.bones.amount);
RandomSeries series = GetRandomSeries();
For(colors)
{
it->r = (u32)(RandomU32(&series) & 0xFF) / 255.0f;
it->g = (u32)(RandomU32(&series) & 0xFF) / 255.0f;
it->b = (u32)(RandomU32(&series) & 0xFF) / 255.0f;
}
VertexFormatPCUNBD *packedData = PushData(arena, VertexFormatPCUNBD, 0);
for(u32 i = 0; i < mesh->positions.amount; i++)
{
VertexFormatPCUNBD *v = PushStruct(arena, VertexFormatPCUNBD);
v->p = mesh->positions[i];
v->uv = mesh->uvs[i];
v->n = mesh->normals[i];
v->c = mesh->colors[i];
u32 unflattendIndex = mesh->skeleton.vertexMap[i];
WeightDataArray weights = mesh->skeleton.vertexToWeightsMap[unflattendIndex];
switch(weights.amount)
{
case 0:
{
v->bw = V4();
v->bi = V4i();
}break;
case 1:
{
v->bw = V4 (weights[0].weight, 0.0f, 0.0f, 0.0f);
v->bi = V4i(weights[0].boneIndex, 0, 0, 0);
}break;
case 2:
{
v->bw = V4 (weights[0].weight, weights[1].weight, 0.0f, 0.0f);
v->bi = V4i(weights[0].boneIndex, weights[1].boneIndex, 0, 0);
}break;
case 3:
{
v->bw = V4 (weights[0].weight, weights[1].weight, weights[2].weight, 0.0f);
v->bi = V4i(weights[0].boneIndex, weights[1].boneIndex, weights[2].boneIndex, 0);
}break;
default:
{
// todo I think the weights are presorted.... check this, otherwise do it in the parsing
v->bw = V4(weights[0].weight, weights[1].weight, weights[2].weight, weights[3].weight);
v->bw /= SumV4(v->bw);
v->bi = V4i(weights[0].boneIndex, weights[1].boneIndex, weights[2].boneIndex, weights[3].boneIndex);
}break;
Assert(SumV4(v->bw) == 1.0f);
}
}
data = packedData;
}
else
{
stride = sizeof(VertexFormatPCUN);
VertexFormatPCUN *packedData = PushData(frameArena, VertexFormatPCUN, 0);
for(u32 i = 0; i < mesh->positions.amount; i++)
{
VertexFormatPCUN *v = PushStruct(frameArena, VertexFormatPCUN);
v->p = mesh->positions[i];
v->c = mesh->colors[i];
v->uv = mesh->uvs[i];
v->n = mesh->normals[i];
}
data = packedData;
}
mesh->vertexFormatSize = stride;
Assert(mesh->positions.amount < 65536); // <, because reset index
//todo look up what those GL_Stream_Draw mean?
glGenBuffers(1, &mesh->vertexVBO);
glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexVBO);
glBufferData(GL_ARRAY_BUFFER, mesh->positions.amount * stride, data, GL_STREAM_DRAW);
glGenBuffers(1, &mesh->indexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indexVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->indices.amount * sizeof(mesh->indices[0]), mesh->indices.data, GL_STREAM_DRAW);
}
static void UpdateWrapingTexture(TextureIndex textureIndex, u32 width, u32 height, u32 *pixels)
{
#if 1
glBindTexture(GL_TEXTURE_2D_ARRAY, globalTextureArray);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, textureIndex.index, width, height, 1, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
#else
glBindTexture(GL_TEXTURE_2D, (GLuint)bitmap.textureHandle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, bitmap.width, bitmap.height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bitmap.pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
#endif
glBindTexture(GL_TEXTURE_2D, 0);
}
struct OpenGLUniformInfo
{
GLuint vertexBuffer = 0xFFFFFFFF;
GLuint indexBuffer = 0xFFFFFFFF;
m4x4 objectTransform = Identity();
v4 scaleColor = V4(1.0f, 1.0f, 1.0f, 1.0f);
m4x4Array boneStates = {};
u32 textureIndex;
};
static void BeginUseProgram(OpenGLContext *context, OpenGLProgram *prog, RenderSetup setup)
{
glUseProgram(prog->program);
// this can be in the setup of the programs
glBindTexture(GL_TEXTURE_2D_ARRAY, globalTextureArray);
glUniformMatrix4fv(prog->projection, 1, GL_TRUE, setup.projection.a[0]);
glUniformMatrix4fv(prog->cameraTransform, 1, GL_TRUE, setup.cameraTransform.a[0]);
if (prog->flags & ShaderFlags_ShadowMapping)
{
glUniformMatrix4fv(prog->shadowTransform, 1, GL_TRUE, setup.shadowMat.a[0]);
// this as well
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, context->shadowBuffer.depth);
glActiveTexture(GL_TEXTURE0);
}
if(prog->flags & ShaderFlags_Phong)
{
v3 lightP = setup.lightP;
glUniform3f(prog->lightP, lightP.x, lightP.y, lightP.z);
v3 camP = setup.cameraP;
glUniform3f(prog->cameraP, camP.x, camP.y, camP.z);
}
}
static void SetupUniforms(OpenGLProgram *prog, OpenGLUniformInfo uniforms)
{
glUniform4f(prog->scaleColor, uniforms.scaleColor.r, uniforms.scaleColor.g, uniforms.scaleColor.b, uniforms.scaleColor.a); // rgba?
glUniformMatrix4fv(prog->objectTransform, 1, GL_TRUE, uniforms.objectTransform.a[0]);
if(prog->flags & ShaderFlags_Animated)
{
glUniformMatrix4fv(prog->boneStates, uniforms.boneStates.amount, GL_TRUE, (GLfloat *)uniforms.boneStates.data);
}
if(prog->flags & ShaderFlags_Textured)
{
glUniform1i(prog->textureIndex, uniforms.textureIndex);
}
glBindBuffer(GL_ARRAY_BUFFER, uniforms.vertexBuffer);
if(uniforms.indexBuffer != 0xFFFFFFFF)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uniforms.indexBuffer);
}
}
static void BeginAttribArrays(OpenGLProgram *prog, u32 vertexFormatSize)
{
glEnableVertexAttribArray(prog->vertP);
glEnableVertexAttribArray(prog->vertC);
glVertexAttribPointer(prog->vertP, 3, GL_FLOAT, false, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, p));
glVertexAttribPointer(prog->vertC, 4, GL_UNSIGNED_BYTE, true, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, c));
if(prog->flags & ShaderFlags_Textured)
{
glEnableVertexAttribArray(prog->vertUV);
glVertexAttribPointer(prog->vertUV, 2, GL_FLOAT, false, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, uv));
}
if(prog->flags & ShaderFlags_MultiTextured)
{
Assert(!(prog->flags & ShaderFlags_Textured));
glEnableVertexAttribArray(prog->vertUV);
glVertexAttribPointer(prog->vertUV, 2, GL_FLOAT, false, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, uv));
glEnableVertexAttribArray(prog->textureIndex);
glVertexAttribIPointer(prog->textureIndex, 1, GL_UNSIGNED_SHORT, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, textureIndex));
}
if(prog->flags & ShaderFlags_Phong)
{
glEnableVertexAttribArray(prog->vertN);
glVertexAttribPointer(prog->vertN, 3, GL_FLOAT, false, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, n));
}
if(prog->flags & ShaderFlags_Animated)
{
glEnableVertexAttribArray(prog->boneIndices);
glVertexAttribIPointer(prog->boneIndices, 4, GL_INT, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, bi));
glEnableVertexAttribArray(prog->boneWeights);
glVertexAttribPointer(prog->boneWeights, 4, GL_FLOAT, false, vertexFormatSize, (void *)OffsetOf(VertexFormatTemplate, bw));
}
}
static void EndAttribArrays(OpenGLProgram *prog)
{
glDisableVertexAttribArray(prog->vertP);
glDisableVertexAttribArray(prog->vertC);
if(prog->flags & ShaderFlags_Textured)
{
glDisableVertexAttribArray(prog->vertUV);
}
if(prog->flags & ShaderFlags_Phong)
{
glDisableVertexAttribArray(prog->vertN);
}
if(prog->flags & ShaderFlags_Animated)
{
glDisableVertexAttribArray(prog->boneIndices);
glDisableVertexAttribArray(prog->boneWeights);
}
if(prog->flags & ShaderFlags_MultiTextured)
{
glDisableVertexAttribArray(prog->vertUV);
glDisableVertexAttribArray(prog->textureIndex);
}
}
static OpenGLContext OpenGLInit(bool modernContext)
{
//GLuint reservedBltTexture;
//glGenTextures(1, &reservedBltTexture);
OpenGLContext ret = {};
OpenGLInfo info = OpenGLGetInfo(modernContext);
ret.info = info;
if (glDebugMessageCallback)
{
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(OpenGLDebugCallback, 0);
}
ret.defaultInternalTextureFormat = GL_RGBA;
if (info.GL_EXT_texture_sRGB)
{
ret.defaultInternalTextureFormat = GL_SRGB_ALPHA;
}
GLuint DummyVertexArray;
glGenVertexArrays(1, &DummyVertexArray);
glBindVertexArray(DummyVertexArray);
glGenBuffers(1, &ret.vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, ret.vertexBuffer);
glGenBuffers(1, &ret.indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ret.indexBuffer);
ret.shaderFile = LoadFile("src/ubershader.glsl", globalAlloc);
ret.blitProgram = CompileBlitProgram();
u32 amountOfMultiSamples = Min(info.amountOfMultiSamples, 4u);
FrameBuffer renderBuffer;
// todo make this passed in
renderBuffer.width = 1280;
renderBuffer.height = 720;
#if 0 // to work around renderdoc not working...
{
GLuint texture_map;
glGenTextures(1, &texture_map);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture_map);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, amountOfMultiSamples, ret.defaultInternalTextureFormat, renderBuffer.width, renderBuffer.height, GL_FALSE);
renderBuffer.texture = texture_map;
glBindTexture(GL_TEXTURE_2D, 0);
GLuint depth_texture;
glGenTextures(1, &depth_texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, depth_texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, amountOfMultiSamples, GL_DEPTH_COMPONENT, renderBuffer.width, renderBuffer.height, GL_FALSE);
renderBuffer.depth = depth_texture;
// Build the framebuffer.
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, texture_map, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, depth_texture, 0);
renderBuffer.id = framebuffer;
}
#else // not multisampled
{
// renderBuffer
GLuint texture_map;
glGenTextures(1, &texture_map);
glBindTexture(GL_TEXTURE_2D, texture_map);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, renderBuffer.width, renderBuffer.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
renderBuffer.texture = texture_map;
glBindTexture(GL_TEXTURE_2D, 0);
GLuint depth_texture;
glGenTextures(1, &depth_texture);
glBindTexture(GL_TEXTURE_2D, depth_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, renderBuffer.width, renderBuffer.height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
renderBuffer.depth = depth_texture;
// Build the framebuffer.
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_map, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture, 0);
renderBuffer.id = framebuffer;
}
#endif
ret.renderBuffer = renderBuffer;
{
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);