WIP: no image but no more segfault

This commit is contained in:
Adrien Bourmault 2023-12-20 18:01:27 +01:00
parent 02880d3dde
commit 9bb7cefc2f
Signed by: neox
GPG Key ID: 2974E1D5F25DFCC8
8 changed files with 244 additions and 149 deletions

View File

@ -41,7 +41,10 @@ NTHREADS= $(shell nproc)
CC=gcc CC=gcc
WARNINGS= -Wall 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 OPTIMIZE= -O3
INCLUDE= $(shell pkg-config --cflags glib-2.0 libxml-2.0 gtk4) 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 LIBS= $(shell pkg-config --libs glib-2.0 libxml-2.0 gtk4) -lGL -lGLU -lm -lepoxy -lX11 -lGLEW

View File

@ -266,6 +266,52 @@ static inline GLuint create_shader(const int stack_id, int type, const char *src
return shader; 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 ***/ /*** TEST ***/
void main_test_graphics (const int stack_id); void main_test_graphics (const int stack_id);

View File

@ -43,6 +43,7 @@ int draw_one_arrow_vertex (const int stack_id,
int arrow_y, int arrow_y,
int arrow_z) int arrow_z)
{ {
struct graphic_stack_t *stack = &graphic_stack[stack_id];
float max = fmax(space_X_int, space_Y_int); float max = fmax(space_X_int, space_Y_int);
max = fmax(max, space_Z_int); max = fmax(max, space_Z_int);
@ -112,8 +113,9 @@ static int rewrite_arrow (const int stack_id,
int y, int y,
int z) int z)
{ {
struct graphic_stack_t *stack = &graphic_stack[stack_id];
printf("WARNING in rewrite_arrow() <> address or address / 5 ? (et pourquoi ?)\n"); 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; return 1;
} }
@ -132,21 +134,22 @@ static inline int create_arrow (int stack_id,
int y, int y,
int z) 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) if (newptr)
graphic_stack[stack_id].arrows_ptr = newptr; stack->arrows_ptr = newptr;
else else
perror("In create arrow, can't allocate new arrow buffer !\n"); perror("In create arrow, can't allocate new arrow buffer !\n");
graphic_stack[stack_id].arrows_ptr[arrows_nb].load = load; stack->arrows_ptr[arrows_nb].load = load;
graphic_stack[stack_id].arrows_ptr[arrows_nb].site = site; stack->arrows_ptr[arrows_nb].site = site;
graphic_stack[stack_id].arrows_ptr[arrows_nb].x = x; stack->arrows_ptr[arrows_nb].x = x;
graphic_stack[stack_id].arrows_ptr[arrows_nb].y = y; stack->arrows_ptr[arrows_nb].y = y;
graphic_stack[stack_id].arrows_ptr[arrows_nb].z = z; 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_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 ++; arrows_nb ++;
@ -165,11 +168,12 @@ static inline int erase_arrow (const int stack_id,
GLint y, GLint y,
GLint z) GLint z)
{ {
if (arrows_nb == 0) assert (graphic_stack[stack_id].buffer_lines_size == struct graphic_stack_t *stack = &graphic_stack[stack_id];
graphic_stack[stack_id].buffer_lines_0_arrow); if (arrows_nb == 0) assert (stack->buffer_lines_size ==
stack->buffer_lines_0_arrow);
if (arrows_nb == 0) { if (arrows_nb == 0) {
graphic_stack[stack_id].buffer_lines_size = stack->buffer_lines_size =
graphic_stack[stack_id].buffer_lines_0_arrow; stack->buffer_lines_0_arrow;
return 0; return 0;
} }
@ -178,41 +182,41 @@ static inline int erase_arrow (const int stack_id,
if (arrow_address_in_list < arrows_nb) if (arrow_address_in_list < arrows_nb)
{ {
graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].load = stack->arrows_ptr[arrow_address_in_list].load =
graphic_stack[stack_id].arrows_ptr[arrows_nb].load; stack->arrows_ptr[arrows_nb].load;
graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].site = stack->arrows_ptr[arrow_address_in_list].site =
graphic_stack[stack_id].arrows_ptr[arrows_nb].site; stack->arrows_ptr[arrows_nb].site;
graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].x = stack->arrows_ptr[arrow_address_in_list].x =
graphic_stack[stack_id].arrows_ptr[arrows_nb].x; stack->arrows_ptr[arrows_nb].x;
graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].y = stack->arrows_ptr[arrow_address_in_list].y =
graphic_stack[stack_id].arrows_ptr[arrows_nb].y; stack->arrows_ptr[arrows_nb].y;
graphic_stack[stack_id].arrows_ptr[arrow_address_in_list].z = stack->arrows_ptr[arrow_address_in_list].z =
graphic_stack[stack_id].arrows_ptr[arrows_nb].z; stack->arrows_ptr[arrows_nb].z;
} }
for (long i = 0; i < 6; i++) 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] stack->buffer_vertex_origin [stack->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_size - 6 + i];
for (long i = 0; i < 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] stack->buffer_colors_origin [stack->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_size - 6 + i];
graphic_stack[stack_id].buffer_vertex_size -= 6; // <<< l'inverse de ce qui est fait dans : graphics_draw_vertex() stack->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() stack->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_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)); void *new_arrows_vertex_ptr = g_realloc(stack->buffer_vertex_origin, stack->buffer_vertex_size * sizeof(GLfloat));
if (new_arrows_vertex_ptr) graphic_stack[stack_id].buffer_vertex_origin = new_arrows_vertex_ptr; 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"); 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)); void *new_arrows_colors_ptr = g_realloc(stack->buffer_colors_origin, stack->buffer_colors_size * sizeof(GLfloat));
if (new_arrows_colors_ptr) graphic_stack[stack_id].buffer_colors_origin = new_arrows_colors_ptr; 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"); 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 */ /* 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)); */ /* void *new_arrows_lines_ptr = g_realloc(stack->buffer_lines_origin, stack->buffer_lines_size * sizeof(GLfloat)); */
/* if (new_arrows_lines_ptr) graphic_stack[stack_id].buffer_lines_origin = new_arrows_lines_ptr; */ /* 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"); */ /* else perror("In graphics.erase_arrow(), can't re_allocate for arrows lines buffer.\n"); */
return arrows_nb; return arrows_nb;
@ -254,16 +258,17 @@ int set_arrow (int stack_id,
int arrow_y, int arrow_y,
int arrow_z) int arrow_z)
{ {
struct graphic_stack_t *stack = &graphic_stack[stack_id];
int address = -1, current_weight = -1; int address = -1, current_weight = -1;
for (int i = 0; i < arrows_nb; i++) for (int i = 0; i < arrows_nb; i++)
if ((site == graphic_stack[stack_id].arrows_ptr[i].site) if ((site == stack->arrows_ptr[i].site)
&& (arrow_x == graphic_stack[stack_id].arrows_ptr[i].x) && (arrow_x == stack->arrows_ptr[i].x)
&& (arrow_y == graphic_stack[stack_id].arrows_ptr[i].y) && (arrow_y == stack->arrows_ptr[i].y)
&& (arrow_z == graphic_stack[stack_id].arrows_ptr[i].z)) && (arrow_z == stack->arrows_ptr[i].z))
{ {
address = i; address = i;
current_weight = graphic_stack[stack_id].arrows_ptr[i].load; current_weight = stack->arrows_ptr[i].load;
break; break;
} }
assert (address <= arrows_nb); assert (address <= arrows_nb);

View File

@ -41,16 +41,26 @@ void graphics_draw_vertex (const int stack_id,
GLfloat y, GLfloat y,
GLfloat z) GLfloat z)
{ {
graphic_stack[stack_id].buffer_vertex_origin = g_realloc (graphic_stack[stack_id].buffer_vertex_origin, printf("stack_id is %d\n", stack_id);
(graphic_stack[stack_id].buffer_vertex_size + 3) * sizeof(GLfloat)); 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; printf("Currently stack->buffer_vertex_origin @ %p\n", stack->buffer_vertex_origin);
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;
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 g,
GLfloat b) GLfloat b)
{ {
graphic_stack[stack_id].buffer_colors_origin = g_realloc (graphic_stack[stack_id].buffer_colors_origin, struct graphic_stack_t *stack = &graphic_stack[stack_id];
(graphic_stack[stack_id].buffer_colors_size + 3) * sizeof(GLfloat));
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; assert (stack->buffer_colors_origin);
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;
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 a,
GLuint b) GLuint b)
{ {
graphic_stack[stack_id].buffer_lines_origin = g_realloc (graphic_stack[stack_id].buffer_lines_origin, struct graphic_stack_t *stack = &graphic_stack[stack_id];
(graphic_stack[stack_id].buffer_lines_size + 2) * sizeof(GLuint));
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; assert (stack->buffer_lines_origin);
graphic_stack[stack_id].buffer_lines_origin[graphic_stack[stack_id].buffer_lines_size + 1] = b;
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 b,
GLuint c) GLuint c)
{ {
graphic_stack[stack_id].buffer_plans_origin = g_realloc (graphic_stack[stack_id].buffer_plans_origin, struct graphic_stack_t *stack = &graphic_stack[stack_id];
(graphic_stack[stack_id].buffer_plans_size + 3) * sizeof(GLuint));
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; assert (stack->buffer_plans_origin);
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;
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) void graphics_draw(const int stack_id)
{ {
struct graphic_stack_t *stack = &graphic_stack[stack_id];
float m[16]; float m[16];
float v[16]; float v[16];
float p[16]; float p[16];
@ -142,14 +160,14 @@ void graphics_draw(const int stack_id)
* rotation angles specified through the GtkRange widgets * rotation angles specified through the GtkRange widgets
*/ */
compute_mvp(p, compute_mvp(p,
graphic_stack[stack_id].rotation_angles[X_AXIS], stack->rotation_angles[X_AXIS],
graphic_stack[stack_id].rotation_angles[Y_AXIS], stack->rotation_angles[Y_AXIS],
graphic_stack[stack_id].rotation_angles[Z_AXIS]); stack->rotation_angles[Z_AXIS]);
compute_i(m); compute_i(m);
compute_i(v); compute_i(v);
/* Use our shaders */ /* Use our shaders */
glUseProgram(graphic_stack[stack_id].program); glUseProgram(stack->program);
// if (pref_show_space_design_mix_colors == 0) // if (pref_show_space_design_mix_colors == 0)
glClearColor(0, 0, 0, 0.4f); 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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Update the "mvp" matrix we use in the shader */ /* Update the "mvp" matrix we use in the shader */
glUniformMatrix4fv(graphic_stack[stack_id].m, 1, GL_FALSE, &m[0]); glUniformMatrix4fv(stack->m, 1, GL_FALSE, &m[0]);
glUniformMatrix4fv(graphic_stack[stack_id].v, 1, GL_FALSE, &v[0]); glUniformMatrix4fv(stack->v, 1, GL_FALSE, &v[0]);
glUniformMatrix4fv(graphic_stack[stack_id].p, 1, GL_FALSE, &p[0]); glUniformMatrix4fv(stack->p, 1, GL_FALSE, &p[0]);
/* Use the vertices in our buffer */ /* Use the vertices in our buffer */
glEnableVertexAttribArray(0); 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); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
// couleurs // couleurs
glEnableVertexAttribArray(1); 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); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
//glEnable(GL_DEPTH_TEST); //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_LINES, stack->buffer_lines_size, GL_UNSIGNED_INT, stack->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_TRIANGLES, stack->buffer_plans_size, GL_UNSIGNED_INT, stack->buffer_plans_origin);
/* We finished using the buffers and program */ /* We finished using the buffers and program */
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);

View File

@ -107,11 +107,12 @@ static void graphics_debug_callback(GLenum source, GLenum type, GLuint id,
int graphics_init(void) int graphics_init(void)
{ {
int cur_id = 0; int cur_id = 0;
struct graphic_stack_t *stack;
g_printerr("[debug] graphics_init()\n"); g_printerr("[debug] graphics_init()\n");
if (graphic_stack == NULL) { 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; graphic_stack_size = 1;
} else { } else {
// Check if there are free slots // Check if there are free slots
@ -134,7 +135,8 @@ int graphics_init(void)
graphic_stack, graphic_stack,
graphic_stack_size); 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);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
@ -143,10 +145,14 @@ int graphics_init(void)
if (!graphics_init_shaders(cur_id)) if (!graphics_init_shaders(cur_id))
return -1; return -1;
print_stack(cur_id);
graphics_init_buffers(cur_id); graphics_init_buffers(cur_id);
glDebugMessageCallback(graphics_debug_callback, NULL); glDebugMessageCallback(graphics_debug_callback, NULL);
print_stack(cur_id);
return cur_id; return cur_id;
} }
@ -159,7 +165,7 @@ int graphics_init(void)
*/ */
bool graphics_shutdown(const int id) bool graphics_shutdown(const int id)
{ {
struct graphic_stack_t stack; struct graphic_stack_t *stack;
if (id == 0 || if (id == 0 ||
id >= graphic_stack_size || id >= graphic_stack_size ||
@ -167,16 +173,16 @@ bool graphics_shutdown(const int id)
graphic_stack == NULL) graphic_stack == NULL)
return false; return false;
stack = graphic_stack[id]; stack = &graphic_stack[id];
glDeleteBuffers(1, &stack.position_buffer); glDeleteBuffers(1, &stack->position_buffer);
glDeleteBuffers(1, &stack.color_buffer); glDeleteBuffers(1, &stack->color_buffer);
glDeleteProgram(stack.program); glDeleteProgram(stack->program);
g_free(stack.buffer_vertex_origin); g_free(stack->buffer_vertex_origin);
g_free(stack.buffer_colors_origin); g_free(stack->buffer_colors_origin);
g_free(stack.buffer_lines_origin); g_free(stack->buffer_lines_origin);
g_free(stack.buffer_plans_origin); g_free(stack->buffer_plans_origin);
if (graphic_stack_size == 1) { if (graphic_stack_size == 1) {
free(graphic_stack); 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); 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; 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) { if (randomize) {
space_X = 1 + rand() % 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, draw_space_ridges_vertex (stack_id, stack->buffer_vertex_size,
space_X, space_Y, space_Z, console); space_X, space_Y, space_Z);
draw_space_ridges_lines (console); draw_space_ridges_lines (stack_id);
draw_grids_on_space_faces_vertex (space_X, space_Y, space_Z, console); draw_grids_on_space_faces_vertex (stack_id, space_X, space_Y, space_Z);
draw_grids_on_space_faces_lines (graphic_stack[stack_id].buffer_lines_size, draw_grids_on_space_faces_lines (stack_id, stack->buffer_lines_size,
space_X, space_Y, space_Z, console); space_X, space_Y, space_Z);
graphic_stack[stack_id].buffer_vertex_0_arrow = stack->buffer_vertex_0_arrow =
graphic_stack[stack_id].buffer_vertex_size; stack->buffer_vertex_size;
graphic_stack[stack_id].buffer_colors_0_arrow = stack->buffer_colors_0_arrow =
graphic_stack[stack_id].buffer_colors_size; stack->buffer_colors_size;
graphic_stack[stack_id].buffer_lines_0_arrow = stack->buffer_lines_0_arrow =
graphic_stack[stack_id].buffer_lines_size; 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++) if (randomize) for (int i = 0; i < specified_arrows_nb; i++)
{ {
graphic_stack[stack_id].arrows_nb = stack->arrows_nb =
set_arrow (stack_id, graphic_stack[stack_id].arrows_nb, space_X, space_Y, space_Z, set_arrow (stack_id, stack->arrows_nb, space_X, space_Y, space_Z,
1, // load 1, // load
rand() % 6, // site rand() % 6, // site
rand() % space_X, // x rand() % space_X, // x
@ -277,10 +283,10 @@ void main_test_graphics (const int stack_id)
if (i > 0 && i % 100000 == 0) printf("\n"); if (i > 0 && i % 100000 == 0) printf("\n");
} }
else for (int i = 0; i < 6; i++) else for (int i = 0; i < 6; i++)
graphic_stack[stack_id].arrows_nb = stack->arrows_nb =
set_arrow (stack_id, graphic_stack[stack_id].arrows_nb, 1, 1, 1, 1, i, 0, 0, 0); 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); free(stack->arrows_ptr);
graphic_stack[stack_id].arrows_ptr = NULL; stack->arrows_ptr = NULL;
graphic_stack[stack_id].arrows_nb = 0; stack->arrows_nb = 0;
} }

View File

@ -37,8 +37,7 @@
*/ */
void graphics_init_buffers(const int stack_id) void graphics_init_buffers(const int stack_id)
{ {
struct graphic_stack_t *stack = &graphic_stack[stack_id];
// XXX TODO get stack from stack_id
//XXX //XXX
main_test_graphics (stack_id); main_test_graphics (stack_id);
@ -48,9 +47,9 @@ void graphics_init_buffers(const int stack_id)
glGenBuffers(1, &vertex_buffer); glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, glBufferData(GL_ARRAY_BUFFER,
graphic_stack[stack_id].buffer_vertex_size * stack->buffer_vertex_size *
sizeof(graphic_stack[stack_id].buffer_vertex_origin[0]), sizeof(stack->buffer_vertex_origin[0]),
graphic_stack[stack_id].buffer_vertex_origin, stack->buffer_vertex_origin,
GL_STATIC_DRAW); GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -58,9 +57,9 @@ void graphics_init_buffers(const int stack_id)
// colors // colors
glGenBuffers(1, &color_buffer); glGenBuffers(1, &color_buffer);
glBindBuffer(GL_ARRAY_BUFFER, color_buffer); glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glBufferData(GL_ARRAY_BUFFER, graphic_stack[stack_id].buffer_colors_size * glBufferData(GL_ARRAY_BUFFER, stack->buffer_colors_size *
sizeof(graphic_stack[stack_id].buffer_colors_origin[0]), sizeof(stack->buffer_colors_origin[0]),
graphic_stack[stack_id].buffer_colors_origin, stack->buffer_colors_origin,
GL_STATIC_DRAW); GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -68,9 +67,9 @@ void graphics_init_buffers(const int stack_id)
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
glBindVertexArray(vao); glBindVertexArray(vao);
graphic_stack[stack_id].vao = vao; stack->vao = vao;
graphic_stack[stack_id].position_buffer = vertex_buffer; stack->position_buffer = vertex_buffer;
graphic_stack[stack_id].color_buffer = color_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) bool graphics_init_shaders(const int stack_id)
{ {
struct graphic_stack_t *stack = &graphic_stack[stack_id];
char *vertex_shader; char *vertex_shader;
char *fragment_shader; char *fragment_shader;
int status; int status;
@ -98,7 +98,7 @@ bool graphics_init_shaders(const int stack_id)
vertex = create_shader(stack_id, GL_VERTEX_SHADER, vertex_shader); vertex = create_shader(stack_id, GL_VERTEX_SHADER, vertex_shader);
if(vertex == 0) { if(vertex == 0) {
graphic_stack[stack_id].program = 0; stack->program = 0;
g_free(vertex_shader); g_free(vertex_shader);
return false; return false;
} }
@ -111,7 +111,7 @@ bool graphics_init_shaders(const int stack_id)
if(fragment == 0) { if(fragment == 0) {
glDeleteShader(vertex); glDeleteShader(vertex);
graphic_stack[stack_id].program = 0; stack->program = 0;
g_free(vertex_shader); g_free(vertex_shader);
g_free(fragment_shader); g_free(fragment_shader);
return false; return false;
@ -163,10 +163,10 @@ bool graphics_init_shaders(const int stack_id)
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
graphic_stack[stack_id].program = program; stack->program = program;
graphic_stack[stack_id].m = m; stack->m = m;
graphic_stack[stack_id].v = v; stack->v = v;
graphic_stack[stack_id].p = p; stack->p = p;
g_free(vertex_shader); g_free(vertex_shader);
g_free(fragment_shader); g_free(fragment_shader);

View File

@ -248,10 +248,6 @@ void on_glarea_realize(GtkWidget *widget)
return; 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); gtk_gl_area_set_auto_render(GTK_GL_AREA(widget), true);
} }

View File

@ -83,11 +83,20 @@ long ui_is_graphic_stack_ready(void *container_widget)
*/ */
bool ui_init_graphic_stack(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 // look for stack_index entry
for (int i = 0; i < stack_index_size; i++) { 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) { if (stack_index[i].container_widget == (void *)container_widget) {
stack_index[i].stack_id = graphics_init(); 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; return true;
else else
return false; return false;
@ -248,6 +257,8 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget)
assert(target_widget); assert(target_widget);
g_printerr("[debug] ui_setup_glarea() : target is %p\n", target_widget);
if (stack_index == NULL) { if (stack_index == NULL) {
stack_index = g_malloc(sizeof(struct stack_index_t)); stack_index = g_malloc(sizeof(struct stack_index_t));
stack_index_size = 1; stack_index_size = 1;
@ -262,9 +273,11 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget)
stack_index = stack_index =
g_realloc(stack_index, g_realloc(stack_index,
++stack_index_size * sizeof(struct stack_index_t)); ++stack_index_size * sizeof(struct stack_index_t));
}
stack_index[stack_index_size-1].container_widget = stack_index[stack_index_size-1].container_widget =
(void*)target_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", g_printerr("[debug] ui_setup_glarea() : stack_index (@0x%p) had %ld elements\n",
stack_index, stack_index,
@ -273,33 +286,41 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget)
gl_area = GTK_WIDGET(gtk_gl_area_new()); gl_area = GTK_WIDGET(gtk_gl_area_new());
assert(gl_area); 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, g_signal_connect(gl_area,
"realize", "realize",
G_CALLBACK(on_glarea_realize), NULL); G_CALLBACK(on_glarea_realize), NULL);
g_signal_connect(gl_area, g_signal_connect(gl_area,
"unrealize", "unrealize",
G_CALLBACK(on_glarea_unrealize), NULL); 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); // We need to initialize and free GL resources, so we use
gtk_widget_set_vexpand(gl_area, TRUE); // the realize and unrealize signals on the widget
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);
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), */ /* gtk_gl_area_set_required_version(GTK_GL_AREA(gl_area), */
/* GL_TARGET_MINOR_VERSION, */ /* GL_TARGET_MINOR_VERSION, */
/* GL_TARGET_MAJOR_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; return true;
} }