-
Notifications
You must be signed in to change notification settings - Fork 16
/
gltext.h
1339 lines (1001 loc) · 34.8 KB
/
gltext.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
// Repository: https://github.com/vallentin/glText
// License: https://github.com/vallentin/glText/blob/master/LICENSE.md
//
// Date Created: September 24, 2013
// Last Modified: July 02, 2020
// In one C or C++ file, define GLT_IMPLEMENTATION prior to inclusion to create the implementation.
// #define GLT_IMPLEMENTATION
// #include "gltext.h"
// Copyright (c) 2013-2018 Christian Vallentin
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
// Refrain from using any exposed macros, functions
// or structs prefixed with an underscore. As these
// are only intended for internal purposes. Which
// additionally means they can be removed, renamed
// or changed between minor updates without notice.
#ifndef GL_TEXT_H
#define GL_TEXT_H
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(__gl_h_) && !defined(__glcorearb_h_)
# error OpenGL header must be included prior to including glText header
#endif
#include <stdlib.h> /* malloc(), calloc(), free() */
#include <string.h> /* memset(), memcpy(), strlen() */
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, uint64_t */
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(GLT_DEBUG)
# define GLT_DEBUG 1
#endif
#ifdef GLT_DEBUG
# include <assert.h> /* assert() */
# define _GLT_ASSERT(expression) assert(expression)
#else
# define _GLT_ASSERT(expression)
#endif
#ifdef GLT_DEBUG_PRINT
# include <stdio.h> /* printf */
#endif
#define _GLT_STRINGIFY(str) #str
#define _GLT_STRINGIFY_TOKEN(str) _GLT_STRINGIFY(str)
#define GLT_STRINGIFY_VERSION(major, minor, patch) _GLT_STRINGIFY(major) "." _GLT_STRINGIFY(minor) "." _GLT_STRINGIFY(patch)
#define GLT_NAME "glText"
#define GLT_VERSION_MAJOR 1
#define GLT_VERSION_MINOR 1
#define GLT_VERSION_PATCH 6
#define GLT_VERSION GLT_STRINGIFY_VERSION(GLT_VERSION_MAJOR, GLT_VERSION_MINOR, GLT_VERSION_PATCH)
#define GLT_NAME_VERSION GLT_NAME " " GLT_VERSION
#define GLT_NULL 0
#define GLT_NULL_HANDLE 0
#ifdef GLT_IMPORTS
# define GLT_API extern
#else
# ifndef GLT_STATIC
# define GLT_STATIC
# endif
# define GLT_API static
#endif
#define GLT_LEFT 0
#define GLT_TOP 0
#define GLT_CENTER 1
#define GLT_RIGHT 2
#define GLT_BOTTOM 2
static GLboolean gltInitialized = GL_FALSE;
typedef struct GLTtext GLTtext;
GLT_API GLboolean gltInit(void);
GLT_API void gltTerminate(void);
GLT_API GLTtext* gltCreateText(void);
GLT_API void gltDeleteText(GLTtext *text);
#define gltDestroyText gltDeleteText
GLT_API GLboolean gltSetText(GLTtext *text, const char *string);
GLT_API const char* gltGetText(GLTtext *text);
GLT_API void gltViewport(GLsizei width, GLsizei height);
GLT_API void gltBeginDraw();
GLT_API void gltEndDraw();
GLT_API void gltDrawText(GLTtext *text, const GLfloat mvp[16]);
GLT_API void gltDrawText2D(GLTtext *text, GLfloat x, GLfloat y, GLfloat scale);
GLT_API void gltDrawText2DAligned(GLTtext *text, GLfloat x, GLfloat y, GLfloat scale, int horizontalAlignment, int verticalAlignment);
GLT_API void gltDrawText3D(GLTtext *text, GLfloat x, GLfloat y, GLfloat z, GLfloat scale, GLfloat view[16], GLfloat projection[16]);
GLT_API void gltColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
GLT_API void gltGetColor(GLfloat *r, GLfloat *g, GLfloat *b, GLfloat *a);
GLT_API GLfloat gltGetLineHeight(GLfloat scale);
GLT_API GLfloat gltGetTextWidth(const GLTtext *text, GLfloat scale);
GLT_API GLfloat gltGetTextHeight(const GLTtext *text, GLfloat scale);
GLT_API GLboolean gltIsCharacterSupported(const char c);
GLT_API GLint gltCountSupportedCharacters(const char *str);
GLT_API GLboolean gltIsCharacterDrawable(const char c);
GLT_API GLint gltCountDrawableCharacters(const char *str);
GLT_API GLint gltCountNewLines(const char *str);
// After this point everything you'll see is the
// implementation and all the internal stuff.
// Use it at your own discretion, but be warned
// that everything can have changed in the next
// commit, so be careful relying on any of it.
#ifdef GLT_IMPLEMENTATION
#define _GLT_TEXT2D_POSITION_LOCATION 0
#define _GLT_TEXT2D_TEXCOORD_LOCATION 1
#define _GLT_TEXT2D_POSITION_SIZE 2
#define _GLT_TEXT2D_TEXCOORD_SIZE 2
#define _GLT_TEXT2D_VERTEX_SIZE (_GLT_TEXT2D_POSITION_SIZE + _GLT_TEXT2D_TEXCOORD_SIZE)
#define _GLT_TEXT2D_POSITION_OFFSET 0
#define _GLT_TEXT2D_TEXCOORD_OFFSET _GLT_TEXT2D_POSITION_SIZE
#define _GLT_MAT4_INDEX(row, column) ((row) + (column) * 4)
static const char *_gltFontGlyphCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`*#=[]\"";
#define _gltFontGlyphCount 83
#define _gltFontGlyphMinChar ' '
#define _gltFontGlyphMaxChar 'z'
static const int _gltFontGlyphHeight = 17; // Line height
typedef struct _GLTglyph {
char c;
GLint x, y;
GLint w, h;
GLfloat u1, v1;
GLfloat u2, v2;
GLboolean drawable;
} _GLTglyph;
typedef struct _GLTglyphdata {
uint32_t x, y;
uint32_t w, h;
uint32_t marginLeft, marginTop;
uint32_t marginRight, marginBottom;
uint16_t dataWidth, dataHeight;
} _GLTglyphdata;
static _GLTglyph _gltFontGlyphs[_gltFontGlyphCount];
#define _gltFontGlyphLength (_gltFontGlyphMaxChar - _gltFontGlyphMinChar + 1)
static _GLTglyph _gltFontGlyphs2[_gltFontGlyphLength];
static GLuint _gltText2DShader = GLT_NULL_HANDLE;
static GLuint _gltText2DFontTexture = GLT_NULL_HANDLE;
static GLint _gltText2DShaderMVPUniformLocation = -1;
static GLint _gltText2DShaderColorUniformLocation = -1;
static GLfloat _gltText2DProjectionMatrix[16];
struct GLTtext {
char *_text;
GLsizei _textLength;
GLboolean _dirty;
GLsizei vertexCount;
GLfloat *_vertices;
GLuint _vao;
GLuint _vbo;
};
GLT_API void _gltGetViewportSize(GLint *width, GLint *height);
GLT_API void _gltMat4Mult(const GLfloat lhs[16], const GLfloat rhs[16], GLfloat result[16]);
GLT_API void _gltUpdateBuffers(GLTtext *text);
GLT_API GLboolean _gltCreateText2DShader(void);
GLT_API GLboolean _gltCreateText2DFontTexture(void);
GLT_API GLTtext* gltCreateText(void)
{
GLTtext *text = (GLTtext*)calloc(1, sizeof(GLTtext));
_GLT_ASSERT(text);
if (!text)
return GLT_NULL;
glGenVertexArrays(1, &text->_vao);
glGenBuffers(1, &text->_vbo);
_GLT_ASSERT(text->_vao);
_GLT_ASSERT(text->_vbo);
if (!text->_vao || !text->_vbo)
{
gltDeleteText(text);
return GLT_NULL;
}
glBindVertexArray(text->_vao);
glBindBuffer(GL_ARRAY_BUFFER, text->_vbo);
glEnableVertexAttribArray(_GLT_TEXT2D_POSITION_LOCATION);
glVertexAttribPointer(_GLT_TEXT2D_POSITION_LOCATION, _GLT_TEXT2D_POSITION_SIZE, GL_FLOAT, GL_FALSE, (_GLT_TEXT2D_VERTEX_SIZE * sizeof(GLfloat)), (const void*)(_GLT_TEXT2D_POSITION_OFFSET * sizeof(GLfloat)));
glEnableVertexAttribArray(_GLT_TEXT2D_TEXCOORD_LOCATION);
glVertexAttribPointer(_GLT_TEXT2D_TEXCOORD_LOCATION, _GLT_TEXT2D_TEXCOORD_SIZE, GL_FLOAT, GL_FALSE, (_GLT_TEXT2D_VERTEX_SIZE * sizeof(GLfloat)), (const void*)(_GLT_TEXT2D_TEXCOORD_OFFSET * sizeof(GLfloat)));
glBindVertexArray(0);
return text;
}
GLT_API void gltDeleteText(GLTtext *text)
{
if (!text)
return;
if (text->_vao)
{
glDeleteVertexArrays(1, &text->_vao);
text->_vao = GLT_NULL_HANDLE;
}
if (text->_vbo)
{
glDeleteBuffers(1, &text->_vbo);
text->_vbo = GLT_NULL_HANDLE;
}
if (text->_text)
free(text->_text);
if (text->_vertices)
free(text->_vertices);
free(text);
}
GLT_API GLboolean gltSetText(GLTtext *text, const char *string)
{
if (!text)
return GL_FALSE;
int strLength = 0;
if (string)
strLength = strlen(string);
if (strLength)
{
if (text->_text)
{
if (strcmp(string, text->_text) == 0)
return GL_TRUE;
free(text->_text);
text->_text = GLT_NULL;
}
text->_text = (char*)malloc((strLength + 1) * sizeof(char));
if (text->_text)
{
memcpy(text->_text, string, (strLength + 1) * sizeof(char));
text->_textLength = strLength;
text->_dirty = GL_TRUE;
return GL_TRUE;
}
}
else
{
if (text->_text)
{
free(text->_text);
text->_text = GLT_NULL;
}
else
{
return GL_TRUE;
}
text->_textLength = 0;
text->_dirty = GL_TRUE;
return GL_TRUE;
}
return GL_FALSE;
}
GLT_API const char* gltGetText(GLTtext *text)
{
if (text && text->_text)
return text->_text;
return "\0";
}
GLT_API void gltViewport(GLsizei width, GLsizei height)
{
_GLT_ASSERT(width > 0);
_GLT_ASSERT(height > 0);
const GLfloat left = 0.0f;
const GLfloat right = (GLfloat)width;
const GLfloat bottom = (GLfloat)height;
const GLfloat top = 0.0f;
const GLfloat zNear = -1.0f;
const GLfloat zFar = 1.0f;
const GLfloat projection[16] = {
(2.0f / (right - left)), 0.0f, 0.0f, 0.0f,
0.0f, (2.0f / (top - bottom)), 0.0f, 0.0f,
0.0f, 0.0f, (-2.0f / (zFar - zNear)), 0.0f,
-((right + left) / (right - left)),
-((top + bottom) / (top - bottom)),
-((zFar + zNear) / (zFar - zNear)),
1.0f,
};
memcpy(_gltText2DProjectionMatrix, projection, 16 * sizeof(GLfloat));
}
GLT_API void gltBeginDraw()
{
glUseProgram(_gltText2DShader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _gltText2DFontTexture);
}
GLT_API void gltEndDraw()
{
}
#define _gltDrawText() \
glUniformMatrix4fv(_gltText2DShaderMVPUniformLocation, 1, GL_FALSE, mvp); \
\
glBindVertexArray(text->_vao); \
glDrawArrays(GL_TRIANGLES, 0, text->vertexCount);
GLT_API void gltDrawText(GLTtext *text, const GLfloat mvp[16])
{
if (!text)
return;
if (text->_dirty)
_gltUpdateBuffers(text);
if (!text->vertexCount)
return;
_gltDrawText();
}
GLT_API void gltDrawText2D(GLTtext *text, GLfloat x, GLfloat y, GLfloat scale)
{
if (!text)
return;
if (text->_dirty)
_gltUpdateBuffers(text);
if (!text->vertexCount)
return;
#ifndef GLT_MANUAL_VIEWPORT
GLint viewportWidth, viewportHeight;
_gltGetViewportSize(&viewportWidth, &viewportHeight);
gltViewport(viewportWidth, viewportHeight);
#endif
const GLfloat model[16] = {
scale, 0.0f, 0.0f, 0.0f,
0.0f, scale, 0.0f, 0.0f,
0.0f, 0.0f, scale, 0.0f,
x, y, 0.0f, 1.0f,
};
GLfloat mvp[16];
_gltMat4Mult(_gltText2DProjectionMatrix, model, mvp);
_gltDrawText();
}
GLT_API void gltDrawText2DAligned(GLTtext *text, GLfloat x, GLfloat y, GLfloat scale, int horizontalAlignment, int verticalAlignment)
{
if (!text)
return;
if (text->_dirty)
_gltUpdateBuffers(text);
if (!text->vertexCount)
return;
if (horizontalAlignment == GLT_CENTER)
x -= gltGetTextWidth(text, scale) * 0.5f;
else if (horizontalAlignment == GLT_RIGHT)
x -= gltGetTextWidth(text, scale);
if (verticalAlignment == GLT_CENTER)
y -= gltGetTextHeight(text, scale) * 0.5f;
else if (verticalAlignment == GLT_RIGHT)
y -= gltGetTextHeight(text, scale);
gltDrawText2D(text, x, y, scale);
}
GLT_API void gltDrawText3D(GLTtext *text, GLfloat x, GLfloat y, GLfloat z, GLfloat scale, GLfloat view[16], GLfloat projection[16])
{
if (!text)
return;
if (text->_dirty)
_gltUpdateBuffers(text);
if (!text->vertexCount)
return;
const GLfloat model[16] = {
scale, 0.0f, 0.0f, 0.0f,
0.0f, -scale, 0.0f, 0.0f,
0.0f, 0.0f, scale, 0.0f,
x, y + (GLfloat)_gltFontGlyphHeight * scale, z, 1.0f,
};
GLfloat mvp[16];
GLfloat vp[16];
_gltMat4Mult(projection, view, vp);
_gltMat4Mult(vp, model, mvp);
_gltDrawText();
}
GLT_API void gltColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
{
glUniform4f(_gltText2DShaderColorUniformLocation, r, g, b, a);
}
GLT_API void gltGetColor(GLfloat *r, GLfloat *g, GLfloat *b, GLfloat *a)
{
GLfloat color[4];
glGetUniformfv(_gltText2DShader, _gltText2DShaderColorUniformLocation, color);
if (r) (*r) = color[0];
if (g) (*g) = color[1];
if (b) (*b) = color[2];
if (a) (*a) = color[3];
}
GLT_API GLfloat gltGetLineHeight(GLfloat scale)
{
return (GLfloat)_gltFontGlyphHeight * scale;
}
GLT_API GLfloat gltGetTextWidth(const GLTtext *text, GLfloat scale)
{
if (!text || !text->_text)
return 0.0f;
GLfloat maxWidth = 0.0f;
GLfloat width = 0.0f;
_GLTglyph glyph;
char c;
int i;
for (i = 0; i < text->_textLength; i++)
{
c = text->_text[i];
if ((c == '\n') || (c == '\r'))
{
if (width > maxWidth)
maxWidth = width;
width = 0.0f;
continue;
}
if (!gltIsCharacterSupported(c))
{
#ifdef GLT_UNKNOWN_CHARACTER
c = GLT_UNKNOWN_CHARACTER;
if (!gltIsCharacterSupported(c))
continue;
#else
continue;
#endif
}
glyph = _gltFontGlyphs2[c - _gltFontGlyphMinChar];
width += (GLfloat)glyph.w;
}
if (width > maxWidth)
maxWidth = width;
return maxWidth * scale;
}
GLT_API GLfloat gltGetTextHeight(const GLTtext *text, GLfloat scale)
{
if (!text || !text->_text)
return 0.0f;
return (GLfloat)(gltCountNewLines(text->_text) + 1) * gltGetLineHeight(scale);
}
GLT_API GLboolean gltIsCharacterSupported(const char c)
{
if (c == '\t') return GL_TRUE;
if (c == '\n') return GL_TRUE;
if (c == '\r') return GL_TRUE;
int i;
for (i = 0; i < _gltFontGlyphCount; i++)
{
if (_gltFontGlyphCharacters[i] == c)
return GL_TRUE;
}
return GL_FALSE;
}
GLT_API GLint gltCountSupportedCharacters(const char *str)
{
if (!str)
return 0;
GLint count = 0;
while ((*str) != '\0')
{
if (gltIsCharacterSupported(*str))
count++;
str++;
}
return count;
}
GLT_API GLboolean gltIsCharacterDrawable(const char c)
{
if (c < _gltFontGlyphMinChar) return GL_FALSE;
if (c > _gltFontGlyphMaxChar) return GL_FALSE;
if (_gltFontGlyphs2[c - _gltFontGlyphMinChar].drawable)
return GL_TRUE;
return GL_FALSE;
}
GLT_API GLint gltCountDrawableCharacters(const char *str)
{
if (!str)
return 0;
GLint count = 0;
while ((*str) != '\0')
{
if (gltIsCharacterDrawable(*str))
count++;
str++;
}
return count;
}
GLT_API GLint gltCountNewLines(const char *str)
{
GLint count = 0;
while ((str = strchr(str, '\n')) != NULL)
{
count++;
str++;
}
return count;
}
GLT_API void _gltGetViewportSize(GLint *width, GLint *height)
{
GLint dimensions[4];
glGetIntegerv(GL_VIEWPORT, dimensions);
if (width) (*width) = dimensions[2];
if (height) (*height) = dimensions[3];
}
GLT_API void _gltMat4Mult(const GLfloat lhs[16], const GLfloat rhs[16], GLfloat result[16])
{
int c, r, i;
for (c = 0; c < 4; c++)
{
for (r = 0; r < 4; r++)
{
result[_GLT_MAT4_INDEX(r, c)] = 0.0f;
for (i = 0; i < 4; i++)
result[_GLT_MAT4_INDEX(r, c)] += lhs[_GLT_MAT4_INDEX(r, i)] * rhs[_GLT_MAT4_INDEX(i, c)];
}
}
}
GLT_API void _gltUpdateBuffers(GLTtext *text)
{
if (!text || !text->_dirty)
return;
if (text->_vertices)
{
text->vertexCount = 0;
free(text->_vertices);
text->_vertices = GLT_NULL;
}
if (!text->_text || !text->_textLength)
{
text->_dirty = GL_FALSE;
return;
}
const GLsizei countDrawable = gltCountDrawableCharacters(text->_text);
if (!countDrawable)
{
text->_dirty = GL_FALSE;
return;
}
const GLsizei vertexCount = countDrawable * 2 * 3; // 3 vertices in a triangle, 2 triangles in a quad
const GLsizei vertexSize = _GLT_TEXT2D_VERTEX_SIZE;
GLfloat *vertices = (GLfloat*)malloc(vertexCount * vertexSize * sizeof(GLfloat));
if (!vertices)
return;
GLsizei vertexElementIndex = 0;
GLfloat glyphX = 0.0f;
GLfloat glyphY = 0.0f;
GLfloat glyphWidth;
const GLfloat glyphHeight = (GLfloat)_gltFontGlyphHeight;
const GLfloat glyphAdvanceX = 0.0f;
const GLfloat glyphAdvanceY = 0.0f;
_GLTglyph glyph;
char c;
int i;
for (i = 0; i < text->_textLength; i++)
{
c = text->_text[i];
if (c == '\n')
{
glyphX = 0.0f;
glyphY += glyphHeight + glyphAdvanceY;
continue;
}
else if (c == '\r')
{
glyphX = 0.0f;
continue;
}
if (!gltIsCharacterSupported(c))
{
#ifdef GLT_UNKNOWN_CHARACTER
c = GLT_UNKNOWN_CHARACTER;
if (!gltIsCharacterSupported(c))
continue;
#else
continue;
#endif
}
glyph = _gltFontGlyphs2[c - _gltFontGlyphMinChar];
glyphWidth = (GLfloat)glyph.w;
if (glyph.drawable)
{
vertices[vertexElementIndex++] = glyphX;
vertices[vertexElementIndex++] = glyphY;
vertices[vertexElementIndex++] = glyph.u1;
vertices[vertexElementIndex++] = glyph.v1;
vertices[vertexElementIndex++] = glyphX + glyphWidth;
vertices[vertexElementIndex++] = glyphY + glyphHeight;
vertices[vertexElementIndex++] = glyph.u2;
vertices[vertexElementIndex++] = glyph.v2;
vertices[vertexElementIndex++] = glyphX + glyphWidth;
vertices[vertexElementIndex++] = glyphY;
vertices[vertexElementIndex++] = glyph.u2;
vertices[vertexElementIndex++] = glyph.v1;
vertices[vertexElementIndex++] = glyphX;
vertices[vertexElementIndex++] = glyphY;
vertices[vertexElementIndex++] = glyph.u1;
vertices[vertexElementIndex++] = glyph.v1;
vertices[vertexElementIndex++] = glyphX;
vertices[vertexElementIndex++] = glyphY + glyphHeight;
vertices[vertexElementIndex++] = glyph.u1;
vertices[vertexElementIndex++] = glyph.v2;
vertices[vertexElementIndex++] = glyphX + glyphWidth;
vertices[vertexElementIndex++] = glyphY + glyphHeight;
vertices[vertexElementIndex++] = glyph.u2;
vertices[vertexElementIndex++] = glyph.v2;
}
glyphX += glyphWidth + glyphAdvanceX;
}
text->vertexCount = vertexCount;
text->_vertices = vertices;
glBindBuffer(GL_ARRAY_BUFFER, text->_vbo);
glBufferData(GL_ARRAY_BUFFER, vertexCount * _GLT_TEXT2D_VERTEX_SIZE * sizeof(GLfloat), vertices, GL_DYNAMIC_DRAW);
text->_dirty = GL_FALSE;
}
GLT_API GLboolean gltInit(void)
{
if (gltInitialized)
return GL_TRUE;
if (!_gltCreateText2DShader())
return GL_FALSE;
if (!_gltCreateText2DFontTexture())
return GL_FALSE;
gltInitialized = GL_TRUE;
return GL_TRUE;
}
GLT_API void gltTerminate(void)
{
if (_gltText2DShader != GLT_NULL_HANDLE)
{
glDeleteProgram(_gltText2DShader);
_gltText2DShader = GLT_NULL_HANDLE;
}
if (_gltText2DFontTexture != GLT_NULL_HANDLE)
{
glDeleteTextures(1, &_gltText2DFontTexture);
_gltText2DFontTexture = GLT_NULL_HANDLE;
}
gltInitialized = GL_FALSE;
}
static const GLchar* _gltText2DVertexShaderSource =
"#version 330 core\n"
"\n"
"in vec2 position;\n"
"in vec2 texCoord;\n"
"\n"
"uniform mat4 mvp;\n"
"\n"
"out vec2 fTexCoord;\n"
"\n"
"void main()\n"
"{\n"
" fTexCoord = texCoord;\n"
" \n"
" gl_Position = mvp * vec4(position, 0.0, 1.0);\n"
"}\n";
static const GLchar* _gltText2DFragmentShaderSource =
"#version 330 core\n"
"\n"
"out vec4 fragColor;\n"
"\n"
"uniform sampler2D diffuse;\n"
"\n"
"uniform vec4 color = vec4(1.0, 1.0, 1.0, 1.0);\n"
"\n"
"in vec2 fTexCoord;\n"
"\n"
"void main()\n"
"{\n"
" fragColor = texture(diffuse, fTexCoord) * color;\n"
"}\n";
GLT_API GLboolean _gltCreateText2DShader(void)
{
GLuint vertexShader, fragmentShader;
GLint compileStatus, linkStatus;
#ifdef GLT_DEBUG_PRINT
GLint infoLogLength;
GLsizei infoLogSize;
GLchar *infoLog;
#endif
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &_gltText2DVertexShaderSource, NULL);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus != GL_TRUE)
{
#ifdef GLT_DEBUG_PRINT
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &infoLogLength);
// If no information log exists 0 is returned or 1 for a
// string only containing the null termination char.
if (infoLogLength > 1)
{
infoLogSize = infoLogLength * sizeof(GLchar);
infoLog = (GLchar*)malloc(infoLogSize);
glGetShaderInfoLog(vertexShader, infoLogSize, NULL, infoLog);
printf("Vertex Shader #%u <Info Log>:\n%s\n", vertexShader, infoLog);
free(infoLog);
}
#endif
glDeleteShader(vertexShader);
gltTerminate();
#ifdef GLT_DEBUG
_GLT_ASSERT(compileStatus == GL_TRUE);
return GL_FALSE;
#else
return GL_FALSE;
#endif
}
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &_gltText2DFragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus != GL_TRUE)
{
#ifdef GLT_DEBUG_PRINT
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &infoLogLength);
// If no information log exists 0 is returned or 1 for a
// string only containing the null termination char.
if (infoLogLength > 1)
{
infoLogSize = infoLogLength * sizeof(GLchar);
infoLog = (GLchar*)malloc(infoLogSize);
glGetShaderInfoLog(fragmentShader, infoLogSize, NULL, infoLog);
printf("Fragment Shader #%u <Info Log>:\n%s\n", fragmentShader, infoLog);
free(infoLog);
}
#endif
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
gltTerminate();
#ifdef GLT_DEBUG
_GLT_ASSERT(compileStatus == GL_TRUE);
return GL_FALSE;
#else
return GL_FALSE;
#endif
}
_gltText2DShader = glCreateProgram();
glAttachShader(_gltText2DShader, vertexShader);
glAttachShader(_gltText2DShader, fragmentShader);
glBindAttribLocation(_gltText2DShader, _GLT_TEXT2D_POSITION_LOCATION, "position");
glBindAttribLocation(_gltText2DShader, _GLT_TEXT2D_TEXCOORD_LOCATION, "texCoord");
glBindFragDataLocation(_gltText2DShader, 0, "fragColor");
glLinkProgram(_gltText2DShader);
glDetachShader(_gltText2DShader, vertexShader);
glDeleteShader(vertexShader);
glDetachShader(_gltText2DShader, fragmentShader);
glDeleteShader(fragmentShader);
glGetProgramiv(_gltText2DShader, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE)
{
#ifdef GLT_DEBUG_PRINT
glGetProgramiv(_gltText2DShader, GL_INFO_LOG_LENGTH, &infoLogLength);
// If no information log exists 0 is returned or 1 for a
// string only containing the null termination char.
if (infoLogLength > 1)
{
infoLogSize = infoLogLength * sizeof(GLchar);
infoLog = (GLchar*)malloc(infoLogSize);
glGetProgramInfoLog(_gltText2DShader, infoLogSize, NULL, infoLog);
printf("Program #%u <Info Log>:\n%s\n", _gltText2DShader, infoLog);
free(infoLog);
}
#endif
gltTerminate();
#ifdef GLT_DEBUG
_GLT_ASSERT(linkStatus == GL_TRUE);
return GL_FALSE;
#else
return GL_FALSE;
#endif
}