-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathOpenGLText.cpp
More file actions
896 lines (839 loc) · 27.8 KB
/
OpenGLText.cpp
File metadata and controls
896 lines (839 loc) · 27.8 KB
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
/*
developed by Tristan Lorach - Copyright (c) 2012 NVIDIA Corporation. All rights reserved.
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA
OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT, OR
CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS
OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY
OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Please direct any questions to tlorach@nvidia.com (Tristan Lorach)
===================================================================================
Example on how to use it.
init time:
OpenGLText oglText;
...
if(!oglText.init(font_name, canvas_width, canvas_height))
return false;
render time:
oglText.beginString();
float bbStr[2];
char *tmpStr = "Hello world";
oglText.stringSize(tmpStr, bbStr);
oglText.drawString(posX - bbStr[0]*0.5, posY - bbStr[1], tmpStr, 0,0xF0F0F0F0);
...
oglText.endString(); // will render the whole at once
*/
//#define USE_QUADS
#define USE_INSTANCED_ARRAYS
#define NV_REPORT_COMPILE_ERRORS
#define USE_PSEUDO_INSTANCING
#ifdef USE_QUADS
# define TOPOLOGY_PRIM GL_QUADS
# define PRIMNUMBER 4
#else
# define TOPOLOGY_PRIM GL_TRIANGLES
# define PRIMNUMBER 6
#endif
#ifdef USE_INSTANCED_ARRAYS
# define USE_FONT_METRIC_AS_TBO
#endif
#ifdef USE_PSEUDO_INSTANCING
# define USE_INSTANCED_ARRAYS
# define USE_FONT_METRIC_AS_TBO
#endif
#pragma warning(disable:4244) // dble to float conversion warning
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#ifdef WIN32
#include <direct.h>
#define MKDIR(file) _mkdir((file))
#else
#include <sys/stat.h>
#define MKDIR(file) mkdir((file),0777)
#endif
#include <errno.h>
#include <math.h>
#include <math.h>
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#endif
#include "OpenGLText.h"
#include "tga.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
///////////////////////////////////////////////////////////////////////////////////////
//
//
#ifdef USE_INSTANCED_ARRAYS
char* OpenGLText::cWidgetVSSource2 = {
"#version 140\n\
uniform vec4 canvas; \n"
#ifdef USE_FONT_METRIC_AS_TBO
"uniform samplerBuffer glyphTexOffset;\n"
#else
"in vec4 TexCoord;\n"
#endif
#ifndef USE_PSEUDO_INSTANCING
"in vec4 Position;\n"
"in int Glyph;\n"
#else
"uniform samplerBuffer quads;\n"
#endif
"out vec2 vsTC;\n\
\n\
void main()\n\
{\n"
#ifdef USE_PSEUDO_INSTANCING
#ifdef USE_QUADS
"int i = gl_VertexID / 4;\n"
#else
"int i = gl_VertexID / 6;\n"
#endif
"vec4 Position = texelFetch(quads, i*2);\n"
"int Glyph = int(texelFetch(quads, i*2+1).x);\n"
#endif
#ifdef USE_QUADS
"int id = gl_VertexID & 3;\n"
#else
"int id = gl_VertexID % 6;\n"
#endif
"float x = Position.x;\n\
float y = Position.y;\n\
float w = Position.z;\n\
float h = Position.w;\n\
vec4 p = vec4(x,y,0,1);\n"
#ifdef USE_QUADS
"if((id) == 1) { p.x += w; }\n"
"else if((id) == 2) { p.x += w; p.y += h; }\n"
"else if((id) == 3) { p.y += h; }\n"
#else
"switch(id) {\n\
case 0: break;\n\
case 3:\n\
case 1: p.x += w; break;\n\
case 5:\n\
case 2: p.y += h; break;\n\
case 4: p.x += w; p.y += h; break; \n\
}\n"
#endif
#ifdef USE_FONT_METRIC_AS_TBO
"int g = int(Glyph);\n\
vec4 ginfo = texelFetch(glyphTexOffset, g);\n\
x = ginfo.x;\n\
y = ginfo.y;\n\
w = ginfo.z;\n\
h = ginfo.w;\n"
#else
"x = TexCoord.x;\n\
y = TexCoord.y;\n\
w = TexCoord.z;\n\
h = TexCoord.w;\n"
#endif
"vec2 tc = vec2(x,y);\n"
#ifdef USE_QUADS
"if((id) == 1) { tc.x += w; }\n"
"else if((id) == 2) { tc.x += w; tc.y += h; }\n"
"else if((id) == 3) { tc.y += h; }\n"
#else
"switch(id) {\n\
case 0: break;\n\
case 3:\n\
case 1: tc.x += w; break;\n\
case 5:\n\
case 2: tc.y += h; break;\n\
case 4: tc.x += w; tc.y += h; break; \n\
}\n"
#endif
"gl_Position = vec4( ((p.x / canvas.x)*canvas.z*2.0 - 1.0), \n\
((p.y / canvas.y)*2.0 - 1.0), 0, 1.0); \n\
vsTC = tc; \n\
}\n\
"};
char* OpenGLText::cWidgetFSSource2 = {
"#version 140\n\
uniform vec4 color; \n\
uniform sampler2D fontTex;\n\
in vec2 vsTC;\n\
out vec4 fragColor;\n\
\n\
void main()\n\
{\n\
float a = (texture2D( fontTex, vsTC.xy ).x); \n\
fragColor.rgb = color.rgb; \n\
fragColor.a = color.a * a;\n\
}\n\
"};
#else
char* OpenGLText::cWidgetVSSource2 = {
"#version 140\n\
uniform vec4 canvas; \n\
\n\
in vec4 Position;\n\
in vec4 TexCoord;\n\
out vec2 vsTC;\n\
\n\
void main()\n\
{\n\
gl_Position = vec4( (((Position.x) / canvas.x)*canvas.z*2.0 - 1.0), \n\
(((Position.y) / canvas.y)*2.0 - 1.0), 0, 1.0); \n\
vsTC = TexCoord.xy; \n\
}\n\
"};
char* OpenGLText::cWidgetFSSource2 = {
"#version 140\n\
uniform vec4 color; \n\
uniform sampler2D fontTex;\n\
in vec2 vsTC;\n\
out vec4 fragColor;\n\
\n\
void main()\n\
{\n\
float distance = (texture2D( fontTex, vsTC.xy ).x); \n\
fragColor.rgb = color.rgb; \n\
fragColor.a = color.a * distance;\n\
}\n\
"};
#endif
///////////////////////////////////////////////////////////////////////////////////////
// Backup only the states that we really need to setup
// the rendering. We might want to back them up once
//
struct TextBackupState
{
struct vtxAttribData {
GLvoid* ptr;
#ifdef USE_INSTANCED_ARRAYS
int divisor;
#endif
int enabled;
int size;
int type;
int normalized;
int stride;
int bufferBinding;
};
TextBackupState() { valid = false; }
~TextBackupState() { if(valid) {
free(attribs);
} }
void backup()
{
glGetIntegerv(GL_POLYGON_MODE, (GLint*)mode);
cull = glIsEnabled(GL_CULL_FACE);
//glGetIntegerv(GL_CULL_FACE_MODE, (GLint*)&);
stenciltest = glIsEnabled(GL_STENCIL_TEST);
glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint*)&stencilmask);
depthtest = glIsEnabled(GL_DEPTH_TEST);
glGetIntegerv(GL_DEPTH_WRITEMASK, (GLint*)&depthmask);
blend = glIsEnabled(GL_BLEND);
glGetIntegerv(GL_BLEND_SRC, (GLint*)&sfactor);
glGetIntegerv(GL_BLEND_DST, (GLint*)&dfactor);
glGetIntegerv(GL_COLOR_WRITEMASK, (GLint*)&red);
primrestart = glIsEnabled(GL_PRIMITIVE_RESTART);
glGetIntegerv(GL_PRIMITIVE_RESTART_INDEX, (GLint*)&primrestartid);
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVtxAttribs);
attribs = (vtxAttribData*)malloc(sizeof(vtxAttribData)*maxVtxAttribs);
for(int i=0; i<maxVtxAttribs; i++)
{
#ifdef USE_INSTANCED_ARRAYS
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_DIVISOR, &(attribs[i].divisor));
#endif
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &(attribs[i].enabled));
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &(attribs[i].size));
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &(attribs[i].type));
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &(attribs[i].normalized));
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &(attribs[i].stride));
glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &(attribs[i].bufferBinding));
glGetVertexAttribPointerv(i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &(attribs[i].ptr));
}
valid = true;
}
void restore()
{
if(!valid)
return;
glPolygonMode(GL_FRONT_AND_BACK, mode[0]);
//glPolygonMode(GL_BACK, mode[1]); deprecated in Core GL !!!!!!
if(cull) glEnable(GL_CULL_FACE);
else glDisable(GL_CULL_FACE);
if(stenciltest) glEnable(GL_STENCIL_TEST);
else glDisable(GL_STENCIL_TEST);
glStencilMask( stencilmask );
if(depthtest) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
glDepthMask( depthmask );
if(blend) glEnable(GL_BLEND);
else glDisable(GL_BLEND);
glBlendFunc(sfactor, dfactor);
glColorMask( red,green,blue,alpha );
glPrimitiveRestartIndex(primrestartid);
if(primrestart) glEnable(GL_PRIMITIVE_RESTART);
else glDisable(GL_PRIMITIVE_RESTART);
for(int i=0; i<maxVtxAttribs; i++)
{
if(attribs[i].enabled)
glEnableVertexAttribArray(i);
else
glDisableVertexAttribArray(i);
glBindBuffer(GL_ARRAY_BUFFER, attribs[i].bufferBinding);
glVertexAttribPointer(i, attribs[i].size, attribs[i].type, attribs[i].normalized, attribs[i].stride, attribs[i].ptr);
#ifdef USE_INSTANCED_ARRAYS
glVertexAttribDivisor(i, attribs[i].divisor);
#endif
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
// TODO: texture
}
void setStates()
{
// fill mode always
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable( GL_CULL_FACE );
// Stencil / Depth buffer and test disabled
glDisable(GL_STENCIL_TEST);
glStencilMask( 0 );
glDisable(GL_DEPTH_TEST);
glDepthMask( GL_FALSE );
// Blend on for alpha
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Color active
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
glPrimitiveRestartIndex(-1);
glEnable(GL_PRIMITIVE_RESTART);
}
bool valid;
int maxVtxAttribs;
vtxAttribData *attribs;
GLenum mode[2];
GLenum cull;
GLenum stenciltest;
int stencilmask;
GLenum depthtest;
GLint depthmask;
GLenum blend;
GLenum sfactor; GLenum dfactor;
GLint red; GLint green; GLint blue; GLint alpha;
GLuint primrestartid;
GLenum primrestart;
};
static TextBackupState bs;
///////////////////////////////////////////////////////////////////////////////////////
//
//
OpenGLText::OpenGLText()
{
c_fontNbChars = 128;
c_fontHeight = 16;
c_fontWidth = 724;
m_widgetProgram = 0;
m_vShader = 0;
m_fShader = 0;
m_vbo = 0;
#ifdef USE_FONT_METRIC_AS_TBO
m_boGlyphTexOffset = 0;
m_GlyphTexOffset = 0;
#endif
m_vbosz = 0;
m_canvasVar = 0;
m_color = 0;
m_fontTex = 0;
m_vertexDepth = 1.f;
m_indexOffset = 0;
glyphInfos = NULL;
allocated = false;
//m_window;
};
OpenGLText::~OpenGLText()
{
//if(m_vShader)
//glDeleteSh
m_widgetProgram = 0;
m_vShader = 0;
m_fShader = 0;
if(glyphInfos && allocated)
delete glyphInfos;
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
inline GLuint OpenGLText::CompileGLSLShader( GLenum target, const char* shader)
{
GLuint object;
object = glCreateShader( target);
if (!object)
return object;
glShaderSource( object, 1, &shader, NULL);
glCompileShader(object);
// check if shader compiled
GLint compiled = 0;
glGetShaderiv(object, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
#ifdef NV_REPORT_COMPILE_ERRORS
char temp[256] = "";
glGetShaderInfoLog( object, 256, NULL, temp);
fprintf( stderr, "Compile failed:\n%s\n", temp);
#endif
glDeleteShader( object);
return 0;
}
return object;
}
// Create a program composed of vertex and fragment shaders.
inline GLuint OpenGLText::LinkGLSLProgram( GLuint vertexShader, GLuint fragmentShader)
{
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
#ifdef NV_REPORT_COMPILE_ERRORS
// Get error log.
GLint charsWritten, infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
char * infoLog = new char[infoLogLength];
glGetProgramInfoLog(program, infoLogLength, &charsWritten, infoLog);
if((infoLogLength > 0)&&(infoLog[0] != '\0'))
fprintf( stderr, "Link failed:\n%s\n", infoLog);
delete [] infoLog;
#endif
// Test linker result.
GLint linkSucceed = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkSucceed);
if (linkSucceed == GL_FALSE)
{
glDeleteProgram(program);
return 0;
}
return program;
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
void OpenGLText::changeCanvas(int w, int h)
{
m_canvas.w = w;
m_canvas.h = h;
m_canvas.ratio = ((float)m_canvas.winh*w)/((float)m_canvas.winw*h);
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
void OpenGLText::changeSize(int w, int h)
{
m_canvas.winw = w;
m_canvas.winh = h;
m_canvas.ratio = ((float)h*m_canvas.w)/((float)w*m_canvas.h);
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
bool OpenGLText::init(unsigned char *imageData, FileHeader *glyphInfos_, int w, int h)
{
if(glyphInfos && allocated)
delete glyphInfos;
glyphInfos = glyphInfos_;
allocated = false;
if(m_fontTex == 0)
glGenTextures( 1, &m_fontTex );
GLenum extFmt = GL_RED;
glTextureImage2DEXT(m_fontTex, GL_TEXTURE_2D, 0, extFmt, glyphInfos->texwidth, glyphInfos->texheight, 0, extFmt, GL_UNSIGNED_BYTE, imageData);
glTextureParameterf(m_fontTex, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameterf(m_fontTex, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameterf(m_fontTex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameterf(m_fontTex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLenum err = glGetError();
return init(w,h);
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
bool OpenGLText::init(const char * fontName, int w, int h)
{
//
// Load the Font if possible
//
if(fontName)
{
char fname[200];
sprintf(fname, "%s.tga", fontName);
TGA* fontTGA = new TGA;
TGA::TGAError err = fontTGA->load(fname);
if(err != TGA::TGA_NO_ERROR)
return false;
sprintf(fname, "%s.bin", fontName);
FILE *fd = fopen(fname, "rb");
if(fd)
{
if(glyphInfos && allocated)
delete glyphInfos;
glyphInfos = new FileHeader;
allocated = true;
int r = (int)fread(glyphInfos, 1, sizeof(FileHeader), fd);
fclose(fd);
// Change the shaders to use for this case
int u = fontTGA->m_nImageWidth;
int h = fontTGA->m_nImageHeight;
if(m_fontTex == 0)
glGenTextures( 1, &m_fontTex );
GLenum extFmt;
switch(fontTGA->m_texFormat)
{
case TGA::RGB:
extFmt = GL_RGB;
break;
case TGA::RGBA:
extFmt = GL_RGBA;
break;
case TGA::ALPHA:
extFmt = GL_LUMINANCE;
break;
}
glTextureImage2DEXT(m_fontTex, GL_TEXTURE_2D, 0, extFmt, u, h, 0, extFmt, GL_UNSIGNED_BYTE, fontTGA->m_nImageData);
glTextureParameterf(m_fontTex, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameterf(m_fontTex, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameterf(m_fontTex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameterf(m_fontTex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLenum err = glGetError();
}
delete fontTGA;
}
return init(w,h);
}
bool OpenGLText::init(int w, int h)
{
m_canvas.w = w;
m_canvas.h = h;
m_canvas.ratio = 1.0;
if (m_widgetProgram == 0)
{
m_vShader = CompileGLSLShader( GL_VERTEX_SHADER, cWidgetVSSource2);
if (!m_vShader)
fprintf(stderr, "Vertex shader compile failed\n");
m_fShader = CompileGLSLShader( GL_FRAGMENT_SHADER, cWidgetFSSource2);
if (!m_fShader)
fprintf(stderr, "Fragment shader compile failed\n");
//CHECKGLERRORS();
m_widgetProgram = LinkGLSLProgram( m_vShader, m_fShader );
//CHECKGLERRORS();
GLint fontTexLoc = glGetUniformLocation( m_widgetProgram, "fontTex");
m_canvasVar = glGetUniformLocation( m_widgetProgram, "canvas" );
m_color = glGetUniformLocation( m_widgetProgram, "color" );
glProgramUniform1i(m_widgetProgram, fontTexLoc, 0); //Texture unit 0 is for font Tex.
glGenBuffers(1, &m_vbo);
#ifdef USE_FONT_METRIC_AS_TBO
// Upload the font metric in a TBO
float *tcs = new float[256*4];
for(int i=0; i<256; i++)
{
tcs[(4*i)+0] = glyphInfos->glyphs[i].norm.u;
tcs[(4*i)+1] = glyphInfos->glyphs[i].norm.v;
tcs[(4*i)+2] = glyphInfos->glyphs[i].norm.width;
tcs[(4*i)+3] = glyphInfos->glyphs[i].norm.height;
}
GLenum err = glGetError();
m_locGlyphTexOffset = glGetUniformLocation( m_widgetProgram, "glyphTexOffset" );
glProgramUniform1i(m_widgetProgram, m_locGlyphTexOffset, 2);
err = glGetError();
glGenBuffers( 1, &m_boGlyphTexOffset);
glGenTextures( 1, &m_texGlyphTexOffset);
glBindBuffer(GL_TEXTURE_BUFFER, m_boGlyphTexOffset);
glBufferData(GL_TEXTURE_BUFFER, 256*4*sizeof(float), tcs, GL_STATIC_DRAW);
delete [] tcs;
#else
locTc = glGetAttribLocation( m_widgetProgram, "TexCoord" );
#endif
#ifdef USE_PSEUDO_INSTANCING
//
// In this case, send Quad attributes as TBO
//
m_locQuads = glGetUniformLocation( m_widgetProgram, "quads" );
glGenTextures( 1, &m_texQuads );
glProgramUniform1i(m_widgetProgram, m_locQuads, 1);
#else
#ifdef USE_INSTANCED_ARRAYS
locGlyph = glGetAttribLocation( m_widgetProgram, "Glyph" );
#endif
locPos = glGetAttribLocation( m_widgetProgram, "Position" );
#endif
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
void OpenGLText::pushVertex( Vertex* v )
{
m_vertices.push_back( *v );
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
void OpenGLText::beginString()
{
m_indexOffset = 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
void OpenGLText::endString()
{
if(!m_vertices.empty())
{
bs.setStates();
// Note: we could also downsize the buffer if the size is really smaller than the one currently allocated
// this could be done after few iterations, if we see that the size if still smaller than the allocation...
if(m_vbosz < m_vertices.size())
{
#ifdef USE_PSEUDO_INSTANCING
glBindBuffer( GL_TEXTURE_BUFFER, m_vbo ); // ??!?!? otherewize it crashes
glBufferData(GL_TEXTURE_BUFFER, m_vertices.size()*sizeof(Vertex), &(m_vertices.front()), GL_STREAM_DRAW);
glBindBuffer( GL_TEXTURE_BUFFER, 0 );
#else
glBindBuffer( GL_ARRAY_BUFFER, m_vbo ); // ??!?!? otherewize it crashes
glNamedBufferData(m_vbo, m_vertices.size()*sizeof(Vertex), &(m_vertices.front()), GL_STREAM_DRAW);
glBindBuffer( GL_ARRAY_BUFFER, 0 );
#endif
m_vbosz = m_vertices.size();
}
//else glNamedBufferSubData(m_vbo, 0, m_vertices.size()*sizeof(Vertex), &(m_vertices.front()));
#ifndef USE_PSEUDO_INSTANCING
//
// Vertex attribute format definition
//
static Vertex* pVtxOffset = NULL;
glVertexAttribFormat(locPos , 4, GL_FLOAT, GL_FALSE, (GLuint)pVtxOffset->pos);
glVertexAttribBinding(locPos, 1);
#ifndef USE_FONT_METRIC_AS_TBO
glVertexAttribFormat(locTc , 4, GL_FLOAT, GL_FALSE, (GLuint)pVtxOffset->tc);
glVertexAttribBinding(locTc, 1);
#endif
#ifdef USE_INSTANCED_ARRAYS
glVertexAttribIFormat(locGlyph, 1, GL_INT, (GLuint)&pVtxOffset->iattr);
glVertexAttribBinding(locGlyph, 1);
//
// Divisor for instancing case
//
glVertexBindingDivisor(1, PRIMNUMBER);
#endif
//
// bind the VBO to the buffer binding #0
//
glBindVertexBuffer(1, m_vbo, 0, sizeof(Vertex));
//
// enable attributes to use
//
glEnableVertexAttribArray( locPos );
#ifndef USE_FONT_METRIC_AS_TBO
glEnableVertexAttribArray( locTc );
#endif
#ifdef USE_INSTANCED_ARRAYS
glEnableVertexAttribArray( locGlyph );
#endif
#else //USE_PSEUDO_INSTANCING
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_BUFFER, m_texQuads);
glTextureBuffer(m_texQuads, GL_RGBA32F, m_vbo);
#endif
#ifdef USE_FONT_METRIC_AS_TBO
//
// Textures/TBO for the fonts and metric
//
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_BUFFER, m_texGlyphTexOffset);
glTextureBuffer(m_texGlyphTexOffset, GL_RGBA32F, m_boGlyphTexOffset);
#endif
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fontTex);
glTextureBuffer(m_texQuads, GL_RGBA32F, m_vbo);
//
// activate the program
//
glUseProgram( m_widgetProgram );
glProgramUniform4f( m_widgetProgram, m_canvasVar, m_canvas.w, m_canvas.h, m_canvas.ratio, 0 );
//
// Draw
//
#ifdef USE_PSEUDO_INSTANCING
glDrawArrays( TOPOLOGY_PRIM, 0, m_vbosz * PRIMNUMBER);
#else
#ifdef USE_INSTANCED_ARRAYS
glDrawArraysInstanced( TOPOLOGY_PRIM, 0, PRIMNUMBER, m_vbosz*PRIMNUMBER);
#else
glDrawArrays( TOPOLOGY_PRIM, 0, m_vbosz);
#endif
#endif //#ifdef USE_PSEUDO_INSTANCING
glUseProgram( 0 );
#ifndef USE_PSEUDO_INSTANCING
// switch vertex attribs back off
glDisableVertexAttribArray( locPos );
#ifndef USE_FONT_METRIC_AS_TBO
glDisableVertexAttribArray( locTc );
#endif
#ifdef USE_INSTANCED_ARRAYS
glDisableVertexAttribArray( locGlyph );
#endif
#endif
GLenum err = glGetError();
//assert(err == 0);
m_vertices.resize(0);
}
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
void OpenGLText::stringSize(const char *text, float *sz)
{
if(!glyphInfos)
return;
int h = glyphInfos->pix.ascent + glyphInfos->pix.descent + glyphInfos->pix.linegap;
float lPosX = 0+1;
float lPosY = 0 ;
float lLinePosX = lPosX;
float lLinePosY = lPosY;
const char* c = text;
while (*c != '\0')
{
if ( *c == '\n')
{
lPosX = lLinePosX;
lLinePosY -= h;
lPosY = lLinePosY;
}
else if ( *c > 128 || *c < 0 )
{
}
else
{
GlyphInfo &g = glyphInfos->glyphs[*c];
//int pX = lPosX + g.pix.offX;
//int pY = lPosY - g.pix.height - g.pix.offY;
lPosX += g.pix.advance;
lPosY += 0;
}
c++;
}
sz[0] = lPosX;
sz[1] = lPosY + h;
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
float OpenGLText::drawString( int x, int y, const char * text, int nbLines, unsigned long color)
{
float color4f[4];
color4f[0] = (float)(color>>24 & 0xFF)/255.0;
color4f[1] = (float)(color>>16 & 0xFF)/255.0;
color4f[2] = (float)(color>>8 & 0xFF)/255.0;
color4f[3] = (float)(color & 0xFF)/255.0;
return drawString( x, y, text, nbLines, color4f);
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
float OpenGLText::drawString( int x, int y, const char * text, int nbLines, float * color4f)
{
if(!glyphInfos)
return 0;
int h = glyphInfos->pix.ascent + glyphInfos->pix.descent + glyphInfos->pix.linegap;
float usedHeight = 0;
float lPosX = x+1;
float lPosY = y ;
if (nbLines > 1)
{
lPosY += h * (nbLines - 1);
}
float lLinePosX = lPosX;
float lLinePosY = lPosY;
const char* c = text;
Vertex v;
glProgramUniform4fv(m_widgetProgram, m_color, 1, color4f);
m_vertexDepth = 1.0;
while (*c != '\0')
{
if ( *c == '\n')
{
lPosX = lLinePosX;
lLinePosY -= h;
usedHeight += h;
lPosY = lLinePosY;
}
else if ( *c > 128 || *c < 0 )
{
}
else
{
GlyphInfo &g = glyphInfos->glyphs[*c];
int pX = lPosX + g.pix.offX;
int pY = lPosY - g.pix.height - g.pix.offY;
#ifdef USE_INSTANCED_ARRAYS
v.setPos( pX , pY, g.pix.width, g.pix.height);
#ifndef USE_FONT_METRIC_AS_TBO
v.setTC ( g.norm.u, g.norm.v, g.norm.width, g.norm.height);
#endif
// to find back the Texture coords in atlas
v.iattr = (*c);
pushVertex(&v);
#else
#ifdef USE_QUADS
v.setPos( pX , pY, 0,1);
v.setTC ( g.norm.u, g.norm.v, 0,0);
pushVertex(&v);
v.setPos( pX + g.pix.width , pY, 0,1);
v.setTC ( g.norm.u+g.norm.width, g.norm.v, 0,0);
pushVertex(&v);
v.setPos( pX + g.pix.width, pY + g.pix.height, 0,1);
v.setTC ( g.norm.u+g.norm.width, g.norm.v+g.norm.height, 0,0);
pushVertex(&v);
v.setPos( pX , pY + g.pix.height, 0,1);
v.setTC ( g.norm.u, g.norm.v+g.norm.height, 0,0);
pushVertex(&v);
#else
v.setPos( pX , pY, 0,1);
v.setTC ( g.norm.u, g.norm.v, 0,0);
pushVertex(&v);
v.setPos( pX + g.pix.width , pY, 0,1);
v.setTC ( g.norm.u+g.norm.width, g.norm.v, 0,0);
pushVertex(&v);
v.setPos( pX , pY + g.pix.height, 0,1);
v.setTC ( g.norm.u, g.norm.v+g.norm.height, 0,0);
pushVertex(&v);
v.setPos( pX + g.pix.width , pY, 0,1);
v.setTC ( g.norm.u+g.norm.width, g.norm.v, 0,0);
pushVertex(&v);
v.setPos( pX + g.pix.width, pY + g.pix.height, 0,1);
v.setTC ( g.norm.u+g.norm.width, g.norm.v+g.norm.height, 0,0);
pushVertex(&v);
v.setPos( pX, pY + g.pix.height, 0,1);
v.setTC ( g.norm.u, g.norm.v+g.norm.height, 0,0);
pushVertex(&v);
#endif
#endif
lPosX += g.pix.advance;
lPosY += 0;
}
c++;
}
m_vertexDepth = 1.f;
return usedHeight;
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
void OpenGLText::BackupStates()
{
if(!bs.valid)
bs.backup();
}
void OpenGLText::RestoreStates()
{
bs.restore();
}