-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview18.cpp
325 lines (254 loc) · 8.47 KB
/
view18.cpp
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
#include "view18.h"
#include "global.h"
#include "util.h"
#include <ft2build.h>
#include FT_FREETYPE_H
GLuint view18_program = 0;
// TODO: the font miss something, weird
GLint view18_attribute_coord = -1, view18_uniform_tex = -1,
view18_uniform_color = -1;
GLuint view18_vbo = 0;
struct view18_point {
GLfloat x;
GLfloat y;
GLfloat s;
GLfloat t;
};
FT_Library view18_ft;
FT_Face view18_face;
// Maximum texture width
#define MAXWIDTH 1024
const char *view18_fontfilename = "data/FreeSans.ttf";
/**
* The atlas struct holds a texture that contains the visible US-ASCII characters
* of a certain font rendered with a certain character height.
* It also contains an array that contains all the information necessary to
* generate the appropriate vertex and texture coordinates for each character.
*
* After the constructor is run, you don't need to use any FreeType functions anymore.
*/
struct atlas {
GLuint tex; // texture object
int w; // width of texture in pixels
int h; // height of texture in pixels
struct {
float ax; // advance.x
float ay; // advance.y
float bw; // bitmap.width;
float bh; // bitmap.height;
float bl; // bitmap_left;
float bt; // bitmap_top;
float tx; // x offset of glyph in texture coordinates
float ty; // y offset of glyph in texture coordinates
} c[128]; // character information
atlas(FT_Face face, int height) {
FT_Set_Pixel_Sizes(face, 0, height);
FT_GlyphSlot g = face->glyph;
int roww = 0;
int rowh = 0;
w = 0;
h = 0;
memset(c, 0, sizeof(c));
/* Find minimum size for a texture holding all visible ASCII characters */
for (int i = 32; i < 128; i++) {
if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
fprintf(stderr, "Loading character %c failed!\n", i);
continue;
}
if (roww + g->bitmap.width + 1 >= MAXWIDTH) {
w = std::max(w, roww);
h += rowh;
roww = 0;
rowh = 0;
}
roww += g->bitmap.width + 1;
rowh = std::max(rowh, g->bitmap.rows);
}
w = std::max(w, roww);
h += rowh;
/* Create a texture that will be used to hold all ASCII glyphs */
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(view18_uniform_tex, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
GL_UNSIGNED_BYTE, 0);
/* We require 1 byte alignment when uploading texture data */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/* Clamping to edges is important to prevent artifacts when scaling */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
/* Linear filtering usually looks best for text */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Paste all glyph bitmaps into the texture, remembering the offset */
int ox = 0;
int oy = 0;
rowh = 0;
for (int i = 32; i < 128; i++) {
if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
fprintf(stderr, "Loading character %c failed!\n", i);
continue;
}
if (ox + g->bitmap.width + 1 >= MAXWIDTH) {
oy += rowh;
rowh = 0;
ox = 0;
}
glTexSubImage2D(GL_TEXTURE_2D, 0, ox, oy, g->bitmap.width,
g->bitmap.rows, GL_ALPHA, GL_UNSIGNED_BYTE,
g->bitmap.buffer);
c[i].ax = g->advance.x >> 6;
c[i].ay = g->advance.y >> 6;
c[i].bw = g->bitmap.width;
c[i].bh = g->bitmap.rows;
c[i].bl = g->bitmap_left;
c[i].bt = g->bitmap_top;
c[i].tx = ox / (float)w;
c[i].ty = oy / (float)h;
rowh = std::max(rowh, g->bitmap.rows);
ox += g->bitmap.width + 1;
}
// fprintf(stderr, "Generated a %d x %d (%d kb) texture atlas\n",
// w, h, w * h / 1024);
}
~atlas() {
glDeleteTextures(1, &tex);
}
};
atlas *view18_a48;
atlas *view18_a24;
atlas *view18_a12;
int view18_initResources()
{
glClearColor(1.0, 1.0, 1.0, 0);
/* Enable blending, necessary for our alpha texture */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* Initialize the FreeType2 library */
if (FT_Init_FreeType(&view18_ft)) {
fprintf(stderr, "Could not init freetype library\n");
return 1;
}
/* Load a font */
if (FT_New_Face(view18_ft, view18_fontfilename, 0, &view18_face)) {
fprintf(stderr, "Could not open font %s\n", view18_fontfilename);
return 1;
}
view18_program = create_program("glsl/text.18.v.glsl",
"glsl/text.18.f.glsl");
view18_attribute_coord = get_attrib(view18_program, "coord");
view18_uniform_tex = get_uniform(view18_program, "tex");
view18_uniform_color = get_uniform(view18_program, "color");
if(view18_attribute_coord == -1 || view18_uniform_tex == -1
|| view18_uniform_color == -1) {
return 0;
}
// Create the vertex buffer object
glGenBuffers(1, &view18_vbo);
/* Create texture atlasses for several font sizes */
view18_a48 = new atlas(view18_face, 48);
view18_a24 = new atlas(view18_face, 24);
view18_a12 = new atlas(view18_face, 12);
return 0;
}
/**
* Render text using the currently loaded font and currently set font size.
* Rendering starts at coordinates (x, y), z is always 0.
* The pixel coordinates that the FreeType2 library uses are scaled by (sx, sy).
*/
void view18_render_text(const char *text, atlas *a,
float x, float y,
float sx, float sy)
{
const uint8_t *p;
/* Use the texture containing the atlas */
glBindTexture(GL_TEXTURE_2D, a->tex);
glUniform1i(view18_uniform_tex, 0);
/* Set up the VBO for our vertex data */
glEnableVertexAttribArray(view18_attribute_coord);
glBindBuffer(GL_ARRAY_BUFFER, view18_vbo);
glVertexAttribPointer(view18_attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0);
view18_point coords[6 * strlen(text)];
int c = 0;
/* Loop through all characters */
for (p = (const uint8_t *)text; *p; p++)
{
/* Calculate the vertex and texture coordinates */
float x2 = x + a->c[*p].bl * sx;
float y2 = -y - a->c[*p].bt * sy;
float w = a->c[*p].bw * sx;
float h = a->c[*p].bh * sy;
/* Advance the cursor to the start of the next character */
x += a->c[*p].ax * sx;
y += a->c[*p].ay * sy;
/* Skip glyphs that have no pixels */
if (!w || !h) {
continue;
}
coords[c++] = (view18_point) {
x2, -y2, a->c[*p].tx, a->c[*p].ty};
coords[c++] = (view18_point) {
x2 + w, -y2, a->c[*p].tx + a->c[*p].bw / a->w, a->c[*p].ty};
coords[c++] = (view18_point) {
x2, -y2 - h, a->c[*p].tx, a->c[*p].ty + a->c[*p].bh / a->h};
coords[c++] = (view18_point) {
x2 + w, -y2, a->c[*p].tx + a->c[*p].bw / a->w, a->c[*p].ty};
coords[c++] = (view18_point) {
x2, -y2 - h, a->c[*p].tx, a->c[*p].ty + a->c[*p].bh / a->h};
coords[c++] = (view18_point) {
x2 + w, -y2 - h, a->c[*p].tx + a->c[*p].bw / a->w,
a->c[*p].ty + a->c[*p].bh / a->h};
}
// GLfloat blue[4] = {0, 0, 1, 1};
// glUniform4fv(view18_uniform_color, 1, blue);
/* Draw all the character on the screen in one go */
glBufferData(GL_ARRAY_BUFFER, sizeof(coords), coords, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, c);
glDisableVertexAttribArray(view18_attribute_coord);
}
void view18Display()
{
float sx = 2.0 / glutGet(GLUT_WINDOW_WIDTH);
float sy = 2.0 / glutGet(GLUT_WINDOW_HEIGHT);
glUseProgram(view18_program);
/* White background */
glClear(GL_COLOR_BUFFER_BIT);
GLfloat black[4] = {0, 0, 0, 1};
GLfloat red[4] = {1, 0, 0, 1};
GLfloat transparent_green[4] = {0, 1, 0, 0.5};
/* Set color to black */
glUniform4fv(view18_uniform_color, 1, transparent_green);
/* Effects of alignment */
view18_render_text("B", view18_a48, -1 + 8 * sx, 1 - 50 * sy, sx, sy);
// render_text("The Quick Brown Fox Jumps Over The Lazy Dog",
// -1 + 8 * sx, 1 - 50 * sy, sx, sy,
// view18_face, view18_uniform_tex, view18_attribute_coord,
// view18_vbo);
glutSwapBuffers();
}
void view18_freeResources()
{
glDisable(GL_BLEND);
glDeleteProgram(view18_program);
glDeleteBuffers(1, &view18_vbo);
}
void view18_entry(Window *window)
{
window->display = view18Display;
window->entry = viewEntry;
window->init = view18_initResources;
window->free = view18_freeResources;
}
#ifdef TEST_ALONE
int main(int argc, char *argv[])
{
Window window;
resetWindow(&window);
view18_entry(&window);
if (mini_initWindow(argc, argv, &window) == 0){
glutMainLoop();
}
return 0;
}
#endif /* TEST_ALONE */