diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index c29c87d..2eff4c0 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -38,17 +38,18 @@ #include "../../include/base.h" #include "../../include/ui.h" #include "../../include/graphics.h" -#include "../../include/graphics_cube.h" #define VERTEX_SHADER_FILE "src/shaders/shader.vert" #define FRAG_SHADER_FILE "src/shaders/shader.frag" #define K 0 GLfloat *vertex_base = NULL; +GLfloat *color_base = NULL; GLubyte *indices = NULL; int dim = 0; int state_size = 0; int vertex_nb = 0; +int colors_nb = 0; int segments_nb = 0; int arrows_nb = 0; @@ -314,10 +315,11 @@ bool graphics_shutdown(const void *gl_area) entry = NULL; gl_area_array = g_realloc(gl_area_array, - (gl_area_size()) + (gl_area_size()) * sizeof(struct gl_area_entry *)); g_free(vertex_base); + g_free(color_base); g_free(indices); return true; } @@ -722,6 +724,8 @@ static bool compute_space_3D(struct gl_area_entry *entry, int state_size){ assert(k * 12 < vertex_nb); assert(k * 12 + 11 < vertex_nb); + assert(k * 12 < colors_nb); + assert(k * 12 + 11 < colors_nb); *(vertex_base + k * 12 + 0) = - x; *(vertex_base + k * 12 + 1) = 1.0f / state_size; @@ -738,6 +742,32 @@ static bool compute_space_3D(struct gl_area_entry *entry, int state_size){ *(vertex_base + k * 12 + 9) = - x; *(vertex_base + k * 12 + 10) = - 1.0f / state_size; *(vertex_base + k * 12 + 11) = 1.0f / state_size; + + *(color_base + k * 12 + 0) = 0.8f; // rouge + *(color_base + k * 12 + 1) = 0.2f; + *(color_base + k * 12 + 2) = 0.2f; + + *(color_base + k * 12 + 3) = 0.8f; // jaune + *(color_base + k * 12 + 4) = 0.8f; + *(color_base + k * 12 + 5) = 0.2f; + + *(color_base + k * 12 + 6) = 0.2f; // vert + *(color_base + k * 12 + 7) = 0.8f; + *(color_base + k * 12 + 8) = 0.2f; + + *(color_base + k * 12 + 9) = 0.8f; // magenta + *(color_base + k * 12 + 10) = 0.2f; + *(color_base + k * 12 + 11) = 0.8f; + +/* 0.8, 0.8, 0.8, // blanc */ +/* 0.8, 0.8, 0.2, // jaune */ +/* 0.8, 0.2, 0.2, // rouge */ +/* 0.2, 0.2, 0.2, // noir */ +/* 0.2, 0.2, 0.2, // gris */ +/* 0.2, 0.8, 0.8, // cyan */ +/* 0.2, 0.8, 0.2, // vert */ +/* 0.8, 0.2, 0.8, // magenta */ + } if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size); @@ -802,46 +832,48 @@ void graphics_init_buffers(const void *gl_area) { struct gl_area_entry *entry; GLuint vao, vertex_buffer, color_buffer; - int vgap = 0, igap = 0; + int vgap = 0, igap = 0, cgap = 0; entry = find_entry_from_ptr(gl_area); //{1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21}; dim = 1; state_size = 13; // 2 < state_size < 32 - arrows_nb = 6; + arrows_nb = 4; vertex_nb = (state_size + 1) * 6 + arrows_nb * 9; vertex_nb = (state_size + 1) * 12 + arrows_nb * 15; + colors_nb = (state_size + 1) * 12 + arrows_nb * 0; // TODO segments_nb = (2 + state_size) + (arrows_nb * 3); segments_nb = (4 + state_size * 4) + (arrows_nb * 8) + 2; // + 2; pour les 2 diagonales entry->indices_nb = segments_nb * 2; - printf("Initialization of buffers with %u indices and %u vertices & state_size = %d\n", - entry->indices_nb, vertex_nb, state_size); + printf("Initialization of buffers with %u indices, %u vertices and %u colors & state_size = %d\n", + entry->indices_nb, vertex_nb, colors_nb, state_size); //XXX g_malloc vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2); + color_base = g_malloc0(colors_nb * sizeof(GLfloat) * 2); indices = g_malloc0(segments_nb * 2 * sizeof(GLubyte) * 2); -// compute_space_2D(entry, state_size); vgap += (6 * state_size); igap += state_size * 2 + 4; +// compute_space_2D(entry, state_size); vgap += (6 * state_size); igap += state_size * 2 + 4; cgap += (6 * state_size); - compute_space_3D(entry, state_size); vgap += (12 * state_size); igap += state_size * 8 + 8; + compute_space_3D(entry, state_size); vgap += (12 * state_size); igap += state_size * 8 + 8; cgap += (12 * state_size); - /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 9; igap += 6; ++arrows_nb; */ - /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 9; igap += 6; */ - /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 9; igap += 6; */ - /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 9; igap += 6; */ - /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 9; igap += 6; */ - /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 9; igap += 6; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */ - compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; ++arrows_nb; - compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 15; igap += 16; - compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; - compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; - compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 15; igap += 16; - compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 15; igap += 16; + compute_arrow_3D(entry, state_size, vgap, igap, 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; // We only use one VAO, so we always keep it bound glGenVertexArrays(1, &vao); @@ -852,16 +884,17 @@ void graphics_init_buffers(const void *gl_area) glBufferData(GL_ARRAY_BUFFER, vertex_nb * 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, sizeof(color_base), color_base, GL_STATIC_DRAW); */ - /* glBindBuffer(GL_ARRAY_BUFFER, 0); */ + glGenBuffers(1, &color_buffer); + glBindBuffer(GL_ARRAY_BUFFER, color_buffer); + glBufferData(GL_ARRAY_BUFFER, colors_nb * sizeof(color_base), color_base, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); if(entry != NULL) { entry->vao = vao; entry->position_buffer = vertex_buffer; - //entry->color_buffer = color_buffer; + entry->color_buffer = color_buffer; } } @@ -915,9 +948,9 @@ void graphics_draw(const void *gl_area) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); // couleurs - /* glEnableVertexAttribArray(1); */ - /* glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer); */ - /* glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); */ + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); glEnable(GL_DEPTH_TEST);