1.80 <GtkGLArea>

Class: <GtkGLArea>

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.

Drawing with GtkGLArea

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.

Properties

Property: glarea:allowed-apis self

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).

Property: glarea:api self

The API currently in use.

Its value is a <GdkGLAPI>.

It is read only.

Property: glarea:auto-render self

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).

Property: glarea:context self

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.

Property: glarea:has-depth-buffer self

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).

Property: glarea:has-stencil-buffer self

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).

Property: glarea:use-es self

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).

Procedures

Method: glarea:attach-buffers self
Method: attach-buffers self

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

Method: glarea:get-allowed-apis self ⇒ %return
Method: get-allowed-apis self ⇒ %return

Gets the allowed APIs.

See glarea:set-allowed-apis.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a <GdkGLAPI>.

the allowed APIs

Method: glarea:get-api self ⇒ %return
Method: get-api self ⇒ %return

Gets the API that is currently in use.

If the GL area has not been realized yet, 0 is returned.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a <GdkGLAPI>.

the currently used API

Method: glarea:get-auto-render? self ⇒ %return
Method: get-auto-render? self ⇒ %return

Returns whether the area is in auto render mode or not.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a boolean.

#t if the self is auto rendering, #f otherwise

Method: glarea:get-context self ⇒ %return
Method: get-context self ⇒ %return

Retrieves the <GdkGLContext> used by self.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a <GdkGLContext>, or #f.

the <GdkGLContext>

Method: glarea:get-error self ⇒ %return
Method: get-error self ⇒ %return

Gets the current error set on the self.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a <GError>, or #f.

the <GError>

Method: glarea:get-has-depth-buffer? self ⇒ %return
Method: get-has-depth-buffer? self ⇒ %return

Returns whether the area has a depth buffer.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a boolean.

#t if the self has a depth buffer, #f otherwise

Method: glarea:get-has-stencil-buffer? self ⇒ %return
Method: get-has-stencil-buffer? self ⇒ %return

Returns whether the area has a stencil buffer.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a boolean.

#t if the self has a stencil buffer, #f otherwise

Method: glarea:get-required-version self ⇒ major minor
Method: get-required-version self ⇒ major minor

Retrieves the required version of OpenGL.

See glarea:set-required-version.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

major

Returned: a 32-bit integer.

return location for the required major version

minor

Returned: a 32-bit integer.

return location for the required minor version

Method: glarea:get-use-es? self ⇒ %return
Method: get-use-es? self ⇒ %return

Returns whether the <GtkGLArea> should use OpenGL ES.

See glarea:set-use-es.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

%return

Returned: a boolean.

#t if the <GtkGLArea> should create an OpenGL ES context and #f otherwise

Method: glarea:make-current self
Method: make-current self

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

Procedure: glarea:new ⇒ %return

Creates a new <GtkGLArea> widget.

%return

Returned: a <GtkWidget>.

a new <GtkGLArea>

Method: glarea:queue-render self
Method: queue-render self

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

Method: glarea:set-allowed-apis self apis
Method: set-allowed-apis self apis

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

apis

Passed: a <GdkGLAPI>, or a list of its symbols, or one of them.

the allowed APIs

Method: glarea:set-auto-render self auto-render
Method: set-auto-render self auto-render

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

auto-render

Passed: a boolean.

a boolean

Method: glarea:set-error self [error]
Method: set-error self [error]

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

error

Passed, or left out: a <GError>, or #f.

a new <GError>, or #f to unset the error

Method: glarea:set-has-depth-buffer self has-depth-buffer
Method: set-has-depth-buffer self has-depth-buffer

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

has-depth-buffer

Passed: a boolean.

#t to add a depth buffer

Method: glarea:set-has-stencil-buffer self has-stencil-buffer
Method: set-has-stencil-buffer self has-stencil-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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

has-stencil-buffer

Passed: a boolean.

#t to add a stencil buffer

Method: glarea:set-required-version self major minor
Method: set-required-version self major minor

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

major

Passed: a 32-bit integer.

the major version

minor

Passed: a 32-bit integer.

the minor version

Method: glarea:set-use-es self use-es
Method: set-use-es self use-es

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.

self

Passed: a <GtkGLArea>.

a <GtkGLArea>

use-es

Passed: a boolean.

whether to use OpenGL or OpenGL ES

Signals

Signal: glarea:create-context

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.

self

Passed: a <GtkGLArea>.

%return

Returned: a <GdkGLContext>.

a newly created <GdkGLContext>; the <GtkGLArea> widget will take ownership of the returned value.

Signal: glarea:render

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.

self

Passed: a <GtkGLArea>.

context

Passed: a <GdkGLContext>.

the <GdkGLContext> used by area

%return

Returned: a boolean.

#t to stop other handlers from being invoked for the event. #f to propagate the event further.

Signal: glarea:resize

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.

self

Passed: a <GtkGLArea>.

width

Passed: a 32-bit integer.

the width of the viewport

height

Passed: a 32-bit integer.

the height of the viewport

Virtual methods

Virtual method: glarea:do-render implementor self context ⇒ %return

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.

implementor

Passed: the class whose implementation to call: <GtkGLArea>, or a class that derives from it, or its GType number.

self

Passed: a <GtkGLArea>.

context

Passed: a <GdkGLContext>.

%return

Returned: a boolean.

Virtual method: glarea:do-resize implementor self width height

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).

implementor

Passed: the class whose implementation to call: <GtkGLArea>, or a class that derives from it, or its GType number.

self

Passed: a <GtkGLArea>.

width

Passed: a 32-bit integer.

height

Passed: a 32-bit integer.