gtk.glarea
Module for [GLArea] class
Types 3
#GtkGLArea is a widget that allows drawing with OpenGL.
#GtkGLArea sets up its own #GdkGLContext for the window it creates, 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.
In order to draw, you have to connect to the #GtkGLArea::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.
Drawing with GtkGLArea
The simplest way to draw using OpenGL commands in a #GtkGLArea is to create a widget instance and connect to the #GtkGLArea::render signal:
// 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);The render() function will be called when the #GtkGLArea is ready for you to draw its content:
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 window
return TRUE;
}If you need to initialize OpenGL state, e.g. buffer objects or shaders, you should use the #GtkWidget::realize signal; you can use the #GtkWidget::unrealize signal to clean up. Since the #GdkGLContext 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:
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 #GtkGLArea::create-context signal.
GLAreaGidBuilder builder()Get builder for [gtk.glarea.GLArea] Returns: New builder objectbool autoRender() @propertyGet `autoRender` property. Returns: If set to true the #GtkGLArea::render signal will be emitted every time the widget draws. This is the default and is useful if drawing the widget is faster.void autoRender(bool propval) @propertySet `autoRender` property. Params: propval = If set to true the #GtkGLArea::render signal will be emitted every time the widget draws. This is the default and is useful if drawing the widget is fas...gdk.glcontext.GLContext context() @propertyGet `context` property. Returns: The #GdkGLContext used by the #GtkGLArea widget.bool hasAlpha() @propertyGet `hasAlpha` property. Returns: If set to true the buffer allocated by the widget will have an alpha channel component, and when rendering to the window the result will be composited over whateve...void hasAlpha(bool propval) @propertySet `hasAlpha` property. Params: propval = If set to true the buffer allocated by the widget will have an alpha channel component, and when rendering to the window the result will be composited ove...bool hasDepthBuffer() @propertyGet `hasDepthBuffer` property. Returns: If set to true the widget will allocate and enable a depth buffer for the target framebuffer.void hasDepthBuffer(bool propval) @propertySet `hasDepthBuffer` property. Params: propval = If set to true the widget will allocate and enable a depth buffer for the target framebuffer.bool hasStencilBuffer() @propertyGet `hasStencilBuffer` property. Returns: If set to true the widget will allocate and enable a stencil buffer for the target framebuffer.void hasStencilBuffer(bool propval) @propertySet `hasStencilBuffer` property. Params: propval = If set to true the widget will allocate and enable a stencil buffer for the target framebuffer.bool useEs() @propertyGet `useEs` property. Returns: If set to true the widget will try to create a #GdkGLContext using OpenGL ES instead of OpenGL.void useEs(bool propval) @propertySet `useEs` property. Params: propval = If set to true the widget will try to create a #GdkGLContext using OpenGL ES instead of OpenGL.void attachBuffers()Ensures that the area framebuffer object is made the current draw and read target, and that all the required buffers for the area are created and bound to the frambuffer.bool getAutoRender()Returns whether the area is in auto render mode or not. Returns: true if the area is auto rendering, false otherwisegdk.glcontext.GLContext getContext()Retrieves the #GdkGLContext used by area. Returns: the #GdkGLContextglib.error.ErrorWrap getError()Gets the current error set on the area. Returns: the #GError or nullbool getHasAlpha()Returns whether the area has an alpha component. Returns: true if the area has an alpha component, false otherwisebool getHasDepthBuffer()Returns whether the area has a depth buffer. Returns: true if the area has a depth buffer, false otherwisebool getHasStencilBuffer()Returns whether the area has a stencil buffer. Returns: true if the area has a stencil buffer, false otherwisevoid getRequiredVersion(out int major, out int minor)Retrieves the required version of OpenGL set using [gtk.glarea.GLArea.setRequiredVersion].bool getUseEs()Retrieves the value set by [gtk.glarea.GLArea.setUseEs]. Returns: true if the #GtkGLArea should create an OpenGL ES context and false otherwisevoid makeCurrent()Ensures that the #GdkGLContext used by area is associated with the #GtkGLArea.void queueRender()Marks the currently rendered data (if any) as invalid, and queues a redraw of the widget, ensuring that the #GtkGLArea::render signal is emitted during the draw.void setAutoRender(bool autoRender)If auto_render is true the #GtkGLArea::render signal will be emitted every time the widget draws. This is the default and is useful if drawing the widget is faster.void setError(glib.error.ErrorWrap error = null)Sets an error on the area which will be shown instead of the GL rendering. This is useful in the #GtkGLArea::create-context signal if GL context creation fails.void setHasAlpha(bool hasAlpha)If has_alpha is true the buffer allocated by the widget will have an alpha channel component, and when rendering to the window the result will be composited over whatever is below the widget.void setHasDepthBuffer(bool hasDepthBuffer)If hasdepthbuffer is true the widget will allocate and enable a depth buffer for the target framebuffer. Otherwise there will be none.void setHasStencilBuffer(bool hasStencilBuffer)If hasstencilbuffer is true the widget will allocate and enable a stencil buffer for the target framebuffer. Otherwise there will be none.void setRequiredVersion(int major, int minor)Sets the required version of OpenGL to be used when creating the context for the widget.void setUseEs(bool useEs)Sets whether the area should create an OpenGL or an OpenGL ES context.gulong connectCreateContext(T)(T callback, Flag!"After" after = No.After) if (isCallable!T
&& is(ReturnType!T : gdk.glcontext.GLContext)
&& (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gtk.glarea.GLArea)))
&& Parameters!T.length < 2)Connect to `CreateContext` signal.gulong connectRender(T)(T callback, Flag!"After" after = No.After) if (isCallable!T
&& is(ReturnType!T == bool)
&& (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gdk.glcontext.GLContext)))
&& (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : gtk.glarea.GLArea)))
&& Parameters!T.length < 3)Connect to `Render` signal.gulong connectResize(T)(T callback, Flag!"After" after = No.After) if (isCallable!T
&& is(ReturnType!T == void)
&& (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == int)))
&& (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == int)))
&& (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : gtk.glarea.GLArea)))
&& Parameters!T.length < 4)Connect to `Resize` signal.T autoRender(bool propval)Set `autoRender` property. Params: propval = If set to true the #GtkGLArea::render signal will be emitted every time the widget draws. This is the default and is useful if drawing the widget is fas...T hasAlpha(bool propval)Set `hasAlpha` property. Params: propval = If set to true the buffer allocated by the widget will have an alpha channel component, and when rendering to the window the result will be composited ove...T hasDepthBuffer(bool propval)Set `hasDepthBuffer` property. Params: propval = If set to true the widget will allocate and enable a depth buffer for the target framebuffer. Returns: Builder instance for fluent chainingT hasStencilBuffer(bool propval)Set `hasStencilBuffer` property. Params: propval = If set to true the widget will allocate and enable a stencil buffer for the target framebuffer. Returns: Builder instance for fluent chainingT useEs(bool propval)Set `useEs` property. Params: propval = If set to true the widget will try to create a #GdkGLContext using OpenGL ES instead of OpenGL.