Skip to content

Commit 1e810b3

Browse files
committed
Update gui
1 parent 65bdb48 commit 1e810b3

File tree

3 files changed

+196
-55
lines changed

3 files changed

+196
-55
lines changed

src/test_gui.c

+133-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "munit.h"
22
#include "xyz_gui.h"
33

4-
54
static GLFWwindow *test_setup(void) {
65
// GLFW
76
glfwInit();
@@ -478,8 +477,138 @@ int test_gui(void) {
478477
const char *window_title = "viz";
479478
const int window_width = 1024;
480479
const int window_height = 768;
481-
gui_setup(window_title, window_width, window_height);
482-
gui_loop();
480+
gui_t *gui = gui_malloc(window_title, window_width, window_height);
481+
482+
for (int i = 0; i < 10; ++i) {
483+
gui_poll(gui);
484+
gui_update(gui);
485+
}
486+
gui_free(gui);
487+
488+
return 0;
489+
}
490+
491+
int test_components(void) {
492+
// Setup
493+
const char *window_title = "viz";
494+
const int window_width = 1024;
495+
const int window_height = 768;
496+
gui_t *gui = gui_malloc(window_title, window_width, window_height);
497+
498+
// Rect
499+
gl_rect_t rect;
500+
gl_bounds_t rect_bounds = (gl_bounds_t){10, 10, 100, 100};
501+
gl_color_t rect_color = (gl_color_t){1.0f, 0.0f, 1.0f};
502+
gl_rect_setup(&rect, rect_bounds, rect_color);
503+
504+
// Cube
505+
gl_float_t cube_T[4 * 4] = {0};
506+
gl_eye(cube_T, 4, 4);
507+
cube_T[12] = 0.0;
508+
cube_T[13] = 0.0;
509+
cube_T[14] = 1.0;
510+
gl_float_t cube_size = 0.5f;
511+
gl_color_t cube_color = (gl_color_t){0.9, 0.4, 0.2};
512+
gl_cube_t cube;
513+
gl_cube_setup(&cube, cube_T, cube_size, cube_color);
514+
515+
// Frustum
516+
gl_float_t frustum_T[4 * 4];
517+
gl_eye(frustum_T, 4, 4);
518+
gl_float_t frustum_size = 0.5f;
519+
gl_color_t frustum_color = (gl_color_t){0.9, 0.4, 0.2};
520+
gl_float_t frustum_lw = 1.0f;
521+
gl_frustum_t frustum;
522+
gl_frustum_setup(&frustum,
523+
frustum_T,
524+
frustum_size,
525+
frustum_color,
526+
frustum_lw);
527+
528+
// Axes
529+
gl_float_t axes_T[4 * 4];
530+
gl_eye(axes_T, 4, 4);
531+
gl_float_t axes_size = 0.5f;
532+
gl_float_t axes_lw = 5.0f;
533+
gl_axes3d_t axes;
534+
gl_axes3d_setup(&axes, axes_T, axes_size, axes_lw);
535+
536+
// Grid
537+
gl_float_t grid_size = 0.5f;
538+
gl_float_t grid_lw = 5.0f;
539+
gl_color_t grid_color = (gl_color_t){0.9, 0.4, 0.2};
540+
gl_grid3d_t grid;
541+
gl_grid3d_setup(&grid, grid_size, grid_color, grid_lw);
542+
543+
// Points
544+
gl_points3d_t points3d;
545+
gl_color_t points_color = (gl_color_t){1.0, 0.0, 0.0};
546+
gl_float_t point_size = 2.0;
547+
size_t num_points = 2e3;
548+
gl_float_t *points_data = malloc(sizeof(gl_float_t) * num_points * 6);
549+
for (size_t i = 0; i < num_points; ++i) {
550+
points_data[i * 6 + 0] = gl_randf(-1.0f, 1.0f);
551+
points_data[i * 6 + 1] = gl_randf(-1.0f, 1.0f);
552+
points_data[i * 6 + 2] = gl_randf(-1.0f, 1.0f);
553+
points_data[i * 6 + 3] = points_color.r;
554+
points_data[i * 6 + 4] = points_color.g;
555+
points_data[i * 6 + 5] = points_color.b;
556+
}
557+
gl_points3d_setup(&points3d, points_data, num_points, point_size);
558+
559+
// Line
560+
gl_float_t line_lw = 5.0f;
561+
gl_color_t line_color = (gl_color_t){1.0, 0.0, 0.0};
562+
size_t line_size = 1000;
563+
float radius = 3.0f;
564+
float dtheta = 2 * M_PI / line_size;
565+
float theta = 0.0f;
566+
float *line_data = malloc(sizeof(float) * line_size * 3);
567+
for (size_t i = 0; i < line_size; ++i) {
568+
line_data[i * 3 + 0] = radius * sin(theta);
569+
line_data[i * 3 + 1] = 0.0f;
570+
line_data[i * 3 + 2] = radius * cos(theta);
571+
theta += dtheta;
572+
}
573+
gl_line3d_t line;
574+
gl_line3d_setup(&line, line_data, line_size, line_color, line_lw);
575+
576+
// Image
577+
int width = 0;
578+
int height = 0;
579+
int channels = 0;
580+
const char *image_path = "/home/chutsu/smile.jpeg";
581+
stbi_set_flip_vertically_on_load(1);
582+
uint8_t *image_data = stbi_load(image_path, &width, &height, &channels, 0);
583+
gl_image_t image;
584+
gl_image_setup(&image, 10, 120, image_data, width, height, channels);
585+
586+
// Text
587+
gl_text_t text;
588+
gl_color_t text_color = (gl_color_t){1.0, 1.0, 1.0};
589+
int text_size = 18;
590+
gl_text_setup(&text, text_size);
591+
592+
// Render
593+
while (gui_poll(gui)) {
594+
draw_rect(&rect);
595+
draw_cube(&cube);
596+
draw_frustum(&frustum);
597+
draw_axes3d(&axes);
598+
draw_grid3d(&grid);
599+
draw_points3d(&points3d);
600+
draw_line3d(&line);
601+
draw_image(&image);
602+
draw_text(&text, "Hello World", 10, 350, text_color);
603+
604+
gui_update(gui);
605+
}
606+
607+
// Clean up
608+
free(points_data);
609+
free(line_data);
610+
stbi_image_free(image_data);
611+
gui_free(gui);
483612

484613
return 0;
485614
}
@@ -531,6 +660,7 @@ void test_suite(void) {
531660
// MU_ADD_TEST(test_gl_model_load);
532661
#if CI_MODE == 0
533662
MU_ADD_TEST(test_gui);
663+
MU_ADD_TEST(test_components);
534664
MU_ADD_TEST(test_sandbox);
535665
#endif
536666
}

src/xyz_gui.c

+41-45
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#endif
1212

1313
// GLOBAL VARIABLES
14-
// GLFWwindow *_window;
1514
char _window_title[100] = {0};
1615
int _window_loop = 1;
1716
int _window_width = 0;
@@ -32,7 +31,6 @@ double _cursor_last_y = 0.0;
3231
int _cursor_is_dragging = 0;
3332
int _ui_engaged = 0;
3433

35-
gl_char_t _chars[128];
3634
int _key_q = 0;
3735
int _key_w = 0;
3836
int _key_a = 0;
@@ -603,28 +601,28 @@ int gl_save_frame_buffer(const int width, const int height, const char *fp) {
603601

604602
void gl_shader_setup(gl_shader_t *shader) {
605603
shader->program_id = 0;
606-
shader->texture_id = 0;
607-
shader->VAO = 0;
608-
shader->VBO = 0;
609-
shader->EBO = 0;
604+
// shader->texture_id = 0;
605+
// shader->VAO = 0;
606+
// shader->VBO = 0;
607+
// shader->EBO = 0;
610608
}
611609

612610
void gl_shader_cleanup(gl_shader_t *shader) {
613611
if (glIsProgram(shader->program_id) == GL_TRUE) {
614612
glDeleteProgram(shader->program_id);
615613
}
616614

617-
if (glIsVertexArray(shader->VAO) == GL_TRUE) {
618-
glDeleteVertexArrays(1, &shader->VAO);
619-
}
620-
621-
if (glIsBuffer(shader->VBO) == GL_TRUE) {
622-
glDeleteBuffers(1, &shader->VBO);
623-
}
624-
625-
if (glIsBuffer(shader->EBO) == GL_TRUE) {
626-
glDeleteBuffers(1, &shader->EBO);
627-
}
615+
// if (glIsVertexArray(shader->VAO) == GL_TRUE) {
616+
// glDeleteVertexArrays(1, &shader->VAO);
617+
// }
618+
//
619+
// if (glIsBuffer(shader->VBO) == GL_TRUE) {
620+
// glDeleteBuffers(1, &shader->VBO);
621+
// }
622+
//
623+
// if (glIsBuffer(shader->EBO) == GL_TRUE) {
624+
// glDeleteBuffers(1, &shader->EBO);
625+
// }
628626
}
629627

630628
gl_uint_t gl_compile(const char *src, const int type) {
@@ -2001,7 +1999,6 @@ void setup_points3d_shader(gl_shader_t *shader) {
20011999
if (shader->program_id == GL_FALSE) {
20022000
FATAL("Failed to create shaders to draw points!");
20032001
}
2004-
shader->VAO = 0;
20052002
}
20062003

20072004
void gl_points3d_setup(gl_points3d_t *points3d,
@@ -2092,7 +2089,6 @@ void setup_line3d_shader(gl_shader_t *shader) {
20922089
if (shader->program_id == GL_FALSE) {
20932090
FATAL("Failed to create shaders!");
20942091
}
2095-
shader->VAO = -1;
20962092
}
20972093

20982094
void gl_line3d_setup(gl_line3d_t *line,
@@ -2196,8 +2192,6 @@ void setup_image_shader(gl_shader_t *shader) {
21962192
if (shader->program_id == GL_FALSE) {
21972193
FATAL("Failed to create shaders!");
21982194
}
2199-
shader->texture_id = -1;
2200-
shader->VAO = -1;
22012195
}
22022196

22032197
void gl_image_setup(gl_image_t *image,
@@ -2358,24 +2352,21 @@ void gl_char_print(const gl_char_t *ch) {
23582352

23592353
void setup_text_shader(gl_shader_t *shader) {
23602354
assert(shader);
2361-
2362-
// Setup
2363-
const gl_float_t text_size = 18;
2364-
2365-
// Compile shader
23662355
gl_shader_setup(shader);
23672356
shader->program_id = gl_shader(GL_TEXT_VS, GL_TEXT_FS, NULL);
23682357
if (shader->program_id == GL_FALSE) {
23692358
FATAL("Failed to create shaders!");
23702359
}
2360+
}
23712361

2362+
void gl_text_setup(gl_text_t *text, const int text_size) {
23722363
// VAO
2373-
glGenVertexArrays(1, &shader->VAO);
2374-
glBindVertexArray(shader->VAO);
2364+
glGenVertexArrays(1, &text->VAO);
2365+
glBindVertexArray(text->VAO);
23752366

23762367
// VBO
2377-
glGenBuffers(1, &shader->VBO);
2378-
glBindBuffer(GL_ARRAY_BUFFER, shader->VBO);
2368+
glGenBuffers(1, &text->VBO);
2369+
glBindBuffer(GL_ARRAY_BUFFER, text->VBO);
23792370
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
23802371
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
23812372
glEnableVertexAttribArray(0);
@@ -2439,40 +2430,45 @@ void setup_text_shader(gl_shader_t *shader) {
24392430
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
24402431

24412432
// Store character for later use
2442-
_chars[c].texture_id = texture_id;
2443-
_chars[c].size[0] = face->glyph->bitmap.width;
2444-
_chars[c].size[1] = face->glyph->bitmap.rows;
2445-
_chars[c].bearing[0] = face->glyph->bitmap_left;
2446-
_chars[c].bearing[1] = face->glyph->bitmap_top;
2447-
_chars[c].offset = face->glyph->advance.x;
2433+
text->chars[c].texture_id = texture_id;
2434+
text->chars[c].size[0] = face->glyph->bitmap.width;
2435+
text->chars[c].size[1] = face->glyph->bitmap.rows;
2436+
text->chars[c].bearing[0] = face->glyph->bitmap_left;
2437+
text->chars[c].bearing[1] = face->glyph->bitmap_top;
2438+
text->chars[c].offset = face->glyph->advance.x;
24482439
}
24492440
glBindTexture(GL_TEXTURE_2D, 0);
24502441
FT_Done_Face(face);
24512442
FT_Done_FreeType(ft);
24522443
}
24532444

2454-
void text_width_height(const char *s, gl_float_t *w, gl_float_t *h) {
2445+
void text_width_height(gl_text_t *text,
2446+
const char *s,
2447+
gl_float_t *w,
2448+
gl_float_t *h) {
24552449
assert(s);
24562450
assert(w);
24572451
assert(h);
24582452

24592453
float x = 0.0f;
2460-
gl_char_t *hch = &_chars[(int) 'H'];
2461-
gl_char_t *ch = &_chars[(int) s[0]];
2454+
gl_char_t *hch = &text->chars[(int) 'H'];
2455+
gl_char_t *ch = &text->chars[(int) s[0]];
24622456

24632457
for (size_t i = 0; i < strlen(s); ++i) {
2464-
ch = &_chars[(int) s[i]];
2458+
ch = &text->chars[(int) s[i]];
24652459
x += (ch->offset >> 6);
24662460
}
24672461

24682462
*w = x + ch->bearing[0];
24692463
*h = (hch->bearing[1] - ch->bearing[1]) + ch->size[1];
24702464
}
24712465

2472-
void draw_text(const char *s,
2466+
void draw_text(gl_text_t *text,
2467+
const char *s,
24732468
const float x,
24742469
const float y,
24752470
const gl_color_t c) {
2471+
assert(text);
24762472
assert(s);
24772473

24782474
// Setup projection matrix
@@ -2488,13 +2484,13 @@ void draw_text(const char *s,
24882484
gl_set_color(shader->program_id, "text_color", c);
24892485
gl_set_int(shader->program_id, "text", 0);
24902486
glActiveTexture(GL_TEXTURE0);
2491-
glBindVertexArray(shader->VAO);
2487+
glBindVertexArray(text->VAO);
24922488

24932489
// Render text
24942490
float x_ = x;
2495-
gl_char_t *hch = &_chars[(int) 'H'];
2491+
gl_char_t *hch = &text->chars[(int) 'H'];
24962492
for (size_t i = 0; i < strlen(s); ++i) {
2497-
gl_char_t *ch = &_chars[(int) s[i]];
2493+
gl_char_t *ch = &text->chars[(int) s[i]];
24982494
const float xpos = x_ + ch->bearing[0] * scale;
24992495
const float ypos = y + (hch->bearing[1] - ch->bearing[1]) * scale;
25002496
const float w = ch->size[0] * scale;
@@ -2516,7 +2512,7 @@ void draw_text(const char *s,
25162512
glBindTexture(GL_TEXTURE_2D, ch->texture_id);
25172513

25182514
// Update content of VBO memory
2519-
glBindBuffer(GL_ARRAY_BUFFER, shader->VBO);
2515+
glBindBuffer(GL_ARRAY_BUFFER, text->VBO);
25202516
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
25212517
glBindBuffer(GL_ARRAY_BUFFER, 0);
25222518

src/xyz_gui.h

+22-7
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ int gl_save_frame_buffer(const int width, const int height, const char *fp);
218218

219219
typedef struct gl_shader_t {
220220
gl_uint_t program_id;
221-
gl_uint_t texture_id;
222-
gl_uint_t VAO;
223-
gl_uint_t VBO;
224-
gl_uint_t EBO;
221+
// gl_uint_t texture_id;
222+
// gl_uint_t VAO;
223+
// gl_uint_t VBO;
224+
// gl_uint_t EBO;
225225
} gl_shader_t;
226226

227227
void gl_shader_setup(gl_shader_t *shader);
@@ -465,9 +465,24 @@ void draw_image(gl_image_t *image);
465465

466466
// TEXT //////////////////////////////////////////////////////////////////////
467467

468-
void setup_text_shader(gl_shader_t *text);
469-
void text_width_height(const char *s, gl_float_t *w, gl_float_t *h);
470-
void draw_text(const char *s, const float x, const float y, const gl_color_t c);
468+
typedef struct gl_text_t {
469+
gl_uint_t VAO;
470+
gl_uint_t VBO;
471+
gl_uint_t EBO;
472+
gl_char_t chars[128];
473+
} gl_text_t;
474+
475+
void setup_text_shader(gl_shader_t *shader);
476+
void gl_text_setup(gl_text_t *text, const int text_size);
477+
void text_width_height(gl_text_t *text,
478+
const char *s,
479+
gl_float_t *w,
480+
gl_float_t *h);
481+
void draw_text(gl_text_t *text,
482+
const char *s,
483+
const float x,
484+
const float y,
485+
const gl_color_t c);
471486

472487
// MESH //////////////////////////////////////////////////////////////////////
473488

0 commit comments

Comments
 (0)