~ubuntu-branches/debian/sid/gtkglext/sid

« back to all changes in this revision

Viewing changes to examples/pixmap-mixed.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2004-04-03 17:43:17 UTC
  • Revision ID: james.westby@ubuntu.com-20040403174317-d5gb2d2gftaligp8
Tags: upstream-1.0.6
ImportĀ upstreamĀ versionĀ 1.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pixmap-mixed.c:
 
3
 * Simple off-screen rendering example for mixing OpenGL and GDK rendering.
 
4
 *
 
5
 * written by Naofumi Yasufuku  <naofumi@users.sourceforge.net>
 
6
 */
 
7
 
 
8
#include <stdlib.h>
 
9
 
 
10
#include <gtk/gtk.h>
 
11
 
 
12
#include <gtk/gtkgl.h>
 
13
 
 
14
#ifdef G_OS_WIN32
 
15
#define WIN32_LEAN_AND_MEAN 1
 
16
#include <windows.h>
 
17
#endif
 
18
 
 
19
#include <GL/gl.h>
 
20
#include <GL/glu.h>
 
21
 
 
22
static GdkGLConfig *glconfig = NULL;
 
23
static GdkGLContext *glcontext = NULL;
 
24
static GdkPixmap *pixmap = NULL;
 
25
 
 
26
static void
 
27
init (void)
 
28
{
 
29
  GLUquadricObj *qobj;
 
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};
 
32
 
 
33
  qobj = gluNewQuadric ();
 
34
  gluQuadricDrawStyle (qobj, GLU_FILL);
 
35
  glNewList (1, GL_COMPILE);
 
36
  gluSphere (qobj, 1.0, 20, 20);
 
37
  glEndList ();
 
38
 
 
39
  glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
 
40
  glLightfv (GL_LIGHT0, GL_POSITION, light_position);
 
41
  glEnable (GL_LIGHTING);
 
42
  glEnable (GL_LIGHT0);
 
43
  glEnable (GL_DEPTH_TEST);
 
44
 
 
45
  glClearColor (1.0, 1.0, 1.0, 1.0);
 
46
  glClearDepth (1.0);
 
47
 
 
48
  glMatrixMode (GL_PROJECTION);
 
49
  glLoadIdentity ();
 
50
  gluPerspective (40.0, 1.0, 1.0, 10.0);
 
51
 
 
52
  glMatrixMode (GL_MODELVIEW);
 
53
  glLoadIdentity ();
 
54
  gluLookAt (0.0, 0.0, 3.0,
 
55
             0.0, 0.0, 0.0,
 
56
             0.0, 1.0, 0.0);
 
57
  glTranslatef (0.0, 0.0, -3.0);
 
58
}
 
59
 
 
60
static gboolean
 
61
configure_event (GtkWidget         *widget,
 
62
                 GdkEventConfigure *event,
 
63
                 gpointer           data)
 
