I am trying to create a library where it's possible to create a OpenGL context with GTK3 which gets rendered automatically or with 1 function call (something similar to a swap-buffers function). I was looking at the source code of GtkGLArea
, which seems to have the following order of execution:
- A new
GtkGLArea
is created with the superclassGtkWidget
. - When the
realize
signal of the superclass is captured, aGdkGLContext
is created and arealize
signal is given to the user. The user is supposed to put the initial OpenGL functions in a function passed to this signal. - The
draw
signal of the superclass is also captured, some OpenGL initialization code is called for binding the framebuffer (or texture) and the renderbuffer. The buffers are created if they don't exist. After this therender
signal is given where the user is supposed to put the rendering OpenGL code. Finally thegdk_cairo_draw_from_gl
function is called to draw the renderbuffer (or texture) on the Cairo context of the superclass passed through thedraw
signal.
For the user it basically comes down to this:
void realize(GtkGLarea *area){
// OpenGL initialization functions
}
gboolean render(GtkGLArea *area, GdkGLContext *context){
// OpenGL rendering functions
return TRUE;
}
// Init functions, setup a GTK window
GtkWidget *gl_area = gtk_gl_area_new();
g_signal_connect(gl_area, "realize", G_CALLBACK(realize), NULL);
g_signal_connect(gl_area, "render", G_CALLBACK(render), NULL);
// Add gl_area to the window and display the window
But that's not what I want, what I want is this:
// Init functions, setup a GTK window
// Setup a OpenGL context on the GTK window
// OpenGL initialization functions
while(1){ // Main loop
if(gtk_events_pending()){
gtk_main_iteration();
}
// OpenGL rendering functions
swapBuffers();
}
What would be the best way to achieve this? I tried this (unsuccessfully) by simulating the functions called around the signals, and by using a GdkGLContext
created on a GdkWindow
taken from a GtkDrawingArea
.
These are the options which I can imagine as solutions now:
- Create a custom GTK3 class extending
GtkWidget
and usingGdkGLContext
and somehow track when the drawing signal is called so the appropriate OpenGL functions are called around that signal. - Find a way to ignore the whole GTK3 part and try to use the underlying Xlib functions to create a OpenGL context through X11 (which isn't portable at all).
- Use a evil hack with threads and
setjmp
andlongjmp
to enter and exit the signals from the swapBuffers function.
None of these "solutions" are really appealing to me, is the thing I want impossible, or am I just missing some information?