gtk.glarea.GLArea sets up its own gdk.glcontext.GLContext, and creates a custom
GL framebuffer that the widget will do GL rendering onto. It also ensures
that this framebuffer is the default GL rendering target when rendering.
The completed rendering is integrated into the larger GTK scene graph as
a texture.
In order to draw, you have to connect to the gtk.glarea.GLArea.render
signal, or subclass gtk.glarea.GLArea and override the GtkGLAreaClass.render
virtual function.
The gtk.glarea.GLArea widget ensures that the gdk.glcontext.GLContext is associated with
the widget's drawing area, and it is kept updated when the size and
position of the drawing area changes.
The render() function will be called when the gtk.glarea.GLArea is ready
for you to draw its content:
The initial contents of the framebuffer are transparent.
static gboolean
render (GtkGLArea *area, GdkGLContext *context)
{
// inside this function it's safe to use GL; the given// GdkGLContext has been made current to the drawable// surface used by the `GtkGLArea` and the viewport has// already been set to be the size of the allocation// we can start by clearing the buffer
glClearColor (0, 0, 0, 0);
glClear (GL_COLOR_BUFFER_BIT);
// draw your object// draw_an_object ();// we completed our drawing; the draw commands will be// flushed at the end of the signal emission chain, and// the buffers will be drawn on the windowreturn TRUE;
}
void setup_glarea (void)
{
// create a GtkGLArea instance
GtkWidget *gl_area = gtk_gl_area_new ();
// connect to the "render" signal
g_signal_connect (gl_area, "render", G_CALLBACK (render), NULL);
}
An example of how to safely initialize the GL state is:
staticvoid
on_realize (GtkGLarea *area)
{
// We need to make the context current if we want to// call GL API
gtk_gl_area_make_current (area);
// If there were errors during the initialization or// when trying to make the context current, this// function will return a GError for you to catchif (gtk_gl_area_get_error (area) != NULL)
return;
// You can also use gtk_gl_area_set_error() in order// to show eventual initialization errors on the// GtkGLArea widget itself
GError *internal_error = NULL;
init_buffer_objects (&error);
if (error != NULL)
{
gtk_gl_area_set_error (area, error);
g_error_free (error);
return;
}
init_shaders (&error);
if (error != NULL)
{
gtk_gl_area_set_error (area, error);
g_error_free (error);
return;
}
}
If you need to change the options for creating the gdk.glcontext.GLContext
you should use the signal@Gtk.GLArea::create-context signal.
gtk.glarea.GLArea is a widget that allows drawing with OpenGL.
gtk.glarea.GLArea sets up its own gdk.glcontext.GLContext, and creates a custom GL framebuffer that the widget will do GL rendering onto. It also ensures that this framebuffer is the default GL rendering target when rendering. The completed rendering is integrated into the larger GTK scene graph as a texture.
In order to draw, you have to connect to the gtk.glarea.GLArea.render signal, or subclass gtk.glarea.GLArea and override the GtkGLAreaClass.render virtual function.
The gtk.glarea.GLArea widget ensures that the gdk.glcontext.GLContext is associated with the widget's drawing area, and it is kept updated when the size and position of the drawing area changes.
Drawing with GtkGLArea
The simplest way to draw using OpenGL commands in a gtk.glarea.GLArea is to create a widget instance and connect to the gtk.glarea.GLArea.render signal:
The render() function will be called when the gtk.glarea.GLArea is ready for you to draw its content:
The initial contents of the framebuffer are transparent.
If you need to initialize OpenGL state, e.g. buffer objects or shaders, you should use the gtk.widget.Widget.realize signal; you can use the gtk.widget.Widget.unrealize signal to clean up. Since the gdk.glcontext.GLContext creation and initialization may fail, you will need to check for errors, using gtk.glarea.GLArea.getError.
An example of how to safely initialize the GL state is:
If you need to change the options for creating the gdk.glcontext.GLContext you should use the signal@Gtk.GLArea::create-context signal.