src/graphics, src/ui: reworked GLArea generation and setup

This commit is contained in:
Adrien Bourmault 2024-01-10 11:51:53 +01:00
parent 9bb7cefc2f
commit ff35840a26
6 changed files with 80 additions and 67 deletions

View File

@ -86,7 +86,7 @@ extern struct graphic_stack_t *graphic_stack;
* *
* @return true if initialized * @return true if initialized
*/ */
int graphics_init(void); int graphics_init(void *error_buffer);
/* /*
* Draws the current buffer to a gl_area * Draws the current buffer to a gl_area
@ -104,7 +104,7 @@ void graphics_draw(const int stack_id);
* *
* @return true if success * @return true if success
*/ */
bool graphics_shutdown(const int stack_id); bool graphics_shutdown(const int stack_id, void *error_buffer);
/* /*
* Initializes the shaders of a gl_area and link them to a program * Initializes the shaders of a gl_area and link them to a program

View File

@ -189,7 +189,7 @@ long ui_get_graphic_stack(void *container_widget);
* *
* @returns bool, true if success * @returns bool, true if success
*/ */
bool ui_init_graphic_stack(void *container_widget); bool ui_init_graphic_stack(void *container_widget, GError *error_buffer);
/* /*
* Look for stack entry and shutdowns OpenGL for it * Look for stack entry and shutdowns OpenGL for it
@ -198,7 +198,7 @@ bool ui_init_graphic_stack(void *container_widget);
* *
* @returns bool, true if success * @returns bool, true if success
*/ */
bool ui_shutdown_graphic_stack(void *container_widget); bool ui_shutdown_graphic_stack(void *container_widget, GError *error_buffer);
void ui_clean_stack_index(void); void ui_clean_stack_index(void);
@ -229,3 +229,4 @@ void ui_shutdown_all_graphic_stacks(void);
* @returns bool, true if success * @returns bool, true if success
*/ */
bool ui_update_axis_stack(GtkWidget *container_widget, int axis, int value); bool ui_update_axis_stack(GtkWidget *container_widget, int axis, int value);

View File

