-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathOpenGLText.h
More file actions
174 lines (161 loc) · 5.11 KB
/
OpenGLText.h
File metadata and controls
174 lines (161 loc) · 5.11 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
/*
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
*/
#ifndef OpenGLText_H__
#define OpenGLText_H__
#include <vector>
class OpenGLText
{
public:
//
// Header and structure for what is in the .bin file of the font
//
struct GlyphInfo
{
struct Pix // pixel oriented data
{
int u, v;
int width, height;
int advance;
int offX, offY;
};
struct Norm // normalized data
{
float u, v; // position in the map in normalized coords
float width, height;
float advance;
float offX, offY;
};
Pix pix;
Norm norm;
};
struct FileHeader
{
int texwidth, texheight;
struct Pix
{
int ascent;
int descent;
int linegap;
};
struct Norm
{
float ascent;
float descent;
float linegap;
};
Pix pix;
Norm norm;
GlyphInfo glyphs[256];
};
OpenGLText();
~OpenGLText();
static void BackupStates();
static void RestoreStates();
void beginString();
void endString();
void stringSize(const char *text, float *sz);
float drawString( int x, int y, const char * text, int nbLines, unsigned long color);
float drawString( int x, int y, const char * text, int nbLines, float * color4f);
bool init(const char * fontName, int w, int h);
bool init(unsigned char *imageData, FileHeader *glyphInfos, int w, int h);
void changeCanvas(int w, int h);
void changeSize(int w, int h);
private:
bool init(int w, int h);
static char* cWidgetVSSource2;
static char* cWidgetFSSource2;
unsigned int c_fontNbChars;
unsigned int c_fontHeight;
unsigned int c_fontWidth;
unsigned int m_widgetProgram;
unsigned int m_vShader;
unsigned int m_fShader;
unsigned int m_canvasVar;
unsigned int m_color;
unsigned int m_depthNFRSVar;
unsigned int m_fontTex;
float m_vertexDepth;
int m_indexOffset;
unsigned int m_vbo;
unsigned int m_vbosz;
#ifdef USE_FONT_METRIC_AS_TBO
unsigned int m_GlyphTexOffset;
unsigned int m_boGlyphTexOffset;
unsigned int m_texGlyphTexOffset;
unsigned int m_locGlyphTexOffset;
#else
unsigned int locTc;
#endif
#ifdef USE_PSEUDO_INSTANCING
unsigned int m_locQuads;
unsigned int m_texQuads;
#else
unsigned int locPos;
unsigned int locGlyph;
#endif
struct TCanvas
{
float w,h;
float winw,winh;
float ratio;
};
TCanvas m_canvas;
struct Vertex
{
float pos[4];
#ifndef USE_FONT_METRIC_AS_TBO
float tc[4];
#endif
#ifdef USE_PSEUDO_INSTANCING
float iattr;
float dummy[3]; // to align with the fact we do 2 texelFetch over vec4
#else
int iattr;
#endif
Vertex()
{ memset(this, 0, sizeof(Vertex)); }
void setPos( float fx, float fy, float fz, float fw )
{
pos[0] = fx; pos[1] = fy; pos[2] = fz; pos[3] = fw;
}
#ifndef USE_FONT_METRIC_AS_TBO
void setTC( float fx, float fy, float fz, float fw )
{
tc[0] = fx; tc[1] = fy; tc[2] = fz; tc[3] = fw;
}
#endif
};
std::vector< Vertex > m_vertices;
FileHeader *glyphInfos;
bool allocated;
GLuint CompileGLSLShader( GLenum target, const char* shader);
GLuint LinkGLSLProgram( GLuint vertexShader, GLuint fragmentShader);
void pushVertex( Vertex* v );
};
#endif