Package ch.bailu.gtk.gtk
Class GLArea
- All Implemented Interfaces:
PointerInterface
`GtkGLArea` is a widget that allows drawing with OpenGL.
![An example GtkGLArea](glarea.png)
`GtkGLArea` sets up its own [class@Gdk.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.
In order to draw, you have to connect to the [signal@Gtk.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 [signal@Gtk.GLArea::render] signal:
The `render()` function will be called when the `GtkGLArea` is ready
for you to draw its content:
```c
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;
}
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 [signal@Gtk.Widget::realize] signal;
you can use the [signal@Gtk.Widget::unrealize] signal to clean up.
Since the `GdkGLContext` creation and initialization may fail, you
will need to check for errors, using [method@Gtk.GLArea.get_error].
An example of how to safely initialize the GL state is:
```c
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 [signal@Gtk.GLArea::create-context] signal.
![An example GtkGLArea](glarea.png)
`GtkGLArea` sets up its own [class@Gdk.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.
In order to draw, you have to connect to the [signal@Gtk.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 [signal@Gtk.GLArea::render] signal:
The `render()` function will be called when the `GtkGLArea` is ready
for you to draw its content:
```c
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;
}
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 [signal@Gtk.Widget::realize] signal;
you can use the [signal@Gtk.Widget::unrealize] signal to clean up.
Since the `GdkGLContext` creation and initialization may fail, you
will need to check for errors, using [method@Gtk.GLArea.get_error].
An example of how to safely initialize the GL state is:
```c
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 [signal@Gtk.GLArea::create-context] signal.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
static interface
Nested classes/interfaces inherited from class ch.bailu.gtk.gtk.Widget
Widget.OnDestroy, Widget.OnDestroyNotify, Widget.OnDirectionChanged, Widget.OnHide, Widget.OnKeynavFailed, Widget.OnMap, Widget.OnMnemonicActivate, Widget.OnMoveFocus, Widget.OnQueryTooltip, Widget.OnRealize, Widget.OnShow, Widget.OnStateFlagsChanged, Widget.OnTickCallback, Widget.OnUnmap, Widget.OnUnrealize
Nested classes/interfaces inherited from class ch.bailu.gtk.gobject.Object
Object.OnBindingTransformFunc, Object.OnDuplicateFunc, Object.OnNotify, Object.OnToggleNotify, Object.OnWeakNotify
-
Field Summary
Fields inherited from class ch.bailu.gtk.gtk.Widget
SIGNAL_ON_DESTROY, SIGNAL_ON_DIRECTION_CHANGED, SIGNAL_ON_HIDE, SIGNAL_ON_KEYNAV_FAILED, SIGNAL_ON_MAP, SIGNAL_ON_MNEMONIC_ACTIVATE, SIGNAL_ON_MOVE_FOCUS, SIGNAL_ON_QUERY_TOOLTIP, SIGNAL_ON_REALIZE, SIGNAL_ON_SHOW, SIGNAL_ON_STATE_FLAGS_CHANGED, SIGNAL_ON_UNMAP, SIGNAL_ON_UNREALIZE
Fields inherited from class ch.bailu.gtk.gobject.Object
SIGNAL_ON_NOTIFY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionImplements interfaceAccessible
.Implements interfaceBuildable
.Implements interfaceConstraintTarget
.void
Binds buffers to the framebuffer.boolean
Returns whether the area is in auto render mode or not.static ClassHandler
Retrieves the `GdkGLContext` used by @area.getError()
Gets the current error set on the @area.boolean
Returns whether the area has a depth buffer.boolean
Returns whether the area has a stencil buffer.static int
static long
static TypeSystem.TypeSize
void
getRequiredVersion
(Int major, Int minor) Retrieves the required version of OpenGL.static long
static TypeSystem.TypeSize
boolean
getUseEs()
Returns whether the `GtkGLArea` should use OpenGL ES.void
Ensures that the `GdkGLContext` used by @area is associated with
the `GtkGLArea`.Connect to signal "create-context".onRender
(GLArea.OnRender signal) Connect to signal "render".onResize
(GLArea.OnResize signal) Connect to signal "resize".void
Marks the currently rendered data (if any) as invalid, and queues
a redraw of the widget.void
setAutoRender
(boolean auto_render) Sets whether the `GtkGLArea` is in auto render mode.void
Sets an error on the area which will be shown instead of the
GL rendering.void
setHasDepthBuffer
(boolean has_depth_buffer) Sets whether the `GtkGLArea` should use a depth buffer.void
setHasStencilBuffer
(boolean has_stencil_buffer) Sets whether the `GtkGLArea` should use a stencil buffer.void
setRequiredVersion
(int major, int minor) Sets the required version of OpenGL to be used when creating
the context for the widget.void
setUseEs
(boolean use_es) Sets whether the @area should create an OpenGL or an OpenGL ES context.Methods inherited from class ch.bailu.gtk.gtk.Widget
actionSetEnabled, actionSetEnabled, activate, activateAction, activateAction, activateActionVariant, activateActionVariant, activateDefault, addController, addCssClass, addCssClass, addMnemonicLabel, addTickCallback, allocate, childFocus, computeBounds, computeExpand, computePoint, computeTransform, contains, createPangoContext, createPangoLayout, createPangoLayout, disposeTemplate, dragCheckThreshold, errorBell, getAllocatedBaseline, getAllocatedHeight, getAllocatedWidth, getAllocation, getAncestor, getCanFocus, getCanTarget, getChildVisible, getClipboard, getCssClasses, getCssName, getCursor, getDefaultDirection, getDirection, getDisplay, getFirstChild, getFocusable, getFocusChild, getFocusOnClick, getFontMap, getFontOptions, getFrameClock, getHalign, getHasTooltip, getHeight, getHexpand, getHexpandSet, getLastChild, getLayoutManager, getMapped, getMarginBottom, getMarginEnd, getMarginStart, getMarginTop, getName, getNative, getNextSibling, getOpacity, getOverflow, getPangoContext, getParent, getPreferredSize, getPrevSibling, getPrimaryClipboard, getRealized, getReceivesDefault, getRequestMode, getRoot, getScaleFactor, getSensitive, getSettings, getSize, getSizeRequest, getStateFlags, getStyleContext, getTemplateChild, getTemplateChild, getTooltipMarkup, getTooltipText, getValign, getVexpand, getVexpandSet, getVisible, getWidth, grabFocus, hasCssClass, hasCssClass, hasDefault, hasFocus, hasVisibleFocus, hide, inDestruction, initTemplate, insertActionGroup, insertActionGroup, insertAfter, insertBefore, isAncestor, isDrawable, isFocus, isSensitive, isVisible, keynavFailed, listMnemonicLabels, map, measure, mnemonicActivate, observeChildren, observeControllers, onDestroy, onDirectionChanged, onHide, onKeynavFailed, onMap, onMnemonicActivate, onMoveFocus, onQueryTooltip, onRealize, onShow, onStateFlagsChanged, onUnmap, onUnrealize, pick, queueAllocate, queueDraw, queueResize, realize, removeController, removeCssClass, removeCssClass, removeMnemonicLabel, removeTickCallback, setCanFocus, setCanTarget, setChildVisible, setCssClasses, setCursor, setCursorFromName, setCursorFromName, setDefaultDirection, setDirection, setFocusable, setFocusChild, setFocusOnClick, setFontMap, setFontOptions, setHalign, setHasTooltip, setHexpand, setHexpandSet, setLayoutManager, setMarginBottom, setMarginEnd, setMarginStart, setMarginTop, setName, setName, setOpacity, setOverflow, setParent, setReceivesDefault, setSensitive, setSizeRequest, setStateFlags, setTooltipMarkup, setTooltipMarkup, setTooltipText, setTooltipText, setValign, setVexpand, setVexpandSet, setVisible, shouldLayout, show, sizeAllocate, snapshotChild, triggerTooltipQuery, unmap, unparent, unrealize, unsetStateFlags
Methods inherited from class ch.bailu.gtk.gobject.Object
addToggleRef, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, disconnect, disconnect, dupData, dupData, dupQdata, forceFloating, freezeNotify, get, get, getData, getData, getProperty, getProperty, getQdata, interfaceFindProperty, interfaceInstallProperty, isFloating, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, replaceData, replaceData, replaceQdata, runDispose, set, set, setData, setData, setDataFull, setDataFull, setProperty, setProperty, setQdata, setQdataFull, stealData, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref
Methods inherited from class ch.bailu.gtk.type.Pointer
asCPointer, cast, connectSignal, disconnectSignals, disconnectSignals, equals, hashCode, throwIfNull, throwNullPointerException, toString, unregisterCallbacks, unregisterCallbacks
Methods inherited from class ch.bailu.gtk.type.Type
asCPointer, asCPointer, asCPointerNotNull, asJnaPointer, asJnaPointer, asPointer, asPointer, cast, cast, throwIfNull
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface ch.bailu.gtk.type.PointerInterface
asCPointerNotNull, asJnaPointer, asPointer, isNotNull, isNull
-
Field Details
-
SIGNAL_ON_CREATE_CONTEXT
- See Also:
-
SIGNAL_ON_RENDER
- See Also:
-
SIGNAL_ON_RESIZE
- See Also:
-
-
Constructor Details
-
GLArea
-
GLArea
public GLArea()Creates a new `GtkGLArea` widget.
-
-
Method Details
-
getClassHandler
-
attachBuffers
public void attachBuffers()Binds buffers to the framebuffer.
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 framebuffer.
This function is automatically called before emitting the
[signal@Gtk.GLArea::render] signal, and doesn't normally need to be
called by application code. -
getAutoRender
public boolean getAutoRender()Returns whether the area is in auto render mode or not.- Returns:
- %TRUE if the @area is auto rendering, %FALSE otherwise
-
getContext
Retrieves the `GdkGLContext` used by @area.- Returns:
- the `GdkGLContext`
-
getError
Gets the current error set on the @area.- Returns:
- the `GError`
-
getHasDepthBuffer
public boolean getHasDepthBuffer()Returns whether the area has a depth buffer.- Returns:
- %TRUE if the @area has a depth buffer, %FALSE otherwise
-
getHasStencilBuffer
public boolean getHasStencilBuffer()Returns whether the area has a stencil buffer.- Returns:
- %TRUE if the @area has a stencil buffer, %FALSE otherwise
-
getRequiredVersion
Retrieves the required version of OpenGL.
See [method@Gtk.GLArea.set_required_version].- Parameters:
major
- return location for the required major versionminor
- return location for the required minor version
-
getUseEs
public boolean getUseEs()Returns whether the `GtkGLArea` should use OpenGL ES.
See [method@Gtk.GLArea.set_use_es].- Returns:
- %TRUE if the `GtkGLArea` should create an OpenGL ES context and %FALSE otherwise
-
makeCurrent
public void makeCurrent()Ensures that the `GdkGLContext` used by @area is associated with
the `GtkGLArea`.
This function is automatically called before emitting the
[signal@Gtk.GLArea::render] signal, and doesn't normally need
to be called by application code. -
queueRender
public void queueRender()Marks the currently rendered data (if any) as invalid, and queues
a redraw of the widget.
This ensures that the [signal@Gtk.GLArea::render] signal
is emitted during the draw.
This is only needed when [method@Gtk.GLArea.set_auto_render] has
been called with a %FALSE value. The default behaviour is to
emit [signal@Gtk.GLArea::render] on each draw. -
setAutoRender
public void setAutoRender(boolean auto_render) Sets whether the `GtkGLArea` is in auto render mode.
If @auto_render is %TRUE the [signal@Gtk.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 %FALSE 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
[method@Gtk.GLArea.queue_render] must be called. This mode is
useful when the scene changes seldom, but takes a long time to redraw.- Parameters:
auto_render
- a boolean
-
setError
Sets an error on the area which will be shown instead of the
GL rendering.
This is useful in the [signal@Gtk.GLArea::create-context]
signal if GL context creation fails.- Parameters:
error
- a new `GError`, or %NULL to unset the error
-
setHasDepthBuffer
public void setHasDepthBuffer(boolean has_depth_buffer) Sets whether the `GtkGLArea` should use a depth buffer.
If @has_depth_buffer is %TRUE the widget will allocate and
enable a depth buffer for the target framebuffer. Otherwise
there will be none.- Parameters:
has_depth_buffer
- %TRUE to add a depth buffer
-
setHasStencilBuffer
public void setHasStencilBuffer(boolean has_stencil_buffer) Sets whether the `GtkGLArea` should use a stencil buffer.
If @has_stencil_buffer is %TRUE the widget will allocate and
enable a stencil buffer for the target framebuffer. Otherwise
there will be none.- Parameters:
has_stencil_buffer
- %TRUE to add a stencil buffer
-
setRequiredVersion
public void setRequiredVersion(int major, int 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.- Parameters:
major
- the major versionminor
- the minor version
-
setUseEs
public void setUseEs(boolean use_es) Sets whether the @area should create an OpenGL or an OpenGL ES context.
You should check the capabilities of the `GdkGLContext` before drawing
with either API.- Parameters:
use_es
- whether to use OpenGL or OpenGL ES
-
onCreateContext
Connect to signal "create-context".
SeeGLArea.OnCreateContext.onCreateContext()
for signal description.
FieldSIGNAL_ON_CREATE_CONTEXT
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
onRender
Connect to signal "render".
SeeGLArea.OnRender.onRender(ch.bailu.gtk.gdk.GLContext)
for signal description.
FieldSIGNAL_ON_RENDER
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
onResize
Connect to signal "resize".
SeeGLArea.OnResize.onResize(int, int)
for signal description.
FieldSIGNAL_ON_RESIZE
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
asAccessible
Implements interfaceAccessible
. Call this to get access to interface functions.- Overrides:
asAccessible
in classWidget
- Returns:
Accessible
-
asBuildable
Implements interfaceBuildable
. Call this to get access to interface functions.- Overrides:
asBuildable
in classWidget
- Returns:
Buildable
-
asConstraintTarget
Implements interfaceConstraintTarget
. Call this to get access to interface functions.- Overrides:
asConstraintTarget
in classWidget
- Returns:
ConstraintTarget
-
getTypeID
public static long getTypeID() -
getParentTypeID
public static long getParentTypeID() -
getTypeSize
-
getParentTypeSize
-
getInstanceSize
public static int getInstanceSize()
-