@ -41,20 +41,20 @@ void graphics_draw_vertex (const int stack_id,
GLfloat y, GLfloat y,
GLfloat z) GLfloat z)
{ {
printf("stack_id is %d\n", stack_id); //g_printerr("stack_id is %d\n", stack_id);
printf("stack_id is %d\n", stack_id); //g_printerr("stack_id is %d\n", stack_id);
printf("graphic_stack is at %p\n", graphic_stack); //g_printerr("graphic_stack is at %p\n", graphic_stack);
printf("graphic_stack[stack_id] is at %p\n", graphic_stack + stack_id); //g_printerr("graphic_stack[stack_id] is at %p\n", graphic_stack + stack_id);
struct graphic_stack_t *stack = &graphic_stack[stack_id]; struct graphic_stack_t *stack = &graphic_stack[stack_id];
//assert (stack->buffer_vertex_origin); //assert (stack->buffer_vertex_origin);
printf("Currently stack->buffer_vertex_origin @ %p\n", stack->buffer_vertex_origin); //g_printerr("Currently stack->buffer_vertex_origin @ %p\n", stack->buffer_vertex_origin);
stack->buffer_vertex_origin = stack->buffer_vertex_origin =
g_realloc (stack->buffer_vertex_origin, g_realloc (stack->buffer_vertex_origin,
(stack->buffer_vertex_size + 3) * sizeof(GLfloat)); (stack->buffer_vertex_size + 3) * sizeof(GLfloat));
print_stack(stack_id); //print_stack(stack_id);
stack->buffer_vertex_origin[stack->buffer_vertex_size + 0] = x; 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 + 1] = y;
@ -150,6 +150,10 @@ void graphics_draw(const int stack_id)
{ {
struct graphic_stack_t *stack = &graphic_stack[stack_id]; struct graphic_stack_t *stack = &graphic_stack[stack_id];
//g_printerr("[debug] graphics_draw() started\n");
//print_stack(stack_id);
float m[16]; float m[16];
float v[16]; float v[16];
float p[16]; float p[16];
@ -169,9 +173,7 @@ void graphics_draw(const int stack_id)
/* Use our shaders */ /* Use our shaders */
glUseProgram(stack->program); glUseProgram(stack->program);
// if (pref_show_space_design_mix_colors == 0) glClearColor(0, 0, 0, 1);
glClearColor(0, 0, 0, 0.4f);
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 */
@ -201,4 +203,5 @@ void graphics_draw(const int stack_id)
glUseProgram(0); glUseProgram(0);
glFlush(); glFlush();
//g_printerr("[debug] graphics_draw() ended\n");
} }

View File

@ -91,7 +91,7 @@ static void graphics_debug_callback(GLenum source, GLenum type, GLuint id,
code = glGetError(); code = glGetError();
string = gluErrorString(code); string = gluErrorString(code);
printf("[%s] %s (%s) from %s: %s\n", g_printerr("[%s] %s (%s) from %s: %s\n",
errseverity, string, errtype, errsource, msg); errseverity, string, errtype, errsource, msg);
} }
@ -104,16 +104,17 @@ static void graphics_debug_callback(GLenum source, GLenum type, GLuint id,
* *
* @return id if initialized * @return id if initialized
*/ */
int graphics_init(void) int graphics_init(void *error_buffer)
{ {
int cur_id = 0; int cur_id = 0;
struct graphic_stack_t *stack; 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_malloc0(sizeof(struct graphic_stack_t)); graphic_stack = g_malloc0(sizeof(struct graphic_stack_t));
graphic_stack_size = 1; graphic_stack_size = 1;
/* g_printerr("[debug] graphics_init(): init graphic_stack @ %p\n", graphic_stack); */
} else { } else {
// Check if there are free slots // Check if there are free slots
if (free_stack_slot_size) { if (free_stack_slot_size) {
@ -131,9 +132,9 @@ int graphics_init(void)
} }
} }
g_printerr("[debug] graphics_init() : graphic_stack (@0x%p) has %ld elements\n", /* g_printerr("[debug] graphics_init() : graphic_stack (@0x%p) has %ld elements\n", */
graphic_stack, /* graphic_stack, */
graphic_stack_size); /* graphic_stack_size); */
stack = &graphic_stack[cur_id]; stack = &graphic_stack[cur_id];
stack->id = cur_id; stack->id = cur_id;
@ -145,13 +146,13 @@ int graphics_init(void)
if (!graphics_init_shaders(cur_id)) if (!graphics_init_shaders(cur_id))
return -1; return -1;
print_stack(cur_id); //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); //print_stack(cur_id);
return cur_id; return cur_id;
} }
@ -163,12 +164,11 @@ int graphics_init(void)
* *
* @return true if success * @return true if success
*/ */
bool graphics_shutdown(const int id) bool graphics_shutdown(const int id, void *error_buffer)
{ {
struct graphic_stack_t *stack; struct graphic_stack_t *stack;
if (id == 0 || if (id >= graphic_stack_size ||
id >= graphic_stack_size ||
graphic_stack_size == 0 || graphic_stack_size == 0 ||
graphic_stack == NULL) graphic_stack == NULL)
return false; return false;

View File

@ -211,7 +211,6 @@ void on_axis_value_change(GtkAdjustment *adjustment, gpointer data)
gtk_adjustment_get_value(adjustment)); gtk_adjustment_get_value(adjustment));
/* Update the contents of the GL drawing area */ /* Update the contents of the GL drawing area */
//gtk_widget_queue_draw(gl_area);
} }
gboolean on_glarea_render(GtkGLArea *area, GdkGLContext *context) gboolean on_glarea_render(GtkGLArea *area, GdkGLContext *context)
@ -235,6 +234,11 @@ gboolean on_glarea_render(GtkGLArea *area, GdkGLContext *context)
/* We need to set up our state when we realize the GtkGLArea widget */ /* We need to set up our state when we realize the GtkGLArea widget */
void on_glarea_realize(GtkWidget *widget) void on_glarea_realize(GtkWidget *widget)
{ {
GError *internal_error = NULL;
// Make the GL context current to be able to call the GL API
gtk_gl_area_make_current(GTK_GL_AREA(widget));
// Check if the widget is a glarea // Check if the widget is a glarea
if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) { if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) {
ui_send_internal_notification("An OpenGL error occured !"); ui_send_internal_notification("An OpenGL error occured !");
@ -242,7 +246,8 @@ void on_glarea_realize(GtkWidget *widget)
} }
// Link graphical stack to widget // Link graphical stack to widget
if (ui_init_graphic_stack(gtk_widget_get_parent(widget)) == false) { if (ui_init_graphic_stack(gtk_widget_get_parent(widget),
internal_error) == false) {
ui_send_internal_notification( ui_send_internal_notification(
"Failed to link the graphic stack to widgets !"); "Failed to link the graphic stack to widgets !");
return; return;
@ -254,6 +259,11 @@ void on_glarea_realize(GtkWidget *widget)
/* We should tear down the state when unrealizing */ /* We should tear down the state when unrealizing */
void on_glarea_unrealize(GtkWidget *widget) void on_glarea_unrealize(GtkWidget *widget)
{ {
GError *internal_error = NULL;
// Make the GL context current to be able to call the GL API
gtk_gl_area_make_current(GTK_GL_AREA(widget));
// Check if the widget is a glarea // Check if the widget is a glarea
if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) { if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) {
ui_send_internal_notification("An OpenGL error occured !"); ui_send_internal_notification("An OpenGL error occured !");
@ -261,7 +271,8 @@ void on_glarea_unrealize(GtkWidget *widget)
} }
// Destroy graphic stack // Destroy graphic stack
if (ui_shutdown_graphic_stack(ui_shutdown_graphic_stack) == false) { if (ui_shutdown_graphic_stack(gtk_widget_get_parent(widget),
internal_error) == false) {
ui_send_internal_notification( ui_send_internal_notification(
"Failed to shutdown the graphic stack !"); "Failed to shutdown the graphic stack !");
return; return;
@ -340,3 +351,4 @@ void on_openfile_response_complete(GObject *source_object,
ui_enable_action("togglesidebar"); ui_enable_action("togglesidebar");
ui_set_stack(RUN_MODE); ui_set_stack(RUN_MODE);
} }

View File

@ -32,6 +32,7 @@
struct stack_index_t { struct stack_index_t {
long stack_id; long stack_id;
void *container_widget; void *container_widget;
void *gl_area;
}; };
static struct stack_index_t *stack_index = NULL; static struct stack_index_t *stack_index = NULL;
@ -81,22 +82,22 @@ long ui_is_graphic_stack_ready(void *container_widget)
* *
* @returns bool, true if success * @returns bool, true if success
*/ */
bool ui_init_graphic_stack(void *container_widget) bool ui_init_graphic_stack(void *container_widget, GError *error_buffer)
{ {
g_printerr("[debug] ui_init_graphic_stack()\n"); //g_printerr("[debug] ui_init_graphic_stack()\n");
g_printerr("[debug] ui_init_graphic_stack() : target is %p\n", container_widget); //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() : i is %d\n", i);
g_printerr("[debug] ui_init_graphic_stack() : target would be %p\n", //g_printerr("[debug] ui_init_graphic_stack() : target would be %p\n",
stack_index[i].container_widget); //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(&error_buffer);
g_printerr("[debug] ui_init_graphic_stack() : stack_id is %d\n", //g_printerr("[debug] ui_init_graphic_stack() : stack_id is %d\n",
stack_index[i].stack_id); //stack_index[i].stack_id);
if (stack_index[i].stack_id > 0) if (stack_index[i].stack_id >= 0)
return true; return true;
else else
return false; return false;
@ -112,12 +113,13 @@ bool ui_init_graphic_stack(void *container_widget)
* *
* @returns bool, true if success * @returns bool, true if success
*/ */
bool ui_shutdown_graphic_stack(void *container_widget) bool ui_shutdown_graphic_stack(void *container_widget, GError *error_buffer)
{ {
// 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++) {
if (stack_index[i].container_widget == (void *)container_widget) { if (stack_index[i].container_widget == (void *)container_widget) {
if (graphics_shutdown(stack_index[i].stack_id) == false) { if (graphics_shutdown(stack_index[i].stack_id,
&error_buffer) == false) {
return false; return false;
} }
stack_index[i].stack_id = 0; stack_index[i].stack_id = 0;
@ -169,6 +171,7 @@ bool ui_update_axis_stack(GtkWidget *container_widget, int axis, int value)
for (int i = 0; i < stack_index_size; i++) { for (int i = 0; i < stack_index_size; i++) {
if (stack_index[i].container_widget == (void *)container_widget) { if (stack_index[i].container_widget == (void *)container_widget) {
graphic_stack[stack_index[i].stack_id].rotation_angles[axis] = value; graphic_stack[stack_index[i].stack_id].rotation_angles[axis] = value;
gtk_widget_queue_draw((GtkWidget*)(stack_index[i].gl_area));
return true; return true;
} }
} }
@ -186,7 +189,7 @@ void ui_shutdown_all_graphic_stacks(void)
{ {
// 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++) {
graphics_shutdown(stack_index[i].stack_id); graphics_shutdown(stack_index[i].stack_id, NULL);
} }
return; return;
} }
@ -253,11 +256,11 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget)
{ {
GtkWidget *gl_area; GtkWidget *gl_area;
g_printerr("[debug] ui_setup_glarea()\n"); //g_printerr("[debug] ui_setup_glarea()\n");
assert(target_widget); assert(target_widget);
g_printerr("[debug] ui_setup_glarea() : target is %p\n", 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));
@ -275,17 +278,16 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget)
++stack_index_size * sizeof(struct stack_index_t)); ++stack_index_size * sizeof(struct stack_index_t));
} }
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);
gl_area = GTK_WIDGET(gtk_gl_area_new()); gl_area = GTK_WIDGET(gtk_gl_area_new());
assert(gl_area); assert(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);
gtk_gl_area_set_auto_render(GTK_GL_AREA(gl_area), true);
// The main "draw" call for GtkGLArea // The main "draw" call for GtkGLArea
g_signal_connect(GTK_GL_AREA(gl_area), g_signal_connect(GTK_GL_AREA(gl_area),
"render", "render",
@ -299,24 +301,19 @@ bool ui_setup_glarea(int target_mode, GtkWidget *target_widget)
"unrealize", "unrealize",
G_CALLBACK(on_glarea_unrealize), NULL); G_CALLBACK(on_glarea_unrealize), NULL);
stack_index[stack_index_size-1].container_widget =
(void*)target_widget;
stack_index[stack_index_size-1].gl_area = (void*)gl_area;
//g_printerr("[debug] ui_setup_glarea() : set target to %p\n", target_widget);
//g_printerr("[debug] ui_setup_glarea() : stack_index (@0x%p) had %ld elements\n",
//stack_index,
//stack_index_size);
gtk_box_append(GTK_BOX(target_widget), gl_area); gtk_box_append(GTK_BOX(target_widget), gl_area);
//gtk_widget_show(GTK_WIDGET(gl_area)); gtk_widget_show(GTK_WIDGET(gl_area));
// We need to initialize and free GL resources, so we use
// the realize and unrealize signals on the widget
//
/* 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); */
// Create sliders // Create sliders
for(int i = 0; i < N_AXIS; i++) for(int i = 0; i < N_AXIS; i++)