3
* Simple off-screen rendering example for mixing OpenGL and GDK rendering.
5
* written by Naofumi Yasufuku <naofumi@users.sourceforge.net>
12
#include <gtk/gtkgl.h>
15
#define WIN32_LEAN_AND_MEAN 1
22
static GdkGLConfig *glconfig = NULL;
23
static GdkGLContext *glcontext = NULL;
24
static GdkPixmap *pixmap = NULL;
30
static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};
31
static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
33
qobj = gluNewQuadric ();
34
gluQuadricDrawStyle (qobj, GLU_FILL);
35
glNewList (1, GL_COMPILE);
36
gluSphere (qobj, 1.0, 20, 20);
39
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
40
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
41
glEnable (GL_LIGHTING);
43
glEnable (GL_DEPTH_TEST);
45
glClearColor (1.0, 1.0, 1.0, 1.0);
48
glMatrixMode (GL_PROJECTION);
50
gluPerspective (40.0, 1.0, 1.0, 10.0);
52
glMatrixMode (GL_MODELVIEW);
54
gluLookAt (0.0, 0.0, 3.0,
57
glTranslatef (0.0, 0.0, -3.0);
61
configure_event (GtkWidget *widget,
62
GdkEventConfigure *event,
65
GdkGLDrawable *gldrawable;
66
static gboolean is_initialized = FALSE;
69
* Create an OpenGL off-screen rendering area.
73
g_object_unref (G_OBJECT (pixmap));
75
pixmap = gdk_pixmap_new (widget->window,
76
widget->allocation.width,
77
widget->allocation.height,
81
* Set OpenGL-capability to the pixmap
84
gldrawable = GDK_GL_DRAWABLE (gdk_pixmap_set_gl_capability (pixmap,
89
* Create OpenGL rendering context (not direct).
92
if (glcontext == NULL)
94
glcontext = gdk_gl_context_new (gldrawable,
98
if (glcontext == NULL)
100
g_print ("Connot create the OpenGL rendering context\n");
101
if (gtk_main_level () != 0)
106
g_print ("The OpenGL rendering context is created\n");
109
/*** OpenGL BEGIN ***/
110
if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
116
is_initialized = TRUE;
120
widget->allocation.width, widget->allocation.height);
122
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
125
gdk_gl_drawable_wait_gl (gldrawable);
128
gdk_draw_rectangle (GDK_DRAWABLE (gldrawable),
129
widget->style->black_gc,
131
widget->allocation.width/10,
132
widget->allocation.height/10,
133
widget->allocation.width*8/10,
134
widget->allocation.height*8/10);
137
gdk_gl_drawable_wait_gdk (gldrawable);
143
gdk_gl_drawable_gl_end (gldrawable);
152
expose_event (GtkWidget *widget,
153
GdkEventExpose *event,
156
gdk_draw_drawable (widget->window,
157
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
159
event->area.x, event->area.y,
160
event->area.x, event->area.y,
161
event->area.width, event->area.height);
167
destroy_gl_context (gpointer data)
169
if (glconfig != NULL)
170
g_object_unref (G_OBJECT (glconfig));
172
if (glcontext != NULL)
173
g_object_unref (G_OBJECT (glcontext));
179
print_gl_config_attrib (GdkGLConfig *glconfig,
180
const gchar *attrib_str,
186
g_print ("%s = ", attrib_str);
187
if (gdk_gl_config_get_attrib (glconfig, attrib, &value))
190
g_print ("%s\n", value == TRUE ? "TRUE" : "FALSE");
192
g_print ("%d\n", value);
195
g_print ("*** Cannot get %s attribute value\n", attrib_str);
199
examine_gl_config_attrib (GdkGLConfig *glconfig)
201
g_print ("\nOpenGL visual configurations :\n\n");
203
g_print ("gdk_gl_config_is_rgba (glconfig) = %s\n",
204
gdk_gl_config_is_rgba (glconfig) ? "TRUE" : "FALSE");
205
g_print ("gdk_gl_config_is_double_buffered (glconfig) = %s\n",
206
gdk_gl_config_is_double_buffered (glconfig) ? "TRUE" : "FALSE");
207
g_print ("gdk_gl_config_is_stereo (glconfig) = %s\n",
208
gdk_gl_config_is_stereo (glconfig) ? "TRUE" : "FALSE");
209
g_print ("gdk_gl_config_has_alpha (glconfig) = %s\n",
210
gdk_gl_config_has_alpha (glconfig) ? "TRUE" : "FALSE");
211
g_print ("gdk_gl_config_has_depth_buffer (glconfig) = %s\n",
212
gdk_gl_config_has_depth_buffer (glconfig) ? "TRUE" : "FALSE");
213
g_print ("gdk_gl_config_has_stencil_buffer (glconfig) = %s\n",
214
gdk_gl_config_has_stencil_buffer (glconfig) ? "TRUE" : "FALSE");
215
g_print ("gdk_gl_config_has_accum_buffer (glconfig) = %s\n",
216
gdk_gl_config_has_accum_buffer (glconfig) ? "TRUE" : "FALSE");
220
print_gl_config_attrib (glconfig, "GDK_GL_USE_GL", GDK_GL_USE_GL, TRUE);
221
print_gl_config_attrib (glconfig, "GDK_GL_BUFFER_SIZE", GDK_GL_BUFFER_SIZE, FALSE);
222
print_gl_config_attrib (glconfig, "GDK_GL_LEVEL", GDK_GL_LEVEL, FALSE);
223
print_gl_config_attrib (glconfig, "GDK_GL_RGBA", GDK_GL_RGBA, TRUE);
224
print_gl_config_attrib (glconfig, "GDK_GL_DOUBLEBUFFER", GDK_GL_DOUBLEBUFFER, TRUE);
225
print_gl_config_attrib (glconfig, "GDK_GL_STEREO", GDK_GL_STEREO, TRUE);
226
print_gl_config_attrib (glconfig, "GDK_GL_AUX_BUFFERS", GDK_GL_AUX_BUFFERS, FALSE);
227
print_gl_config_attrib (glconfig, "GDK_GL_RED_SIZE", GDK_GL_RED_SIZE, FALSE);
228
print_gl_config_attrib (glconfig, "GDK_GL_GREEN_SIZE", GDK_GL_GREEN_SIZE, FALSE);
229
print_gl_config_attrib (glconfig, "GDK_GL_BLUE_SIZE", GDK_GL_BLUE_SIZE, FALSE);
230
print_gl_config_attrib (glconfig, "GDK_GL_ALPHA_SIZE", GDK_GL_ALPHA_SIZE, FALSE);
231
print_gl_config_attrib (glconfig, "GDK_GL_DEPTH_SIZE", GDK_GL_DEPTH_SIZE, FALSE);
232
print_gl_config_attrib (glconfig, "GDK_GL_STENCIL_SIZE", GDK_GL_STENCIL_SIZE, FALSE);
233
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_RED_SIZE", GDK_GL_ACCUM_RED_SIZE, FALSE);
234
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_GREEN_SIZE", GDK_GL_ACCUM_GREEN_SIZE, FALSE);
235
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_BLUE_SIZE", GDK_GL_ACCUM_BLUE_SIZE, FALSE);
236
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_ALPHA_SIZE", GDK_GL_ACCUM_ALPHA_SIZE, FALSE);
249
GtkWidget *drawing_area;
256
gtk_init (&argc, &argv);
262
gtk_gl_init (&argc, &argv);
265
* Query OpenGL extension version.
268
gdk_gl_query_version (&major, &minor);
269
g_print ("\nOpenGL extension version - %d.%d\n",
273
* Configure OpenGL-capable visual.
276
/* Try single-buffered visual */
277
glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB |
280
if (glconfig == NULL)
282
g_print ("*** No appropriate OpenGL-capable visual found.\n");
286
examine_gl_config_attrib (glconfig);
292
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
293
gtk_window_set_title (GTK_WINDOW (window), "pixmap");
296
* If window manager doesn't watch the WM_COLORMAP_WINDOWS property on
297
* the top-level window, we have to set OpenGL window's colormap to the
300
gtk_widget_set_colormap (window,
301
gdk_gl_config_get_colormap (glconfig));
303
g_signal_connect (G_OBJECT (window), "delete_event",
304
G_CALLBACK (gtk_main_quit), NULL);
310
vbox = gtk_vbox_new (FALSE, 0);
311
gtk_container_add (GTK_CONTAINER (window), vbox);
312
gtk_widget_show (vbox);
315
* Drawing area for drawing OpenGL scene.
318
drawing_area = gtk_drawing_area_new ();
319
gtk_widget_set_size_request (drawing_area, 200, 200);
321
/* Set OpenGL-capable colormap. */
322
gtk_widget_set_colormap (drawing_area,
323
gdk_gl_config_get_colormap (glconfig));
325
g_signal_connect (G_OBJECT (drawing_area), "configure_event",
326
G_CALLBACK (configure_event), NULL);
327
g_signal_connect (G_OBJECT (drawing_area), "expose_event",
328
G_CALLBACK (expose_event), NULL);
330
gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
332
gtk_widget_show (drawing_area);
335
* Simple quit button.
338
button = gtk_button_new_with_label ("Quit");
340
g_signal_connect (G_OBJECT (button), "clicked",
341
G_CALLBACK (gtk_main_quit), NULL);
343
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
345
gtk_widget_show (button);
351
gtk_widget_show (window);
357
/* Destroy the GLX context explicitly when application is terminated. */
358
gtk_quit_add (0, (GtkFunction) destroy_gl_context, NULL);