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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
|
/* 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 systen 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;
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;
}
gbglShaderError gbgl__link_shader(gbglShader *shader) {
gbglShaderError err = gbglShaderError_None;
i32 i, status;
shader->program = glCreateProgram();
for (i = 0; i < gbglShader_Count; i++) {
if (shader->type_flags & GB_BIT(i))
glAttachShader(shader->program, shader->shaders[i]);
}
glLinkProgram(shader->program);
glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
if (!status) {
gb_local_persist char log_info[4096];
glGetProgramInfoLog(shader->program, gb_size_of(log_info), NULL, log_info);
gb_printf_err("Shader linking failed:\n %s \n", log_info);
err = gbglShaderError_Linking;
}
for (i = 0; i < gbglShader_Count; i++) {
if (shader->type_flags & GB_BIT(i))
glDetachShader(shader->program, shader->shaders[i]);
}
return err;
}
gbglShaderError gbgl_load_shader_from_file(gbglShader *shader, u32 type_bits, char const *filename) {
gbglShaderError err = gbglShaderError_None;
b32 loaded_shader[gbglShader_Count] = {0};
i32 i;
gb_zero_item(shader);
shader->type_flags = type_bits;
gb_strncpy(shader->base_name, filename, gb_size_of(shader->base_name));
for (i = 0; i < gbglShader_Count; i++) {
if (type_bits & GB_BIT(i)) {
err = gbgl__load_single_shader_from_file(shader, cast(gbglShaderType)i, filename);
if (err != gbglShaderError_None)
return err;
loaded_shader[i] = true;
}
}
err = gbgl__link_shader(shader);
return err;
}
gbglShaderError gbgl_load_shader_from_memory_vf(gbglShader *s, char const *vert_source, char const *frag_source) {
gbglShaderError err = gbglShaderError_None;
gb_zero_item(s);
s->type_flags = GB_BIT(gbglShader_Vertex) | GB_BIT(gbglShader_Fragment);
err = gbgl__load_single_shader_from_memory(s, gbglShader_Vertex, vert_source);
if (err != gbglShaderError_None) return err;
err = gbgl__load_single_shader_from_memory(s, gbglShader_Fragment, frag_source);
if (err != gbglShaderError_None) return err;
err = gbgl__link_shader(s);
return err;
}
gbglShaderError gbgl_load_shader_from_memory_vfg(gbglShader *s, char const *vert_source, char const *frag_source, char const *geom_source) {
gbglShaderError err = gbglShaderError_None;
gb_zero_item(s);
s->type_flags = GB_BIT(gbglShader_Vertex) | GB_BIT(gbglShader_Fragment) | GB_BIT(gbglShader_Geometry);
err = gbgl__load_single_shader_from_memory(s, gbglShader_Vertex, vert_source);
if (err != gbglShaderError_None) return err;
err = gbgl__load_single_shader_from_memory(s, gbglShader_Fragment, frag_source);
if (err != gbglShaderError_None) return err;
err = gbgl__load_single_shader_from_memory(s, gbglShader_Geometry, geom_source);
if (err != gbglShaderError_None) return err;
err = gbgl__link_shader(s);
return err;
}
gb_inline void gbgl_destroy_shader(gbglShader *shader) {
i32 i;
for (i = 0; i < gbglShader_Count; i++) {
if (shader->type_flags & GB_BIT(i)) {
gb_file_close(&shader->files[i]);
glDeleteShader(shader->shaders[i]);
}
}
glDeleteProgram(shader->program);
for (i = 0; i < shader->uniform_count; i++) {
gbgl_free(shader->uniform_names[i]);
}
}
gb_inline b32 gbgl_has_shader_changed(gbglShader *shader) {
i32 i;
for (i = 0; i < gbglShader_Count; i++) {
if (shader->type_flags & GB_BIT(i)) {
if (gb_file_has_changed(&shader->files[i])) {
return true;
}
}
}
return false;
}
b32 gbgl_reload_shader(gbglShader *shader) {
i32 i;
for (i = 0; i < gbglShader_Count; i++) {
if (shader->type_flags & GB_BIT(i)) {
if (gbgl__load_single_shader_from_file(shader, cast(gbglShaderType)i, shader->base_name) != gbglShaderError_None)
return false;
}
}
if (gbgl__link_shader(shader) != gbglShaderError_None)
return false;
for (i = 0; i < shader->uniform_count; i++)
shader->uniform_locs[i] = glGetUniformLocation(shader->program, shader->uniform_names[i]);
return true;
}
gb_inline void gbgl_use_shader(gbglShader *s) { glUseProgram(s ? s->program : 0); }
gb_inline b32 gbgl_is_shader_in_use(gbglShader *s) {
if (s) {
i32 curr = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, &curr);
return (curr == cast(i32)s->program);
}
return false;
}
i32 gbgl_get_uniform(gbglShader *s, char const *name) {
i32 i, loc = -1;
for (i = 0; i < s->uniform_count; i++) {
if (gb_strcmp(s->uniform_names[i], name) == 0) {
return s->uniform_locs[i];
}
}
GB_ASSERT_MSG(s->uniform_count < GBGL_MAX_UNIFORM_COUNT,
"Uniform array for shader is full");
loc = glGetUniformLocation(s->program, name);
s->uniform_names[s->uniform_count] = gb_alloc_str(gb_heap_allocator(), name);
s->uniform_locs[s->uniform_count] = loc;
s->uniform_count++;
return loc;
}
gb_inline void gbgl_set_uniform_int(gbglShader *s, char const *name, i32 i) {
glUniform1i(gbgl_get_uniform(s, name), i);
}
gb_inline void gbgl_set_uniform_float(gbglShader *s, char const *name, f32 f) {
glUniform1f(gbgl_get_uniform(s, name), f);
}
gb_inline void gbgl_set_uniform_vec2(gbglShader *s, char const *name, f32 const *v) {
glUniform2fv(gbgl_get_uniform(s, name), 1, v);
}
gb_inline void gbgl_set_uniform_vec3(gbglShader *s, char const *name, f32 const *v) {
glUniform3fv(gbgl_get_uniform(s, name), 1, v);
}
gb_inline void gbgl_set_uniform_vec4(gbglShader *s, char const *name, f32 const *v) {
glUniform4fv(gbgl_get_uniform(s, name), 1, v);
}
gb_inline void gbgl_set_uniform_mat4(gbglShader *s, char const *name, f32 const *m) {
gbgl_set_uniform_mat4_count(s, name, m, 1);
}
gb_inline void gbgl_set_uniform_mat4_count(gbglShader *s, char const *name, f32 const *m, isize count) {
glUniformMatrix4fv(gbgl_get_uniform(s, name), count, false, m);
}
gb_inline void gbgl_set_uniform_colour(gbglShader *s, char const *name, gbglColour col) {
f32 v[4];
v[0] = col.r / 255.0f;
v[1] = col.g / 255.0f;
v[2] = col.b / 255.0f;
v[3] = col.a / 255.0f;
gbgl_set_uniform_vec4(s, name, v);
}
////////////////////////////////////////////////////////////////
//
// Render Buffer
//
//
b32 gbgl_init_render_buffer(gbglRenderBuffer *rb, i32 width, i32 height, i32 channel_count) {
if ((rb->width == width) && (rb->height == height) && (rb->channel_count == channel_count)) return true;
gbgl_destroy_render_buffer(rb);
gb_zero_item(rb);
rb->width = width;
rb->height = height;
glEnable(GL_FRAMEBUFFER_SRGB);
glGenFramebuffers(1, &rb->handle);
glBindFramebuffer(GL_FRAMEBUFFER, rb->handle);
gbgl_load_texture2d_from_memory(&rb->colour_texture, NULL, width, height, channel_count);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rb->colour_texture.handle, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
{
u32 draw_buffers[] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(gb_count_of(draw_buffers), draw_buffers);
}
{
u32 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
gb_printf_err("Framebuffer Status: 0x%x\n", status);
return false;
}
}
return true;
}
gb_inline void gbgl_destroy_render_buffer(gbglRenderBuffer *rb) {
if (rb->handle)
glDeleteFramebuffers(1, &rb->handle);
gbgl_destroy_texture(&rb->colour_texture);
}
gb_inline void gbgl_render_to_buffer(gbglRenderBuffer const *rb) {
GB_ASSERT_NOT_NULL(rb);
glViewport(0, 0, rb->width, rb->height);
glBindFramebuffer(GL_FRAMEBUFFER, rb->handle);
}
gb_inline void gbgl_render_to_screen(i32 width, i32 height) {
glViewport(0, 0, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
////////////////////////////////////////////////////////////////
//
// Texture
//
//
b32 gbgl_load_texture2d_from_memory(gbglTexture *tex, void const *data, i32 width, i32 height, i32 channel_count) {
b32 result = true;
gb_zero_item(tex);
tex->width = width;
tex->height = height;
tex->channel_count = channel_count;
tex->data_type = gbglBufferData_u8;
tex->type = gbglgTexture_2D;
glGenTextures(1, &tex->handle);
glBindTexture(GL_TEXTURE_2D, tex->handle);
glTexImage2D(GL_TEXTURE_2D, 0,
gbglInternalTextureFormat_8[channel_count-1],
width, height, 0,
gbglTextureFormat[channel_count-1],
GL_UNSIGNED_BYTE, data);
glBindTexture(GL_TEXTURE_2D, 0);
glGenerateMipmap(GL_TEXTURE_2D);
glFinish();
return result;
}
b32 gbgl_load_texture2d_from_file(gbglTexture *texture, b32 flip_vertically, char const *filename, ...) {
b32 result;
u8 *data;
int width, height, comp;
char *path;
va_list va;
va_start(va, filename);
path = gb_bprintf_va(filename, va);
va_end(va);
stbi_set_flip_vertically_on_load(flip_vertically);
data = stbi_load(path, &width, &height, &comp, 0);
if (data == NULL) {
gb_printf_err("Failed to load image: %s\n", path);
result = false;
} else {
result = gbgl_load_texture2d_from_memory(texture, data, width, height, comp);
stbi_image_free(data);
}
return result;
}
gb_inline b32 gbgl_make_texture2d_coloured(gbglTexture *t, gbglColour colour) {
return gbgl_load_texture2d_from_memory(t, &colour.rgba, 1, 1, 4);
}
gb_inline void gbgl_bind_texture2d(gbglTexture const *t, u32 position, u32 sampler) {
GB_ASSERT(t->type == gbglgTexture_2D);
if (position > 31) {
position = 31;
gb_printf_err("Textures can only bound to position [0 ... 31]\n");
gb_printf_err("Will bind to position [31]\n");
}
glActiveTexture(GL_TEXTURE0 + position);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, t ? t->handle : 0);
glBindSampler(position, sampler);
}
gb_inline void gbgl_destroy_texture(gbglTexture *t) {
if (t->handle) {
glDeleteTextures(1, &t->handle);
}
}
////////////////////////////////////////////////////////////////
//
// Font
//
//
#if !defined(GBGL_NO_FONTS)
gb_inline GB_COMPARE_PROC(gbgl__kern_pair_compare) {
gbglKernPair *kp0 = cast(gbglKernPair *)a;
gbglKernPair *kp1 = cast(gbglKernPair *)b;
return kp0->packed - kp1->packed;
}
gb_inline GB_COMPARE_PROC(gbgl__glyph_map_compare) {
gbglGlyphMapKVPair g0 = *cast(gbglGlyphMapKVPair *)a;
gbglGlyphMapKVPair g1 = *cast(gbglGlyphMapKVPair *)b;
return g0.codepoint - g1.codepoint;
}
b32 gbgl_get_packed_font_dim(gbglFontCache *cache, gbglFontCachedTTF *ttf, i32 *width, i32 *height) {
b32 result = true;
stbtt_pack_context spc;
b32 ext_w = true;
isize sanity_counter = 0, i, j;
*width = *height = (1<<5);
for (j = 0; j < cache->codepoint_count; j++) {
cache->rect_cache[j].x = cache->rect_cache[j].y = 0;
}
for (;;) {
i32 res = stbtt_PackBegin(&spc, NULL, *width, *height, 0, 1, 0);
GB_ASSERT(res == 1);
if (res != 1) {
return false;
} else {
stbrp_context *rp_ctx;
b32 fit = true;
gb_zero_array(cache->rect_cache, cache->codepoint_count);
rp_ctx = cast(stbrp_context *)spc.pack_info;
stbtt_PackFontRangesGatherRects(&spc, &ttf->finfo, cache->ranges, cache->codepoint_count, cache->rect_cache);
gb_sort_array(cache->rect_cache, cache->codepoint_count, rect_height_compare);
for (i = 0; i < cache->codepoint_count; i++) {
stbrp__findresult fr = stbrp__skyline_pack_rectangle(rp_ctx, cache->rect_cache[i].w, cache->rect_cache[i].h);
if (fr.prev_link) {
cache->rect_cache[i].x = cast(stbrp_coord)fr.x;
cache->rect_cache[i].y = cast(stbrp_coord)fr.y;
} else {
int res;
if (ext_w) {
ext_w = false;
*width <<= 1;
} else {
ext_w = true;
*height <<= 1;
}
fit = false;
for (j = 0; j < cache->codepoint_count; j++) {
cache->rect_cache[j].x = cache->rect_cache[j].y = 0;
}
stbtt_PackEnd(&spc);
res = stbtt_PackBegin(&spc, NULL, *width, *height, 0, 1, 0);
GB_ASSERT(res == 1);
if (res != 1) {
result = false;
goto done;
}
break;
}
}
if (fit) {
result = true;
goto done;
}
if (++sanity_counter > 32) {
result = false;
goto done;
}
}
}
done:
stbtt_PackEnd(&spc);
return result;
}
#if 0
void gbgl_destroy_font_cache(gbglFontCache *fc) {
gbglFontCachedTTF *curr_ttf = fc->ttf_buffer;
gbglFontCachedTTF *next_ttf = NULL;
gbglFont *curr_font = fc->fonts;
gbglFont *next_font = NULL;
gbgl_free(fc->font_char_list);
gbgl_free(fc->codepoints);
gbgl_free(fc->ranges);
gbgl_free(fc->packed_char_data);
gbgl_free(fc->rect_cache);
// NOTE(bill): Free all linked listed ttfs
while (curr_ttf) {
gbgl_free(curr_ttf->name);
gbgl_free(curr_ttf->ttf);
next_ttf = curr_ttf->next;
gbgl_free(curr_ttf);
curr_ttf = next_ttf;
}
// NOTE(bill): Free all linked listed fonts
while (curr_font) {
gbgl_free(curr_font->ttf_filename);
gbgl_destroy_texture(&curr_font->texture);
gbgl_free(curr_font->glyph_map);
gbgl_free(curr_font->glyphs);
gbgl_free(curr_font->kern_table);
next_font = curr_font->next;
gbgl_free(curr_font);
curr_font = next_font;
}
}
#endif
gb_inline gbglFont * gbgl_load_font_from_file(gbglFontCache *fc, char const *ttf_filename, f32 font_size) {
gbglFont *f = gbgl_get_font_only_from_cache(fc, ttf_filename, font_size);
if (f) return f;
return gbgl_cache_font(fc, ttf_filename, font_size);
}
gb_inline gbglFont * gbgl_get_font_only_from_cache(gbglFontCache *fc, char const *ttf_filename, f32 font_size) {
gbglFont *f = fc->fonts;
while (f) {
if (f->size == font_size && gb_strcmp(ttf_filename, f->ttf_filename) == 0) {
return f;
}
f = f->next;
}
return NULL;
}
gbglFont * gbgl_cache_font(gbglFontCache *fc, char const *ttf_filename, f32 font_size) {
gbglFont *f = gbgl_get_font_only_from_cache(fc, ttf_filename, font_size);
gbglFontCachedTTF *ttf = NULL;
isize i;
if (f) { // NOTE(bill): The font is already cached
return f;
}
if (!fc->fonts) {
fc->fonts = cast(gbglFont *)gbgl_malloc(gb_size_of(gbglFont));
f = fc->fonts;
} else {
f = fc->fonts;
while (f && f->next)
f = f->next;
f->next = cast(gbglFont *)gbgl_malloc(gb_size_of(gbglFont));
f = f->next;
}
GB_ASSERT_NOT_NULL(f);
if (!f) {
gb_printf_err("Failed to cache font\n");
return NULL;
}
gb_zero_item(f);
// NOTE(bill): Make sure the character list file has been loaded
if (!fc->font_char_list) {
isize codepoint_count = 0, cpi = 0;
fc->font_char_list = GBGL_FONT_CHAR_LIST;
fc->font_char_list_count = gb_strlen(GBGL_FONT_CHAR_LIST);
for (i = 0; i < fc->font_char_list_count; i++) {
char32 c;
isize utf8_len = gb_utf8_decode_len(fc->font_char_list + i, fc->font_char_list_count, &c);
i += utf8_len-1;
codepoint_count++;
}
fc->codepoint_count = codepoint_count;
fc->ranges = cast(stbtt_pack_range *)gbgl_malloc(gb_size_of(stbtt_pack_range) * codepoint_count);
fc->codepoints = cast(char32 *) gbgl_malloc(gb_size_of(char32) * codepoint_count);
fc->packed_char_data = cast(stbtt_packedchar *)gbgl_malloc(gb_size_of(stbtt_packedchar) * codepoint_count);
fc->rect_cache = cast(stbrp_rect *) gbgl_malloc(gb_size_of(stbrp_rect) * codepoint_count);
if (!fc->ranges || !fc->codepoints || !fc->packed_char_data) {
gb_printf_err("Unable to get memory for fonts");
}
for (i = 0; i < fc->font_char_list_count; i++) {
isize utf8_len = gb_utf8_decode_len(fc->font_char_list+i, fc->font_char_list_count, fc->codepoints+cpi);
i += utf8_len-1;
cpi++;
}
GB_ASSERT(cpi == fc->codepoint_count);
for (i = 0; i < fc->codepoint_count; i++) {
fc->ranges[i].first_unicode_codepoint_in_range = fc->codepoints[i];
fc->ranges[i].array_of_unicode_codepoints = 0;
fc->ranges[i].num_chars = 1;
fc->ranges[i].chardata_for_range = fc->packed_char_data + i;
}
}
{
gbglFontCachedTTF **ttf_cache = &fc->ttf_buffer;
while (*ttf_cache) {
if (gb_strcmp((*ttf_cache)->name, ttf_filename) == 0)
break;
ttf_cache = &(*ttf_cache)->next;
}
if (!*ttf_cache) {
isize name_len;
gbFile file;
*ttf_cache = cast(gbglFontCachedTTF *)gbgl_malloc(gb_size_of(gbglFontCachedTTF));
GB_ASSERT_NOT_NULL(*ttf_cache);
(*ttf_cache)->name = NULL;
(*ttf_cache)->ttf = NULL;
gb_zero_item(&(*ttf_cache)->finfo);
(*ttf_cache)->next = NULL;
name_len = gb_strlen(ttf_filename);
(*ttf_cache)->name = cast(char *)gbgl_malloc(name_len+1);
gb_strncpy((*ttf_cache)->name, ttf_filename, name_len);
(*ttf_cache)->name[name_len] = '\0';
if (gb_file_open(&file, ttf_filename) == gbFileError_None) {
i64 len = gb_file_size(&file);
(*ttf_cache)->ttf = cast(u8 *)gbgl_malloc(len);
GB_ASSERT_NOT_NULL((*ttf_cache)->ttf);
gb_file_read_at(&file, (*ttf_cache)->ttf, len, 0);
gb_file_close(&file);
} else {
GB_PANIC("Could not open ttf file");
}
stbtt_InitFont(&(*ttf_cache)->finfo, (*ttf_cache)->ttf, stbtt_GetFontOffsetForIndex((*ttf_cache)->ttf, 0));
}
ttf = *ttf_cache;
GB_ASSERT_NOT_NULL(ttf);
}
// NOTE(bill): Set the range for the this look up
for (i = 0; i < fc->codepoint_count; i++)
fc->ranges[i].font_size = font_size;
{ // NOTE(bill): Figure out smallest non-square power of 2 texture size
i32 w, h;
if (gbgl_get_packed_font_dim(fc, ttf, &w, &h)) {
isize str_len, i, j;
// NOTE(bill): Setup the font data
f->glyph_count = fc->codepoint_count;
f->bitmap_width = w;
f->bitmap_height = h;
f->size = font_size;
str_len = gb_strlen(ttf_filename);
f->ttf_filename = cast(char *)gbgl_malloc(str_len+1);
gb_strncpy(f->ttf_filename, ttf_filename, str_len);
f->glyph_map = cast(gbglGlyphMapKVPair *)gbgl_malloc(gb_size_of(*f->glyph_map) * f->glyph_count);
f->glyphs = cast(gbglGlyphInfo *) gbgl_malloc(gb_size_of(*f->glyphs) * f->glyph_count);
if (!f->glyph_map || !f->glyphs) {
f = NULL;
return f;
} else {
stbtt_pack_context spc;
u8 *px;
i32 res;
f32 scale;
px = cast(u8 *)gbgl_malloc(w * h);
res = stbtt_PackBegin(&spc, px, w, h, 0, 1, NULL);
GB_ASSERT(res == 1);
res = stbtt_PackFontRanges(&spc, ttf->ttf, 0, fc->ranges, fc->codepoint_count);
GB_ASSERT(res == 1);
stbtt_PackEnd(&spc);
gbgl_load_texture2d_from_memory(&f->texture, px, w, h, 1);
gbgl_free(px);
scale = stbtt_ScaleForPixelHeight(&ttf->finfo, font_size);
stbtt_GetFontVMetrics(&ttf->finfo, &f->ascent, &f->descent, &f->line_gap);
f->ascent = cast(i32)(cast(f32)f->ascent * scale);
f->descent = cast(i32)(cast(f32)f->descent * scale);
f->line_gap = cast(i32)(cast(f32)f->line_gap * scale);
for (i = 0; i < f->glyph_count; i++) {
gbglGlyphInfo *gi = f->glyphs + i;
gi->s0 = cast(f32)fc->packed_char_data[i].x0;
gi->t0 = cast(f32)fc->packed_char_data[i].y0;
gi->s1 = cast(f32)fc->packed_char_data[i].x1;
gi->t1 = cast(f32)fc->packed_char_data[i].y1;
gi->xoff = cast(i16)fc->packed_char_data[i].xoff;
gi->yoff = cast(i16)fc->packed_char_data[i].yoff;
gi->xadv = fc->packed_char_data[i].xadvance;
}
for (i = 0; i < f->glyph_count; i++) {
f->glyph_map[i].codepoint = fc->codepoints[i];
f->glyph_map[i].index = i;
}
gb_sort_array(f->glyph_map, f->glyph_count, gbgl__glyph_map_compare);
{ // Kerning Table
isize kps_count = 0;
for (i = 0; i < f->glyph_count; i++) {
for (j = 0; j < f->glyph_count; j++) {
i32 kern = stbtt_GetCodepointKernAdvance(&ttf->finfo, fc->codepoints[i], fc->codepoints[j]);
if (kern != 0)
kps_count++;
}
}
f->kern_pair_count = kps_count;
if (kps_count > 0) {
int ikp = 0;
f->kern_table = cast(gbglKernPair *)gbgl_malloc(gb_size_of(*f->kern_table) * kps_count);
for (i = 0; i < f->glyph_count; i++) {
for (j = 0; j < f->glyph_count; j++) {
isize kern = stbtt_GetCodepointKernAdvance(&ttf->finfo, fc->codepoints[i], fc->codepoints[j]);
if (kern != 0) {
gbglKernPair *kp = f->kern_table + ikp++;
kp->i0 = cast(u16)i;
kp->i1 = cast(u16)j;
kp->kern = cast(f32)kern * scale;
}
}
}
gb_sort_array(f->kern_table, f->kern_pair_count, gbgl__kern_pair_compare);
}
}
}
} else {
GB_PANIC("Failure loading font");
gb_zero_item(&f);
}
}
return f;
}
gb_inline GB_COMPARE_PROC(gbgl__font_glyph_map_search_proc) {
gbglGlyphMapKVPair const *gm = cast(gbglGlyphMapKVPair const *)a;
char32 ucp = *cast(char32 const *)b;
return cast(i32)(cast(i64)gm->codepoint - cast(i64)ucp);
}
gb_inline gbglGlyphInfo * gbgl_get_glyph_info(gbglFont *font, char32 codepoint, isize *out_index) {
isize index = gb_binary_search_array(font->glyph_map, font->glyph_count, &codepoint, gbgl__font_glyph_map_search_proc);
if (index >= 0) {
GB_ASSERT(codepoint == font->glyph_map[index].codepoint);
if (out_index)
*out_index = font->glyph_map[index].index;
return font->glyphs + font->glyph_map[index].index;
}
return NULL;
}
gb_inline f32 gbgl_get_font_kerning_from_glyph_indices(gbglFont *font, isize left_index, isize right_index) {
isize needle = (right_index << 16) | (left_index & 0xff);
isize f = 0;
isize l = font->kern_pair_count - 1;
isize m = (f + l) >> 1;
while (f <= l) {
isize cmp = font->kern_table[m].packed - needle;
if (cmp < 0)
f = m + 1;
else if (cmp > 0)
l = m - 1;
else
return font->kern_table[m].kern;
m = (f + l) >> 1;
}
return 0.0f;
}
void gbgl_get_string_dimensions(gbglFont *font, char const *str, f32 *out_width, f32 *out_height) {
isize len, char_count, i;
f32 w = 0.0f;
f32 h = 0.0f;
char const *ptr = str;
len = gb_strlen(str);
char_count = gb_utf8_strnlen(str, len);
for (i = 0; i < char_count; i++) {
char32 cp;
isize byte_len, curr_index;
gbglGlyphInfo *gi;
byte_len = gb_utf8_decode_len(ptr, len-(ptr-str), &cp);
ptr += byte_len;
gi = gbgl_get_glyph_info(font, cp, &curr_index);
if (gi) {
f32 kern = 0;
if (i < char_count-1) {
isize next_index;
char32 next_cp = 0;
gbglGlyphInfo *ngi;
gb_utf8_decode_len(ptr, len-(ptr-str), &next_cp);
ngi = gbgl_get_glyph_info(font, next_cp, &next_index);
if (ngi) kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
}
w += gi->xadv + kern;
}
}
if (out_width) *out_width = w;
if (out_height) *out_height = h;
}
f32 gbgl_get_sub_string_width(gbglFont *font, char const *str, isize char_count) {
isize i, len;
f32 w = 0;
char const *ptr = str;
len = gb_strlen(str);
for (i = 0; i < char_count; i++) {
if (*ptr == 0) {
break;
} else {
char32 cp;
isize byte_len, curr_index;
f32 kern = 0;
gbglGlyphInfo *gi;
byte_len = gb_utf8_decode_len(ptr, len-(ptr-str), &cp);
ptr += byte_len;
if (ptr - str > char_count)
break;
gi = gbgl_get_glyph_info(font, cp, &curr_index);
if (i < char_count-1) {
isize next_index;
char32 next_cp = 0;
gb_utf8_decode_len(ptr, len-(ptr-str), &next_cp);
gbgl_get_glyph_info(font, next_cp, &next_index);
kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
}
w += gi->xadv + kern;
}
}
return w;
}
i32 gbgl_get_wrapped_line_count(gbglFont *font, char const *str, isize max_len, isize max_width) {
isize i, str_len, char_count, line_count = 1;
f32 w = 0;
char const *ptr = str;
str_len = gb_strnlen(str, max_len);
char_count = gb_utf8_strnlen(str, str_len);
for (i = 0; i < char_count; i++) {
char32 cp;
isize byte_len, curr_index;
gbglGlyphInfo *gi;
f32 kern = 0;
byte_len = gb_utf8_decode_len(ptr, str_len-(ptr-str), &cp);
ptr += byte_len;
// NOTE(bill): Check calculation here
if (ptr-str >= max_len-6)
break;
gi = gbgl_get_glyph_info(font, cp, &curr_index);
if (gi) {
if (w + gi->xadv >= cast(f32)max_width) {
line_count++;
w = 0.0f;
}
}
if (i < char_count-1) {
char32 next_cp;
isize next_index;
gb_utf8_decode_len(ptr, str_len-(ptr-str), &next_cp);
gbgl_get_glyph_info(font, next_cp, &next_index);
kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
}
if (gi) {
w += gi->xadv + kern;
}
}
return line_count;
}
gb_inline f32 gbgl_get_string_width(gbglFont *font, char const *str, isize max_len) {
isize len = gb_strnlen(str, max_len);
isize char_count = gb_utf8_strnlen(str, len);
return gbgl_get_sub_string_width(font, str, char_count);
}
#endif
////////////////////////////////////////////////////////////////
//
// Basic State
//
//
#if !defined(GBGL_NO_BASIC_STATE)
void gbgl_bs_init(gbglBasicState *bs, i32 window_width, i32 window_height) {
isize i;
gbgl_bs_set_resolution(bs, window_width, window_height);
glGenVertexArrays(1, &bs->vao);
glBindVertexArray(bs->vao);
bs->vbo = gbgl_make_vbo(NULL, gb_size_of(gbglBasicVertex) * GBGL_BS_MAX_VERTEX_COUNT, GL_DYNAMIC_DRAW);
for (i = 0; i < GBGL_BS_MAX_INDEX_COUNT / 6; i++) {
bs->indices[i*6 + 0] = i*4 + 0;
bs->indices[i*6 + 1] = i*4 + 1;
bs->indices[i*6 + 2] = i*4 + 2;
bs->indices[i*6 + 3] = i*4 + 2;
bs->indices[i*6 + 4] = i*4 + 3;
bs->indices[i*6 + 5] = i*4 + 0;
}
bs->ebo = gbgl_make_ebo(bs->indices, gb_size_of(u16) * GBGL_BS_MAX_INDEX_COUNT, GL_STATIC_DRAW);
bs->nearest_sampler = gbgl_make_sampler(GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
bs->linear_sampler = gbgl_make_sampler(GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
gbgl_load_shader_from_memory_vf(&bs->ortho_tex_shader,
"#version 410 core\n"
"layout (location = 0) in vec4 a_position;\n"
"layout (location = 1) in vec2 a_tex_coord;\n"
"uniform mat4 u_ortho_mat;\n"
"out vec2 v_tex_coord;\n"
"void main(void) {\n"
" gl_Position = u_ortho_mat * a_position;\n"
" v_tex_coord = a_tex_coord;\n"
"}\n",
"#version 410 core\n"
"precision mediump float;"
"in vec2 v_tex_coord;\n"
"layout (binding = 0) uniform sampler2D u_tex;\n"
"out vec4 o_colour;\n"
"void main(void) {\n"
" o_colour = texture2D(u_tex, v_tex_coord);\n"
"}\n"
);
gbgl_load_shader_from_memory_vf(&bs->ortho_col_shader,
"#version 410 core\n"
"precision mediump float;"
"layout (location = 0) in vec4 a_position;\n"
"uniform mat4 u_ortho_mat;\n"
"void main(void) {\n"
" gl_Position = u_ortho_mat * a_position;\n"
"}\n",
"#version 410 core\n"
"uniform vec4 u_colour;\n"
"out vec4 o_colour;\n"
"void main(void) {\n"
" o_colour = u_colour;\n"
"}\n"
);
#if !defined(GBGL_NO_FONTS)
gbgl_load_shader_from_memory_vf(&bs->font_shader,
"#version 410 core\n"
"layout (location = 0) in vec4 a_position;\n"
"layout (location = 1) in vec2 a_tex_coord;\n"
"uniform mat4 u_ortho_mat;\n"
"out vec2 v_tex_coord;\n"
"void main(void) {\n"
" gl_Position = u_ortho_mat * a_position;\n"
" v_tex_coord = a_tex_coord;\n"
"}\n",
"#version 410 core\n"
"in vec2 v_tex_coord;\n"
"uniform vec4 u_colour;\n"
"layout (binding = 0) uniform sampler2D u_tex;\n"
"out vec4 o_colour;\n"
"void main(void) {\n"
" o_colour = u_colour * texture2D(u_tex, v_tex_coord).r;\n"
"}\n"
);
glGenVertexArrays(1, &bs->font_vao);
glBindVertexArray(bs->font_vao);
bs->font_vbo = gbgl_make_vbo(NULL, gb_size_of(gbglBasicVertex) * GBGL_MAX_RENDER_STRING_LENGTH * 4, GL_DYNAMIC_DRAW);
for (i = 0; i < GBGL_MAX_RENDER_STRING_LENGTH; i++) {
bs->font_indices[i*6 + 0] = i*4 + 0;
bs->font_indices[i*6 + 1] = i*4 + 1;
bs->font_indices[i*6 + 2] = i*4 + 2;
bs->font_indices[i*6 + 3] = i*4 + 2;
bs->font_indices[i*6 + 4] = i*4 + 3;
bs->font_indices[i*6 + 5] = i*4 + 0;
}
bs->font_ebo = gbgl_make_ebo(bs->font_indices, gb_size_of(u16) * GBGL_MAX_RENDER_STRING_LENGTH * 6, GL_STATIC_DRAW);
bs->font_samplers[0] = gbgl_make_sampler(GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
bs->font_samplers[1] = gbgl_make_sampler(GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
bs->text_params[gbglTextParam_MaxWidth] .val_i32 = 0;
bs->text_params[gbglTextParam_Justify] .val_i32 = gbglJustify_Left;
bs->text_params[gbglTextParam_TextureFilter].val_i32 = 0;
#endif
}
gb_inline void gbgl_bs_set_resolution(gbglBasicState *bs, i32 window_width, i32 window_height) {
f32 left = 0.0f;
f32 right = cast(f32)window_width;
f32 bottom = 0.0f;
f32 top = cast(f32)window_height;
f32 znear = 0.0f;
f32 zfar = 1.0f;
bs->width = window_width;
bs->height = window_height;
bs->ortho_mat[0] = 2.0f / (right - left);
bs->ortho_mat[1] = 0.0f;
bs->ortho_mat[2] = 0.0f;
bs->ortho_mat[3] = 0.0f;
bs->ortho_mat[4] = 0.0f;
bs->ortho_mat[5] = 2.0f / (top - bottom);
bs->ortho_mat[6] = 0.0f;
bs->ortho_mat[7] = 0.0f;
bs->ortho_mat[8] = 0.0f;
bs->ortho_mat[9] = 0.0f;
bs->ortho_mat[10] = -2.0f / (zfar - znear);
bs->ortho_mat[11] = 0.0f;
bs->ortho_mat[12] = -(right + left) / (right - left);
bs->ortho_mat[13] = -(top + bottom) / (top - bottom);
bs->ortho_mat[14] = -(zfar + znear) / (zfar - znear);
bs->ortho_mat[15] = 1.0f;
}
gb_inline void gbgl_bs_begin(gbglBasicState *bs, i32 window_width, i32 window_height) {
glBindVertexArray(bs->vao);
glDisable(GL_SCISSOR_TEST);
gbgl_bs_set_resolution(bs, window_width, window_height);
}
gb_inline void gbgl_bs_end(gbglBasicState *bs) {
glBindVertexArray(0);
}
void gbgl_bs_draw_textured_rect(gbglBasicState *bs, gbglTexture *tex, f32 x, f32 y, f32 w, f32 h, b32 v_up) {
bs->vertices[0].x = x;
bs->vertices[0].y = y;
bs->vertices[0].u = 0.0f;
bs->vertices[0].v = v_up ? 0.0f : 1.0f;
bs->vertices[1].x = x + w;
bs->vertices[1].y = y;
bs->vertices[1].u = 1.0f;
bs->vertices[1].v = v_up ? 0.0f : 1.0f;
bs->vertices[2].x = x + w;
bs->vertices[2].y = y + h;
bs->vertices[2].u = 1.0f;
bs->vertices[2].v = v_up ? 1.0f : 0.0f;
bs->vertices[3].x = x;
bs->vertices[3].y = y + h;
bs->vertices[3].u = 0.0f;
bs->vertices[3].v = v_up ? 1.0f : 0.0f;
gbgl_use_shader(&bs->ortho_tex_shader);
gbgl_set_uniform_mat4(&bs->ortho_tex_shader, "u_ortho_mat", bs->ortho_mat);
gbgl_bind_texture2d(tex, 0, bs->nearest_sampler);
gbgl_vbo_copy(bs->vbo, bs->vertices, 4*gb_size_of(bs->vertices[0]), 0);
gbgl_vert_ptr_aa(0, 2, gbglBasicVertex, x);
gbgl_vert_ptr_aa(1, 2, gbglBasicVertex, u);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bs->ebo);
glEnable(GL_BLEND);
glBlendEquationi(0, GL_FUNC_ADD);
glBlendFunci(0, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);
}
gb_inline void gbgl_bs_draw_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col) {
gbgl_bs_draw_quad(bs,
x, y,
x+w, y,
x+w, y+h,
x, y+h,
col);
}
gb_inline void gbgl_bs_draw_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col, f32 thickness) {
gbgl_bs_draw_quad_outline(bs,
x, y,
x+w, y,
x+w, y+h,
x, y+h,
col,
thickness);
}
gb_internal void gbgl__bs_setup_ortho_colour_state(gbglBasicState *bs, isize vertex_count, gbglColour col) {
gbgl_use_shader(&bs->ortho_col_shader);
gbgl_set_uniform_mat4(&bs->ortho_col_shader, "u_ortho_mat", bs->ortho_mat);
gbgl_set_uniform_colour(&bs->ortho_col_shader, "u_colour", col);
gbgl_vbo_copy(bs->vbo, bs->vertices, vertex_count*gb_size_of(bs->vertices[0]), 0);
gbgl_vert_ptr_aa(0, 2, gbglBasicVertex, x);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bs->ebo);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
gb_inline 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) {
bs->vertices[0].x = x0;
bs->vertices[0].y = y0;
bs->vertices[1].x = x1;
bs->vertices[1].y = y1;
bs->vertices[2].x = x2;
bs->vertices[2].y = y2;
bs->vertices[3].x = x3;
bs->vertices[3].y = y3;
gbgl__bs_setup_ortho_colour_state(bs, 4, col);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);
}
gb_inline 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) {
bs->vertices[0].x = x0;
bs->vertices[0].y = y0;
bs->vertices[1].x = x1;
bs->vertices[1].y = y1;
bs->vertices[2].x = x2;
bs->vertices[2].y = y2;
bs->vertices[3].x = x3;
bs->vertices[3].y = y3;
gbgl__bs_setup_ortho_colour_state(bs, 4, col);
glLineWidth(thickness);
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
gb_inline void gbgl_bs_draw_line(gbglBasicState *bs, f32 x0, f32 y0, f32 x1, f32 y1, gbglColour col, f32 thickness) {
bs->vertices[0].x = x0;
bs->vertices[0].y = y0;
bs->vertices[1].x = x1;
bs->vertices[1].y = y1;
gbgl__bs_setup_ortho_colour_state(bs, 2, col);
glLineWidth(thickness);
glDrawArrays(GL_LINES, 0, 2);
}
gb_inline 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) {
isize i;
bs->vertices[0].x = x;
bs->vertices[0].y = y;
for (i = 0; i < 31; i++) {
f32 t = cast(f32)i / 30.0f;
f32 a = gbgl_lerp(min_angle, max_angle, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[i+1].x = x + c*radius_a;
bs->vertices[i+1].y = y + s*radius_b;
}
gbgl__bs_setup_ortho_colour_state(bs, 32, col);
glDrawArrays(GL_TRIANGLE_FAN, 0, 32);
}
gb_inline 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) {
isize i;
for (i = 0; i < 32; i++) {
f32 t = cast(f32)i / 31.0f;
f32 a = gbgl_lerp(min_angle, max_angle, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[i+1].x = x + c*radius_a;
bs->vertices[i+1].y = y + s*radius_b;
}
gbgl__bs_setup_ortho_colour_state(bs, 32, col);
glLineWidth(thickness);
glDrawArrays(GL_LINES, 0, 32);
}
gb_inline void gbgl_bs_draw_circle(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col) {
gbgl_bs_draw_elliptical_arc(bs, x, y, radius, radius, 0, GBGL_TAU, col);
}
gb_inline void gbgl_bs_draw_circle_outline(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col, f32 thickness) {
gbgl_bs_draw_elliptical_arc_outline(bs, x, y, radius, radius, 0, GBGL_TAU, col, thickness);
}
void gbgl_bs_draw_rounded_rect_corners(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, u32 corners) {
if ((2.0f*roundness > gbgl_abs(w)) ||
(2.0f*roundness > gbgl_abs(h))) {
roundness = 0.5f*gbgl_min(gbgl_abs(w), gbgl_abs(h));
}
if (roundness == 0 || corners == 0) {
gbgl_bs_draw_rect(bs, x, y, w, h, col);
} else {
isize i, vc = 0;
bs->vertices[vc].x = x + 0.5f*w;
bs->vertices[vc].y = y + 0.5f*h;
vc++;
if (corners & 1) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.5f*GBGL_TAU, 0.75f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + roundness + c*roundness;
bs->vertices[vc].y = y + roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x;
bs->vertices[vc].y = y;
vc++;
}
if (corners & 2) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.75f*GBGL_TAU, 1.00f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + w - roundness + c*roundness;
bs->vertices[vc].y = y + roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x + w;
bs->vertices[vc].y = y;
vc++;
}
if (corners & 4) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.00f*GBGL_TAU, 0.25f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + w - roundness + c*roundness;
bs->vertices[vc].y = y + h - roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x + w;
bs->vertices[vc].y = y + h;
vc++;
}
if (corners & 8) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.25f*GBGL_TAU, 0.50f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + roundness + c*roundness;
bs->vertices[vc].y = y + h - roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x;
bs->vertices[vc].y = y + h;
vc++;
}
if (corners & 1) {
bs->vertices[vc].x = x;
bs->vertices[vc].y = y + roundness;
} else {
bs->vertices[vc].x = x;
bs->vertices[vc].y = y;
}
vc++;
gbgl__bs_setup_ortho_colour_state(bs, vc, col);
glDrawArrays(GL_TRIANGLE_FAN, 0, vc);
}
}
gb_inline void gbgl_bs_draw_rounded_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col) {
gbgl_bs_draw_rounded_rect_corners(bs, x, y, w, h, roundness, col, 1|2|4|8);
}
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) {
if ((2.0f*roundness > gbgl_abs(w)) ||
(2.0f*roundness > gbgl_abs(h))) {
roundness = 0.5f*gbgl_min(gbgl_abs(w), gbgl_abs(h));
}
if (roundness == 0 || corners == 0) {
gbgl_bs_draw_rect_outline(bs, x, y, w, h, col, thickness);
} else {
isize i, vc = 0;
if (corners & 1) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.5f*GBGL_TAU, 0.75f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + roundness + c*roundness;
bs->vertices[vc].y = y + roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x;
bs->vertices[vc].y = y;
vc++;
}
if (corners & 2) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.75f*GBGL_TAU, 1.00f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + w - roundness + c*roundness;
bs->vertices[vc].y = y + roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x + w;
bs->vertices[vc].y = y;
vc++;
}
if (corners & 4) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.00f*GBGL_TAU, 0.25f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + w - roundness + c*roundness;
bs->vertices[vc].y = y + h - roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x + w;
bs->vertices[vc].y = y + h;
vc++;
}
if (corners & 8) {
for (i = 0; i < 6; i++) {
f32 t = cast(f32)i / 5.0f;
f32 a = gbgl_lerp(0.25f*GBGL_TAU, 0.50f*GBGL_TAU, t);
f32 c = gbgl_cos(a);
f32 s = gbgl_sin(a);
bs->vertices[vc].x = x + roundness + c*roundness;
bs->vertices[vc].y = y + h - roundness + s*roundness;
vc++;
}
} else {
bs->vertices[vc].x = x;
bs->vertices[vc].y = y + h;
vc++;
}
gbgl__bs_setup_ortho_colour_state(bs, vc, col);
glLineWidth(thickness);
glDrawArrays(GL_LINE_LOOP, 0, vc);
}
}
gb_inline void gbgl_bs_draw_rounded_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, f32 thickness) {
gbgl_bs_draw_rounded_rect_corners_outline(bs, x, y, w, h, roundness, col, thickness, 1|2|4|8);
}
#if !defined(GBGL_NO_FONTS)
isize gbgl_bs_draw_substring(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *str, isize len) {
isize char_count = gb_utf8_strnlen(str, len);
isize line_count = 0;
if (char_count > 0) {
char const *ptr = str;
f32 sf = 1.0f / cast(f32)font->bitmap_width;
f32 tf = 1.0f / cast(f32)font->bitmap_height;
f32 ox, oy;
f32 px, py;
isize glyph_count = 0, i;
f32 font_height = font->size;
i32 max_width = bs->text_params[gbglTextParam_MaxWidth].val_i32;
gbglJustifyType justify = cast(gbglJustifyType)bs->text_params[gbglTextParam_Justify].val_i32;
if (justify == gbglJustify_Centre) {
f32 width;
gbgl_get_string_dimensions(font, ptr, &width, NULL);
x = gbgl_round(x - width*0.5f);
} else if (justify == gbglJustify_Right) {
f32 width;
gbgl_get_string_dimensions(font, ptr, &width, NULL);
x = gbgl_round(x - width);
}
line_count = 1;
ox = x;
oy = y;
px = ox;
py = oy;
for (i = 0; i < char_count; i++) {
char32 cp;
isize byte_len, curr_index, draw_this_glyph_count = 1, j;
gbglGlyphInfo *gi;
byte_len = gb_utf8_decode_len(ptr, len-(ptr-str), &cp);
ptr += byte_len;
if (ptr - str > len)
break;
if (cp == '\t') {
draw_this_glyph_count = GBGL_TAB_CHARACTER_WIDTH;
cp = ' '; // TODO(bill): Set tab to be space
}
gi = gbgl_get_glyph_info(font, cp, &curr_index);
if (!gi) {
gi = gbgl_get_glyph_info(font, ' ', &curr_index);
}
if (gi) {
for (j = 0; j < draw_this_glyph_count; j++) {
f32 s0, t0, s1, t1;
f32 x0, y0, x1, y1;
f32 kern = 0.0f;
if (cp == '\r' || cp == '\n' ||
(max_width > 0 && px - ox + gi->xadv >= cast(f32)max_width)) {
px = ox;
py -= font_height;
line_count += 2;
if (cp == '\r' || cp == '\n') {
if (cp == '\r' && ptr[1] == '\n')
ptr++;
continue;
}
}
s0 = cast(f32)gi->s0 * sf;
t0 = cast(f32)gi->t0 * tf;
s1 = cast(f32)gi->s1 * sf;
t1 = cast(f32)gi->t1 * tf;
x0 = px + gi->xoff;
y0 = py - gi->yoff;
x1 = x0 + (gi->s1 - gi->s0);
y1 = y0 + (gi->t0 - gi->t1);
bs->font_vertices[glyph_count*4 + 0].x = x0;
bs->font_vertices[glyph_count*4 + 0].y = y0;
bs->font_vertices[glyph_count*4 + 0].u = s0;
bs->font_vertices[glyph_count*4 + 0].v = t0;
bs->font_vertices[glyph_count*4 + 1].x = x1;
bs->font_vertices[glyph_count*4 + 1].y = y0;
bs->font_vertices[glyph_count*4 + 1].u = s1;
bs->font_vertices[glyph_count*4 + 1].v = t0;
bs->font_vertices[glyph_count*4 + 2].x = x1;
bs->font_vertices[glyph_count*4 + 2].y = y1;
bs->font_vertices[glyph_count*4 + 2].u = s1;
bs->font_vertices[glyph_count*4 + 2].v = t1;
bs->font_vertices[glyph_count*4 + 3].x = x0;
bs->font_vertices[glyph_count*4 + 3].y = y1;
bs->font_vertices[glyph_count*4 + 3].u = s0;
bs->font_vertices[glyph_count*4 + 3].v = t1;
glyph_count++;
if (i < char_count-1) {
isize next_index;
char32 next_cp = 0;
gbglGlyphInfo *ngi;
gb_utf8_decode_len(ptr, len-(ptr-str), &next_cp);
ngi = gbgl_get_glyph_info(font, next_cp, &next_index);
if (ngi) {
kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
}
}
px += gi->xadv + kern;
}
}
}
if (glyph_count > 0) {
isize sampler_index = 0;
gbgl_use_shader(&bs->font_shader);
gbgl_set_uniform_mat4(&bs->font_shader, "u_ortho_mat", bs->ortho_mat);
gbgl_set_uniform_colour(&bs->font_shader, "u_colour", col);
GB_ASSERT(bs->text_params[gbglTextParam_TextureFilter].val_i32 < gb_count_of(bs->font_samplers));
if (bs->text_params[gbglTextParam_TextureFilter].val_i32 < gb_count_of(bs->font_samplers))
sampler_index = bs->text_params[gbglTextParam_TextureFilter].val_i32;
gbgl_bind_texture2d(&font->texture, 0, bs->font_samplers[sampler_index]);
gbgl_vbo_copy(bs->font_vbo, bs->font_vertices, gb_size_of(bs->font_vertices[0]) * glyph_count * 4, 0);
gbgl_vert_ptr_aa(0, 2, gbglBasicVertex, x);
gbgl_vert_ptr_aa(1, 2, gbglBasicVertex, u);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bs->font_ebo);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDrawElements(GL_TRIANGLES, glyph_count*6, GL_UNSIGNED_SHORT, NULL);
}
}
return line_count;
}
isize gbgl_bs_draw_string(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, ...) {
isize len;
va_list va;
va_start(va, fmt);
len = gbgl_bs_draw_string_va(bs, font, x, y, col, fmt, va);
va_end(va);
return len;
}
gb_inline isize gbgl_bs_draw_string_va(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, va_list va) {
isize len = gb_snprintf_va(bs->font_text_buffer, gb_size_of(bs->font_text_buffer),
fmt, va);
isize char_count = gb_utf8_strnlen(bs->font_text_buffer, len);
if (char_count <= 0)
return 0;
return gbgl_bs_draw_substring(bs, font, x, y, col, bs->font_text_buffer, len);
}
#endif // !defined(GBGL_NO_FONTS)
#endif // !defined(GBGL_NO_BASIC_STATE)
#endif
|