-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.cpp
More file actions
645 lines (586 loc) · 22.1 KB
/
graphics.cpp
File metadata and controls
645 lines (586 loc) · 22.1 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
#define _GNU_SOURCE
#define GL_GLEXT_PROTOTYPES
#include <string>
#include <errno.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include "common.hpp"
#include "input.hpp"
#include "linmath.h"
#define ZOOM_SENSITIVITY 1.2
static int win_width = 1024; // actual size of the client area
static int win_height = 1024;
///////////////////////////////////////////////////////////////////////////////
// ============================== Text rendering ==============================
static GLuint text_shader = GL_INVALID_VALUE;
static GLint text_char_pos_attrib;
static GLint text_texture_uniform;
static GLint text_color_uniform;
static GLint text_projection_uniform;
static GLint text_pos_uniform;
static mat4x4 text_projection;
static GLuint text_vbo = GL_INVALID_VALUE;
static FT_Library freetype;
static char* text_buff = NULL;
static size_t text_buff_length = 1024;
enum align
{
align_right = 0x1,
align_bottom = 0x2,
align_top_left = 0x0,
align_top_right = align_right,
align_bottom_left = align_bottom,
align_bottom_right = align_right | align_bottom,
};
struct font_point
{
GLfloat x;
GLfloat y;
GLfloat s;
GLfloat t;
};
static struct font
{
int size; // font size
int height; // distance between baselines
GLuint texture; // texture object
int texture_width; // width of texture in pixels
int texture_height; // height of texture in pixels
struct c
{
int x; // bitmap.left;
int y; // bitmap.top;
int w; // bitmap.width;
int h; // bitmap.height;
int dx; // advance.x
int dy; // advance.y
int tx; // x offset of the glyph in the texture
int ty; // y offset of the glyph in the texture
} chars[128]; // characters
} *font = NULL;
// Inspired by https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_02
static struct font* new_font(const char* font_path, int size)
{
const int texture_max_width = 1024;
struct font* font = (struct font*)calloc(1, sizeof(struct font));
FT_Face face;
if (FT_New_Face(freetype, font_path, 0, &face)) {
fprintf(stderr, "Cannot open font '%s'\n", font_path);
free(font);
return NULL;
}
font->size = size;
font->height = size * face->height / face->units_per_EM;
FT_Set_Pixel_Sizes(face, 0, size);
FT_GlyphSlot g = face->glyph;
int roww = 0;
int rowh = 0;
font->texture_width = 0;
font->texture_height = 0;
for (int i = 32; i <= 126; i++) { // all characters from space to tilde
if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
fprintf(stderr, "Font '%s': cannot load character '%c'\n", font_path, i);
continue;
}
if (roww + g->bitmap.width + 1 >= texture_max_width) {
if (roww > font->texture_width)
font->texture_width = roww;
font->texture_height += rowh;
roww = 0;
rowh = 0;
}
roww += g->bitmap.width + 1;
if (g->bitmap.rows > rowh)
rowh = g->bitmap.rows;
}
if (roww > font->texture_width)
font->texture_width = roww;
font->texture_height += rowh;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &font->texture);
glBindTexture(GL_TEXTURE_2D, font->texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, font->texture_width, font->texture_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
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, "Font '%s': cannot load character '%c'\n", font_path, i);
continue;
}
if (ox + g->bitmap.width + 1 >= texture_max_width) {
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);
font->chars[i].dx = g->advance.x >> 6;
font->chars[i].dy = g->advance.y >> 6;
font->chars[i].w = g->bitmap.width;
font->chars[i].h = g->bitmap.rows;
font->chars[i].x = g->bitmap_left;
font->chars[i].y = g->bitmap_top;
font->chars[i].tx = ox;
font->chars[i].ty = oy;
if (g->bitmap.rows > rowh)
rowh = g->bitmap.rows;
ox += g->bitmap.width + 1;
}
FT_Done_Face(face);
return font;
}
static void draw_text(struct font* font, int x, int y, enum align align, const char* format, ...)
{
int length;
va_list argptr;
do {
va_start(argptr, format);
length = vsnprintf(text_buff, text_buff_length, format, argptr);
va_end(argptr);
if (length >= text_buff_length) {
text_buff_length = 2 * length;
text_buff = (char*)realloc(text_buff, text_buff_length);
continue;
}
} while (false);
if (length < 0)
return;
vec2 text_pos = { x, win_height - y - font->height };
y = 0;
struct font_point coords[6*length];
struct font_point* coord = coords;
unsigned char *p = (unsigned char*)text_buff;
while (*p) {
int line_length = 0;
x = 0;
while (*p != '\0' && *p != '\n') {
font::c* c = &font->chars[*p];
if (c->w && c->h) {
int left = x + c->x;
int right = x + c->x + c->w;
int top = y + c->y - c->h;
int bottom = y + c->y;
float tex_left = (float)c->tx / font->texture_width;
float tex_right = (float)(c->tx + c->w) / font->texture_width;
float tex_top = (float)(c->ty + c->h) / font->texture_height;
float tex_bottom = (float)c->ty / font->texture_height;
*(coord++) = (struct font_point){ left, bottom, tex_left, tex_bottom };
*(coord++) = (struct font_point){ right, bottom, tex_right, tex_bottom };
*(coord++) = (struct font_point){ left, top, tex_left, tex_top };
*(coord++) = (struct font_point){ right, bottom, tex_right, tex_bottom };
*(coord++) = (struct font_point){ left, top, tex_left, tex_top };
*(coord++) = (struct font_point){ right, top, tex_right, tex_top };
line_length++;
}
x += c->dx;
p++;
}
if (align & align_right)
for (int i = 1; i <= 6*line_length; i++)
(coord-i)->x -= x;
y -= font->height;
if (*p == '\n')
p++;
}
if (align & align_bottom)
text_pos[1] -= y; // y is negative
int n = coord - coords; // total number of printable characters
glBindTexture(GL_TEXTURE_2D, font->texture);
glUseProgram(text_shader);
glUniform1i(text_texture_uniform, 0);
glUniform2fv(text_pos_uniform, 1, text_pos);
glEnableVertexAttribArray(text_char_pos_attrib);
glVertexAttribPointer(text_char_pos_attrib, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, text_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(coords), coords, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, n);
glDisableVertexAttribArray(text_char_pos_attrib);
}
///////////////////////////////////////////////////////////////////////////////
// ============================= Window functions =============================
static GLFWwindow* window = NULL;
static bool glfw_initialized = false;
static bool fullscreen = false;
static bool maximized = true;
static int restored_x;
static int restored_y;
static int restored_width = 1024;
static int restored_height = 1024;
static bool need_update_view = true;
// Dealing with window state changes
static void update_window()
{
bool toggle_fullscreen = input.double_click || input.f % 2;
bool toggle_maximize = (maximized != glfwGetWindowAttrib(window, GLFW_MAXIMIZED));
if (toggle_fullscreen) {
fullscreen = !fullscreen;
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
if (fullscreen) {
if (!maximized) {
glfwGetWindowPos(window, &restored_x, &restored_y);
glfwGetWindowSize(window, &restored_width, &restored_height);
}
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
} else {
glfwSetWindowMonitor(window, NULL, restored_x, restored_y, restored_width, restored_height, mode->refreshRate);
if (maximized)
glfwMaximizeWindow(window);
}
}
if (toggle_maximize && !fullscreen) {
maximized = !maximized;
if (!maximized) {
glfwSetWindowPos(window, restored_x, restored_y);
glfwSetWindowSize(window, restored_width, restored_height);
}
}
// If just moving around, track resotred size/position
if (!fullscreen && !maximized && !toggle_maximize) {
glfwGetWindowPos(window, &restored_x, &restored_y);
glfwGetWindowSize(window, &restored_width, &restored_height);
}
if (toggle_fullscreen || toggle_maximize)
need_update_view = true;
}
static void glfw_error(int error, const char* description)
{
fprintf(stderr, "GLFW error: %s\n", description);
}
static void glfw_move(GLFWwindow* window, int xpos, int ypos)
{
update_window();
}
static void glfw_resize(GLFWwindow* window, int width, int height)
{
win_width = width;
win_height = height;
update_window();
need_update_view = true;
}
///////////////////////////////////////////////////////////////////////////////
// ============================= General graphics =============================
static vec2 view_center = { 0, 0 };
static float zoom;
static GLuint star_texture = GL_INVALID_VALUE;
static float* star_texture_values = NULL;
static int star_texture_buff_size = 0;
static GLuint star_shader = GL_INVALID_VALUE;
static mat4x4 projection;
static GLint star_projection_uniform = GL_INVALID_VALUE;
static GLint star_texture_uniform = GL_INVALID_VALUE;
static GLint star_position_attribute = GL_INVALID_VALUE;
static GLint star_color_attribute = GL_INVALID_VALUE;
static GLuint star_position_vbo = GL_INVALID_VALUE;
static GLuint star_color_vbo = GL_INVALID_VALUE;
// Log the latest error associated with the object
static void gl_log(GLuint object)
{
GLint log_length = 0;
if (glIsShader(object)) {
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
} else if (glIsProgram(object)) {
glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length);
} else {
fputs("gl_log: not a shader or a text_shader\n", stderr);
return;
}
GLchar* log = (GLchar*)malloc(log_length);
if (glIsShader(object))
glGetShaderInfoLog(object, log_length, NULL, log);
else if (glIsProgram(object))
glGetProgramInfoLog(object, log_length, NULL, log);
fprintf(stderr, "%s\n", log);
free(log);
}
static GLuint make_shader(GLenum type, const char *filename)
{
auto file = read_file(std::string(filename));
GLint length = file.length();
GLchar *source = new char[length];
strcpy(source, file.c_str());
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, (const GLchar* const*)&source, &length);
delete source;
glCompileShader(shader);
GLint shader_ok;
glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
if (!shader_ok) {
fprintf(stderr, "Failed to compile shader '%s':\n", filename);
gl_log(shader);
glDeleteShader(shader);
return 0;
}
return shader;
}
static GLuint make_shader_program(const char *vertex_file, const char *fragment_file)
{
GLuint program = glCreateProgram();
if (vertex_file) {
GLuint shader = make_shader(GL_VERTEX_SHADER, vertex_file);
if (!shader)
return GL_INVALID_VALUE;
glAttachShader(program, shader);
glDeleteShader(shader); // mark for deletion
}
if (fragment_file) {
GLuint shader = make_shader(GL_FRAGMENT_SHADER, fragment_file);
if(!shader)
return GL_INVALID_VALUE;
glAttachShader(program, shader);
glDeleteShader(shader);
}
glLinkProgram(program);
GLint link_ok;
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
fputs("Shader linking failed:\n", stderr);
gl_log(program);
glDeleteProgram(program);
return GL_INVALID_VALUE;
}
return program;
}
// Recalculate client area
static void update_view()
{
need_update_view = false;
glViewport(0, 0, win_width, win_height);
view_center[0] -= input.panx / zoom;
view_center[1] += input.pany / zoom;
// Update zoom
if (input.scroll) {
float new_zoom = zoom * pow(ZOOM_SENSITIVITY, input.scroll);
struct vecd2 mouse; // mouse position in pixels
glfwGetCursorPos(window, &mouse.x, &mouse.y);
mouse.x = mouse.x - 0.5*win_width;
mouse.y = 0.5*win_height - mouse.y;
// Preserve the world coordinate under mouse when zooming
view_center[0] += (float)mouse.x * (1/zoom - 1/new_zoom);
view_center[1] += (float)mouse.y * (1/zoom - 1/new_zoom);
zoom = new_zoom;
}
// Re-generate the star sprite
glUseProgram(star_shader);
if ((input.scroll || !star_texture_values) && zoom < 1000) {
const float star_size = 0.5; // equals to star.vert::star_size
int star_texture_size = 2.0f * star_size * zoom;
if (star_texture_buff_size < star_texture_size * star_texture_size) {
star_texture_buff_size = star_texture_size * star_texture_size;
star_texture_values = (float*)realloc(star_texture_values, star_texture_buff_size * sizeof(float));
}
for (int x = 0; x < (star_texture_size+1)/2; x++)
for (int y = 0; y <= x; y++) {
float dx = (0.5f * star_texture_size - x) / zoom / star_size;
float dy = (0.5f * star_texture_size - y) / zoom / star_size;
float alpha = 0.001f / (dx*dx + dy*dy);
//float alpha = exp(-100.0*(dx*dx + dy*dy)); // Airy disk approximated with a Gaussian profile
int x2 = star_texture_size-1-x;
int y2 = star_texture_size-1-y;
// exploit symmetry
star_texture_values[x + y * star_texture_size] = alpha;
star_texture_values[x + y2 * star_texture_size] = alpha;
star_texture_values[x2 + y * star_texture_size] = alpha;
star_texture_values[x2 + y2 * star_texture_size] = alpha;
star_texture_values[y + x * star_texture_size] = alpha;
star_texture_values[y + x2 * star_texture_size] = alpha;
star_texture_values[y2 + x * star_texture_size] = alpha;
star_texture_values[y2 + x2 * star_texture_size] = alpha;
}
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, star_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, star_texture_size, star_texture_size, 0, GL_ALPHA, GL_FLOAT, star_texture_values);
}
mat4x4_identity(projection);
mat4x4_ortho(projection,
-0.5f*win_width/zoom + view_center[0],
0.5f*win_width/zoom + view_center[0],
-0.5f*win_height/zoom + view_center[1],
0.5f*win_height/zoom + view_center[1],
-1, 1);
glUniformMatrix4fv(star_projection_uniform, 1, GL_FALSE, (const GLfloat*)projection);
mat4x4_identity(text_projection);
mat4x4_ortho(text_projection, 0, win_width, 0, win_height, -1, 1);
glUseProgram(text_shader);
glUniformMatrix4fv(text_projection_uniform, 1, GL_FALSE, (const GLfloat*)text_projection);
}
void finalize_graphics()
{
if (star_shader != GL_INVALID_VALUE) {
glDeleteProgram(star_shader);
star_shader = GL_INVALID_VALUE;
}
if (text_shader != GL_INVALID_VALUE) {
glDeleteProgram(text_shader);
text_shader = GL_INVALID_VALUE;
}
if (star_position_vbo != GL_INVALID_VALUE) {
glDeleteBuffers(1, &star_position_vbo);
star_position_vbo = GL_INVALID_VALUE;
}
if (star_color_vbo != GL_INVALID_VALUE) {
glDeleteBuffers(1, &star_color_vbo);
star_color_vbo = GL_INVALID_VALUE;
}
if (text_vbo != GL_INVALID_VALUE) {
glDeleteBuffers(1, &text_vbo);
text_vbo = GL_INVALID_VALUE;
}
if (star_texture != GL_INVALID_VALUE) {
glDeleteTextures(1, &star_texture);
star_texture = GL_INVALID_VALUE;
}
if (star_texture_values) {
free(star_texture_values);
star_texture_values = NULL;
}
if (font) {
glDeleteTextures(1, &font->texture);
free(font);
font = NULL;
}
if (text_buff) {
free(text_buff);
text_buff = NULL;
}
if (window) {
glfwDestroyWindow(window);
window = NULL;
}
if (glfw_initialized) {
glfwTerminate();
glfw_initialized = false;
}
}
GLFWwindow* init_graphics()
{
// Init OpenGL and GLFW
glfwSetErrorCallback(glfw_error);
glfw_initialized = glfwInit();
if (!glfw_initialized) {
finalize_graphics();
return NULL;
}
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
glfwWindowHint(GLFW_SAMPLES, config.msaa);
window = glfwCreateWindow(win_width, win_height, "Constel++", NULL, NULL);
if (!window) {
finalize_graphics();
return NULL;
}
glfwSetWindowPosCallback(window, glfw_move);
glfwSetFramebufferSizeCallback(window, glfw_resize);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
restored_x = (mode->width - restored_width) / 2;
restored_y = (mode->height - restored_height) / 2;
zoom = config.default_zoom;
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
fputs("glewInit failed\n", stderr);
finalize_graphics();
return NULL;
}
glEnable(GL_BLEND);
// Init stars
star_shader = make_shader_program("star.vert", "star.frag");
if (!star_shader) {
finalize_graphics();
return NULL;
}
star_projection_uniform = glGetUniformLocation(star_shader, "projection");
star_texture_uniform = glGetUniformLocation(star_shader, "texture");
star_position_attribute = glGetAttribLocation(star_shader, "star_position");
star_color_attribute = glGetAttribLocation(star_shader, "star_color");
glGenBuffers(1, &star_position_vbo);
glBindBuffer(GL_ARRAY_BUFFER, star_position_vbo);
glVertexAttribDivisor(star_position_attribute, 1);
glGenBuffers(1, &star_color_vbo);
glVertexAttribDivisor(star_color_attribute, 1);
glVertexAttribPointer(star_color_attribute, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * config.stars, disp_star_color, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, star_color_vbo);
glEnableVertexAttribArray(star_color_attribute);
glGenTextures(1, &star_texture);
glBindTexture(GL_TEXTURE_2D, star_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUseProgram(star_shader);
glUniform1i(star_texture_uniform, 1);
// Init text
if (config.show_status) {
text_buff = (char*)malloc(text_buff_length * sizeof(*text_buff));
glGenBuffers(1, &text_vbo);
if (FT_Init_FreeType(&freetype)) {
fputs("FT_Init_FreeType failed\n", stderr);
finalize_graphics();
return NULL;
}
text_shader = make_shader_program("text.vert", "text.frag");
if(text_shader == 0) {
finalize_graphics();
return NULL;
}
text_projection_uniform = glGetUniformLocation(text_shader, "projection");
text_pos_uniform = glGetUniformLocation(text_shader, "text_pos");
text_char_pos_attrib = glGetAttribLocation(text_shader, "char_pos");
text_texture_uniform = glGetUniformLocation(text_shader, "texture");
text_color_uniform = glGetUniformLocation(text_shader, "color");
glUseProgram(text_shader);
glUniform4fv(text_color_uniform, 1, config.text_color);
font = new_font(config.font.c_str(), config.text_size);
if (!font) {
finalize_graphics();
return NULL;
}
}
return window;
}
void draw()
{
// Update window and client area state
if (input.double_click || input.f % 2 || (maximized != glfwGetWindowAttrib(window, GLFW_MAXIMIZED)))
update_window();
if (need_update_view || input.scroll || input.panx || input.pany)
update_view();
glClear(GL_COLOR_BUFFER_BIT);
// Draw stars
// comment the next line for a more realistic and less spectacular rendering
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glUseProgram(star_shader);
glEnableVertexAttribArray(star_position_attribute);
glVertexAttribPointer(star_position_attribute, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * config.stars, disp_star_position, GL_STREAM_DRAW);
glBindTexture(GL_TEXTURE_2D, star_texture);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, config.stars);
// Draw text
if (config.show_status) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(text_shader);
char zoom_text[64];
if (zoom >= 2 * config.default_zoom)
snprintf(zoom_text, sizeof(zoom_text), "%.0fx", zoom/config.default_zoom);
else
snprintf(zoom_text, sizeof(zoom_text), "1:%.0f", (float)config.default_zoom/zoom);
draw_text(font, win_width - font->chars[' '].dx, font->chars[' '].dx/2, align_top_right,
"X: %.2f Y: %.2f\n"
"Zoom: %s\n"
"%.0f FPS",
view_center[0], view_center[1],
zoom_text,
get_fps_period(1)+0.5f);
}
glfwSwapBuffers(window);
}