diff --git a/Makefile b/Makefile index 7d8487a..7012b2b 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,10 @@ NTHREADS= $(shell nproc) CC=gcc WARNINGS= -Wall -DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always +DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always -fsanitize=bounds -fstack-check + #-fsanitize=address \ + #-fsanitize=pointer-compare -fsanitize=pointer-subtract \ + #-fsanitize=leak -fsanitize=undefined -fsanitize=null -fsanitize=bounds OPTIMIZE= -O3 INCLUDE= $(shell pkg-config --cflags glib-2.0 libxml-2.0 gtk4) LIBS= $(shell pkg-config --libs glib-2.0 libxml-2.0 gtk4) -lGL -lGLU -lm -lepoxy -lX11 -lGLEW diff --git a/include/graphics.h b/include/graphics.h index e473cdd..d163bb4 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -266,6 +266,52 @@ static inline GLuint create_shader(const int stack_id, int type, const char *src return shader; } +static inline void print_stack(int stack_id) +{ + static int n = 0; + + printf("\n[n=%d]***************", n); + + printf("id = %d\tmode = %d\n", + graphic_stack[stack_id].id, + graphic_stack[stack_id].mode); + + printf("rotation_angles = "); + for (int i = 0; i < N_AXIS; i++) { + printf("%f\t", graphic_stack[stack_id].rotation_angles[i]); // Rotation angles on each axis + } + printf("\n"); + + printf("vao = %d\n", graphic_stack[stack_id].vao); + printf("position_buffer = %d\n", graphic_stack[stack_id].position_buffer); + printf("color_buffer = %d\n", graphic_stack[stack_id].color_buffer); + printf("program = %d\n", graphic_stack[stack_id].program); + printf("m = %d\n", graphic_stack[stack_id].m); + printf("v = %d\n", graphic_stack[stack_id].v); + printf("p = %d\n", graphic_stack[stack_id].p); + + printf("arrows_ptr = %p\n", graphic_stack[stack_id].arrows_ptr); + printf("arrows_nb = %ld\n", graphic_stack[stack_id].arrows_nb); + + printf("buffer_vertex_origin = %p\n", graphic_stack[stack_id].buffer_vertex_origin); + printf("buffer_colors_origin = %p\n", graphic_stack[stack_id].buffer_colors_origin); + printf("buffer_lines_origin = %p\n", graphic_stack[stack_id].buffer_lines_origin); + printf("buffer_plans_origin = %p\n", graphic_stack[stack_id].buffer_plans_origin); + + printf("buffer_vertex_size = %ld\n", graphic_stack[stack_id].buffer_vertex_size); + printf("buffer_colors_size = %ld\n", graphic_stack[stack_id].buffer_colors_size); + printf("buffer_lines_size = %ld\n", graphic_stack[stack_id].buffer_lines_size); + printf("buffer_plans_size = %ld\n", graphic_stack[stack_id].buffer_plans_size); + + printf("buffer_vertex_0_arrow = %p\n", graphic_stack[stack_id].buffer_vertex_0_arrow); + printf("buffer_colors_0_arrow = %p\n", graphic_stack[stack_id].buffer_colors_0_arrow); + printf("buffer_lines_0_arrow = %p\n", graphic_stack[stack_id].buffer_lines_0_arrow); + printf("buffer_plans_0_arrow = %p\n", graphic_stack[stack_id].buffer_plans_0_arrow); + + printf("********************\n"); + n++; +} + /*** TEST ***/ void main_test_graphics (const int stack_id); diff --git a/src/graphics/arrows.c b/src/graphics/arrows.c index f5fa798..9e0336c 100644 --- a/src/graphics/arrows.c +++ b/src/graphics/arrows.c @@ -43,6 +43,7 @@ int draw_one_arrow_vertex (const int stack_id, int arrow_y, int arrow_z) { + struct graphic_stack_t *stack = &graphic_stack[stack_id]; float max = fmax(space_X_int, space_Y_int); max = fmax(max, space_Z_int); @@ -112,8 +113,9 @@ static int rewrite_arrow (const int stack_id, int y, int z) { + struct graphic_stack_t *stack = &graphic_stack[stack_id]; printf("WARNING in rewrite_arrow() <> address or address / 5 ? (et pourquoi ?)\n"); - graphic_stack[stack_id].arrows_ptr[address].load = load; + stack->arrows_ptr[address].load = load; return 1; } @@ -132,21 +134,22 @@ static inline int create_arrow (int stack_id, int y, int z) { - void *newptr = g_realloc(graphic_stack[stack_id].arrows_ptr, (arrows_nb + 1) * sizeof(struct arrow_t)); + struct graphic_stack_t *stack = &graphic_stack[stack_id]; + void *newptr = g_realloc(stack->arrows_ptr, (arrows_nb + 1) * sizeof(struct arrow_t)); if (newptr) - graphic_stack[stack_id].arrows_ptr = newptr; + stack->arrows_ptr = newptr; else perror("In create arrow, can't allocate new arrow buffer !\n"); - graphic_stack[stack_id].arrows_ptr[arrows_nb].load = load; - graphic_stack[stack_id].arrows_ptr[arrows_nb].site = site; - graphic_stack[stack_id].arrows_ptr[arrows_nb].x = x; - graphic_stack[stack_id].arrows_ptr[arrows_nb].y = y; - graphic_stack[stack_id].arrows_ptr[arrows_nb].z = z; + stack->arrows_ptr[arrows_nb].load = load; + stack->arrows_ptr[arrows_nb].site = site; + stack->arrows_ptr[arrows_nb].x = x; + stack->arrows_ptr[arrows_nb].y = y; + stack->arrows_ptr[arrows_nb].z = z; draw_one_arrow_vertex(stack_id, space_X, space_Y, space_Z, load, site, x, y, z); - draw_one_arrow_line (stack_id, graphic_stack[stack_id].buffer_vertex_size / 3 - 2); + draw_one_arrow_line (stack_id, stack->buffer_vertex_size / 3 - 2); arrows_nb ++; @@ -165,11 +168,12 @@ static inline int erase_arrow (const int stack_id, GLint y, GLint z) { - if (arrows_nb == 0) assert (graphic_stack[stack_id].buffer_lines_size == - graphic_stack[stack_id].buffer_lines_0_arrow); + struct graphic_stack_t *stack = &graphic_stack[stack_id]; + if (arrows_nb == 0) assert (stack->buffer_lines_size == + stack->buffer_lines_0_arrow); if (arrows_nb == 0) { - graphic_stack[stack_id].buffer_lines_size = - graphic_stack[stack_id].buffer_lines_0_arrow; + stack->buffer_lines_size = + stack->buffer_lines_0_arrow; return 0; } @@ -178,41 +182,41 @@ static inline int erase_arrow (const int stack_id, if (arrow_address_in_list < arrows_nb) { - graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].load = - graphic_stack[stack_id].arrows_ptr[arrows_nb].load; - graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].site = - graphic_stack[stack_id].arrows_ptr[arrows_nb].site; - graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].x = - graphic_stack[stack_id].arrows_ptr[arrows_nb].x; - graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].y = - graphic_stack[stack_id].arrows_ptr[arrows_nb].y; - graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].z = - graphic_stack[stack_id].arrows_ptr[arrows_nb].z; + stack->arrows_ptr[arrow_address_in_list].load = + stack->arrows_ptr[arrows_nb].load; + stack->arrows_ptr[arrow_address_in_list].site = + stack->arrows_ptr[arrows_nb].site; + stack->arrows_ptr[arrow_address_in_list].x = + stack->arrows_ptr[arrows_nb].x; + stack->arrows_ptr[arrow_address_in_list].y = + stack->arrows_ptr[arrows_nb].y; + stack->arrows_ptr[arrow_address_in_list].z = + stack->arrows_ptr[arrows_nb].z; } for (long i = 0; i < 6; i++) - graphic_stack[stack_id].buffer_vertex_origin [graphic_stack[stack_id].buffer_vertex_0_arrow + arrow_address_in_list * 6 + i] - = graphic_stack[stack_id].buffer_vertex_origin [graphic_stack[stack_id].buffer_vertex_size - 6 + i]; + stack->buffer_vertex_origin [stack->buffer_vertex_0_arrow + arrow_address_in_list * 6 + i] + = stack->buffer_vertex_origin [stack->buffer_vertex_size - 6 + i]; for (long i = 0; i < 6; i++) - graphic_stack[stack_id].buffer_colors_origin [graphic_stack[stack_id].buffer_colors_0_arrow + arrow_address_in_list * 6 + i] - = graphic_stack[stack_id].buffer_colors_origin [graphic_stack[stack_id].buffer_colors_size - 6 + i]; + stack->buffer_colors_origin [stack->buffer_colors_0_arrow + arrow_address_in_list * 6 + i] + = stack->buffer_colors_origin [stack->buffer_colors_size - 6 + i]; - graphic_stack[stack_id].buffer_vertex_size -= 6; // <<< l'inverse de ce qui est fait dans : graphics_draw_vertex() - graphic_stack[stack_id].buffer_colors_size -= 6; // <<< l'inverse de ce qui est fait dans : graphics_draw_colors() - graphic_stack[stack_id].buffer_lines_size -= 2; // <<< l'inverse de ce qui est fait dans : graphics_draw_line() + stack->buffer_vertex_size -= 6; // <<< l'inverse de ce qui est fait dans : graphics_draw_vertex() + stack->buffer_colors_size -= 6; // <<< l'inverse de ce qui est fait dans : graphics_draw_colors() + stack->buffer_lines_size -= 2; // <<< l'inverse de ce qui est fait dans : graphics_draw_line() - void *new_arrows_vertex_ptr = g_realloc(graphic_stack[stack_id].buffer_vertex_origin, graphic_stack[stack_id].buffer_vertex_size * sizeof(GLfloat)); - if (new_arrows_vertex_ptr) graphic_stack[stack_id].buffer_vertex_origin = new_arrows_vertex_ptr; + void *new_arrows_vertex_ptr = g_realloc(stack->buffer_vertex_origin, stack->buffer_vertex_size * sizeof(GLfloat)); + if (new_arrows_vertex_ptr) stack->buffer_vertex_origin = new_arrows_vertex_ptr; else perror("In graphics.erase_arrow(), can't re_allocate for arrows vertex buffer.\n"); - void *new_arrows_colors_ptr = g_realloc(graphic_stack[stack_id].buffer_colors_origin, graphic_stack[stack_id].buffer_colors_size * sizeof(GLfloat)); - if (new_arrows_colors_ptr) graphic_stack[stack_id].buffer_colors_origin = new_arrows_colors_ptr; + void *new_arrows_colors_ptr = g_realloc(stack->buffer_colors_origin, stack->buffer_colors_size * sizeof(GLfloat)); + if (new_arrows_colors_ptr) stack->buffer_colors_origin = new_arrows_colors_ptr; else perror("In graphics.erase_arrow(), can't re_allocate for arrows colors buffer.\n"); /* Il ne faut pas réécrire ce qui suit: ces lignes dessinent maintenant à partir d'autres vertex */ - /* void *new_arrows_lines_ptr = g_realloc(graphic_stack[stack_id].buffer_lines_origin, graphic_stack[stack_id].buffer_lines_size * sizeof(GLfloat)); */ - /* if (new_arrows_lines_ptr) graphic_stack[stack_id].buffer_lines_origin = new_arrows_lines_ptr; */ + /* void *new_arrows_lines_ptr = g_realloc(stack->buffer_lines_origin, stack->buffer_lines_size * sizeof(GLfloat)); */ + /* if (new_arrows_lines_ptr) stack->buffer_lines_origin = new_arrows_lines_ptr; */ /* else perror("In graphics.erase_arrow(), can't re_allocate for arrows lines buffer.\n"); */ return arrows_nb; @@ -254,16 +258,17 @@ int set_arrow (int stack_id, int arrow_y, int arrow_z) { + struct graphic_stack_t *stack = &graphic_stack[stack_id]; int address = -1, current_weight = -1; for (int i = 0; i < arrows_nb; i++) - if ((site == graphic_stack[stack_id].arrows_ptr[i].site) - && (arrow_x == graphic_stack[stack_id].arrows_ptr[i].x) - && (arrow_y == graphic_stack[stack_id].arrows_ptr[i].y) - && (arrow_z == graphic_stack[stack_id].arrows_ptr[i].z)) + if ((site == stack->arrows_ptr[i].site) + && (arrow_x == stack->arrows_ptr[i].x) + && (arrow_y == stack->arrows_ptr[i].y) + && (arrow_z == stack->arrows_ptr[i].z)) { address = i; - current_weight = graphic_stack[stack_id].arrows_ptr[i].load; + current_weight = stack->arrows_ptr[i].load; break; } assert (address <= arrows_nb); diff --git a/src/graphics/draw.c b/src/graphics/draw.c index 36eb981..43aedb2 100644 --- a/src/graphics/draw.c +++ b/src/graphics/draw.c @@ -41,16 +41,26 @@ void graphics_draw_vertex (const int stack_id, GLfloat y, GLfloat z) { - graphic_stack[stack_id].buffer_vertex_origin = g_realloc (graphic_stack[stack_id].buffer_vertex_origin, - (graphic_stack[stack_id].buffer_vertex_size + 3) * sizeof(GLfloat)); + printf("stack_id is %d\n", stack_id); + printf("stack_id is %d\n", stack_id); + printf("graphic_stack is at %p\n", graphic_stack); + printf("graphic_stack[stack_id] is at %p\n", graphic_stack + stack_id); + struct graphic_stack_t *stack = &graphic_stack[stack_id]; - assert (graphic_stack[stack_id].buffer_vertex_origin); + //assert (stack->buffer_vertex_origin); - graphic_stack[stack_id].buffer_vertex_origin[graphic_stack[stack_id].buffer_vertex_size + 0] = x; - graphic_stack[stack_id].buffer_vertex_origin[graphic_stack[stack_id].buffer_vertex_size + 1] = y; - graphic_stack[stack_id].buffer_vertex_origin[graphic_stack[stack_id].buffer_vertex_size + 2] = z; + printf("Currently stack->buffer_vertex_origin @ %p\n", stack->buffer_vertex_origin); - graphic_stack[stack_id].buffer_vertex_size += 3; + stack->buffer_vertex_origin = + g_realloc (stack->buffer_vertex_origin, + (stack->buffer_vertex_size + 3) * sizeof(GLfloat)); + print_stack(stack_id); + + stack->buffer_vertex_origin[stack->buffer_vertex_size + 0] = x; + stack->buffer_vertex_origin[stack->buffer_vertex_size + 1] = y; + stack->buffer_vertex_origin[stack->buffer_vertex_size + 2] = z; + + stack->buffer_vertex_size += 3; } /* @@ -65,16 +75,18 @@ void graphics_draw_color (const int stack_id, GLfloat g, GLfloat b) { - graphic_stack[stack_id].buffer_colors_origin = g_realloc (graphic_stack[stack_id].buffer_colors_origin, - (graphic_stack[stack_id].buffer_colors_size + 3) * sizeof(GLfloat)); + struct graphic_stack_t *stack = &graphic_stack[stack_id]; - assert (graphic_stack[stack_id].buffer_colors_origin); + stack->buffer_colors_origin = g_realloc (stack->buffer_colors_origin, + (stack->buffer_colors_size + 3) * sizeof(GLfloat)); - graphic_stack[stack_id].buffer_colors_origin[graphic_stack[stack_id].buffer_colors_size + 0] = r; - graphic_stack[stack_id].buffer_colors_origin[graphic_stack[stack_id].buffer_colors_size + 1] = g; - graphic_stack[stack_id].buffer_colors_origin[graphic_stack[stack_id].buffer_colors_size + 2] = b; + assert (stack->buffer_colors_origin); - graphic_stack[stack_id].buffer_colors_size += 3; + stack->buffer_colors_origin[stack->buffer_colors_size + 0] = r; + stack->buffer_colors_origin[stack->buffer_colors_size + 1] = g; + stack->buffer_colors_origin[stack->buffer_colors_size + 2] = b; + + stack->buffer_colors_size += 3; } /* @@ -88,15 +100,17 @@ void graphics_draw_line (const int stack_id, GLuint a, GLuint b) { - graphic_stack[stack_id].buffer_lines_origin = g_realloc (graphic_stack[stack_id].buffer_lines_origin, - (graphic_stack[stack_id].buffer_lines_size + 2) * sizeof(GLuint)); + struct graphic_stack_t *stack = &graphic_stack[stack_id]; - assert (graphic_stack[stack_id].buffer_lines_origin); + stack->buffer_lines_origin = g_realloc (stack->buffer_lines_origin, + (stack->buffer_lines_size + 2) * sizeof(GLuint)); - graphic_stack[stack_id].buffer_lines_origin[graphic_stack[stack_id].buffer_lines_size + 0] = a; - graphic_stack[stack_id].buffer_lines_origin[graphic_stack[stack_id].buffer_lines_size + 1] = b; + assert (stack->buffer_lines_origin); - graphic_stack[stack_id].buffer_lines_size += 2; + stack->buffer_lines_origin[stack->buffer_lines_size + 0] = a; + stack->buffer_lines_origin[stack->buffer_lines_size + 1] = b; + + stack->buffer_lines_size += 2; } /* @@ -111,16 +125,18 @@ void graphics_draw_plan (const int stack_id, GLuint b, GLuint c) { - graphic_stack[stack_id].buffer_plans_origin = g_realloc (graphic_stack[stack_id].buffer_plans_origin, - (graphic_stack[stack_id].buffer_plans_size + 3) * sizeof(GLuint)); + struct graphic_stack_t *stack = &graphic_stack[stack_id]; - assert (graphic_stack[stack_id].buffer_plans_origin); + stack->buffer_plans_origin = g_realloc (stack->buffer_plans_origin, + (stack->buffer_plans_size + 3) * sizeof(GLuint)); - graphic_stack[stack_id].buffer_plans_origin[graphic_stack[stack_id].buffer_plans_size + 0] = a; - graphic_stack[stack_id].buffer_plans_origin[graphic_stack[stack_id].buffer_plans_size + 1] = b; - graphic_stack[stack_id].buffer_plans_origin[graphic_stack[stack_id].buffer_plans_size + 2] = c; + assert (stack->buffer_plans_origin); - graphic_stack[stack_id].buffer_plans_size += 3; + stack->buffer_plans_origin[stack->buffer_plans_size + 0] = a; + stack->buffer_plans_origin[stack->buffer_plans_size + 1] = b; + stack->buffer_plans_origin[stack->buffer_plans_size + 2] = c; + + stack->buffer_plans_size += 3; } /* @@ -132,6 +148,8 @@ void graphics_draw_plan (const int stack_id, */ void graphics_draw(const int stack_id) { + struct graphic_stack_t *stack = &graphic_stack[stack_id]; + float m[16]; float v[16]; float p[16]; @@ -142,14 +160,14 @@ void graphics_draw(const int stack_id) * rotation angles specified through the GtkRange widgets */ compute_mvp(p, - graphic_stack[stack_id].rotation_angles[X_AXIS], - graphic_stack[stack_id].rotation_angles[Y_AXIS], - graphic_stack[stack_id].rotation_angles[Z_AXIS]); + stack->rotation_angles[X_AXIS], + stack->rotation_angles[Y_AXIS], + stack->rotation_angles[Z_AXIS]); compute_i(m); compute_i(v); /* Use our shaders */ - glUseProgram(graphic_stack[stack_id].program); + glUseProgram(stack->program); // if (pref_show_space_design_mix_colors == 0) glClearColor(0, 0, 0, 0.4f); @@ -157,24 +175,24 @@ void graphics_draw(const int stack_id) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Update the "mvp" matrix we use in the shader */ - glUniformMatrix4fv(graphic_stack[stack_id].m, 1, GL_FALSE, &m[0]); - glUniformMatrix4fv(graphic_stack[stack_id].v, 1, GL_FALSE, &v[0]); - glUniformMatrix4fv(graphic_stack[stack_id].p, 1, GL_FALSE, &p[0]); + glUniformMatrix4fv(stack->m, 1, GL_FALSE, &m[0]); + glUniformMatrix4fv(stack->v, 1, GL_FALSE, &v[0]); + glUniformMatrix4fv(stack->p, 1, GL_FALSE, &p[0]); /* Use the vertices in our buffer */ glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, graphic_stack[stack_id].position_buffer); + glBindBuffer(GL_ARRAY_BUFFER, stack->position_buffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); // couleurs glEnableVertexAttribArray(1); - glBindBuffer(GL_ARRAY_BUFFER, graphic_stack[stack_id].color_buffer); + glBindBuffer(GL_ARRAY_BUFFER, stack->color_buffer); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); //glEnable(GL_DEPTH_TEST); - glDrawElements(GL_LINES, graphic_stack[stack_id].buffer_lines_size, GL_UNSIGNED_INT, graphic_stack[stack_id].buffer_lines_origin); - glDrawElements(GL_TRIANGLES, graphic_stack[stack_id].buffer_plans_size, GL_UNSIGNED_INT, graphic_stack[stack_id].buffer_plans_origin); + glDrawElements(GL_LINES, stack->buffer_lines_size, GL_UNSIGNED_INT, stack->buffer_lines_origin); + glDrawElements(GL_TRIANGLES, stack->buffer_plans_size, GL_UNSIGNED_INT, stack->buffer_plans_origin); /* We finished using the buffers and program */ glDisableVertexAttribArray(0); diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index a8d493d..cd2e8db 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -107,11 +107,12 @@ static void graphics_debug_callback(GLenum source, GLenum type, GLuint id, int graphics_init(void) { int cur_id = 0; + struct graphic_stack_t *stack; g_printerr("[debug] graphics_init()\n"); if (graphic_stack == NULL) { - graphic_stack = g_malloc(sizeof(struct graphic_stack_t)); + graphic_stack = g_malloc0(sizeof(struct graphic_stack_t)); graphic_stack_size = 1; } else { // Check if there are free slots @@ -134,7 +135,8 @@ int graphics_init(void) graphic_stack, graphic_stack_size); - graphic_stack[cur_id].id = cur_id; + stack = &graphic_stack[cur_id]; + stack->id = cur_id; glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); @@ -143,10 +145,14 @@ int graphics_init(void) if (!graphics_init_shaders(cur_id)) return -1; + print_stack(cur_id); + graphics_init_buffers(cur_id); glDebugMessageCallback(graphics_debug_callback, NULL); + print_stack(cur_id); + return cur_id; } @@ -159,7 +165,7 @@ int graphics_init(void) */ bool graphics_shutdown(const int id) { - struct graphic_stack_t stack; + struct graphic_stack_t *stack; if (id == 0 || id >= graphic_stack_size || @@ -167,16 +173,16 @@ bool graphics_shutdown(const int id) graphic_stack == NULL) return false; - stack = graphic_stack[id]; + stack = &graphic_stack[id]; - glDeleteBuffers(1, &stack.position_buffer); - glDeleteBuffers(1, &stack.color_buffer); - glDeleteProgram(stack.program); + glDeleteBuffers(1, &stack->position_buffer); + glDeleteBuffers(1, &stack->color_buffer); + glDeleteProgram(stack->program); - g_free(stack.buffer_vertex_origin); - g_free(stack.buffer_colors_origin); - g_free(stack.buffer_lines_origin); - g_free(stack.buffer_plans_origin); + g_free(stack->buffer_vertex_origin); + g_free(stack->buffer_colors_origin); + g_free(stack->buffer_lines_origin); + g_free(stack->buffer_plans_origin); if (graphic_stack_size == 1) { free(graphic_stack); @@ -220,11 +226,11 @@ void main_test_graphics (const int stack_id) /*------------------------------------------------------------------------*/ - + struct graphic_stack_t *stack = &graphic_stack[stack_id]; srand(time(NULL)); int rand(void); void srand(unsigned int seed); int randomize = 10, console = 0, space_X = 1, space_Y = 1, space_Z = 1; - graphic_stack[stack_id].arrows_nb = 0; + stack->arrows_nb = 0; if (randomize) { space_X = 1 + rand() % randomize; @@ -241,19 +247,19 @@ void main_test_graphics (const int stack_id) /*------------------------------------------------------------------------*/ - draw_space_ridges_vertex (graphic_stack[stack_id].buffer_vertex_size, - space_X, space_Y, space_Z, console); - draw_space_ridges_lines (console); - draw_grids_on_space_faces_vertex (space_X, space_Y, space_Z, console); - draw_grids_on_space_faces_lines (graphic_stack[stack_id].buffer_lines_size, - space_X, space_Y, space_Z, console); + draw_space_ridges_vertex (stack_id, stack->buffer_vertex_size, + space_X, space_Y, space_Z); + draw_space_ridges_lines (stack_id); + draw_grids_on_space_faces_vertex (stack_id, space_X, space_Y, space_Z); + draw_grids_on_space_faces_lines (stack_id, stack->buffer_lines_size, + space_X, space_Y, space_Z); - graphic_stack[stack_id].buffer_vertex_0_arrow = - graphic_stack[stack_id].buffer_vertex_size; - graphic_stack[stack_id].buffer_colors_0_arrow = - graphic_stack[stack_id].buffer_colors_size; - graphic_stack[stack_id].buffer_lines_0_arrow = - graphic_stack[stack_id].buffer_lines_size; + stack->buffer_vertex_0_arrow = + stack->buffer_vertex_size; + stack->buffer_colors_0_arrow = + stack->buffer_colors_size; + stack->buffer_lines_0_arrow = + stack->buffer_lines_size; /*------------------------------------------------------------------------*/ @@ -265,8 +271,8 @@ void main_test_graphics (const int stack_id) if (randomize) for (int i = 0; i < specified_arrows_nb; i++) { - graphic_stack[stack_id].arrows_nb = - set_arrow (stack_id, graphic_stack[stack_id].arrows_nb, space_X, space_Y, space_Z, + stack->arrows_nb = + set_arrow (stack_id, stack->arrows_nb, space_X, space_Y, space_Z, 1, // load rand() % 6, // site rand() % space_X, // x @@ -277,10 +283,10 @@ void main_test_graphics (const int stack_id) if (i > 0 && i % 100000 == 0) printf("\n"); } else for (int i = 0; i < 6; i++) - graphic_stack[stack_id].arrows_nb = - set_arrow (stack_id, graphic_stack[stack_id].arrows_nb, 1, 1, 1, 1, i, 0, 0, 0); + stack->arrows_nb = + set_arrow (stack_id, stack->arrows_nb, 1, 1, 1, 1, i, 0, 0, 0); - int max_arrows_nb = graphic_stack[stack_id].arrows_nb; + int max_arrows_nb = stack->arrows_nb; /*--------------------------------------------------------------------------------*/ @@ -290,8 +296,8 @@ void main_test_graphics (const int stack_id) /*--------------------------------------------------------------------------------*/ - free(graphic_stack[stack_id].arrows_ptr); - graphic_stack[stack_id].arrows_ptr = NULL; - graphic_stack[stack_id].arrows_nb = 0; + free(stack->arrows_ptr); + stack->arrows_ptr = NULL; + stack->arrows_nb = 0; } diff --git a/src/graphics/init.c b/src/graphics/init.c index d7ceb0f..00a331d 100644 --- a/src/graphics/init.c +++ b/src/graphics/init.c @@ -37,8 +37,7 @@ */ void graphics_init_buffers(const int stack_id) { - - // XXX TODO get stack from stack_id + struct graphic_stack_t *stack = &graphic_stack[stack_id]; //XXX main_test_graphics (stack_id); @@ -48,9 +47,9 @@ void graphics_init_buffers(const int stack_id) glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, - graphic_stack[stack_id].buffer_vertex_size * - sizeof(graphic_stack[stack_id].buffer_vertex_origin[0]), - graphic_stack[stack_id].buffer_vertex_origin, + stack->buffer_vertex_size * + sizeof(stack->buffer_vertex_origin[0]), + stack->buffer_vertex_origin, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -58,9 +57,9 @@ void graphics_init_buffers(const int stack_id) // colors glGenBuffers(1, &color_buffer); glBindBuffer(GL_ARRAY_BUFFER, color_buffer); - glBufferData(GL_ARRAY_BUFFER, graphic_stack[stack_id].buffer_colors_size * - sizeof(graphic_stack[stack_id].buffer_colors_origin[0]), - graphic_stack[stack_id].buffer_colors_origin, + glBufferData(GL_ARRAY_BUFFER, stack->buffer_colors_size * + sizeof(stack->buffer_colors_origin[0]), + stack->buffer_colors_origin, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -68,9 +67,9 @@ void graphics_init_buffers(const int stack_id) glGenVertexArrays(1, &vao); glBindVertexArray(vao); - graphic_stack[stack_id].vao = vao; - graphic_stack[stack_id].position_buffer = vertex_buffer; - graphic_stack[stack_id].color_buffer = color_buffer; + stack->vao = vao; + stack->position_buffer = vertex_buffer; + stack->color_buffer = color_buffer; } /* @@ -82,6 +81,7 @@ void graphics_init_buffers(const int stack_id) */ bool graphics_init_shaders(const int stack_id) { + struct graphic_stack_t *stack = &graphic_stack[stack_id]; char *vertex_shader; char *fragment_shader; int status; @@ -98,7 +98,7 @@ bool graphics_init_shaders(const int stack_id) vertex = create_shader(stack_id, GL_VERTEX_SHADER, vertex_shader); if(vertex == 0) { - graphic_stack[stack_id].program = 0; + stack->program = 0; g_free(vertex_shader); return false; } @@ -111,7 +111,7 @@ bool graphics_init_shaders(const int stack_id) if(fragment == 0) { glDeleteShader(vertex); - graphic_stack[stack_id].program = 0; + stack->program = 0; g_free(vertex_shader); g_free(fragment_shader); return false; @@ -163,10 +163,10 @@ bool graphics_init_shaders(const int stack_id) glDeleteShader(vertex); glDeleteShader(fragment); - graphic_stack[stack_id].program = program; - graphic_stack[stack_id].m = m; - graphic_stack[stack_id].v = v; - graphic_stack[stack_id].p = p; + stack->program = program; + stack->m = m; + stack->v = v; + stack->p = p; g_free(vertex_shader); g_free(fragment_shader); diff --git a/src/ui/events.c b/src/ui/events.c index 303875e..3e1b167 100644 --- a/src/ui/events.c +++ b/src/ui/events.c @@ -248,10 +248,6 @@ void on_glarea_realize(GtkWidget *widget) return; } - // The main "draw" call for GtkGLArea - g_signal_connect(GTK_GL_AREA(widget), - "render", - G_CALLBACK(on_glarea_render), NULL); gtk_gl_area_set_auto_render(GTK_GL_AREA(widget), true); } diff --git a/src/ui/graphics.c b/src/ui/graphics.c index 7770f7d..b467a3e 100644 --- a/src/ui/graphics.c +++ b/src/ui/graphics.c @@ -83,11 +83,20 @@ long ui_is_graphic_stack_ready(void *container_widget) */ bool ui_init_graphic_stack(void *container_widget) { + g_printerr("[debug] ui_init_graphic_stack()\n"); + + g_printerr("[debug] ui_init_graphic_stack() : target is %p\n", container_widget); + // look for stack_index entry for (int i = 0; i < stack_index_size; i++) { + g_printerr("[debug] ui_init_graphic_stack() : i is %d\n", i); + g_printerr("[debug] ui_init_graphic_stack() : target would be %p\n", + stack_index[i].container_widget); if (stack_index[i].container_widget == (void *)container_widget) { stack_index[i].stack_id = graphics_init(); - if (stack_index[i].stack_id) + g_printerr("[debug] ui_init_graphic_stack() : stack_id is %d\n", + stack_index[i].stack_id); + if (stack_index[i].stack_id > 0) return true; else return false; @@ -248,6 +257,8 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget) assert(target_widget); + g_printerr("[debug] ui_setup_glarea() : target is %p\n", target_widget); + if (stack_index == NULL) { stack_index = g_malloc(sizeof(struct stack_index_t)); stack_index_size = 1; @@ -262,10 +273,12 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget) stack_index = g_realloc(stack_index, ++stack_index_size * sizeof(struct stack_index_t)); - stack_index[stack_index_size-1].container_widget = - (void*)target_widget; } + stack_index[stack_index_size-1].container_widget = + (void*)target_widget; + g_printerr("[debug] ui_setup_glarea() : set target to\n", target_widget); + g_printerr("[debug] ui_setup_glarea() : stack_index (@0x%p) had %ld elements\n", stack_index, stack_index_size); @@ -273,33 +286,41 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget) gl_area = GTK_WIDGET(gtk_gl_area_new()); assert(gl_area); - gtk_box_append(GTK_BOX(target_widget), gl_area); + // The main "draw" call for GtkGLArea + g_signal_connect(GTK_GL_AREA(gl_area), + "render", + G_CALLBACK(on_glarea_render), NULL); - // We need to initialize and free GL resources, so we use - // the realize and unrealize signals on the widget - // g_signal_connect(gl_area, "realize", G_CALLBACK(on_glarea_realize), NULL); + g_signal_connect(gl_area, "unrealize", G_CALLBACK(on_glarea_unrealize), NULL); + gtk_box_append(GTK_BOX(target_widget), gl_area); + //gtk_widget_show(GTK_WIDGET(gl_area)); - gtk_widget_set_hexpand(gl_area, TRUE); - gtk_widget_set_vexpand(gl_area, TRUE); - gtk_widget_set_halign(gl_area, GTK_ALIGN_CENTER); - gtk_widget_set_valign(gl_area, GTK_ALIGN_CENTER); - gtk_widget_set_size_request(gl_area, 500, 500); + // We need to initialize and free GL resources, so we use + // the realize and unrealize signals on the widget + // - for(int i = 0; i < N_AXIS; i++) - gtk_box_append(GTK_BOX(target_widget), create_axis_slider(i)); + + /* gtk_widget_set_hexpand(gl_area, TRUE); */ + /* gtk_widget_set_vexpand(gl_area, TRUE); */ + /* gtk_widget_set_halign(gl_area, GTK_ALIGN_CENTER); */ + /* gtk_widget_set_valign(gl_area, GTK_ALIGN_CENTER); */ + /* gtk_widget_set_size_request(gl_area, 500, 500); */ /* gtk_gl_area_set_required_version(GTK_GL_AREA(gl_area), */ /* GL_TARGET_MINOR_VERSION, */ /* GL_TARGET_MAJOR_VERSION); */ - gtk_widget_show(GTK_WIDGET(gl_area)); + // Create sliders + for(int i = 0; i < N_AXIS; i++) + gtk_box_append(GTK_BOX(target_widget), create_axis_slider(i)); + return true; }