From ecae941a1175453c35b72f27e9e258b29d6ba595 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Fri, 30 Jun 2023 11:51:23 +0200 Subject: [PATCH] WIP: graphics restructuration > sapce OK arrows TODO --- include/graphics.h | 5 +- src/graphics/buffers.c | 2 +- src/graphics/graphics.c | 186 ++++++++++++++++++++++------------------ 3 files changed, 105 insertions(+), 88 deletions(-) diff --git a/include/graphics.h b/include/graphics.h index f1695a1..9274fc5 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -41,8 +41,9 @@ struct gl_area_entry { GLuint m; // init_shaders, draw GLuint v; // init_shaders, draw GLuint p; // init_shaders, draw - GLuint line_indices_nb; // init_buffers, draw, compute_space, arrow_2D, 3D - GLuint plan_indices_nb; // init_buffers, draw, compute_space, arrow_2D, 3D +// GLuint line_indices_nb; // now : graphics.c > int buffer_lines_ndx +// GLuint plan_indices_nb; // now : graphics.c > int buffer_plans_ndx + }; /* diff --git a/src/graphics/buffers.c b/src/graphics/buffers.c index 61a7b1b..1aa4d43 100644 --- a/src/graphics/buffers.c +++ b/src/graphics/buffers.c @@ -38,7 +38,7 @@ #include #include -#include "../../include/base.h" +//#include "../../include/base.h" #include "../../include/ui.h" #include "../../include/graphics.h" #include "../../include/buffers.h" diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index e9a3eb7..acae88c 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -36,14 +36,10 @@ #include #include -#include "../../include/base.h" +//#include "../../include/base.h" #include "../../include/ui.h" #include "../../include/graphics.h" #include "../../include/buffers.h" -//#include "../../include/space_2D.h" -//#include "../../include/space_3D.h" -//#include "../../include/arrow_2D.h" -//#include "../../include/arrow_3D.h" #define VERTEX_SHADER_FILE "src/shaders/shader.vert" #define FRAG_SHADER_FILE "src/shaders/shader.frag" @@ -487,13 +483,21 @@ bool graphics_init_shaders(const void *gl_area) /* -------------------------------------------------------------------------- */ -int dim = 0; -int state_size = 0; -int vertex_nb = 0; -int colors_nb = 0; -int lines_nb = 0; -int plans_nb = 0; -int arrows_nb = 0; +// int model_dim = 0; INUTILE (fonction de model_space_size_x, y ,z) */ +int model_space_size_x = 0; +int model_space_size_y = 0; +int model_space_size_z = 0; +int model_arrows_nb = 0; + +int buffer_vertex_size = 0; +int buffer_colors_size = 0; +int buffer_lines_size = 0; +int buffer_plans_size = 0; + /* Pourquoi mettre 'line_indices_nb' dans 'entry' ? */ + /* Pourquoi ne pas y mettre la taille des autres buffers ? */ + /* --- Pas cohérent ... > je déplace (30-06-2023) */ +int buffer_lines_ndx = 0; // previously in graphics.h struct gl_area_entry.GLuint line_indices_nb; +int buffer_plans_ndx = 0; // previously in graphics.h struct gl_area_entry.GLuint plan_indices_nb; bool pref_3D_xor_2D_space = 0; // default view == 0 == 3D bool pref_3D_xor_2D_arrows = 0; // default view == 0 == 3D @@ -504,54 +508,85 @@ bool pref_background_color = 0; // don't know yet... /* -------------------------------------------------------------------------- */ // TODO Tester (presque) toutes les combinaisons possibles de ces variables !!! -// NB mark_unit_space_first & last m'auront surtout servi à debuguer (> à oublier) +// NB mark_unit_space_first & last m'auront surtout servi à debuguer. +// si inutiles, pourraient être oubliées ?... static void get_model_data_and_user_preferences(){ -// The model data (arrows) to use ( from model.xml) +// The model data (arrows) to use (from model.xml) // { 1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21 }; - dim = 1; - state_size = 8; // 2 < state_size < 32 - arrows_nb = 4; // assert : leur emplacement doit être fonction de state_size - pref_3D_xor_2D_space = 0; // default == 0 == 3D + model_space_size_x = 8; // 2 < model_space_size_x < 32 + model_arrows_nb = 4; // assert : leur emplacement est contraint par model_space_size_x + // + pref_3D_xor_2D_space = 1; // default == 0 == 3D pref_3D_xor_2D_arrows = 1; // default == 0 == 3D pref_mark_unit_space_first = 1; // default == 0 == no mark pref_mark_unit_space_last = 1; // default == 0 == no mark pref_background_color = 0; // TODO } -static void compute_vertex_colors_lines_plans_nb(int dim, - int state_size, - int arrows_nb, - bool pref_3D_xor_2D_space, - bool pref_3D_xor_2D_arrows, - bool pref_mark_unit_space_first, - bool pref_mark_unit_space_last, - struct gl_area_entry *entry){ +static void compute_buffers_sizes(int model_space_size_x, + int model_space_size_y, + int model_space_size_z, + int model_arrows_nb, + bool pref_3D_xor_2D_space, + bool pref_3D_xor_2D_arrows, + bool pref_mark_unit_space_first, + bool pref_mark_unit_space_last) +{ - if (pref_3D_xor_2D_space) vertex_nb = (state_size + 1) * 6 + arrows_nb * 9; // case 2D - else vertex_nb = (state_size + 1) * 12 + arrows_nb * 15; // case 3D + if (pref_3D_xor_2D_space) buffer_vertex_size = (model_space_size_x + 1) * 6 + model_arrows_nb * 9; // case 2D + else buffer_vertex_size = (model_space_size_x + 1) * 12 + model_arrows_nb * 15; // case 3D - if (pref_3D_xor_2D_space) lines_nb = (2 + state_size) + (arrows_nb * 3); // case 2D - else lines_nb = (4 + state_size * 4) + (arrows_nb * 8); // case 3D + if (pref_3D_xor_2D_space) buffer_colors_size = (model_space_size_x + 1) * 6 + model_arrows_nb * 9; // case 2D + else buffer_colors_size = (model_space_size_x + 1) * 12 + model_arrows_nb * 15; // case 3D - if (pref_mark_unit_space_first) lines_nb += 2; // 2 diagonal lines - if (pref_mark_unit_space_last) lines_nb += 2; // 2 diagonal lines + if (pref_3D_xor_2D_space) buffer_lines_size = (2 + model_space_size_x) + (model_arrows_nb * 3); // case 2D + else buffer_lines_size = (4 + model_space_size_x * 4) + (model_arrows_nb * 8); // case 3D - plans_nb = 8; // (4 + state_size * 4) + (arrows_nb * 8) + 2; + if (pref_mark_unit_space_first) buffer_lines_size += 2; // 2 diagonal lines + if (pref_mark_unit_space_last) buffer_lines_size += 2; // 2 diagonal lines - entry->line_indices_nb = lines_nb * 2; - entry->plan_indices_nb = plans_nb * 3; + int vgap = 0, cgap = 0, igap = 0; - colors_nb = (state_size + 1) * 12 + arrows_nb * 0; // TODO + vgap += (6 * model_space_size_x); + cgap += (6 * model_space_size_x); + igap += model_space_size_x * 2; - assert(state_size * 6 < vertex_nb); assert(state_size * 6 + 5 < vertex_nb); + if (pref_mark_unit_space_first) igap += 4; // 4 vertices for 2 diagonal lines + if (pref_mark_unit_space_last) igap += 4; // 4 vertices for 2 diagonal lines + + if (pref_3D_xor_2D_space) for (int k = 0; k < model_space_size_x; k++) { + assert(k * 6 < buffer_vertex_size); assert(k * 6 + 5 < buffer_vertex_size); + assert(k * 6 < buffer_colors_size); assert(k * 6 + 5 < buffer_colors_size); + assert(k + 4 < buffer_lines_size); assert(k + 8 < buffer_lines_size); // TODO check this line ! + } else for (int k = 0; k < model_space_size_x; k++) { + assert(k * 12 < buffer_vertex_size); assert(k * 12 + 11 < buffer_vertex_size); + assert(k * 12 < buffer_colors_size); assert(k * 12 + 11 < buffer_colors_size); + assert(k + 8 < buffer_lines_size); assert(k + 16 < buffer_lines_size); // TODO check this line ! + } + + assert(vgap < buffer_vertex_size); assert(vgap + 9 < buffer_vertex_size); + assert(igap < buffer_lines_size); //assert(igap + 5 < buffer_lines_size); + + /* 1 flèche 3D > vgap += 15; igap += 16; cgap += 15; ++model_arrows_nb; */ + + + + + buffer_plans_size = 8; // (4 + model_space_size_x * 4) + (model_arrows_nb * 8) + 2; + + buffer_lines_ndx = buffer_lines_size * 2; + buffer_plans_ndx = buffer_plans_size * 3; + + buffer_colors_size = (model_space_size_x + 1) * 12 + model_arrows_nb * 0; // TODO + + assert(model_space_size_x * 6 < buffer_vertex_size); assert(model_space_size_x * 6 + 5 < buffer_vertex_size); printf("Prefer : mark_unit_space_first = %d mark_unit_space_last = %d 3D_xor_2D_space = %d\n", pref_mark_unit_space_first, pref_mark_unit_space_last, pref_3D_xor_2D_space); - - printf("Initialization of buffers with %u line_ndx, %u vertices, %u colors. state_size = %d\n", - entry->line_indices_nb, vertex_nb, colors_nb, state_size); + printf("Initialization of buffers with %u line_ndx, %u vertices, %u colors. model_space_size_x = %d\n", + buffer_lines_ndx, buffer_vertex_size, buffer_colors_size, model_space_size_x); } /* Initializes the buffer of a gl_area @@ -567,78 +602,59 @@ void graphics_init_buffers(const void *gl_area) entry = find_entry_from_ptr(gl_area); get_model_data_and_user_preferences(); - compute_vertex_colors_lines_plans_nb(dim, state_size, arrows_nb, - pref_3D_xor_2D_space, - pref_3D_xor_2D_arrows, - pref_mark_unit_space_first, - pref_mark_unit_space_last, - entry); + /* une struct en retour pour ne plus utiliser de variables globales ? (facile) */ + compute_buffers_sizes(model_space_size_x, + model_space_size_y, + model_space_size_z, + model_arrows_nb, + pref_3D_xor_2D_space, + pref_3D_xor_2D_arrows, + pref_mark_unit_space_first, + pref_mark_unit_space_last); // g_malloc - vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2); - color_base = g_malloc0(colors_nb * sizeof(GLfloat) * 2); - line_ndx = g_malloc0(lines_nb * 2 * sizeof(GLubyte) * 2); - plan_ndx = g_malloc0(plans_nb * 3 * sizeof(GLubyte) * 2); + vertex_base = g_malloc0(buffer_vertex_size * sizeof(GLfloat) * 2); + color_base = g_malloc0(buffer_colors_size * sizeof(GLfloat) * 2); + line_ndx = g_malloc0(buffer_lines_size * 2 * sizeof(GLubyte) * 2); + plan_ndx = g_malloc0(buffer_plans_size * 3 * sizeof(GLubyte) * 2); - - int vgap = 0, igap = 0, cgap = 0, dgap = 0; - - if (pref_mark_unit_space_first) dgap += 4; // 4 vertices for 2 diagonal lines - if (pref_mark_unit_space_last) dgap += 4; // 4 vertices for 2 diagonal lines - vgap += (6 * state_size); - igap += state_size * 2 + dgap; - cgap += (6 * state_size); - - if (pref_3D_xor_2D_space) compute_space_2D(state_size, + if (pref_3D_xor_2D_space) compute_space_2D(model_space_size_x, vertex_base, color_base, line_ndx, pref_mark_unit_space_first, pref_mark_unit_space_last); - else compute_space_3D(state_size, + else compute_space_3D(model_space_size_x, vertex_base, color_base, line_ndx, pref_mark_unit_space_first, pref_mark_unit_space_last); - if (pref_3D_xor_2D_space) for (int k = 0; k < state_size; k++) { - assert(k * 6 < vertex_nb); assert(k * 6 + 5 < vertex_nb); - assert(k * 6 < colors_nb); assert(k * 6 + 5 < colors_nb); - assert(k + 4 < entry->line_indices_nb); assert(k + 8 < entry->line_indices_nb); // TODO check this line ! - } else for (int k = 0; k < state_size; k++) { - assert(k * 12 < vertex_nb); assert(k * 12 + 11 < vertex_nb); - assert(k * 12 < colors_nb); assert(k * 12 + 11 < colors_nb); - assert(k + 8 < entry->line_indices_nb); assert(k + 16 < entry->line_indices_nb); // TODO check this line ! - } - assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); - assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); - - - /* compute_arrow_3D(state_size, */ + /* compute_arrow_3D(model_space_size_x, */ /* vertex_base, color_base, */ /* line_ndx, plan_ndx, */ /* vgap, igap, cgap, */ /* 1, 0, 0, 0, 0); */ - /* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */ - /* compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */ - /* compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */ - /* compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */ - /* compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */ - /* compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */ + /* vgap += 15; igap += 16; cgap += 15; ++model_arrows_nb; */ + /* compute_arrow_3D(entry, model_space_size_x, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++model_arrows_nb; */ + /* compute_arrow_3D(entry, model_space_size_x, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++model_arrows_nb; */ + /* compute_arrow_3D(entry, model_space_size_x, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++model_arrows_nb; */ + /* compute_arrow_3D(entry, model_space_size_x, vgap, igap, 1, 0, model_space_size_x - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++model_arrows_nb; */ + /* compute_arrow_3D(entry, model_space_size_x, vgap, igap, 1, 1, model_space_size_x - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++model_arrows_nb; */ GLuint vao, vertex_buffer, color_buffer; glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, vertex_nb * sizeof(vertex_base[0]), vertex_base, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, buffer_vertex_size * sizeof(vertex_base[0]), vertex_base, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // colors glGenBuffers(1, &color_buffer); glBindBuffer(GL_ARRAY_BUFFER, color_buffer); - glBufferData(GL_ARRAY_BUFFER, colors_nb * sizeof(color_base), color_base, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, buffer_colors_size * sizeof(color_base), color_base, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // We only use one VAO, so we always keep it bound @@ -708,8 +724,8 @@ void graphics_draw(const void *gl_area) glEnable(GL_DEPTH_TEST); - glDrawElements(GL_LINES, entry->line_indices_nb, GL_UNSIGNED_BYTE, line_ndx); - glDrawElements(GL_TRIANGLES, entry->plan_indices_nb, GL_UNSIGNED_BYTE, plan_ndx); + glDrawElements(GL_LINES, buffer_lines_ndx, GL_UNSIGNED_BYTE, line_ndx); + glDrawElements(GL_TRIANGLES, buffer_plans_ndx, GL_UNSIGNED_BYTE, plan_ndx); /* We finished using the buffers and program */ glDisableVertexAttribArray(0);