From f7f8c5be98c351d6ed4b01c054406f298735a2a0 Mon Sep 17 00:00:00 2001 From: Adrien 'neox' Bourmault Date: Tue, 10 Sep 2024 15:20:25 +0200 Subject: [PATCH] src/util/draw/gl.c: draw a red GL area --- Makefile | 4 +-- manifest.scm | 2 +- src/util/draw/gl.c | 61 +++++++++++++++++++++++++++++++--------------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 3a9f4d4..668b4af 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,8 @@ CPUS ?= $(shell (nproc --all || sysctl -n hw.ncpu) 2>/dev/null || echo 1) MAKEFLAGS += --jobs=$(CPUS) CC=gcc -CFLAGS=`pkg-config --cflags gtk4 gl glib-2.0 libxml-2.0` -LDFLAGS=`pkg-config --libs gtk4 gl glib-2.0 libxml-2.0` -lGL -lGLU -lm -lepoxy -lX11 -lGLEW +CFLAGS=`pkg-config --cflags gtk4 epoxy glib-2.0 libxml-2.0` +LDFLAGS=`pkg-config --libs gtk4 epoxy glib-2.0 libxml-2.0` -lGL -lGLU -lm -lepoxy -lX11 -lGLEW WARNINGS = -Wall DEBUG = -ggdb -fno-omit-frame-pointer #-fdiagnostics-color=always -fsanitize=bounds -fstack-check OPTIMIZE = -O3 diff --git a/manifest.scm b/manifest.scm index 0e742e5..e38e0ea 100644 --- a/manifest.scm +++ b/manifest.scm @@ -17,7 +17,6 @@ "libxml2" "glu" "glew" -;; "glfw" "cglm" "libepoxy" "pango@1.90.0" @@ -25,6 +24,7 @@ "glib" "mesa-headers" "mesa" + "mesa-utils" "libadwaita" ) ) diff --git a/src/util/draw/gl.c b/src/util/draw/gl.c index 5e8decd..88ee2a0 100644 --- a/src/util/draw/gl.c +++ b/src/util/draw/gl.c @@ -27,7 +27,7 @@ #include "../../../include/graphics.h" #include "../../../include/signal.h" -#define GL_AREA_1000 1000 +#define GL_AREA_SIZE 400 struct stack_index_t { long stack_id; @@ -101,18 +101,18 @@ long util_gl_is_ready(void *container_widget) */ bool util_gl_init_stack(void *container_widget, GError *error_buffer) { - g_printerr("[debug] util_gl_init_util_gl_stack()\n"); + g_message("[debug] util_gl_init_util_gl_stack()\n"); - g_printerr("[debug] util_gl_init_util_gl_stack() : target is %p\n", container_widget); + g_message("[debug] util_gl_init_util_gl_stack() : target is %p\n", container_widget); // look for stack_index entry for (int i = 0; i < stack_index_size; i++) { - g_printerr("[debug] util_gl_init_util_gl_stack() : i is %d\n", i); - g_printerr("[debug] util_gl_init_util_gl_stack() : target would be %p\n", + g_message("[debug] util_gl_init_util_gl_stack() : i is %d\n", i); + g_message("[debug] util_gl_init_util_gl_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(&error_buffer); - g_printerr("[debug] util_gl_init_util_gl_stack() : stack_id is %ld\n", + g_message("[debug] util_gl_init_util_gl_stack() : stack_id is %ld\n", stack_index[i].stack_id); if (stack_index[i].stack_id >= 0) return true; @@ -273,11 +273,11 @@ GtkWidget *create_axis_slider(int axis) /* { */ /* GtkWidget *gl_area; */ -/* g_printerr("[debug] util_gl_setup_glarea()\n"); */ +/* g_message("[debug] util_gl_setup_glarea()\n"); */ /* assert(target_widget); */ -/* g_printerr("[debug] util_gl_setup_glarea() : target is %p\n", target_widget); */ +/* g_message("[debug] util_gl_setup_glarea() : target is %p\n", target_widget); */ /* if (stack_index == NULL) { */ /* stack_index = g_malloc(sizeof(struct stack_index_t)); */ @@ -315,9 +315,9 @@ GtkWidget *create_axis_slider(int axis) /* stack_index[stack_index_size-1].gl_area = (void*)gl_area; */ -/* g_printerr("[debug] util_gl_setup_glarea() : set target to %p\n", target_widget); */ +/* g_message("[debug] util_gl_setup_glarea() : set target to %p\n", target_widget); */ -/* g_printerr("[debug] util_gl_setup_glarea() : stack_index (@0x%p) had %ld elements\n", */ +/* g_message("[debug] util_gl_setup_glarea() : stack_index (@0x%p) had %ld elements\n", */ /* stack_index, */ /* stack_index_size); */ @@ -332,31 +332,52 @@ GtkWidget *create_axis_slider(int axis) /* } */ static void -on_realize_debug (GtkGLArea *area) { - // Initialisation du contexte OpenGL +on_realize_debug (GtkGLArea *area) +{ gdk_gl_context_make_current(gtk_gl_area_get_context(area)); - - // Si nécessaire, ajouter d'autres initialisations OpenGL ici + if (gtk_gl_area_get_error(area) != NULL) { + g_message("Failed to create OpenGL context"); + } + gtk_gl_area_set_has_depth_buffer(GTK_GL_AREA(area), TRUE); } static gboolean -on_render_debug (GtkGLArea *area, GdkGLContext *context) { - // Ici vous pouvez dessiner avec OpenGL - glClearColor(0.0, 1.0, 0.0, 1.0); // Effacer en vert +on_render_debug (GtkGLArea *area, GdkGLContext *context) +{ + GLenum err; + while ((err = glGetError()) != GL_NO_ERROR) { + g_warning("OpenGL error: %d", err); + } + + int width, height; + width = gtk_widget_get_allocated_width(GTK_WIDGET(area)); + height = gtk_widget_get_allocated_height(GTK_WIDGET(area)); + g_message("GtkGLArea size: %d x %d", width, height); + + glViewport(0, 0, width, height); + glClearColor(1.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); return TRUE; } -bool util_gl_setup_glarea(int target_mode, GtkWidget *target_widget) { +bool util_gl_setup_glarea (int target_mode, GtkWidget *target_widget) +{ + g_message("[debug] util_gl_setup_glarea()\n"); GtkWidget *gl_area = gtk_gl_area_new(); g_signal_connect(gl_area, "realize", G_CALLBACK(on_realize_debug), NULL); g_signal_connect(gl_area, "render", G_CALLBACK(on_render_debug), NULL); - gtk_box_append(GTK_BOX(target_widget), gl_area); + gtk_widget_set_size_request(gl_area, GL_AREA_SIZE, GL_AREA_SIZE); + gtk_gl_area_set_auto_render(GTK_GL_AREA(gl_area), true); + 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_show(gl_area); + gtk_box_append (GTK_BOX (target_widget), gl_area); + gtk_widget_set_visible (GTK_WIDGET (gl_area), TRUE); return TRUE; }