~ubuntu-branches/ubuntu/raring/gtkglext/raring

« back to all changes in this revision

Viewing changes to .pc/02_fix_gtk-2.20_deprecated_symbols.diff/examples/pixmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Micah Gersten
  • Date: 2011-10-18 23:28:01 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20111018232801-xk8ouag7irr32cmj
Tags: 1.2.0-2fakesync1
Fake sync due to mismatching orig tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pixmap.c:
 
3
 * Simple off-screen OpenGL rendering example.
 
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
    return FALSE;
 
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
  glCallList (1);
 
125
 
 
126
  glFlush ();
 
127
 
 
128
  gdk_gl_drawable_gl_end (gldrawable);
 
129
  /*** OpenGL END ***/
 
130
 
 
131
  return TRUE;
 
132
}
 
133
 
 
134
static gboolean
 
135
expose_event (GtkWidget      *widget,
 
136
              GdkEventExpose *event,
 
137
              gpointer        data)
 
138
{
 
139
  gdk_draw_drawable (widget->window,
 
140
                     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
 
141
                     pixmap,
 
142
                     event->area.x, event->area.y,
 
143
                     event->area.x, event->area.y,
 
144
                     event->area.width, event->area.height);
 
145
 
 
146
  return FALSE;
 
147
}
 
148
 
 
149
static gboolean
 
150
destroy_gl_context (gpointer data)
 
151
{
 
152
  if (glconfig != NULL)
 
153
    g_object_unref (G_OBJECT (glconfig));
 
154
 
 
155
  if (glcontext != NULL)
 
156
    g_object_unref (G_OBJECT (glcontext));
 
157
 
 
158
  return FALSE;
 
159
}
 
160
 
 
161
static void
 
162
print_gl_config_attrib (GdkGLConfig *glconfig,
 
163
                        const gchar *attrib_str,
 
164
                        int          attrib,
 
165
                        gboolean     is_boolean)
 
166
{
 
167
  int value;
 
168
 
 
169
  g_print ("%s = ", attrib_str);
 
170
  if (gdk_gl_config_get_attrib (glconfig, attrib, &value))
 
171
    {
 
172
      if (is_boolean)
 
173
        g_print ("%s\n", value == TRUE ? "TRUE" : "FALSE");
 
174
      else
 
175
        g_print ("%d\n", value);
 
176
    }
 
177
  else
 
178
    g_print ("*** Cannot get %s attribute value\n", attrib_str);
 
179
}
 
180
 
 
181
static void
 
182
examine_gl_config_attrib (GdkGLConfig *glconfig)
 
183
{
 
184
  g_print ("\nOpenGL visual configurations :\n\n");
 
185
 
 
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");
 
200
 
 
201
  g_print ("\n");
 
202
 
 
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);
 
220
 
 
221
  g_print ("\n");
 
222
}
 
223
 
 
224
int
 
225
main (int   argc,
 
226
      char *argv[])
 
227
{
 
228
  gint major, minor;
 
229
 
 
230
  GtkWidget *window;
 
231
  GtkWidget *vbox;
 
232
  GtkWidget *drawing_area;
 
233
  GtkWidget *button;
 
234
 
 
235
  /*
 
236
   * Init GTK.
 
237
   */
 
238
 
 
239
  gtk_init (&argc, &argv);
 
240
 
 
241
  /*
 
242
   * Init GtkGLExt.
 
243
   */
 
244
 
 
245
  gtk_gl_init (&argc, &argv);
 
246
 
 
247
  /*
 
248
   * Query OpenGL extension version.
 
249
   */
 
250
 
 
251
  gdk_gl_query_version (&major, &minor);
 
252
  g_print ("\nOpenGL extension version - %d.%d\n",
 
253
           major, minor);
 
254
 
 
255
  /*
 
256
   * Configure OpenGL-capable visual.
 
257
   */
 
258
 
 
259
  /* Try single-buffered visual */
 
260
  glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB    |
 
261
                                        GDK_GL_MODE_DEPTH  |
 
262
                                        GDK_GL_MODE_SINGLE);
 
263
  if (glconfig == NULL)
 
264
    {
 
265
      g_print ("*** No appropriate OpenGL-capable visual found.\n");
 
266
      exit (1);
 
267
    }
 
268
 
 
269
  examine_gl_config_attrib (glconfig);
 
270
 
 
271
  /*
 
272
   * Top-level window.
 
273
   */
 
274
 
 
275
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
276
  gtk_window_set_title (GTK_WINDOW (window), "pixmap");
 
277
 
 
278
  /*
 
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
 
281
   * top-level window.
 
282
   */
 
283
  gtk_widget_set_colormap (window,
 
284
                           gdk_gl_config_get_colormap (glconfig));
 
285
 
 
286
  g_signal_connect (G_OBJECT (window), "delete_event",
 
287
                    G_CALLBACK (gtk_main_quit), NULL);
 
288
 
 
289
  /*
 
290
   * VBox.
 
291
   */
 
292
 
 
293
  vbox = gtk_vbox_new (FALSE, 0);
 
294
  gtk_container_add (GTK_CONTAINER (window), vbox);
 
295
  gtk_widget_show (vbox);
 
296
 
 
297
  /*
 
298
   * Drawing area for drawing OpenGL scene.
 
299
   */
 
300
 
 
301
  drawing_area = gtk_drawing_area_new ();
 
302
  gtk_widget_set_size_request (drawing_area, 200, 200);
 
303
 
 
304
  /* Set OpenGL-capable colormap. */
 
305
  gtk_widget_set_colormap (drawing_area,
 
306
                           gdk_gl_config_get_colormap (glconfig));
 
307
 
 
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);
 
312
 
 
313
  gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
 
314
 
 
315
  gtk_widget_show (drawing_area);
 
316
 
 
317
  /*
 
318
   * Simple quit button.
 
319
   */
 
320
 
 
321
  button = gtk_button_new_with_label ("Quit");
 
322
 
 
323
  g_signal_connect (G_OBJECT (button), "clicked",
 
324
                    G_CALLBACK (gtk_main_quit), NULL);
 
325
 
 
326
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
 
327
 
 
328
  gtk_widget_show (button);
 
329
 
 
330
  /*
 
331
   * Show window.
 
332
   */
 
333
 
 
334
  gtk_widget_show (window);
 
335
 
 
336
  /*
 
337
   * Main loop.
 
338
   */
 
339
 
 
340
  /* Destroy the GLX context explicitly when application is terminated. */
 
341
  gtk_quit_add (0, (GtkFunction) destroy_gl_context, NULL);
 
342
 
 
343
  gtk_main ();
 
344
 
 
345
  return 0;
 
346
}