-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjunk.cpp
More file actions
557 lines (433 loc) · 15.6 KB
/
Copy pathjunk.cpp
File metadata and controls
557 lines (433 loc) · 15.6 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
/*
// Clear the previous map (if any)
// Clear the previous map (if any)
ch_atlas.clear();
// set the font size
FT_Set_Pixel_Sizes(face, 0, 48);
// initialize variables to keep track of texture atlas size
int atlas_width = 0;
int atlas_height = 0;
// loop through the characters and load their glyphs
for (unsigned char c = 0; c < 128; c++)
{
// load glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// update atlas size variables
atlas_width += face->glyph->bitmap.width;
atlas_height = std::max(atlas_height, static_cast<int>(face->glyph->bitmap.rows));
}
// generate texture for the atlas
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
atlas_width,
atlas_height,
0,
GL_RED,
GL_UNSIGNED_BYTE,
nullptr
);
// set texture options
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// initialize x position for next glyph
int x = 0;
// loop through the characters again and add their glyphs to the atlas
for (unsigned char c = 0; c < 128; c++)
{
// load glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// add glyph to texture atlas
glTexSubImage2D(
GL_TEXTURE_2D,
0,
x,
0,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// store glyph information in character map
Character character = {
textureID,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
ch_atlas.insert(std::pair<char, Character>(c, character));
// update x position for next glyph
x += face->glyph->bitmap.width;
}
*/
/*
// Clear the previous map (if any)
ch_atlas.clear();
// set the font size
FT_Set_Pixel_Sizes(face, 0, 48);
// initialize variables to keep track of texture atlas size
int atlas_width = 0;
int atlas_height = 0;
// loop through the characters and load their glyphs
for (unsigned char c = 0; c < 128; c++)
{
// load glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// generate texture for glyph
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// set texture options
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// update atlas size variables
atlas_width += face->glyph->bitmap.width;
atlas_height = std::max(atlas_height, static_cast<int>(face->glyph->bitmap.rows));
// store character for later use
Character character = {
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
(unsigned int)face->glyph->advance.x,
glm::vec2(0),
glm::vec2(0),
glm::vec2(0),
glm::vec2(0)
};
ch_atlas.insert(std::pair<char, Character>(c, character));
}
// Update the texture dimensions
TextureWidth = atlas_width;
TextureHeight = atlas_height;
// compute texture coordinates for each character
float x = 0;
float y = 0;
for (auto& ch : ch_atlas)
{
Character& character = ch.second;
// compute glyph texture coordinates
character.top_left.x = x / atlas_width;
character.top_left.y = y / atlas_height;
character.bot_right.x = (x + character.Size.x) / atlas_width;
character.bot_right.y = (y + character.Size.y) / atlas_height;
// update x position for next glyph
x += character.Size.x;
}
// compute normalized texture coordinates for whitespace character
Character& space_char = ch_atlas[' '];
space_char.top_left.x = 0.0f;
space_char.top_left.y = 0.0f;
space_char.bot_right.x = space_char.Size.x / atlas_width;
space_char.bot_right.y = space_char.Size.y / atlas_height;
// compute normalized texture coordinates for newline character
Character& newline_char = ch_atlas['\n'];
newline_char.top_left.x = 0.0f;
newline_char.top_left.y = 0.0f;
newline_char.bot_right.x = 0.0f;
newline_char.bot_right.y = 0.0f;
*/
/*
// set size to load glyphs as
FT_Set_Pixel_Sizes(face, 0, 48);
// disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// generate texture atlas
// GLuint texture;
glGenTextures(1, &label_textureID);
glBindTexture(GL_TEXTURE_2D, label_textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 1024, 1024, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
// set texture options
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// initialize atlas variables
int x = 0;
int y = 0;
int max_height = 0;
// loop through all printable ASCII characters
for (unsigned char c = 32; c < 128; c++)
{
// load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
continue;
}
// store character data in struct
Character character =
{
0,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x,
glm::vec4(x, y + face->glyph->bitmap.rows - face->glyph->bitmap_top,
face->glyph->bitmap.width,
-1.0f*face->glyph->bitmap.rows)
};
// add character to map
ch_characters.insert(std::pair<char, Character>(c, character));
// update atlas variables
x += face->glyph->bitmap.width + 1;
max_height = std::max(max_height, int(face->glyph->bitmap.rows));
if (x >= 1024)
{
x = 0;
y += max_height + 1;
max_height = 0;
}
// update texture with glyph data
glTexSubImage2D(GL_TEXTURE_2D,
0,
character.TextureCoords.x,
character.TextureCoords.y,
character.TextureCoords.z,
character.TextureCoords.w,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer);
}
*/
/*
label_store::label_store()
:label_char_list(), label_loc(0), text_color(0), text_angle(0.0f), font_size(0.0f), char_count(0)
{
// Constructor
// Stores individual line
}
label_store::~label_store()
{
}
void label_store::add_label(const char* label, glm::vec2 label_loc, float text_angle, float font_size)
{
// Add the label
unsigned int char_location = 0;
for (const char* c = label; *c != '\0'; c++)
{
//Character ch;
//ch.this_char = *c; // store the character
//ch.texture_id =0; // Set texture id 0
//ch.char_location = char_location;
label_char_list.insert(std::make_pair(char_location, *c));
// Iterate the location
char_location++;
}
this->char_count = char_location;
this->label_loc = label_loc;
this->text_angle = text_angle;
this->font_size = font_size;
}
void label_store::get_buffer(float* label_vertices, unsigned int* label_indices,
unsigned int& label_vertices_count, unsigned int& label_indices_count, std::map<char, Character>& char_atlas)
{
float x_val = label_loc.x;
float y_val = label_loc.y;
// return the buffer of each character (<> font size = 0.04f, <> font gap = 0.01f)
for (auto& char_s : label_char_list)
{
Character ch = char_atlas[char_s.second];
font_size = 0.002f;
float xpos = x_val + (ch.Bearing.x * font_size);
float ypos = y_val - (ch.Size.y - ch.Bearing.y) * font_size;
float w = ch.Size.x * font_size;
float h = ch.Size.y * font_size;
glm::vec4 texture_scaled = ch.TextureCoords / font_size;
// X Y coordinates
// Point 1
// Vertices [0,0] // 0th point
label_vertices[label_vertices_count + 0] = xpos;
label_vertices[label_vertices_count + 1] = ypos + h;
// Label origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = texture_scaled.x;
label_vertices[label_vertices_count + 5] = texture_scaled.y;
// Iterate
label_vertices_count = label_vertices_count + 6;
//__________________________________________________________________________________________
// Point 2
// Vertices [0,1] // 1th point
label_vertices[label_vertices_count + 0] = xpos;
label_vertices[label_vertices_count + 1] = ypos;
// Font origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = texture_scaled.x;
label_vertices[label_vertices_count + 5] = texture_scaled.y + texture_scaled.w;
// Iterate
label_vertices_count = label_vertices_count + 6;
//__________________________________________________________________________________________
// Point 3
// Vertices [1,1] // 3th point
label_vertices[label_vertices_count + 0] = xpos + w;
label_vertices[label_vertices_count + 1] = ypos;
// Font origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = texture_scaled.x + texture_scaled.z;
label_vertices[label_vertices_count + 5] = texture_scaled.y + texture_scaled.w;
// Iterate
label_vertices_count = label_vertices_count + 6;
//__________________________________________________________________________________________
// Point 4
// Vertices [1,0] // 4th point
label_vertices[label_vertices_count + 0] = xpos + w;
label_vertices[label_vertices_count + 1] = ypos + h;
// Font origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = texture_scaled.x + texture_scaled.z;
label_vertices[label_vertices_count + 5] = texture_scaled.y;
// Iterate
label_vertices_count = label_vertices_count + 6;
x_val += (ch.Advance >> 6) * font_size;
//__________________________________________________________________________________________
// Fix the index buffers
// Set the node indices
unsigned int t_id = ((label_indices_count / 6) * 4);
// Triangle 0,1,2
label_indices[label_indices_count + 0] = t_id + 0;
label_indices[label_indices_count + 1] = t_id + 1;
label_indices[label_indices_count + 2] = t_id + 2;
// Triangle 2,3,0
label_indices[label_indices_count + 3] = t_id + 2;
label_indices[label_indices_count + 4] = t_id + 3;
label_indices[label_indices_count + 5] = t_id + 0;
// Increment
label_indices_count = label_indices_count + 6;
}
}
void label_store::get_buffer1(float* label_vertices, unsigned int* label_indices,
unsigned int& label_vertices_count, unsigned int& label_indices_count, std::map<char, Character>& char_atlas)
{
//const float char_gap = font_size * 0.25f;
//glm::vec2 temp_pt;
//glm::vec2 char_pt;
float x_val = label_loc.x;
float y_val = label_loc.y;
// return the buffer of each character (<> font size = 0.04f, <> font gap = 0.01f)
for (auto& char_s : label_char_list)
{
Character ch = char_atlas[char_s.second];
font_size =0.002f;
float xpos = x_val +(ch.Bearing.x * font_size);
float ypos = y_val -(ch.Size.y - ch.Bearing.y) * font_size;
float w = ch.Size.x *font_size;
float h = ch.Size.y *font_size;
// X Y coordinates
// Point 1
// Vertices [0,0] // 0th point
label_vertices[label_vertices_count + 0] = xpos;
label_vertices[label_vertices_count + 1] = ypos+h;
// Label origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = 0;
label_vertices[label_vertices_count + 5] = 0;
// Iterate
label_vertices_count = label_vertices_count + 6;
//__________________________________________________________________________________________
// Point 2
// Vertices [0,1] // 1th point
label_vertices[label_vertices_count + 0] = xpos;
label_vertices[label_vertices_count + 1] = ypos;
// Font origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = 0;
label_vertices[label_vertices_count + 5] = 1;
// Iterate
label_vertices_count = label_vertices_count + 6;
//__________________________________________________________________________________________
// Point 3
// Vertices [1,1] // 3th point
label_vertices[label_vertices_count + 0] = xpos+w;
label_vertices[label_vertices_count + 1] = ypos;
// Font origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = 1;
label_vertices[label_vertices_count + 5] = 1;
// Iterate
label_vertices_count = label_vertices_count + 6;
//__________________________________________________________________________________________
// Point 4
// Vertices [1,0] // 4th point
label_vertices[label_vertices_count + 0] = xpos+w;
label_vertices[label_vertices_count + 1] = ypos+h;
// Font origin
label_vertices[label_vertices_count + 2] = label_loc.x;
label_vertices[label_vertices_count + 3] = label_loc.y;
// Texture Glyph coordinate
label_vertices[label_vertices_count + 4] = 1;
label_vertices[label_vertices_count + 5] = 0;
// Iterate
label_vertices_count = label_vertices_count + 6;
x_val += (ch.Advance >> 6) * font_size;
//__________________________________________________________________________________________
// Fix the index buffers
// Set the node indices
unsigned int t_id = ((label_indices_count / 6) * 4);
// Triangle 0,1,2
label_indices[label_indices_count + 0] = t_id + 0;
label_indices[label_indices_count + 1] = t_id + 1;
label_indices[label_indices_count + 2] = t_id + 2;
// Triangle 2,3,0
label_indices[label_indices_count + 3] = t_id + 2;
label_indices[label_indices_count + 4] = t_id + 3;
label_indices[label_indices_count + 5] = t_id + 0;
// Increment
label_indices_count = label_indices_count + 6;
}
}
glm::vec2 label_store::rotate_pt(glm::vec2& rotate_about, glm::vec2 pt, float& rotation_angle)
{
// Return the rotation point
glm::vec2 translated_pt = pt - rotate_about;
// Apply rotation
float radians = (rotation_angle);// * 3.14159365f) / 180.0f; // convert degrees to radians
float cos_theta = cos(radians);
float sin_theta = sin(radians);
// Rotated point of the corners
glm::vec2 rotated_pt = glm::vec2((translated_pt.x * cos_theta) - (translated_pt.y * sin_theta),
(translated_pt.x * sin_theta) + (translated_pt.y * cos_theta));
return (rotated_pt + rotate_about);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/