-
Notifications
You must be signed in to change notification settings - Fork 273
/
main.cpp
446 lines (411 loc) · 14.2 KB
/
main.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
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
/******************************************************************************\
| OpenGL 4 Example Code. |
| Accompanies written series "Anton's OpenGL 4 Tutorials" |
| Email: anton at antongerdelan dot net |
| First version 5 Feb 2014 |
| Copyright Dr Anton Gerdelan, Trinity College Dublin, Ireland. |
| See individual libraries for respective legal notices |
|******************************************************************************|
| Bitmap Fonts example |
| Here I made a font atlas by hand in GIMP, and I've created a little function |
| called 'tweak_glyphs' that allows me to customise the spacing and height |
| offset for individual glyphs. Ideally I would save these values to a file |
| when I'd finished, so that I can re-use them later. |
| I render two strings of text here. I create an unique set of buffers and |
| VAO for each string of text, and I work out the position and texture coords |
| for each glyph within each string with a function called 'text_to_vbo' |
| which maps each character to texture coordinates within the altas texture, |
| and pulls out the corresponding glyph spacing offsets that we tweaked. |
| I also have a uniform vec4 which lets me customise the colour of the text, |
| which I can do easily because I coloured it in white in the image file. |
\******************************************************************************/
#include "maths_funcs.h"
#include "stb_image.h" // Sean Barrett's image loader
#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
// size of atlas. my handmade image is 16x16 glyphs
#define ATLAS_COLS 16
#define ATLAS_ROWS 16
int g_viewport_width = 800;
int g_viewport_height = 480;
GLuint sp; // shader programme
GLuint sp_text_colour_loc; // location of vec4 "text_colour" uniform
float glyph_y_offsets[256] = { 0.0f };
float glyph_widths[256] = { 0.0f };
/* After tweaking, save these values to a file */
void tweak_glyphs () {
/* lower down glyhs by some factor */
for (int i = 0; i < 255; i++) {
/* default lower-case to half-size spacing */
if (i >= 'a' && i <= 'z') {
glyph_y_offsets[i] = 0.6f;
} else if (i >= 'A' && i <= 'Z') {
glyph_y_offsets[i] = 0.2f;
}
}
glyph_y_offsets['b'] = 0.4f;
glyph_y_offsets['d'] = 0.4f;
glyph_y_offsets['f'] = 0.4f;
glyph_y_offsets['g'] = 0.5f;
glyph_y_offsets['h'] = 0.3f;
glyph_y_offsets['i'] = 0.5f;
glyph_y_offsets['j'] = 0.4f;
glyph_y_offsets['k'] = 0.3f;
glyph_y_offsets['l'] = 0.3f;
glyph_y_offsets['p'] = 0.5f;
glyph_y_offsets['q'] = 0.5f;
glyph_y_offsets['s'] = 0.4f;
glyph_y_offsets['t'] = 0.3f;
glyph_y_offsets['!'] = 0.1f;
glyph_y_offsets[','] = 1.0f;
/* reduce spacing after glyph */
for (int i = 0; i < 255; i++) {
/* default lower-case to half-size spacing */
if (i >= 'a' && i <= 'z') {
glyph_widths[i] = 0.5f;
} else if (i >= 'A' && i <= 'Z') {
glyph_widths[i] = 0.7f;
} else {
glyph_widths[i] = 1.0f;
}
}
glyph_widths[' '] = 0.4f;
glyph_widths['!'] = 0.3f;
glyph_widths[','] = 0.5f;
glyph_widths['A'] = 0.8f;
glyph_widths['B'] = 0.6f;
glyph_widths['D'] = 0.6f;
glyph_widths['F'] = 0.6f;
glyph_widths['L'] = 0.8f;
glyph_widths['M'] = 1.0f;
glyph_widths['N'] = 0.8f;
glyph_widths['P'] = 0.6f;
glyph_widths['R'] = 0.6f;
glyph_widths['S'] = 0.85f;
glyph_widths['T'] = 0.9f;
glyph_widths['U'] = 0.8f;
glyph_widths['V'] = 0.8f;
glyph_widths['W'] = 1.0f;
glyph_widths['e'] = 0.45f;
glyph_widths['f'] = 0.4f;
glyph_widths['g'] = 0.45f;
glyph_widths['h'] = 0.45f;
glyph_widths['i'] = 0.25f;
glyph_widths['j'] = 0.3f;
glyph_widths['k'] = 0.45f;
glyph_widths['l'] = 0.25f;
glyph_widths['p'] = 0.45f;
glyph_widths['q'] = 0.45f;
glyph_widths['r'] = 0.45f;
glyph_widths['t'] = 0.4f;
glyph_widths['u'] = 0.45f;
glyph_widths['v'] = 0.4f;
glyph_widths['w'] = 0.7f;
glyph_widths['y'] = 0.4f;
}
void text_to_vbo (
const char* str,
float at_x,
float at_y,
float scale_px,
GLuint* points_vbo,
GLuint* texcoords_vbo,
int* point_count
) {
int len = strlen (str);
float* points_tmp = (float*)malloc (sizeof (float) * len * 12);
float* texcoords_tmp = (float*)malloc (sizeof (float) * len * 12);
for (int i = 0; i < len; i++) {
// get ascii code as integer
int ascii_code = str[i];
// work out row and column in atlas
int atlas_col = (ascii_code - ' ') % ATLAS_COLS;
int atlas_row = (ascii_code - ' ') / ATLAS_COLS;
// work out texture coordinates in atlas
float s = atlas_col * (1.0 / ATLAS_COLS);
float t = (atlas_row + 1) * (1.0 / ATLAS_ROWS);
// work out position of glyphtriangle_width
float x_pos = at_x;
float y_pos = at_y - scale_px / g_viewport_height *
glyph_y_offsets[ascii_code];
// move next glyph along to the end of this one
if (i + 1 < len) {
// upper-case letters move twice as far
at_x += glyph_widths[ascii_code] * scale_px / g_viewport_width;
}
// add 6 points and texture coordinates to buffers for each glyph
points_tmp[i * 12] = x_pos;
points_tmp[i * 12 + 1] = y_pos;
points_tmp[i * 12 + 2] = x_pos;
points_tmp[i * 12 + 3] = y_pos - scale_px / g_viewport_height;
points_tmp[i * 12 + 4] = x_pos + scale_px / g_viewport_width;
points_tmp[i * 12 + 5] = y_pos - scale_px / g_viewport_height;
points_tmp[i * 12 + 6] = x_pos + scale_px / g_viewport_width;
points_tmp[i * 12 + 7] = y_pos - scale_px / g_viewport_height;
points_tmp[i * 12 + 8] = x_pos + scale_px / g_viewport_width;
points_tmp[i * 12 + 9] = y_pos;
points_tmp[i * 12 + 10] = x_pos;
points_tmp[i * 12 + 11] = y_pos;
texcoords_tmp[i * 12] = s;
texcoords_tmp[i * 12 + 1] = 1.0 - t + 1.0 / ATLAS_ROWS;
texcoords_tmp[i * 12 + 2] = s;
texcoords_tmp[i * 12 + 3] = 1.0 - t;
texcoords_tmp[i * 12 + 4] = s + 1.0 / ATLAS_COLS;
texcoords_tmp[i * 12 + 5] = 1.0 - t;
texcoords_tmp[i * 12 + 6] = s + 1.0 / ATLAS_COLS;
texcoords_tmp[i * 12 + 7] = 1.0 - t;
texcoords_tmp[i * 12 + 8] = s + 1.0 / ATLAS_COLS;
texcoords_tmp[i * 12 + 9] = 1.0 - t + 1.0 / ATLAS_ROWS;
texcoords_tmp[i * 12 + 10] = s;
texcoords_tmp[i * 12 + 11] = 1.0 - t + 1.0 / ATLAS_ROWS;
}
glBindBuffer (GL_ARRAY_BUFFER, *points_vbo);
glBufferData (
GL_ARRAY_BUFFER,
len * 12 * sizeof (float),
points_tmp,
GL_DYNAMIC_DRAW
);
glBindBuffer (GL_ARRAY_BUFFER, *texcoords_vbo);
glBufferData (
GL_ARRAY_BUFFER,
len * 12 * sizeof (float),
texcoords_tmp,
GL_DYNAMIC_DRAW
);
free (points_tmp);
free (texcoords_tmp);
*point_count = len * 6;
}
void create_shaders () {
/* here i used negative y from the buffer as the z value so that it was on
the floor but also that the 'front' was on the top side. also note how i
work out the texture coordinates, st, from the vertex point position */
const char* vs_str =
"#version 410\n"
"layout (location = 0) in vec2 vp;"
"layout (location = 1) in vec2 vt;"
"out vec2 st;"
"void main () {"
" st = vt;"
" gl_Position = vec4 (vp, 0.0, 1.0);"
"}";
const char* fs_str =
"#version 410\n"
"in vec2 st;"
"uniform sampler2D tex;"
"uniform vec4 text_colour;"
"out vec4 frag_colour;"
"void main () {"
" frag_colour = texture (tex, st) * text_colour;"
"}";
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vs_str, NULL);
glCompileShader (vs);
int params = -1;
glGetShaderiv (vs, GL_COMPILE_STATUS, ¶ms);
if (GL_TRUE != params) {
fprintf (stderr, "ERROR: GL shader index %i did not compile\n", vs);
}
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fs_str, NULL);
glCompileShader (fs);
glGetShaderiv (fs, GL_COMPILE_STATUS, ¶ms);
if (GL_TRUE != params) {
fprintf (stderr, "ERROR: GL shader index %i did not compile\n", fs);
}
sp = glCreateProgram ();
glAttachShader (sp, vs);
glAttachShader (sp, fs);
glLinkProgram (sp);
glGetProgramiv (sp, GL_LINK_STATUS, ¶ms);
if (GL_TRUE != params) {
fprintf (
stderr,
"ERROR: could not link shader programme GL index %i\n",
sp
);
}
sp_text_colour_loc = glGetUniformLocation (sp, "text_colour");
}
bool load_texture (const char* file_name, GLuint* tex) {
int x, y, n;
int force_channels = 4;
unsigned char* image_data = stbi_load (file_name, &x, &y, &n, force_channels);
if (!image_data) {
fprintf (stderr, "ERROR: could not load %s\n", file_name);
return false;
}
// NPOT check
if ((x & (x - 1)) != 0 || (y & (y - 1)) != 0) {
fprintf (
stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name
);
}
int width_in_bytes = x * 4;
unsigned char *top = NULL;
unsigned char *bottom = NULL;
unsigned char temp = 0;
int half_height = y / 2;
for (int row = 0; row < half_height; row++) {
top = image_data + row * width_in_bytes;
bottom = image_data + (y - row - 1) * width_in_bytes;
for (int col = 0; col < width_in_bytes; col++) {
temp = *top;
*top = *bottom;
*bottom = temp;
top++;
bottom++;
}
}
glGenTextures (1, tex);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, *tex);
glTexImage2D (
GL_TEXTURE_2D,
0,
GL_RGBA,
x,
y,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image_data
);
glGenerateMipmap (GL_TEXTURE_2D);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
GLfloat max_aniso = 0.0f;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
// set the maximum!
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_aniso);
return true;
}
/* we will tell GLFW to run this function whenever the window is resized */
void glfw_window_size_callback (GLFWwindow* window, int width, int height) {
g_viewport_width = width;
g_viewport_height = height;
/* update any perspective matrices used here */
}
int main () {
// start GL context with helper libraries
assert (glfwInit ());
/* We must specify 3.2 core if on Apple OS X -- other O/S can specify
anything here. I defined 'APPLE' in the makefile for OS X */
#ifdef APPLE
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
GLFWwindow* window = glfwCreateWindow (
g_viewport_width, g_viewport_height, "Bitmap Fonts", NULL, NULL);
glfwSetWindowSizeCallback (window, glfw_window_size_callback);
glfwMakeContextCurrent (window);
glewExperimental = GL_TRUE;
glewInit ();
const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
const GLubyte* version = glGetString (GL_VERSION); // version as a string
printf ("Renderer: %s\n", renderer);
printf ("OpenGL version supported %s\n", version);
/* load font meta-data (spacings for each glyph) */
tweak_glyphs ();
/* set a string of text for lower-case letters */
GLuint first_string_vp_vbo, first_string_vt_vbo, first_string_vao;
glGenBuffers (1, &first_string_vp_vbo);
glGenBuffers (1, &first_string_vt_vbo);
float x_pos = -0.75f;
float y_pos = 0.2f;
float pixel_scale = 64.0f;
const char first_str[] = "abcdefghijklmnopqrstuvwxyz";
int first_string_points = 0;
text_to_vbo (
first_str,
x_pos,
y_pos,
pixel_scale,
&first_string_vp_vbo,
&first_string_vt_vbo,
&first_string_points
);
glGenVertexArrays (1, &first_string_vao);
glBindVertexArray (first_string_vao);
glBindBuffer (GL_ARRAY_BUFFER, first_string_vp_vbo);
glVertexAttribPointer (0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, first_string_vt_vbo);
glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray (1);
/* second string of text for capital letters */
GLuint second_string_vp_vbo, second_string_vt_vbo, second_string_vao;
glGenBuffers (1, &second_string_vp_vbo);
glGenBuffers (1, &second_string_vt_vbo);
x_pos = -1.0f;
y_pos = 1.0f;
pixel_scale = 64.0f;
const char second_str[] = "The human torch was denied a bank loan!";
int second_string_points = 0;
text_to_vbo (
second_str,
x_pos,
y_pos,
pixel_scale,
&second_string_vp_vbo,
&second_string_vt_vbo,
&second_string_points
);
glGenVertexArrays (1, &second_string_vao);
glBindVertexArray (second_string_vao);
glBindBuffer (GL_ARRAY_BUFFER, second_string_vp_vbo);
glVertexAttribPointer (0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, second_string_vt_vbo);
glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray (1);
create_shaders ();
// textures
GLuint tex;
assert (load_texture ("handmade2.png", &tex));
// rendering defaults
//glDepthFunc (GL_LESS); // set depth function
//glEnable (GL_DEPTH_TEST);
glCullFace (GL_BACK); // cull back face
glFrontFace (GL_CCW); // GL_CCW for counter clock-wise
glEnable (GL_CULL_FACE); // cull face
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // partial transparency
glClearColor (0.2, 0.2, 0.6, 1.0);
glViewport (0, 0, g_viewport_width, g_viewport_height);
// start main rendering loop
while (!glfwWindowShouldClose (window)) {
// wipe the drawing surface clear
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, tex);
glUseProgram (sp);
/* Draw text with no depth test and alpha blending */
glDisable (GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBindVertexArray (first_string_vao);
glUniform4f (sp_text_colour_loc, 1.0, 0.0, 1.0, 1.0);
glDrawArrays (GL_TRIANGLES, 0, first_string_points);
glBindVertexArray (second_string_vao);
glUniform4f (sp_text_colour_loc, 1.0, 1.0, 0.0, 1.0);
glDrawArrays (GL_TRIANGLES, 0, second_string_points);
// update other events like input handling
glfwPollEvents ();
if (GLFW_PRESS == glfwGetKey (window, GLFW_KEY_ESCAPE)) {
glfwSetWindowShouldClose (window, 1);
}
glfwSwapBuffers (window);
}
// done
glfwTerminate();
return 0;
}