@@ -233,6 +233,7 @@ int gl_save_frame_buffer(const int width, const int height, const char *fp);
233
233
234
234
typedef struct gl_entity_t {
235
235
gl_float_t T [4 * 4 ];
236
+ gl_float_t P [4 * 4 ];
236
237
237
238
gl_uint_t program_id ;
238
239
gl_uint_t VAO ;
@@ -345,15 +346,9 @@ void gl_camera_zoom(gl_camera_t *camera,
345
346
346
347
typedef struct {
347
348
gl_entity_t entity ;
348
- gl_int_t x ;
349
- gl_int_t y ;
350
- gl_int_t width ;
351
- gl_int_t height ;
349
+ gl_bounds_t bounds ;
352
350
} gl_rect_t ;
353
- gl_rect_t * gl_rect_malloc (const gl_int_t x ,
354
- const gl_int_t y ,
355
- const gl_int_t width ,
356
- const gl_int_t height );
351
+ gl_rect_t * gl_rect_malloc (const gl_bounds_t bounds );
357
352
void gl_rect_free (gl_rect_t * rect );
358
353
void gl_rect_draw (const gl_rect_t * rect , const gl_camera_t * camera );
359
354
@@ -452,16 +447,15 @@ typedef struct {
452
447
} gl_char_t ;
453
448
454
449
typedef struct {
450
+ gl_entity_t entity ;
455
451
gl_char_t data [128 ];
456
452
gl_color_t color ;
457
- gl_float_t P [4 * 4 ];
458
-
459
- gl_uint_t program_id ;
460
- gl_uint_t vao ;
461
- gl_uint_t vbo ;
453
+ gl_color_t bg_color ;
462
454
} gl_text_t ;
463
455
464
456
void gl_char_print (const gl_char_t * ch );
457
+ void gl_text_setup (gl_text_t * text );
458
+ void gl_text_cleanup (gl_text_t * text );
465
459
gl_text_t * gl_text_malloc (void );
466
460
void gl_text_free (gl_text_t * text );
467
461
void gl_text_draw (gl_text_t * text ,
@@ -583,6 +577,7 @@ typedef struct {
583
577
gl_color_t color_hover ;
584
578
gl_color_t color_press ;
585
579
gl_bounds_t bounds ;
580
+ gl_text_t text ;
586
581
587
582
gl_uint_t program_id ;
588
583
gl_uint_t vao ;
@@ -1159,6 +1154,7 @@ void gl_rot2quat(const gl_float_t C[3 * 3], gl_float_t q[4]) {
1159
1154
1160
1155
void gl_entity_setup (gl_entity_t * entity ) {
1161
1156
gl_eye (entity -> T , 4 , 4 );
1157
+ gl_eye (entity -> P , 4 , 4 );
1162
1158
entity -> program_id = 0 ;
1163
1159
entity -> VAO = 0 ;
1164
1160
entity -> VBO = 0 ;
@@ -1648,17 +1644,11 @@ void gl_camera_zoom(gl_camera_t *camera,
1648
1644
" frag_color = vec4(0.5f, 0.5f, 01.0f, 1.0f);\n" \
1649
1645
"}\n"
1650
1646
1651
- gl_rect_t * gl_rect_malloc (const gl_int_t x ,
1652
- const gl_int_t y ,
1653
- const gl_int_t width ,
1654
- const gl_int_t height ) {
1647
+ gl_rect_t * gl_rect_malloc (const gl_bounds_t bounds ) {
1655
1648
// Malloc
1656
1649
gl_rect_t * rect = MALLOC (gl_rect_t , 1 );
1657
1650
gl_entity_setup (& rect -> entity );
1658
- rect -> x = x ;
1659
- rect -> y = y ;
1660
- rect -> width = width ;
1661
- rect -> height = height ;
1651
+ rect -> bounds = bounds ;
1662
1652
1663
1653
// Shader program
1664
1654
rect -> entity .program_id = gl_prog_setup (GL_RECT_VS , GL_RECT_FS , NULL );
@@ -1668,7 +1658,7 @@ gl_rect_t *gl_rect_malloc(const gl_int_t x,
1668
1658
1669
1659
// Vertices
1670
1660
// clang-format off
1671
- const float vertices [4 * 3 ] = {
1661
+ const float vertices [4 * 2 ] = {
1672
1662
-0.5f , -0.5f , // Bottom left
1673
1663
0.5f , -0.5f , // Bottom right
1674
1664
0.5f , 0.5f , // Top right
@@ -2476,12 +2466,15 @@ void gl_line3d_draw(const gl_line3d_t *line, const gl_camera_t *camera) {
2476
2466
#define GL_TEXT_FS \
2477
2467
"#version 330 core\n" \
2478
2468
"in vec2 tex_coords;\n" \
2479
- "out vec4 color ;\n" \
2469
+ "out vec4 frag_color ;\n" \
2480
2470
"uniform sampler2D text;\n" \
2481
2471
"uniform vec3 text_color;\n" \
2472
+ "uniform vec3 bg_color;\n" \
2482
2473
"void main() {\n" \
2483
- " vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, tex_coords).r);\n" \
2484
- " color = vec4(text_color, 1.0) * sampled;\n" \
2474
+ " float alpha = texture(text, tex_coords).r;\n" \
2475
+ " vec4 glyph_rgba = vec4(text_color, alpha);\n" \
2476
+ " vec4 bg_rgba = vec4(bg_color, 1.0);\n" \
2477
+ " frag_color = mix(bg_rgba, glyph_rgba, alpha);\n" \
2485
2478
"}\n"
2486
2479
2487
2480
void gl_char_print (const gl_char_t * ch ) {
@@ -2494,14 +2487,15 @@ void gl_char_print(const gl_char_t *ch) {
2494
2487
printf ("\n" );
2495
2488
}
2496
2489
2497
- gl_text_t * gl_text_malloc (void ) {
2498
- // MALLOC
2499
- gl_text_t * text = MALLOC (gl_text_t , 1 );
2500
- text -> color = (gl_color_t ){1.0 , 1.0 , 1.0 };
2490
+ void gl_text_setup (gl_text_t * text ) {
2491
+ // Initialize
2492
+ gl_entity_setup (& text -> entity );
2493
+ text -> color = (gl_color_t ){1.0 , 0.0 , 0.0 };
2494
+ text -> bg_color = (gl_color_t ){1.0 , 1.0 , 1.0 };
2501
2495
2502
2496
// Compile shader
2503
- text -> program_id = gl_prog_setup (GL_TEXT_VS , GL_TEXT_FS , NULL );
2504
- if (text -> program_id == GL_FALSE ) {
2497
+ text -> entity . program_id = gl_prog_setup (GL_TEXT_VS , GL_TEXT_FS , NULL );
2498
+ if (text -> entity . program_id == GL_FALSE ) {
2505
2499
FATAL ("Failed to create shaders!" );
2506
2500
}
2507
2501
@@ -2566,26 +2560,37 @@ gl_text_t *gl_text_malloc(void) {
2566
2560
FT_Done_FreeType (ft );
2567
2561
2568
2562
// VAO
2569
- glGenVertexArrays (1 , & text -> vao );
2570
- glBindVertexArray (text -> vao );
2563
+ glGenVertexArrays (1 , & text -> entity . VAO );
2564
+ glBindVertexArray (text -> entity . VAO );
2571
2565
2572
2566
// VBO
2573
- glGenBuffers (1 , & text -> vbo );
2574
- glBindBuffer (GL_ARRAY_BUFFER , text -> vbo );
2567
+ glGenBuffers (1 , & text -> entity . VBO );
2568
+ glBindBuffer (GL_ARRAY_BUFFER , text -> entity . VBO );
2575
2569
glBufferData (GL_ARRAY_BUFFER , sizeof (float ) * 6 * 4 , NULL , GL_DYNAMIC_DRAW );
2576
2570
glVertexAttribPointer (0 , 4 , GL_FLOAT , GL_FALSE , 4 * sizeof (float ), 0 );
2577
2571
glEnableVertexAttribArray (0 );
2578
2572
2579
2573
// Clean up
2580
2574
glBindBuffer (GL_ARRAY_BUFFER , 0 ); // Unbind VBO
2581
2575
glBindVertexArray (0 ); // Unbind VAO
2576
+ }
2577
+
2578
+ void gl_text_cleanup (gl_text_t * text ) {
2579
+ // Clean up
2580
+ gl_entity_cleanup (& text -> entity );
2581
+ }
2582
2582
2583
+ gl_text_t * gl_text_malloc (void ) {
2584
+ gl_text_t * text = MALLOC (gl_text_t , 1 );
2585
+ gl_text_setup (text );
2583
2586
return text ;
2584
2587
}
2585
2588
2586
2589
void gl_text_free (gl_text_t * text ) {
2587
- glDeleteVertexArrays (1 , & text -> vao );
2588
- glDeleteBuffers (1 , & text -> vbo );
2590
+ if (text == NULL ) {
2591
+ return ;
2592
+ }
2593
+ gl_text_cleanup (text );
2589
2594
free (text );
2590
2595
}
2591
2596
@@ -2602,14 +2607,15 @@ void gl_text_draw(gl_text_t *text,
2602
2607
const gl_float_t top = * (camera -> window_height );
2603
2608
const gl_float_t znear = -1.0f ;
2604
2609
const gl_float_t zfar = 1.0f ;
2605
- gl_ortho (left , right , bottom , top , znear , zfar , text -> P );
2610
+ gl_ortho (left , right , bottom , top , znear , zfar , text -> entity . P );
2606
2611
2607
2612
// Activate shader
2608
- glUseProgram (text -> program_id );
2609
- assert (gl_prog_set_mat4 (text -> program_id , "projection" , text -> P ) == 0 );
2610
- assert (gl_prog_set_color (text -> program_id , "text_color" , text -> color ) == 0 );
2613
+ glUseProgram (text -> entity .program_id );
2614
+ gl_prog_set_mat4 (text -> entity .program_id , "projection" , text -> entity .P );
2615
+ gl_prog_set_color (text -> entity .program_id , "text_color" , text -> color );
2616
+ gl_prog_set_color (text -> entity .program_id , "bg_color" , text -> bg_color );
2611
2617
glActiveTexture (GL_TEXTURE0 );
2612
- glBindVertexArray (text -> vao );
2618
+ glBindVertexArray (text -> entity . VAO );
2613
2619
2614
2620
// Render text
2615
2621
for (size_t i = 0 ; i < strlen (s ); ++ i ) {
@@ -2635,7 +2641,7 @@ void gl_text_draw(gl_text_t *text,
2635
2641
glBindTexture (GL_TEXTURE_2D , ch -> texture_id );
2636
2642
2637
2643
// Update content of VBO memory
2638
- glBindBuffer (GL_ARRAY_BUFFER , text -> vbo );
2644
+ glBindBuffer (GL_ARRAY_BUFFER , text -> entity . VBO );
2639
2645
glBufferSubData (GL_ARRAY_BUFFER , 0 , sizeof (vertices ), vertices );
2640
2646
glBindBuffer (GL_ARRAY_BUFFER , 0 );
2641
2647
@@ -3463,11 +3469,11 @@ void gui_setup(gui_t *gui) {
3463
3469
glEnable (GL_BLEND );
3464
3470
glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
3465
3471
3466
- assert (glIsEnabled (GL_PROGRAM_POINT_SIZE ) == 1 );
3467
- assert (glIsEnabled (GL_LINE_SMOOTH ) == 1 );
3468
- assert (glIsEnabled (GL_DEPTH_TEST ) == 1 );
3469
- assert (glIsEnabled (GL_CULL_FACE ) == 1 );
3470
- assert (glIsEnabled (GL_BLEND ) == 1 );
3472
+ assert (glIsEnabled (GL_PROGRAM_POINT_SIZE ));
3473
+ assert (glIsEnabled (GL_LINE_SMOOTH ));
3474
+ assert (glIsEnabled (GL_DEPTH_TEST ));
3475
+ assert (glIsEnabled (GL_CULL_FACE ));
3476
+ assert (glIsEnabled (GL_BLEND ));
3471
3477
3472
3478
// Camera
3473
3479
gl_camera_setup (& gui -> camera , & gui -> window_width , & gui -> window_height );
@@ -3508,7 +3514,7 @@ void gui_loop(gui_t *gui) {
3508
3514
gl_cube_malloc (cube_size , cube_pos , cube_color , outline_color );
3509
3515
3510
3516
// Rect
3511
- gl_rect_t * rect = gl_rect_malloc (0 , 0 , 0 , 0 );
3517
+ gl_rect_t * rect = gl_rect_malloc (( gl_bounds_t ){ 0 , 0 , 10 , 10 } );
3512
3518
3513
3519
// Camera frame
3514
3520
const gl_float_t cf_pos [3 ] = {0.0 , 0.0 , 0.0 };
@@ -3582,7 +3588,8 @@ void gui_loop(gui_t *gui) {
3582
3588
// gl_grid3d_draw(grid, &gui->camera);
3583
3589
// gl_line3d_draw(line, &gui->camera);
3584
3590
// gl_points3d_draw(points, &gui->camera, num_points);
3585
- // gl_text_draw(text, &gui->camera, "Here!", 10.0f, 100.0f, 1.0f);
3591
+ gl_text_draw (text , & gui -> camera , "Here!" , 150.0f , 100.0f , 1.0f );
3592
+ // gl_text_draw(text, &gui->camera, "Here2!", 500.0f, 500.0f, 1.0f);
3586
3593
// gl_image_draw(image);
3587
3594
ui_button_draw (gui , & button );
3588
3595
@@ -3616,7 +3623,7 @@ void gui_loop(gui_t *gui) {
3616
3623
"uniform float button_width;\n" \
3617
3624
"uniform float button_height;\n" \
3618
3625
"void main() {\n" \
3619
- " gl_Position = vec4(in_pos, 0.0 , 1.0);\n" \
3626
+ " gl_Position = vec4(in_pos, 0.1 , 1.0);\n" \
3620
3627
"}\n"
3621
3628
3622
3629
#define UI_BUTTON_FS \
@@ -3641,9 +3648,9 @@ void ui_button_setup(ui_button_t *button) {
3641
3648
button -> color = (gl_color_t ){1.0f , 1.0f , 1.0f };
3642
3649
button -> color_hover = (gl_color_t ){1.0f , 0.0f , 0.0f };
3643
3650
button -> color_press = (gl_color_t ){0.0f , 0.0f , 1.0f };
3644
- button -> bounds .x = 20 ;
3651
+ button -> bounds .x = 100 ;
3645
3652
button -> bounds .y = 600 ;
3646
- button -> bounds .w = 200 .0f ;
3653
+ button -> bounds .w = 150 .0f ;
3647
3654
button -> bounds .h = 100.0f ;
3648
3655
3649
3656
button -> program_id = -1 ;
0 commit comments