It derives from <GtkWidget>.
Allows drawing with OpenGL.
<GtkGLArea> sets up its own
<GdkGLContext>, 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 glarea:render signal, or subclass
<GtkGLArea> and override the GtkGLAreaClass.render
virtual function.
The <GtkGLArea> widget ensures that the
<GdkGLContext> is associated with the
widget’s drawing area, and it is kept updated when the size and position
of the drawing area changes.
The simplest way to draw using OpenGL commands in a
<GtkGLArea> is to create a widget instance and
connect to the glarea:render signal:
The render() function will be called when the
<GtkGLArea> 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);
// record the active framebuffer ID, so we can return to it
// with `glBindFramebuffer (GL_FRAMEBUFFER, screen_fb)` should
// we, for instance, intend on utilizing the results of an
// intermediate render texture pass
GLuint screen_fb = 0;
glGetIntegerv (GL_FRAMEBUFFER_BINDING, &screen_fb);
// 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 window
return 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);
}
If you need to initialize OpenGL state, e.g. buffer objects or shaders,
you should use the widget:realize
signal; you can use the widget:unrealize signal to clean up. Since the
<GdkGLContext> creation and initialization
may fail, you will need to check for errors, using glarea:get-error.
An example of how to safely initialize the GL state is:
static void
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 catch
if (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
<GdkGLContext> you should use the
glarea:create-context signal.
The allowed APIs.
Its value is a <GdkGLAPI> (or, to set it, a list
of its symbols, or one of them).
Set it with (set! (glarea:allowed-apis self) value).
The API currently in use.
Its value is a <GdkGLAPI>.
It is read only.
If set to #t the ::render signal will be emitted every time the
widget draws.
This is the default and is useful if drawing the widget is faster.
If set to #f the data from previous rendering is kept around and
will be used for drawing the widget the next time, unless the window is
resized. In order to force a rendering glarea:queue-render must be called. This mode is useful
when the scene changes seldom, but takes a long time to redraw.
Its value is a boolean.
Set it with (set! (glarea:auto-render self) value).
The <GdkGLContext> used by the
<GtkGLArea> widget.
The <GtkGLArea> widget is responsible for creating
the <GdkGLContext> instance. If you need to
render with other kinds of buffers (stencil, depth, etc), use render
buffers.
Its value is a <GdkGLContext>, or #f.
It is read only.
If set to #t the widget will allocate and enable a depth buffer
for the target framebuffer.
Setting this property will enable GL’s depth testing as a side effect.
If you don’t need depth testing, you should call
glDisable(GL_DEPTH_TEST) in your glarea:render handler.
Its value is a boolean.
Set it with (set! (glarea:has-depth-buffer self) value).
If set to #t the widget will allocate and enable a stencil buffer
for the target framebuffer.
Its value is a boolean.
Set it with (set! (glarea:has-stencil-buffer self) value).
If set to #t the widget will try to create a
<GdkGLContext> using OpenGL ES instead of
OpenGL.
Its value is a boolean.
Set it with (set! (glarea:use-es self) value).
Binds buffers to the framebuffer.
Ensures that the self framebuffer object is made the current draw and read target, and that all the required buffers for the self are created and bound to the framebuffer.
This function is automatically called before emitting the
glarea:render signal, and doesn’t
normally need to be called by application code.
Passed: a <GtkGLArea>.
Gets the allowed APIs.
Passed: a <GtkGLArea>.
Returned: a <GdkGLAPI>.
the allowed APIs
Gets the API that is currently in use.
If the GL area has not been realized yet, 0 is returned.
Passed: a <GtkGLArea>.
Returned: a <GdkGLAPI>.
the currently used API
Returns whether the area is in auto render mode or not.
Passed: a <GtkGLArea>.
Returned: a boolean.
#t if the self is auto rendering, #f otherwise
Retrieves the <GdkGLContext> used by
self.
Passed: a <GtkGLArea>.
Returned: a <GdkGLContext>, or #f.
the <GdkGLContext>
Gets the current error set on the self.
Passed: a <GtkGLArea>.
Returned: a <GError>, or #f.
the <GError>
Returns whether the area has a depth buffer.
Passed: a <GtkGLArea>.
Returned: a boolean.
#t if the self has a depth buffer, #f otherwise
Returns whether the area has a stencil buffer.
Passed: a <GtkGLArea>.
Returned: a boolean.
#t if the self has a stencil buffer, #f otherwise
Retrieves the required version of OpenGL.
See glarea:set-required-version.
Passed: a <GtkGLArea>.
Returned: a 32-bit integer.
return location for the required major version
Returned: a 32-bit integer.
return location for the required minor version
Returns whether the <GtkGLArea> should use OpenGL
ES.
See glarea:set-use-es.
Passed: a <GtkGLArea>.
Returned: a boolean.
#t if the <GtkGLArea> should create an
OpenGL ES context and #f otherwise
Ensures that the <GdkGLContext> used by
self is associated with the <GtkGLArea>.
This function is automatically called before emitting the
glarea:render signal, and doesn’t
normally need to be called by application code.
Passed: a <GtkGLArea>.
Creates a new <GtkGLArea> widget.
Returned: a <GtkWidget>.
a new <GtkGLArea>
Marks the currently rendered data (if any) as invalid, and queues a redraw of the widget.
This ensures that the glarea:render
signal is emitted during the draw.
This is only needed when glarea:set-auto-render has been called with a
#f value. The default behaviour is to emit glarea:render on each draw.
Passed: a <GtkGLArea>.
Sets the allowed APIs to create a context with.
You should check glarea:api before
drawing with either API.
By default, all APIs are allowed.
Passed: a <GtkGLArea>.
Passed: a <GdkGLAPI>, or a list of its symbols,
or one of them.
the allowed APIs
Sets whether the <GtkGLArea> is in auto render
mode.
If auto-render is #t the glarea:render signal will be emitted every time the widget
draws. This is the default and is useful if drawing the widget is
faster.
If auto-render is #f the data from previous rendering is
kept around and will be used for drawing the widget the next time,
unless the window is resized. In order to force a rendering
glarea:queue-render must be called.
This mode is useful when the scene changes seldom, but takes a long time
to redraw.
Passed: a <GtkGLArea>.
Passed: a boolean.
a boolean
Sets an error on the area which will be shown instead of the GL rendering.
This is useful in the glarea:create-context signal if GL context creation
fails.
Passed: a <GtkGLArea>.
Passed, or left out: a <GError>, or #f.
a new <GError>, or #f to unset the error
Sets whether the <GtkGLArea> should use a depth
buffer.
If has-depth-buffer is #t the widget will allocate and
enable a depth buffer for the target framebuffer. Otherwise there will
be none.
Passed: a <GtkGLArea>.
Passed: a boolean.
#t to add a depth buffer
Sets whether the <GtkGLArea> should use a stencil
buffer.
If has-stencil-buffer is #t the widget will allocate and
enable a stencil buffer for the target framebuffer. Otherwise there
will be none.
Passed: a <GtkGLArea>.
Passed: a boolean.
#t to add a stencil buffer
Sets the required version of OpenGL to be used when creating the context for the widget.
This function must be called before the area has been realized.
Passed: a <GtkGLArea>.
Passed: a 32-bit integer.
the major version
Passed: a 32-bit integer.
the minor version
Sets whether the self should create an OpenGL or an OpenGL ES context.
You should check the capabilities of the
<GdkGLContext> before drawing with either
API.
Passed: a <GtkGLArea>.
Passed: a boolean.
whether to use OpenGL or OpenGL ES
Connect a handler with (connect object glarea:create-context
handler). It is called as (handler self) ⇒ %return.
Emitted when the widget is being realized.
This allows you to override how the GL context is created. This is useful when you want to reuse an existing GL context, or if you want to try creating different kinds of GL options.
If context creation fails then the signal handler can use
glarea:set-error to register a more
detailed error of how the construction failed.
Passed: a <GtkGLArea>.
Returned: a <GdkGLContext>.
a newly created <GdkGLContext>; the
<GtkGLArea> widget will take ownership of the
returned value.
Connect a handler with (connect object glarea:render handler). It
is called as (handler self context) ⇒ %return.
Emitted every time the contents of the <GtkGLArea>
should be redrawn.
The context is bound to the area prior to emitting this function, and the buffers are painted to the window once the emission terminates.
Passed: a <GtkGLArea>.
Passed: a <GdkGLContext>.
the <GdkGLContext> used by area
Returned: a boolean.
#t to stop other handlers from being invoked for the event.
#f to propagate the event further.
Connect a handler with (connect object glarea:resize handler). It
is called as (handler self width height).
Emitted once when the widget is realized, and then each time the widget is changed while realized.
This is useful in order to keep GL state up to date with the widget size, like for instance camera properties which may depend on the width/height ratio.
The GL context for the area is guaranteed to be current when this signal is emitted.
The default handler sets up the GL viewport.
Passed: a <GtkGLArea>.
Passed: a 32-bit integer.
the width of the viewport
Passed: a 32-bit integer.
the height of the viewport
class closure for the glarea:render
signal
To override it in a new class, give register-type #:vfuncs
`((render . ,procedure)). The procedure is called as (procedure
self context) ⇒ %return.
Passed: the class whose implementation to call:
<GtkGLArea>, or a class that derives from it, or
its GType number.
Passed: a <GtkGLArea>.
Passed: a <GdkGLContext>.
Returned: a boolean.
class closeure for the glarea:resize
signal
To override it in a new class, give register-type #:vfuncs
`((resize . ,procedure)). The procedure is called as (procedure
self width height).
Passed: the class whose implementation to call:
<GtkGLArea>, or a class that derives from it, or
its GType number.
Passed: a <GtkGLArea>.
Passed: a 32-bit integer.
Passed: a 32-bit integer.