64
{
 
65
  GdkGLDrawable *gldrawable;
 
66
  static gboolean is_initialized = FALSE;
 
67
 
 
68
  /*
 
69
   * Create an OpenGL off-screen rendering area.
 
70
   */
 
71
 
 
72
  if (pixmap != NULL)
 
73
    g_object_unref (G_OBJECT (pixmap));
 
74
 
 
75
  pixmap = gdk_pixmap_new (widget->window,
 
76
                           widget->allocation.width,
 
77
                           widget->allocation.height,
 
78
                           -1);
 
79
 
 
80
  /*
 
81
   * Set OpenGL-capability to the pixmap
 
82
   */
 
83
 
 
84
  gldrawable = GDK_GL_DRAWABLE (gdk_pixmap_set_gl_capability (pixmap,
 
85
                                                              glconfig,
 
86
                                                              NULL));
 
87
 
 
88
  /*
 
89
   * Create OpenGL rendering context (not direct).
 
90
   */
 
91
 
 
92
  if (glcontext == NULL)
 
93
    {
 
94
      glcontext = gdk_gl_context_new (gldrawable,
 
95
                                      NULL,
 
96
                                      FALSE,
 
97
                                      GDK_GL_RGBA_TYPE);
 
98
      if (glcontext == NULL)
 
99
        {
 
100
          g_print ("Connot create the OpenGL rendering context\n");
 
101
          if (gtk_main_level () != 0)
 
102
            gtk_main_quit ();
 
103
          return TRUE;
 
104
        }
 
105
 
 
106
      g_print ("The OpenGL rendering context is created\n");
 
107
    }
 
108
 
 
109
  /*** OpenGL BEGIN ***/
 
110
  if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
 
111
    goto NO_GL;
 
112
 
 
113
  if (!is_initialized)
 
114
    {
 
115
      init ();
 
116
      is_initialized = TRUE;
 
117
    }
 
118
 
 
119
  glViewport (0, 0,
 
120
              widget->allocation.width, widget->allocation.height);
 
121
 
 
122
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
123
 
 
124
  /* Sync. */
 
125
  gdk_gl_drawable_wait_gl (gldrawable);
 
126
 
 
127
  /* GDK rendering. */
 
128
  gdk_draw_rectangle (GDK_DRAWABLE (gldrawable),
 
129
                      widget->style->black_gc,
 
130
                      TRUE,
 
131
                      widget->allocation.width/10,
 
132
                      widget->allocation.height/10,
 
133
                      widget->allocation.width*8/10,
 
134
                      widget->allocation.height*8/10);
 
135
 
 
136
  /* Sync. */
 
137
  gdk_gl_drawable_wait_gdk (gldrawable);
 
138
 
 
139
  glCallList (1);
 
140
 
 
141
  glFlush ();
 
142
 
 
143
  gdk_gl_drawable_gl_end (gldrawable);
 
144
  /*** OpenGL END ***/
 
145
 
 
146
 NO_GL:
 
147
 
 
148
  return TRUE;
 
149
}
 
150
 
 
151
static gboolean
 
152
expose_event (GtkWidget      *widget,
 
153
              GdkEventExpose *event,
 
154
              gpointer        data)
 
155
{
 
156
  gdk_draw_drawable (widget->window,
 
157
                     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
 
158
                     pixmap,
 
159
                     event->area.x, event->area.y,
 
160
                     event->area.x, event->area.y,
 
161
                     event->area.width, event->area.height);
 
162
 
 
163
  return FALSE;
 
164
}
 
165
 
 
166
static gboolean
 
167
destroy_gl_context (gpointer data)
 
168
{
 
169
  if (glconfig != NULL)
 
170
    g_object_unref (G_OBJECT (glconfig));
 
171
 
 
172
  if (glcontext != NULL)
 
173
    g_object_unref (G_OBJECT (glcontext));
 
174
 
 
175
  return FALSE;
 
176
}
 
177
 
 
178
static void
 
179
print_gl_config_attrib (GdkGLConfig *glconfig,
 
180
                        const gchar *attrib_str,
 
181
                        int          attrib,
 
182
                        gboolean     is_boolean)
 
183
{
 
184
  int value;
 
185
 
 
186
  g_print ("%s = ", attrib_str);
 
187
  if (gdk_gl_config_get_attrib (glconfig, attrib, &value))
 
188
    {
 
189
      if (is_boolean)
 
190
        g_print ("%s\n", value == TRUE ? "TRUE" : "FALSE");
 
191
      else
 
192
        g_print ("%d\n", value);
 
193
    }
 
194
  else
 
195
    g_print ("*** Cannot get %s attribute value\n", attrib_str);
 
196
}
 
197
 
 
198
static void
 
199
examine_gl_config_attrib (GdkGLConfig *glconfig)
 
200
{
 
201
  g_print ("\nOpenGL visual configurations :\n\n");
 
202
 
 
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");
 
217
 
 
218
  g_print ("\n");
 
219
 
 
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);
 
