3
* Simple off-screen OpenGL rendering example.
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);
128
gdk_gl_drawable_gl_end (gldrawable);
135
expose_event (GtkWidget *widget,
136
GdkEventExpose *event,
139
gdk_draw_drawable (widget->window,
140
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
142
event->area.x, event->area.y,
143
event->area.x, event->area.y,
144
event->area.width, event->area.height);
150
destroy_gl_context (gpointer data)
152
if (glconfig != NULL)
153
g_object_unref (G_OBJECT (glconfig));
155
if (glcontext != NULL)
156
g_object_unref (G_OBJECT (glcontext));
162
print_gl_config_attrib (GdkGLConfig *glconfig,
163
const gchar *attrib_str,
169
g_print ("%s = ", attrib_str);
170
if (gdk_gl_config_get_attrib (glconfig, attrib, &value))
173
g_print ("%s\n", value == TRUE ? "TRUE" : "FALSE");
175
g_print ("%d\n", value);
178
g_print ("*** Cannot get %s attribute value\n", attrib_str);
182
examine_gl_config_attrib (GdkGLConfig *glconfig)
184
g_print ("\nOpenGL visual configurations :\n\n");
186
g_print ("gdk_gl_config_is_rgba (glconfig) = %s\n",
187
gdk_gl_config_is_rgba (glconfig) ? "TRUE" : "FALSE");
188
g_print ("gdk_gl_config_is_double_buffered (glconfig) = %s\n",
189
gdk_gl_config_is_double_buffered (glconfig) ? "TRUE" : "FALSE");
190
g_print ("gdk_gl_config_is_stereo (glconfig) = %s\n",
191
gdk_gl_config_is_stereo (glconfig) ? "TRUE" : "FALSE");
192
g_print ("gdk_gl_config_has_alpha (glconfig) = %s\n",
193
gdk_gl_config_has_alpha (glconfig) ? "TRUE" : "FALSE");
194
g_print ("gdk_gl_config_has_depth_buffer (glconfig) = %s\n",
195
gdk_gl_config_has_depth_buffer (glconfig) ? "TRUE" : "FALSE");
196
g_print ("gdk_gl_config_has_stencil_buffer (glconfig) = %s\n",
197
gdk_gl_config_has_stencil_buffer (glconfig) ? "TRUE" : "FALSE");
198
g_print ("gdk_gl_config_has_accum_buffer (glconfig) = %s\n",
199
gdk_gl_config_has_accum_buffer (glconfig) ? "TRUE" : "FALSE");
203
print_gl_config_attrib (glconfig, "GDK_GL_USE_GL", GDK_GL_USE_GL, TRUE);
204
print_gl_config_attrib (glconfig, "GDK_GL_BUFFER_SIZE", GDK_GL_BUFFER_SIZE, FALSE);
205
print_gl_config_attrib (glconfig, "GDK_GL_LEVEL", GDK_GL_LEVEL, FALSE);
206
print_gl_config_attrib (glconfig, "GDK_GL_RGBA", GDK_GL_RGBA, TRUE);
207
print_gl_config_attrib (glconfig, "GDK_GL_DOUBLEBUFFER", GDK_GL_DOUBLEBUFFER, TRUE);
208
print_gl_config_attrib (glconfig, "GDK_GL_STEREO", GDK_GL_STEREO, TRUE);
209
print_gl_config_attrib (glconfig, "GDK_GL_AUX_BUFFERS", GDK_GL_AUX_BUFFERS, FALSE);
210
print_gl_config_attrib (glconfig, "GDK_GL_RED_SIZE", GDK_GL_RED_SIZE, FALSE);
211
print_gl_config_attrib (glconfig, "GDK_GL_GREEN_SIZE", GDK_GL_GREEN_SIZE, FALSE);
212
print_gl_config_attrib (glconfig, "GDK_GL_BLUE_SIZE", GDK_GL_BLUE_SIZE, FALSE);
213
print_gl_config_attrib (glconfig, "GDK_GL_ALPHA_SIZE", GDK_GL_ALPHA_SIZE, FALSE);
214
print_gl_config_attrib (glconfig, "GDK_GL_DEPTH_SIZE", GDK_GL_DEPTH_SIZE, FALSE);
215
print_gl_config_attrib (glconfig, "GDK_GL_STENCIL_SIZE", GDK_GL_STENCIL_SIZE, FALSE);
216
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_RED_SIZE", GDK_GL_ACCUM_RED_SIZE, FALSE);
217
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_GREEN_SIZE", GDK_GL_ACCUM_GREEN_SIZE, FALSE);
218
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_BLUE_SIZE", GDK_GL_ACCUM_BLUE_SIZE, FALSE);
219
print_gl_config_attrib (glconfig, "GDK_GL_ACCUM_ALPHA_SIZE", GDK_GL_ACCUM_ALPHA_SIZE, FALSE);
232
GtkWidget *drawing_area;
239
gtk_init (&argc, &argv);
245
gtk_gl_init (&argc, &argv);
248
* Query OpenGL extension version.
251
gdk_gl_query_version (&major, &minor);
252
g_print ("\nOpenGL extension version - %d.%d\n",
256
* Configure OpenGL-capable visual.
259
/* Try single-buffered visual */
260
glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB |
263
if (glconfig == NULL)
265
g_print ("*** No appropriate OpenGL-capable visual found.\n");
269
examine_gl_config_attrib (glconfig);
275
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
276
gtk_window_set_title (GTK_WINDOW (window), "pixmap");
279
* If window manager doesn't watch the WM_COLORMAP_WINDOWS property on
280
* the top-level window, we have to set OpenGL window's colormap to the
283
gtk_widget_set_colormap (window,
284
gdk_gl_config_get_colormap (glconfig));
286
g_signal_connect (G_OBJECT (window), "delete_event",
287
G_CALLBACK (gtk_main_quit), NULL);
293
vbox = gtk_vbox_new (FALSE, 0);
294
gtk_container_add (GTK_CONTAINER (window), vbox);
295
gtk_widget_show (vbox);
298
* Drawing area for drawing OpenGL scene.
301
drawing_area = gtk_drawing_area_new ();
302
gtk_widget_set_size_request (drawing_area, 200, 200);
304
/* Set OpenGL-capable colormap. */
305
gtk_widget_set_colormap (drawing_area,
306
gdk_gl_config_get_colormap (glconfig));
308
g_signal_connect (G_OBJECT (drawing_area), "configure_event",
309
G_CALLBACK (configure_event), NULL);
310
g_signal_connect (G_OBJECT (drawing_area), "expose_event",
311
G_CALLBACK (expose_event), NULL);
313
gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
315
gtk_widget_show (drawing_area);
318
* Simple quit button.
321
button = gtk_button_new_with_label ("Quit");
323
g_signal_connect (G_OBJECT (button), "clicked",
324
G_CALLBACK (gtk_main_quit), NULL);
326
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
328
gtk_widget_show (button);
334
gtk_widget_show (window);
340
/* Destroy the GLX context explicitly when application is terminated. */
341
gtk_quit_add (0, (GtkFunction) destroy_gl_context, NULL);