GTK4 and Legacy OpenGL

I wanted to make a small OpenGL animation in GTK4. I have the following code for it, but no triangle is drawn.

The only thing that seems to work is glClearColor. The background flickers red as expected.

Any idea why it doesn’t work?


// $ gcc -o main main.c `pkg-config --cflags --libs gtk4` -lGL


#include <gtk/gtk.h>
#include <GL/gl.h>

static gboolean on_tick(GtkWidget *widget, GdkFrameClock *frame_clock, gpointer user_data) {
    gtk_gl_area_queue_render(GTK_GL_AREA(widget));
    return G_SOURCE_CONTINUE;  
}

static void realize(GtkGLArea *area, gpointer user_data) {
    gtk_gl_area_make_current(area);
    if (gtk_gl_area_get_error(area) != NULL) {
        return;
    }

    gtk_widget_add_tick_callback(GTK_WIDGET(area), on_tick, NULL, NULL);
}

static gboolean render(GtkGLArea *area, GdkGLContext *context) {
    float r = (double)rand() / RAND_MAX;

    glClearColor(r, 0.0, 0.0, 1.0);

    glClear(GL_COLOR_BUFFER_BIT);

    // Zeichnen Sie hier Ihr Objekt
    glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(-0.5, -0.5);
    glColor3f(0.0, 1.0, 0.0);
    glVertex2f(0.5, -0.5);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f(0.0, 0.5);
    glEnd();

    glFlush();
    return TRUE;
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWidget *window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "OpenGL mit GTK4");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);

    GtkWidget *gl_area = gtk_gl_area_new();
    gtk_widget_set_hexpand(gl_area, TRUE);
    gtk_widget_set_vexpand(gl_area, TRUE);

    gtk_window_set_child(GTK_WINDOW(window), gl_area);

    g_signal_connect(gl_area, "render", G_CALLBACK(render), NULL);
    g_signal_connect(gl_area, "realize", G_CALLBACK(realize), NULL);

    gtk_window_present(GTK_WINDOW(window));   
}

int main(int argc, char **argv) {
    GtkApplication *app = gtk_application_new("org.example.opengl", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    int status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return status;
}

```**strong text**

Please, use English on this forum.

Also: don’t use OpenGL 1.x in 2025. You’re about 25 years too late for that.

If you want an example of using recent OpenGL, look at the gtk4-demo application that is provided by GTK itself.

If I understand correctly, legacy OprnGL is not supported by GTK4.
Or can you force the old context?

That’s correct: GTK4 only supports 3.2 core profile contexts or later.

Maybe this is useful but a while ago I made a hacky widget to support external contexts in gtk4. The demo program uses a GL 2.1 context. No guarantee this code is correct or even still works at all.

What surprises me about the whole thing is that I always thought that a widget doesn’t care which OpenGL context you use. Ultimately, under Linux and X11, the context creation always ends up in GLX. Or is it different with Wayland, where the context doesn’t support legacy OpenGL?

“Widgets” don’t care, because widgets don’t use GL directly—or at all. GTK cares about which version of GL is in use, because it has to provide a set of functionality that depends on GL.

Supporting legacy GL is not something we are interested in.

That’s not even remotely true. GTK switched to EGL-X11, given that:

  • we make use of dmabuf objects in order to pass around texture data efficiently, and those are exposed only through EGL
  • we are already using EGL in the Wayland backend, and having a single implementation is more maintainable

GLX is only used as a fallback, in case EGL is not available.

Wayland has nothing to do with supporting GL, legacy or not.