237
 
 
238
  g_print ("\n");
 
239
}
 
240
 
 
241
int
 
242
main (int   argc,
 
243
      char *argv[])
 
244
{
 
245
  gint major, minor;
 
246
 
 
247
  GtkWidget *window;
 
248
  GtkWidget *vbox;
 
249
  GtkWidget *drawing_area;
 
250
  GtkWidget *button;
 
251
 
 
252
  /*
 
253
   * Init GTK.
 
254
   */
 
255
 
 
256
  gtk_init (&argc, &argv);
 
257
 
 
258
  /*
 
259
   * Init GtkGLExt.
 
260
   */
 
261
 
 
262
  gtk_gl_init (&argc, &argv);
 
263
 
 
264
  /*
 
265
   * Query OpenGL extension version.
 
266
   */
 
267
 
 
268
  gdk_gl_query_version (&major, &minor);
 
269
  g_print ("\nOpenGL extension version - %d.%d\n",
 
270
           major, minor);
 
271
 
 
272
  /*
 
273
   * Configure OpenGL-capable visual.
 
274
   */
 
275
 
 
276
  /* Try single-buffered visual */
 
277
  glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB    |
 
278
                                        GDK_GL_MODE_DEPTH  |
 
279
                                        GDK_GL_MODE_SINGLE);
 
280
  if (glconfig == NULL)
 
281
    {
 
282
      g_print ("*** No appropriate OpenGL-capable visual found.\n");
 
283
      exit (1);
 
284
    }
 
285
 
 
286
  examine_gl_config_attrib (glconfig);
 
287
 
 
288
  /*
 
289
   * Top-level window.
 
290
   */
 
291
 
 
292
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
293
  gtk_window_set_title (GTK_WINDOW (window), "pixmap");
 
294
 
 
295
  /*
 
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
 
298
   * top-level window.
 
299
   */
 
300
  gtk_widget_set_colormap (window,
 
301
                           gdk_gl_config_get_colormap (glconfig));
 
302
 
 
303
  g_signal_connect (G_OBJECT (window), "delete_event",
 
304
                    G_CALLBACK (gtk_main_quit), NULL);
 
305
 
 
306
  /*
 
307
   * VBox.
 
308
   */
 
309
 
 
310
  vbox = gtk_vbox_new (FALSE, 0);
 
311
  gtk_container_add (GTK_CONTAINER (window), vbox);
 
312
  gtk_widget_show (vbox);
 
313
 
 
314
  /*
 
315
   * Drawing area for drawing OpenGL scene.
 
316
   */
 
317
 
 
318
  drawing_area = gtk_drawing_area_new ();
 
319
  gtk_widget_set_size_request (drawing_area, 200, 200);
 
320
 
 
321
  /* Set OpenGL-capable colormap. */
 
322
  gtk_widget_set_colormap (drawing_area,
 
323
                           gdk_gl_config_get_colormap (glconfig));
 
324
 
 
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);
 
329
 
 
330
  gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
 
331
 
 
332
  gtk_widget_show (drawing_area);
 
333
 
 
334
  /*
 
335
   * Simple quit button.
 
336
   */
 
337
 
 
338
  button = gtk_button_new_with_label ("Quit");
 
339
 
 
340
  g_signal_connect (G_OBJECT (button), "clicked",
 
341
                    G_CALLBACK (gtk_main_quit), NULL);
 
342
 
 
343
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
 
344
 
 
345
  gtk_widget_show (button);
 
346
 
 
347
  /*
 
348
   * Show window.
 
349
   */
 
350
 
 
351
  gtk_widget_show (window);
 
352
 
 
353
  /*
 
354
   * Main loop.
 
355
   */
 
356
 
 
357
  /* Destroy the GLX context explicitly when application is terminated. */
 
358
  gtk_quit_add (0, (GtkFunction) destroy_gl_context, NULL);
 
359
 
 
360
  gtk_main ();
 
361
 
 
362
  return 0;
 
363
}