-
Notifications
You must be signed in to change notification settings - Fork 47
/
gb_gl.h
2592 lines (2049 loc) · 78 KB
/
gb_gl.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
/* gb.h - v0.05 - OpenGL Helper Library - public domain
- no warranty implied; use at your own risk
This is a single header file with a bunch of useful stuff
for working with OpenGL
===========================================================================
YOU MUST
#define GBGL_IMPLEMENTATION
in EXACTLY _one_ C or C++ file that includes this header, BEFORE the
include like this:
#define GBGL_IMPLEMENTATION
#include "gb_gl.h"
All other files should just #include "gb_gl.h" without #define
Dependencies
NOTE: You may need change the path
This library REQUIRES "stb_image.h" for loading images from file
This library REQUIRES "gb.h" at this moment in time.
If you are using the font library (e.g. GBGL_NO_FONTS is _not_ defined):
This library then REQUIRES "stb_truetype.h" for ttf handling
This library then REQUIRES "stb_rect_pack.h"
NOTE(bill): I may remove these dependencies for the font handling by
embedding the needed types and procedures.
Optional Dependencies
"gb_math.h" as a lot of useful things in it over <math.h>.
Why not have a look at it?
Optional Defines
GBGL_NO_FONTS - Do no use font subsystem
GBGL_NO_BASIC_STATE - Do no use basic state subsystem
Steps for supporting dynamic reload:
You _MUST_ defined you own malloc and free that use whatever
permanent memory system you are using:
#define gbgl_malloc
#define gbgl_free
===========================================================================
Conventions used:
gbglTypesAreLikeThis (None core types)
gbgl_functions_and_variables_like_this
Prefer // Comments
Never use _t suffix for types (I think they are stupid...)
Version History:
0.06 - Enum convention change
0.05 - gbglColour
0.04e - Change brace style because why not?
0.04d - Use new gb.h file handling system
0.04c - Use new gb.h file handling system
0.04b - Work with the new gb.h
0.04a - Better Documentation
0.04 - Remove gb_math.h dependency
0.03a - Better Rounded Rect
0.03 - Basic State Rendering
0.02 - Font Caching and Rendering
0.01 - Initial Version
LICENSE
This software is dual-licensed to the public domain and under the following
license: you are granted a perpetual, irrevocable license to copy, modify,
publish, and distribute this file as you see fit.
WARNING
- This library is _highly_ experimental and features may not work as expected.
- This also means that many functions are not documented.
CREDITS
Written by Ginger Bill
*/
#ifndef GBGL_INCLUDE_GB_GL_H
#define GBGL_INCLUDE_GB_GL_H
#ifndef GB_IMPLEMENTATION
#include "gb.h"
#endif
#ifndef STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif
#if !defined(GBGL_NO_FONTS)
#ifndef STB_RECT_PACK_IMPLEMENTATION
#include "stb_rect_pack.h"
#endif
#ifndef STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
#endif
#endif
#if defined(GBGL_USE_GB_MATH)
#ifndef GB_MATH_IMPLEMENTATION
#include "gb_math.h"
#endif
#define gbgl_lerp(x, y, t) gb_lerp(x, y, t)
#define gbgl_sin(x) gb_sin(x)
#define gbgl_cos(x) gb_cos(x)
#define gbgl_abs(x) gb_abs(x)
#define gbgl_min(x, y) gb_min(x, y)
#define gbgl_max(x, y) gb_max(x, y)
#define gbgl_round(x) gb_round(x)
#else
#if !defined(GBGL_USE_CUSTOM_MATH)
#include <math.h>
#define gbgl_lerp(x, y, t) ((x)*(1.0f-(t)) + (y)*(t))
#define gbgl_sin(x) sinf(x)
#define gbgl_cos(x) cosf(x)
#define gbgl_abs(x) ((x) >= 0 ? (x) : -(x))
#define gbgl_min(x, y) ((x) < (y) ? (x) : (y))
#define gbgl_max(x, y) ((x) > (y) ? (x) : (y))
#define gbgl_round(x) (((x) >= 0.0f) ? floorf((x) + 0.5f) : ceilf((x) - 0.5f))
#endif
#endif
#define gbgl_clamp(x, lower, upper) gbgl_min(gbgl_max((x), (lower)), (upper))
#define gbgl_clamp01(x) gbgl_clamp(x, 0, 1)
#if defined(__cplusplus)
extern "C" {
#endif
#ifndef GBGL_DEF
#define GBGL_DEF extern
#endif
#ifndef gbgl_malloc
#define gbgl_malloc(sz) malloc(sz)
#endif
#ifndef gbgl_free
#define gbgl_free(ptr) free(ptr)
#endif
#ifndef GBGL_TAU
#define GBGL_TAU 6.28318530717958647692528676655900576f
#endif
////////////////////////////////////////////////////////////////
//
// Colour Type
// It's quite useful
// TODO(bill): Does this need to be in this library?
// Can I remove the anonymous struct extension?
//
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4201)
#endif
typedef union gbglColour {
u32 rgba; // NOTE(bill): 0xaabbggrr in little endian
struct { u8 r, g, b, a; };
u8 e[4];
} gbglColour;
GB_STATIC_ASSERT(gb_size_of(gbglColour) == gb_size_of(u32));
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
GB_DEF gbglColour gbgl_colour(f32 r, f32 g, f32 b, f32 a);
gb_global gbglColour const gbglColour_White = {0xffffffff};
gb_global gbglColour const gbglColour_Grey = {0xff808080};
gb_global gbglColour const gbglColour_Black = {0xff000000};
gb_global gbglColour const gbglColour_Red = {0xff0000ff};
gb_global gbglColour const gbglColour_Orange = {0xff0099ff};
gb_global gbglColour const gbglColour_Yellow = {0xff00ffff};
gb_global gbglColour const gbglColour_Green = {0xff00ff00};
gb_global gbglColour const gbglColour_Cyan = {0xffffff00};
gb_global gbglColour const gbglColour_Blue = {0xffff0000};
gb_global gbglColour const gbglColour_Violet = {0xffff007f};
gb_global gbglColour const gbglColour_Magenta = {0xffff00ff};
////////////////////////////////////////////////////////////////
//
// Generic Stuff
//
//
#ifndef GBGL_VERT_PTR_AA_GENERIC
#define GBGL_VERT_PTR_AA_GENERIC
// NOTE(bill) The "default" is just the f32 version
#define gbgl_vert_ptr_aa(index, element_count, Type, var_name) \
gbgl_vert_ptr_aa_f32(index, element_count, Type, var_name)
#define gbgl_vert_ptr_aa_f32(index, element_count, Type, var_name) do { \
glVertexAttribPointer(index, \
element_count, \
GL_FLOAT, \
false, \
gb_size_of(Type), \
(void const *)(gb_offset_of(Type, var_name))); \
glEnableVertexAttribArray(index); \
} while (0)
#define gbgl_vert_ptr_aa_u8(index, element_count, Type, var_name) do { \
glVertexAttribPointer(index, \
element_count, \
GL_UNSIGNED_BYTE, \
false, \
gb_size_of(Type), \
(void const *)(gb_offset_of(Type, var_name))); \
glEnableVertexAttribArray(index); \
} while (0)
#define gbgl_vert_ptr_aa_u8n(index, element_count, Type, var_name) do { \
glVertexAttribPointer(index, \
element_count, \
GL_UNSIGNED_BYTE, \
true, \
gb_size_of(Type), \
(void const *)(gb_offset_of(Type, var_name))); \
glEnableVertexAttribArray(index); \
} while (0)
#define gbgl_vert_ptr_aa_u32(index, element_count, Type, var_name) do { \
glVertexAttribIPointer(index, \
element_count, \
GL_UNSIGNED_INT, \
gb_size_of(Type), \
(void const *)(gb_offset_of(Type, var_name))); \
glEnableVertexAttribArray(index); \
} while (0)
#define gbgl_vert_ptr_aa_u16(index, element_count, Type, var_name) do { \
glVertexAttribIPointer(index, \
element_count, \
GL_UNSIGNED_SHORT, \
gb_size_of(Type), \
(void const *)(gb_offset_of(Type, var_name))); \
glEnableVertexAttribArray(index); \
} while (0)
#define gbgl_vert_ptr_aa_u16n(index, element_count, Type, var_name) do { \
glVertexAttribPointer(index, \
element_count, \
GL_UNSIGNED_SHORT, \
true, \
gb_size_of(Type), \
(void const *)(gb_offset_of(Type, var_name))); \
glEnableVertexAttribArray(index); \
} while (0)
#endif
GBGL_DEF u32 gbgl_make_sampler(u32 min_filter, u32 max_filter, u32 s_wrap, u32 t_wrap);
////////////////////////////////////////////////////////////////
//
// Data Buffers
//
//
typedef enum gbglBufferDataType {
gbglBufferData_u8 = GL_UNSIGNED_BYTE,
gbglBufferData_i8 = GL_BYTE,
gbglBufferData_u16 = GL_UNSIGNED_SHORT,
gbglBufferData_i16 = GL_SHORT,
gbglBufferData_f16 = GL_HALF_FLOAT,
gbglBufferData_u32 = GL_UNSIGNED_INT,
gbglBufferData_i32 = GL_INT,
gbglBufferData_f32 = GL_FLOAT,
gbglBufferData_f8, // NOTE(bill): This is not a "real" OpenGL type but it is needed for internal format enums
} gbglBufferDataType;
// NOTE(bill) index+1 = channels count
#if defined(GBGL_USE_SRGB_TEXTURE_FORMAT)
i32 const gbglTextureFormat[4] = { GL_RED, GL_RG, GL_SRGB8, GL_SRGB8_ALPHA8 };
#else
i32 const gbglTextureFormat[4] = { GL_RED, GL_RG, GL_RGB, GL_RGBA };
#endif
i32 const gbglInternalTextureFormat_8[4] = { GL_R8, GL_RG8, GL_RGB8, GL_RGBA8 };
i32 const gbglInternalTextureFormat_16[4] = { GL_R16, GL_RG16, GL_RGB16, GL_RGBA16 };
i32 const gbglInternalTextureFormat_32[4] = { GL_R32F, GL_RG32F, GL_RGB32F, GL_RGBA32F };
i32 const gbglInternalTextureFormat_u8[4] = { GL_R8UI, GL_RG8UI, GL_RGB8UI, GL_RGB8UI };
i32 const gbglInternalTextureFormat_i8[4] = { GL_R8I, GL_RG8I, GL_RGB8I, GL_RGB8I };
i32 const gbglInternalTextureFormat_f8[4] = { GL_R8, GL_RG8, GL_RGB8, GL_RGB8 };
i32 const gbglInternalTextureFormat_u16[4] = { GL_R16UI, GL_RG16UI, GL_RGB16UI, GL_RGB16UI };
i32 const gbglInternalTextureFormat_i16[4] = { GL_R16I, GL_RG16I, GL_RGB16I, GL_RGB16I };
i32 const gbglInternalTextureFormat_f16[4] = { GL_R16F, GL_RG16F, GL_RGB16F, GL_RGB16F };
i32 const gbglInternalTextureFormat_u32[4] = { GL_R32UI, GL_RG32UI, GL_RGB32UI, GL_RGBA32UI };
i32 const gbglInternalTextureFormat_i32[4] = { GL_R32I, GL_RG32I, GL_RGB32I, GL_RGBA32I };
i32 const gbglInternalTextureFormat_f32[4] = { GL_R32F, GL_RG32F, GL_RGB32F, GL_RGBA32F };
gb_inline i32 gbgl_texture_format(gbglBufferDataType data_type, i32 channel_count) {
GB_ASSERT(gb_is_between(channel_count, 1, 4));
switch (data_type) {
case gbglBufferData_u8: return gbglInternalTextureFormat_u8[channel_count-1];
case gbglBufferData_i8: return gbglInternalTextureFormat_i8[channel_count-1];
case gbglBufferData_f8: return gbglInternalTextureFormat_f8[channel_count-1];
case gbglBufferData_u16: return gbglInternalTextureFormat_u16[channel_count-1];
case gbglBufferData_i16: return gbglInternalTextureFormat_i16[channel_count-1];
case gbglBufferData_f16: return gbglInternalTextureFormat_f16[channel_count-1];
case gbglBufferData_u32: return gbglInternalTextureFormat_u32[channel_count-1];
case gbglBufferData_i32: return gbglInternalTextureFormat_i32[channel_count-1];
case gbglBufferData_f32: return gbglInternalTextureFormat_f32[channel_count-1];
}
return gbglInternalTextureFormat_f32[4-1];
}
typedef struct gbglTBO {
u32 buffer_obj_handle;
u32 buffer_handle;
} gbglTBO;
// NOTE(bill): usage_hint == (GL_STATIC_DRAW, GL_STREAM_DRAW, GL_DYNAMIC_DRAW)
GBGL_DEF u32 gbgl_make_vbo(void const *data, isize size, i32 usage_hint);
GBGL_DEF u32 gbgl_make_ebo(void const *data, isize size, i32 usage_hint);
GBGL_DEF gbglTBO gbgl_make_tbo(gbglBufferDataType data_type, i32 channel_count, void const *data, isize size, i32 usage_hint);
GBGL_DEF void gbgl_vbo_copy(u32 vbo_handle, void *const data, isize size, isize offset);
GBGL_DEF void gbgl_ebo_copy(u32 ebo_handle, void *const data, isize size, isize offset);
GBGL_DEF void gbgl_tbo_copy(gbglTBO tbo, void *const data, isize size, isize offset);
GBGL_DEF void gbgl_bind_vbo(u32 vbo_handle);
GBGL_DEF void gbgl_bind_ebo(u32 ebo_handle);
GBGL_DEF void gbgl_bind_tbo(gbglTBO tbo, i32 sampler_handle, i32 tex_unit);
// NOTE(bill): access = GL_WRITE_ONLY, etc.
GBGL_DEF void *gbgl_map_vbo(u32 vbo_handle, i32 access);
GBGL_DEF void *gbgl_map_ebo(u32 ebo_handle, i32 access);
GBGL_DEF void gbgl_unmap_vbo(void);
GBGL_DEF void gbgl_unmap_ebo(void);
////////////////////////////////////////////////////////////////
//
// Shader
//
//
typedef enum gbglShaderType {
gbglShader_Vertex,
gbglShader_Fragment,
gbglShader_Geometry,
gbglShader_Count,
} gbglShaderType;
i32 const gbglShaderMap[gbglShader_Count] = {
GL_VERTEX_SHADER, /* gbglShader_Vertex */
GL_FRAGMENT_SHADER, /* gbglShader_Fragment */
GL_GEOMETRY_SHADER, /* gbglShader_Geometry */
};
typedef enum gbglShaderError {
gbglShaderError_None,
gbglShaderError_ShaderCompile,
gbglShaderError_Linking,
gbglShaderError_UnableToReadFile,
gbglShaderError_Count,
} gbglShaderError;
#ifndef GBGL_MAX_UNIFORM_COUNT
#define GBGL_MAX_UNIFORM_COUNT 32
#endif
typedef struct gbglShader {
u32 shaders[gbglShader_Count];
u32 program;
i32 uniform_locs[GBGL_MAX_UNIFORM_COUNT];
char *uniform_names[GBGL_MAX_UNIFORM_COUNT];
i32 uniform_count;
u32 type_flags;
gbFile files[gbglShader_Count];
char base_name[64];
} gbglShader;
#ifndef GBGL_SHADER_FILE_EXTENSIONS_DEFINED
#define GBGL_SHADER_FILE_EXTENSIONS_DEFINED
gb_global char const *gbglShaderFileExtensions[gbglShader_Count] = {".vs", ".fs", ".gs"};
#endif
GBGL_DEF gbglShaderError gbgl_load_shader_from_file (gbglShader *s, u32 type_bits, char const *filename);
GBGL_DEF gbglShaderError gbgl_load_shader_from_memory_vf (gbglShader *s, char const *vert_source, char const *frag_source);
GBGL_DEF gbglShaderError gbgl_load_shader_from_memory_vfg(gbglShader *s, char const *vert_source, char const *frag_source, char const *geom_source);
GBGL_DEF void gbgl_destroy_shader (gbglShader *shader);
GBGL_DEF b32 gbgl_has_shader_changed(gbglShader *shader);
GBGL_DEF b32 gbgl_reload_shader (gbglShader *shader); // TODO(bill): Return an error code?
GBGL_DEF void gbgl_use_shader (gbglShader *shader);
GBGL_DEF b32 gbgl_is_shader_in_use (gbglShader *shader);
GBGL_DEF i32 gbgl_get_uniform(gbglShader *shader, char const *name);
GBGL_DEF void gbgl_set_uniform_int (gbglShader *s, char const *name, i32 i);
GBGL_DEF void gbgl_set_uniform_float (gbglShader *s, char const *name, f32 f);
GBGL_DEF void gbgl_set_uniform_vec2 (gbglShader *s, char const *name, f32 const *v);
GBGL_DEF void gbgl_set_uniform_vec3 (gbglShader *s, char const *name, f32 const *v);
GBGL_DEF void gbgl_set_uniform_vec4 (gbglShader *s, char const *name, f32 const *v);
GBGL_DEF void gbgl_set_uniform_mat4 (gbglShader *s, char const *name, f32 const *m);
GBGL_DEF void gbgl_set_uniform_mat4_count(gbglShader *s, char const *name, f32 const *m, isize count);
GBGL_DEF void gbgl_set_uniform_colour (gbglShader *s, char const *name, gbglColour col);
////////////////////////////////////////////////////////////////
//
// Texture
//
//
typedef enum gbglTextureType {
gbglgTexture_2D,
gbglgTexture_CubeMap,
gbglgTexture_Count,
} gbglTextureType;
gb_global i32 const GBGL_TEXTURE_TYPE[gbglgTexture_Count] = {
GL_TEXTURE_2D, /* gbglgTexture_2D */
GL_TEXTURE_CUBE_MAP, /* gbglgTexture_CubeMap */
};
typedef struct gbglTexture {
i32 width, height, channel_count;
gbglBufferDataType data_type;
gbglTextureType type;
u32 handle;
} gbglTexture;
GBGL_DEF b32 gbgl_load_texture2d_from_file (gbglTexture *texture, b32 flip_vertically, char const *filename, ...);
GBGL_DEF b32 gbgl_load_texture2d_from_memory(gbglTexture *texture, void const *data, i32 width, i32 height, i32 channel_count);
GBGL_DEF b32 gbgl_init_texture2d_coloured (gbglTexture *texture, gbglColour colour);
GBGL_DEF void gbgl_destroy_texture (gbglTexture *texture);
GBGL_DEF void gbgl_bind_texture2d(gbglTexture const *texture, u32 position, u32 sampler);
////////////////////////////////////////////////////////////////
//
// Render Buffer
//
//
// TODO(bill): Record depth and stencil and numerous colour attachments
typedef struct gbglRenderBuffer {
i32 width, height;
i32 channel_count;
u32 handle;
gbglTexture colour_texture;
} gbglRenderBuffer;
#define GBGL_MAX_RENDER_COLOUR_BUFFERS 16
gb_global u32 const gbglColourBufferAttachments[GBGL_MAX_RENDER_COLOUR_BUFFERS] = {
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3,
GL_COLOR_ATTACHMENT4,
GL_COLOR_ATTACHMENT5,
GL_COLOR_ATTACHMENT6,
GL_COLOR_ATTACHMENT7,
GL_COLOR_ATTACHMENT8,
GL_COLOR_ATTACHMENT9,
GL_COLOR_ATTACHMENT10,
GL_COLOR_ATTACHMENT11,
GL_COLOR_ATTACHMENT12,
GL_COLOR_ATTACHMENT13,
GL_COLOR_ATTACHMENT14,
GL_COLOR_ATTACHMENT15,
};
GBGL_DEF b32 gbgl_init_render_buffer (gbglRenderBuffer *rb, i32 width, i32 height, i32 channel_count);
GBGL_DEF void gbgl_destroy_render_buffer(gbglRenderBuffer *rb);
GBGL_DEF void gbgl_render_to_buffer(gbglRenderBuffer const *rb);
GBGL_DEF void gbgl_render_to_screen(i32 width, i32 height);
////////////////////////////////////////////////////////////////
//
// Font Stuff
//
//
#if !defined(GBGL_NO_FONTS)
typedef struct gbglGlyphMapKVPair {
char32 codepoint;
u16 index;
} gbglGlyphMapKVPair;
typedef struct gbglGlyphInfo {
f32 s0, t0, s1, t1;
i16 xoff, yoff;
f32 xadv;
} gbglGlyphInfo;
typedef struct gbglKernPair {
union {
i32 packed;
struct { u16 i0, i1; };
};
f32 kern;
} gbglKernPair;
typedef enum gbglJustifyType {
gbglJustify_Left,
gbglJustify_Centre,
gbglJustify_Right,
} gbglJustifyType;
typedef enum gbglTextParamType {
gbglTextParam_Invalid,
gbglTextParam_MaxWidth,
gbglTextParam_Justify,
gbglTextParam_TextureFilter,
gbglTextParam_Count,
} gbglTextParamType;
typedef struct gbglTextParam {
gbglTextParamType type;
union {
f32 val_f32;
i32 val_i32;
};
} gbglTextParam;
typedef struct gbglFont {
isize glyph_count;
isize kern_pair_count;
i32 bitmap_width, bitmap_height;
f32 size;
i32 ascent, descent, line_gap;
char *ttf_filename;
gbglTexture texture;
gbglGlyphMapKVPair *glyph_map;
gbglGlyphInfo * glyphs;
gbglKernPair * kern_table;
struct gbglFont *next; // NOTE(bill): Allow as linked list
} gbglFont;
typedef struct gbglFontCachedTTF {
char * name;
u8 * ttf;
stbtt_fontinfo finfo;
struct gbglFontCachedTTF *next;
} gbglFontCachedTTF;
typedef struct gbglFontCache {
isize font_char_list_count;
char *font_char_list;
isize codepoint_count;
char32 *codepoints;
stbtt_pack_range *ranges;
stbtt_packedchar *packed_char_data;
stbrp_rect * rect_cache;
gbglFontCachedTTF *ttf_buffer;
gbglFont * fonts;
} gbglFontCache;
#if 0
GBGL_DEF void gbgl_destroy_font_cache(gbglFontCache *fc);
#endif
// NOTE(bill): gbgl_load_font_from_file will load from file if it is not found
GBGL_DEF gbglFont *gbgl_load_font_from_file (gbglFontCache *fc, char const *ttf_filename, f32 font_size);
GBGL_DEF gbglFont *gbgl_get_font_only_from_cache(gbglFontCache *fc, char const *ttf_filename, f32 font_size);
GBGL_DEF gbglFont *gbgl_cache_font (gbglFontCache *fc, char const *ttf_filename, f32 font_size);
GBGL_DEF b32 gbgl_get_packed_font_dim (gbglFontCache *cache, gbglFontCachedTTF *ttf, i32 *width, i32 *height);
GBGL_DEF gbglGlyphInfo *gbgl_get_glyph_info (gbglFont *font, char32 codepoint, isize *out_index);
GBGL_DEF f32 gbgl_get_font_kerning_from_glyph_indices(gbglFont *font, isize left_index, isize right_index);
GBGL_DEF void gbgl_get_string_dimensions (gbglFont *font, char const *str, f32 *out_width, f32 *out_height);
GBGL_DEF f32 gbgl_get_sub_string_width (gbglFont *font, char const *str, isize char_count);
GBGL_DEF i32 gbgl_get_wrapped_line_count (gbglFont *font, char const *str, isize max_len, isize max_width);
GBGL_DEF f32 gbgl_get_string_width (gbglFont *font, char const *str, isize max_len);
#endif
////////////////////////////////////////////////////////////////
//
// Basic State
//
//
#if !defined(GBGL_NO_BASIC_STATE)
#ifndef GBGL_BS_MAX_VERTEX_COUNT
#define GBGL_BS_MAX_VERTEX_COUNT 32
#endif
#ifndef GBGL_BS_MAX_INDEX_COUNT
#define GBGL_BS_MAX_INDEX_COUNT 6
#endif
#if !defined(GBGL_NO_FONTS)
#ifndef GBGL_MAX_RENDER_STRING_LENGTH
#define GBGL_MAX_RENDER_STRING_LENGTH 4096
#endif
#ifndef gbglTextParam_Stack_size
#define gbglTextParam_Stack_size 128
#endif
#ifndef GBGL_FONT_CHAR_LIST
#define GBGL_FONT_CHAR_LIST \
"Āā㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľŁł"\
"ŃńŅņņŇňʼnŊŋŌōōŎŏŐőŒœŕŖŗŘřŚśŜŝŞşŠšŢţŤťŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽža!ö"\
"\"#$%%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"\
"ŠšŒœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõøùúûüýþÿ®™£"\
" \t\r\n"
#endif
#ifndef GBGL_PT_TO_PX_SCALE
#define GBGL_PT_TO_PX_SCALE (96.0f / 72.0f)
#endif
#ifndef GBGL_TAB_CHARACTER_WIDTH
#define GBGL_TAB_CHARACTER_WIDTH 4
#endif
#endif
typedef struct gbglBasicVertex {
f32 x, y;
f32 u, v;
} gbglBasicVertex;
typedef struct gbglBasicState {
gbglBasicVertex vertices[GBGL_BS_MAX_VERTEX_COUNT];
u16 indices[GBGL_BS_MAX_INDEX_COUNT];
u32 vao, vbo, ebo;
u32 nearest_sampler;
u32 linear_sampler;
u32 mipmap_sampler;
gbglShader ortho_tex_shader;
gbglShader ortho_col_shader;
f32 ortho_mat[16];
i32 width, height;
#if !defined(GBGL_NO_FONTS)
gbglFontCache font_cache;
gbglShader font_shader;
gbglBasicVertex font_vertices[GBGL_MAX_RENDER_STRING_LENGTH * 4];
u16 font_indices[GBGL_MAX_RENDER_STRING_LENGTH * 6];
u32 font_vao, font_vbo, font_ebo;
char font_text_buffer[GBGL_MAX_RENDER_STRING_LENGTH * 4]; // NOTE(bill): Maximum of 4 bytes per char for utf-8
u32 font_samplers[2];
gbglTextParam text_param_stack[gbglTextParam_Stack_size];
isize text_param_stack_count;
gbglTextParam text_params[gbglTextParam_Count];
#endif
} gbglBasicState;
GBGL_DEF void gbgl_bs_init(gbglBasicState *bs, i32 window_width, i32 window_height);
GBGL_DEF void gbgl_bs_set_resolution(gbglBasicState *bs, i32 window_width, i32 window_height);
GBGL_DEF void gbgl_bs_begin(gbglBasicState *bs, i32 window_width, i32 window_height);
GBGL_DEF void gbgl_bs_end(gbglBasicState *bs);
GBGL_DEF void gbgl_bs_draw_textured_rect(gbglBasicState *bs, gbglTexture *tex, f32 x, f32 y, f32 w, f32 h, b32 v_up);
GBGL_DEF void gbgl_bs_draw_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col);
GBGL_DEF void gbgl_bs_draw_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col, f32 thickness);
GBGL_DEF void gbgl_bs_draw_quad(gbglBasicState *bs,
f32 x0, f32 y0,
f32 x1, f32 y1,
f32 x2, f32 y2,
f32 x3, f32 y3,
gbglColour col);
GBGL_DEF void gbgl_bs_draw_quad_outline(gbglBasicState *bs,
f32 x0, f32 y0,
f32 x1, f32 y1,
f32 x2, f32 y2,
f32 x3, f32 y3,
gbglColour col, f32 thickness);
GBGL_DEF void gbgl_bs_draw_line(gbglBasicState *bs, f32 x0, f32 y0, f32 x1, f32 y1, gbglColour col, f32 thickness);
GBGL_DEF void gbgl_bs_draw_elliptical_arc(gbglBasicState *bs, f32 x, f32 y, f32 radius_a, f32 radius_b, f32 min_angle, f32 max_angle, gbglColour col);
GBGL_DEF void gbgl_bs_draw_elliptical_arc_outline(gbglBasicState *bs, f32 x, f32 y, f32 radius_a, f32 radius_b,
f32 min_angle, f32 max_angle, gbglColour col, f32 thickness);
GBGL_DEF void gbgl_bs_draw_circle(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col);
GBGL_DEF void gbgl_bs_draw_circle_outline(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col, f32 thickness);
// Corners Flags:
// 1 - Bottom Left
// 2 - Bottom Right
// 4 - Top Right
// 8 - Top Left
// NOTE(bill): Apple, please don't sue me!
GBGL_DEF void gbgl_bs_draw_rounded_rect_corners(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, u32 corners);
GBGL_DEF void gbgl_bs_draw_rounded_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col);
GBGL_DEF void gbgl_bs_draw_rounded_rect_corners_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, f32 thickness, u32 corners);
GBGL_DEF void gbgl_bs_draw_rounded_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, f32 thickness);
#if !defined(GBGL_NO_FONTS)
GBGL_DEF isize gbgl_bs_draw_substring(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *str, isize len);
GBGL_DEF isize gbgl_bs_draw_string (gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, ...);
GBGL_DEF isize gbgl_bs_draw_string_va(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, va_list va);
#endif
#endif
#if defined(__cplusplus)
}
#endif
#endif
////////////////////////////////////////////////////////////////
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// Implementation //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
// //
////////////////////////////////////////////////////////////////
#if defined(GBGL_IMPLEMENTATION)
gb_inline gbglColour gbgl_colour(f32 r, f32 g, f32 b, f32 a) {
gbglColour result;
result.r = cast(u8)(gbgl_clamp01(r) * 255.0f);
result.g = cast(u8)(gbgl_clamp01(g) * 255.0f);
result.b = cast(u8)(gbgl_clamp01(b) * 255.0f);
result.a = cast(u8)(gbgl_clamp01(a) * 255.0f);
return result;
}
u32 gbgl_make_sampler(u32 min_filter, u32 max_filter, u32 s_wrap, u32 t_wrap) {
u32 samp;
glGenSamplers(1, &samp);
glSamplerParameteri(samp, GL_TEXTURE_MIN_FILTER, min_filter);
glSamplerParameteri(samp, GL_TEXTURE_MAG_FILTER, max_filter);
glSamplerParameteri(samp, GL_TEXTURE_WRAP_S, s_wrap);
glSamplerParameteri(samp, GL_TEXTURE_WRAP_T, t_wrap);
return samp;
}
////////////////////////////////////////////////////////////////
//
// Data Buffers
//
//
gb_inline u32 gbgl__make_buffer(isize size, void const *data, i32 target, i32 usage_hint) {
u32 buffer_handle;
glGenBuffers(1, &buffer_handle);
glBindBuffer(target, buffer_handle);
glBufferData(target, size, data, usage_hint);
return buffer_handle;
}
gb_inline void gbgl__buffer_copy(u32 buffer_handle, i32 target, void const *data, isize size, isize offset) {
glBindBuffer(target, buffer_handle);
glBufferSubData(target, offset, size, data);
}
// NOTE(bill): usage_hint == (GL_STATIC_DRAW, GL_STREAM_DRAW, GL_DYNAMIC_DRAW)
gb_inline u32 gbgl_make_vbo(void const *data, isize size, i32 usage_hint) {
return gbgl__make_buffer(size, data, GL_ARRAY_BUFFER, usage_hint);
}
gb_inline u32 gbgl_make_ebo(void const *data, isize size, i32 usage_hint) {
return gbgl__make_buffer(size, data, GL_ELEMENT_ARRAY_BUFFER, usage_hint);
}
gb_inline gbglTBO gbgl_make_tbo(gbglBufferDataType data_type, i32 channel_count, void const *data, isize size, i32 usage_hint) {
gbglTBO tbo;
i32 internal_format;
tbo.buffer_obj_handle = gbgl__make_buffer(size, data, GL_TEXTURE_BUFFER, usage_hint);
glGenTextures(1, &tbo.buffer_handle);
glBindTexture(GL_TEXTURE_BUFFER, tbo.buffer_handle);
internal_format = gbgl_texture_format(data_type, channel_count);
glTexBuffer(GL_TEXTURE_BUFFER, internal_format, tbo.buffer_obj_handle);
return tbo;
}
gb_inline void gbgl_vbo_copy(u32 vbo_handle, void *const data, isize size, isize offset) {
gbgl__buffer_copy(vbo_handle, GL_ARRAY_BUFFER, data, size, offset);
}
gb_inline void gbgl_ebo_copy(u32 ebo_handle, void *const data, isize size, isize offset) {
gbgl__buffer_copy(ebo_handle, GL_ELEMENT_ARRAY_BUFFER, data, size, offset);
}
gb_inline void gbgl_tbo_copy(gbglTBO tbo, void *const data, isize size, isize offset) {
gbgl__buffer_copy(tbo.buffer_obj_handle, GL_TEXTURE_BUFFER, data, size, offset);
}
gb_inline void gbgl_bind_vbo(u32 vbo_handle) { glBindBuffer(GL_ARRAY_BUFFER, vbo_handle); }
gb_inline void gbgl_bind_ebo(u32 ebo_handle) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_handle); }
gb_inline void gbgl_bind_tbo(gbglTBO tbo, i32 sampler_handle, i32 tex_unit) {
glActiveTexture(GL_TEXTURE0 + tex_unit);
glBindTexture(GL_TEXTURE_BUFFER, tbo.buffer_handle);
glBindSampler(0, sampler_handle);
}
// NOTE(bill): access = GL_WRITE_ONLY, etc.
gb_inline void * gbgl_map_vbo(u32 vbo_handle, i32 access) {
gbgl_bind_vbo(vbo_handle);
return glMapBuffer(GL_ARRAY_BUFFER, access);
}
gb_inline void * gbgl_map_ebo(u32 ebo_handle, i32 access) {
gbgl_bind_ebo(ebo_handle);
return glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, access);
}
gb_inline void gbgl_unmap_vbo(void) { glUnmapBuffer(GL_ARRAY_BUFFER); }
gb_inline void gbgl_unmap_ebo(void) { glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); }
////////////////////////////////////////////////////////////////
//
// Shader
//
//
gbglShaderError gbgl__load_single_shader_from_file(gbglShader *shader, gbglShaderType type, char const *name) {
gbglShaderError err = gbglShaderError_None;
gbFileError ferr;
gb_local_persist char filepath[1024];
gb_snprintf(filepath, gb_count_of(filepath), "%s%s", name, gbglShaderFileExtensions[type]);
ferr = gb_file_open(&shader->files[type], filepath);
if (ferr != gbFileError_None) {
err = gbglShaderError_UnableToReadFile;
} else {
gb_local_persist char info_log[4096];
i64 file_size = gb_file_size(&shader->files[type]);
char *file_source = cast(char *)gbgl_malloc(file_size+1);
GB_ASSERT_NOT_NULL(file_source);
if (file_source) {
i32 params;
gb_file_read_at(&shader->files[type], file_source, file_size, 0);
file_source[file_size] = '\0';
shader->shaders[type] = glCreateShader(gbglShaderMap[type]);
glShaderSource(shader->shaders[type], 1, &file_source, NULL);
glCompileShader(shader->shaders[type]);
glGetShaderiv(shader->shaders[type], GL_COMPILE_STATUS, ¶ms);
if (!params) {
gb_printf_err("Shader Source:\n%s\n", file_source);
glGetShaderInfoLog(shader->shaders[type], gb_size_of(info_log), NULL, info_log);
gb_printf_err("Shader compilation failed:\n %s\n", info_log);
err = gbglShaderError_ShaderCompile;
}
gbgl_free(file_source);
}
gb_file_close(&shader->files[type]);
}
return err;
}
gbglShaderError gbgl__load_single_shader_from_memory(gbglShader *s, gbglShaderType type, char const *text) {
gbglShaderError err = gbglShaderError_None;
i32 status;
s->shaders[type] = glCreateShader(gbglShaderMap[type]);
glShaderSource(s->shaders[type], 1, &text, 0);
glCompileShader(s->shaders[type]);
glGetShaderiv(s->shaders[type], GL_COMPILE_STATUS, &status);
if (!status) {
gb_local_persist char log_info[4096];
i32 total_len, log_len;
gb_printf_err("Unable to compile shader: %s\n", text);
glGetShaderiv(s->shaders[type], GL_INFO_LOG_LENGTH, &status);
total_len = status;
glGetShaderInfoLog(s->shaders[type], 4095, &log_len, log_info);
gb_printf_err(log_info);
err = gbglShaderError_ShaderCompile;
}
return err;