blob: 8475cab07aff7d3d2947dceb34dc2b0aa5e8ae1a [file] [log] [blame]
Brian Paul355da232001-03-23 22:46:26 +00001/*
2 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22/*
23 * This is a port of the infamous "gears" demo to straight GLX (i.e. no GLUT)
24 * Port by Brian Paul 23 March 2001
25 *
Brian Paulad802332003-04-09 21:47:19 +000026 * Modified by Ian Romanick <idr@us.ibm.com> 09 April 2003 to support
27 * GLX_{MESA,SGI}_swap_control and GLX_OML_sync_control.
Brian Paul355da232001-03-23 22:46:26 +000028 *
Brian Paulad802332003-04-09 21:47:19 +000029 * Command line options:
30 * -display Name of the display to use.
31 * -info print GL implementation information
32 * -swap N Attempt to set the swap interval to 1/N second
33 * -forcegetrate Get the display refresh rate even if the required GLX
34 * extension is not supported.
Brian Paul355da232001-03-23 22:46:26 +000035 */
36
37
38#include <math.h>
39#include <stdlib.h>
40#include <stdio.h>
41#include <string.h>
42#include <X11/Xlib.h>
43#include <X11/keysym.h>
Jouk Jansen370d7ad2003-04-29 07:15:48 +000044#ifndef __VMS
45# include <stdint.h>
46#endif
47# define GLX_GLXEXT_PROTOTYPES
Brian Paul355da232001-03-23 22:46:26 +000048#include <GL/gl.h>
49#include <GL/glx.h>
50
Brian Paulad802332003-04-09 21:47:19 +000051#ifndef GLX_MESA_swap_control
52typedef GLint ( * PFNGLXSWAPINTERVALMESAPROC) (unsigned interval);
53typedef GLint ( * PFNGLXGETSWAPINTERVALMESAPROC) ( void );
54#endif
55
56#if !defined( GLX_OML_sync_control ) && defined( _STDINT_H )
57#define GLX_OML_sync_control 1
58typedef Bool ( * PFNGLXGETMSCRATEOMLPROC) (Display *dpy, GLXDrawable drawable, int32_t *numerator, int32_t *denominator);
59#endif
Brian Paul355da232001-03-23 22:46:26 +000060
61#define BENCHMARK
62
63#ifdef BENCHMARK
64
65/* XXX this probably isn't very portable */
66
67#include <sys/time.h>
68#include <unistd.h>
69
Brian Paulad802332003-04-09 21:47:19 +000070#define NUL '\0'
71
Brian Paul355da232001-03-23 22:46:26 +000072/* return current time (in seconds) */
73static int
74current_time(void)
75{
76 struct timeval tv;
Jouk Jansenf72a3da2002-10-08 08:38:26 +000077#ifdef __VMS
78 (void) gettimeofday(&tv, NULL );
79#else
Brian Paul355da232001-03-23 22:46:26 +000080 struct timezone tz;
81 (void) gettimeofday(&tv, &tz);
Jouk Jansenf72a3da2002-10-08 08:38:26 +000082#endif
Brian Paul355da232001-03-23 22:46:26 +000083 return (int) tv.tv_sec;
84}
85
86#else /*BENCHMARK*/
87
88/* dummy */
89static int
90current_time(void)
91{
92 return 0;
93}
94
95#endif /*BENCHMARK*/
96
97
98
99#ifndef M_PI
100#define M_PI 3.14159265
101#endif
102
103
104static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
105static GLint gear1, gear2, gear3;
106static GLfloat angle = 0.0;
107
Brian Paulad802332003-04-09 21:47:19 +0000108static GLboolean has_OML_sync_control = GL_FALSE;
109static GLboolean has_SGI_swap_control = GL_FALSE;
110static GLboolean has_MESA_swap_control = GL_FALSE;
111
112static char ** extension_table = NULL;
113static unsigned num_extensions;
Brian Paul355da232001-03-23 22:46:26 +0000114
115/*
116 *
117 * Draw a gear wheel. You'll probably want to call this function when
118 * building a display list since we do a lot of trig here.
119 *
120 * Input: inner_radius - radius of hole at center
121 * outer_radius - radius at center of teeth
122 * width - width of gear
123 * teeth - number of teeth
124 * tooth_depth - depth of tooth
125 */
126static void
127gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
128 GLint teeth, GLfloat tooth_depth)
129{
130 GLint i;
131 GLfloat r0, r1, r2;
132 GLfloat angle, da;
133 GLfloat u, v, len;
134
135 r0 = inner_radius;
136 r1 = outer_radius - tooth_depth / 2.0;
137 r2 = outer_radius + tooth_depth / 2.0;
138
139 da = 2.0 * M_PI / teeth / 4.0;
140
141 glShadeModel(GL_FLAT);
142
143 glNormal3f(0.0, 0.0, 1.0);
144
145 /* draw front face */
146 glBegin(GL_QUAD_STRIP);
147 for (i = 0; i <= teeth; i++) {
148 angle = i * 2.0 * M_PI / teeth;
149 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
150 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
151 if (i < teeth) {
152 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
153 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
154 width * 0.5);
155 }
156 }
157 glEnd();
158
159 /* draw front sides of teeth */
160 glBegin(GL_QUADS);
161 da = 2.0 * M_PI / teeth / 4.0;
162 for (i = 0; i < teeth; i++) {
163 angle = i * 2.0 * M_PI / teeth;
164
165 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
166 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
167 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
168 width * 0.5);
169 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
170 width * 0.5);
171 }
172 glEnd();
173
174 glNormal3f(0.0, 0.0, -1.0);
175
176 /* draw back face */
177 glBegin(GL_QUAD_STRIP);
178 for (i = 0; i <= teeth; i++) {
179 angle = i * 2.0 * M_PI / teeth;
180 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
181 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
182 if (i < teeth) {
183 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
184 -width * 0.5);
185 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
186 }
187 }
188 glEnd();
189
190 /* draw back sides of teeth */
191 glBegin(GL_QUADS);
192 da = 2.0 * M_PI / teeth / 4.0;
193 for (i = 0; i < teeth; i++) {
194 angle = i * 2.0 * M_PI / teeth;
195
196 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
197 -width * 0.5);
198 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
199 -width * 0.5);
200 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
201 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
202 }
203 glEnd();
204
205 /* draw outward faces of teeth */
206 glBegin(GL_QUAD_STRIP);
207 for (i = 0; i < teeth; i++) {
208 angle = i * 2.0 * M_PI / teeth;
209
210 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
211 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
212 u = r2 * cos(angle + da) - r1 * cos(angle);
213 v = r2 * sin(angle + da) - r1 * sin(angle);
214 len = sqrt(u * u + v * v);
215 u /= len;
216 v /= len;
217 glNormal3f(v, -u, 0.0);
218 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
219 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
220 glNormal3f(cos(angle), sin(angle), 0.0);
221 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
222 width * 0.5);
223 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
224 -width * 0.5);
225 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
226 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
227 glNormal3f(v, -u, 0.0);
228 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
229 width * 0.5);
230 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
231 -width * 0.5);
232 glNormal3f(cos(angle), sin(angle), 0.0);
233 }
234
235 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
236 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
237
238 glEnd();
239
240 glShadeModel(GL_SMOOTH);
241
242 /* draw inside radius cylinder */
243 glBegin(GL_QUAD_STRIP);
244 for (i = 0; i <= teeth; i++) {
245 angle = i * 2.0 * M_PI / teeth;
246 glNormal3f(-cos(angle), -sin(angle), 0.0);
247 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
248 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
249 }
250 glEnd();
251}
252
253
254static void
255draw(void)
256{
257 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
258
259 glPushMatrix();
260 glRotatef(view_rotx, 1.0, 0.0, 0.0);
261 glRotatef(view_roty, 0.0, 1.0, 0.0);
262 glRotatef(view_rotz, 0.0, 0.0, 1.0);
263
264 glPushMatrix();
265 glTranslatef(-3.0, -2.0, 0.0);
266 glRotatef(angle, 0.0, 0.0, 1.0);
267 glCallList(gear1);
268 glPopMatrix();
269
270 glPushMatrix();
271 glTranslatef(3.1, -2.0, 0.0);
272 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
273 glCallList(gear2);
274 glPopMatrix();
275
276 glPushMatrix();
277 glTranslatef(-3.1, 4.2, 0.0);
278 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
279 glCallList(gear3);
280 glPopMatrix();
281
282 glPopMatrix();
283}
284
285
286/* new window size or exposure */
287static void
288reshape(int width, int height)
289{
290 GLfloat h = (GLfloat) height / (GLfloat) width;
291
292 glViewport(0, 0, (GLint) width, (GLint) height);
293 glMatrixMode(GL_PROJECTION);
294 glLoadIdentity();
295 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
296 glMatrixMode(GL_MODELVIEW);
297 glLoadIdentity();
298 glTranslatef(0.0, 0.0, -40.0);
299}
300
301
302static void
303init(void)
304{
305 static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
306 static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
307 static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
308 static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
309
310 glLightfv(GL_LIGHT0, GL_POSITION, pos);
311 glEnable(GL_CULL_FACE);
312 glEnable(GL_LIGHTING);
313 glEnable(GL_LIGHT0);
314 glEnable(GL_DEPTH_TEST);
315
316 /* make the gears */
317 gear1 = glGenLists(1);
318 glNewList(gear1, GL_COMPILE);
319 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
320 gear(1.0, 4.0, 1.0, 20, 0.7);
321 glEndList();
322
323 gear2 = glGenLists(1);
324 glNewList(gear2, GL_COMPILE);
325 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
326 gear(0.5, 2.0, 2.0, 10, 0.7);
327 glEndList();
328
329 gear3 = glGenLists(1);
330 glNewList(gear3, GL_COMPILE);
331 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
332 gear(1.3, 2.0, 0.5, 10, 0.7);
333 glEndList();
334
335 glEnable(GL_NORMALIZE);
336}
337
338
339/*
340 * Create an RGB, double-buffered window.
341 * Return the window and context handles.
342 */
343static void
344make_window( Display *dpy, const char *name,
345 int x, int y, int width, int height,
346 Window *winRet, GLXContext *ctxRet)
347{
348 int attrib[] = { GLX_RGBA,
349 GLX_RED_SIZE, 1,
350 GLX_GREEN_SIZE, 1,
351 GLX_BLUE_SIZE, 1,
352 GLX_DOUBLEBUFFER,
353 GLX_DEPTH_SIZE, 1,
354 None };
355 int scrnum;
356 XSetWindowAttributes attr;
357 unsigned long mask;
358 Window root;
359 Window win;
360 GLXContext ctx;
361 XVisualInfo *visinfo;
362
363 scrnum = DefaultScreen( dpy );
364 root = RootWindow( dpy, scrnum );
365
366 visinfo = glXChooseVisual( dpy, scrnum, attrib );
367 if (!visinfo) {
368 printf("Error: couldn't get an RGB, Double-buffered visual\n");
369 exit(1);
370 }
371
372 /* window attributes */
373 attr.background_pixel = 0;
374 attr.border_pixel = 0;
375 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
376 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
377 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
378
379 win = XCreateWindow( dpy, root, 0, 0, width, height,
380 0, visinfo->depth, InputOutput,
381 visinfo->visual, mask, &attr );
382
383 /* set hints and properties */
384 {
385 XSizeHints sizehints;
386 sizehints.x = x;
387 sizehints.y = y;
388 sizehints.width = width;
389 sizehints.height = height;
390 sizehints.flags = USSize | USPosition;
391 XSetNormalHints(dpy, win, &sizehints);
392 XSetStandardProperties(dpy, win, name, name,
393 None, (char **)NULL, 0, &sizehints);
394 }
395
396 ctx = glXCreateContext( dpy, visinfo, NULL, True );
397 if (!ctx) {
398 printf("Error: glXCreateContext failed\n");
399 exit(1);
400 }
401
402 XFree(visinfo);
403
404 *winRet = win;
405 *ctxRet = ctx;
406}
407
408
409static void
410event_loop(Display *dpy, Window win)
411{
412 while (1) {
413 while (XPending(dpy) > 0) {
414 XEvent event;
415 XNextEvent(dpy, &event);
416 switch (event.type) {
417 case Expose:
418 /* we'll redraw below */
419 break;
420 case ConfigureNotify:
421 reshape(event.xconfigure.width, event.xconfigure.height);
422 break;
423 case KeyPress:
424 {
425 char buffer[10];
426 int r, code;
427 code = XLookupKeysym(&event.xkey, 0);
428 if (code == XK_Left) {
429 view_roty += 5.0;
430 }
431 else if (code == XK_Right) {
432 view_roty -= 5.0;
433 }
434 else if (code == XK_Up) {
435 view_rotx += 5.0;
436 }
437 else if (code == XK_Down) {
438 view_rotx -= 5.0;
439 }
440 else {
441 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
442 NULL, NULL);
443 if (buffer[0] == 27) {
444 /* escape */
445 return;
446 }
447 }
448 }
449 }
450 }
451
452 /* next frame */
453 angle += 2.0;
454
455 draw();
456 glXSwapBuffers(dpy, win);
457
458 /* calc framerate */
459 {
460 static int t0 = -1;
461 static int frames = 0;
462 int t = current_time();
463
464 if (t0 < 0)
465 t0 = t;
466
467 frames++;
468
469 if (t - t0 >= 5.0) {
470 GLfloat seconds = t - t0;
471 GLfloat fps = frames / seconds;
472 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
473 fps);
474 t0 = t;
475 frames = 0;
476 }
477 }
478 }
479}
480
481
Brian Paulad802332003-04-09 21:47:19 +0000482/**
483 * Display the refresh rate of the display using the GLX_OML_sync_control
484 * extension.
485 */
486
487static void
488show_refresh_rate( Display * dpy )
489{
490#ifdef GLX_OML_sync_control
491 PFNGLXGETMSCRATEOMLPROC get_msc_rate;
492 int32_t n;
493 int32_t d;
494
495 get_msc_rate = (PFNGLXGETMSCRATEOMLPROC) glXGetProcAddressARB( (const GLubyte *) "glXGetMscRateOML" );
496 if ( get_msc_rate != NULL ) {
497 (*get_msc_rate)( dpy, glXGetCurrentDrawable(), &n, &d );
498 printf( "refresh rate: %.1fHz\n", (float) n / d );
499 return;
500 }
501#endif
502 printf( "glXGetMscRateOML not supported.\n" );
503}
504
505
506/**
507 * Fill in the table of extension strings from a supplied extensions string
508 * (as returned by glXQueryExtensionsString).
509 *
510 * \param string String of GLX extensions.
511 * \sa is_extension_supported
512 */
513
514static void
515make_extension_table( const char * string )
516{
517 char ** string_tab;
518 unsigned num_strings;
519 unsigned base;
520 unsigned idx;
521 unsigned i;
522
523 /* Count the number of spaces in the string. That gives a base-line
524 * figure for the number of extension in the string.
525 */
526
527 num_strings = 1;
528 for ( i = 0 ; string[i] != NUL ; i++ ) {
529 if ( string[i] == ' ' ) {
530 num_strings++;
531 }
532 }
533
534 string_tab = (char **) malloc( sizeof( char * ) * num_strings );
535 if ( string_tab == NULL ) {
536 return;
537 }
538
539 base = 0;
540 idx = 0;
541
542 while ( string[ base ] != NUL ) {
543 /* Determine the length of the next extension string.
544 */
545
546 for ( i = 0
547 ; (string[ base + i ] != NUL) && (string[ base + i ] != ' ')
548 ; i++ ) {
549 /* empty */ ;
550 }
551
552 if ( i > 0 ) {
553 /* If the string was non-zero length, add it to the table. We
554 * can get zero length strings if there is a space at the end of
555 * the string or if there are two (or more) spaces next to each
556 * other in the string.
557 */
558
559 string_tab[ idx ] = malloc( sizeof( char ) * (i + 1) );
560 if ( string_tab[ idx ] == NULL ) {
561 return;
562 }
563
564 (void) memcpy( string_tab[ idx ], & string[ base ], i );
565 string_tab[ idx ][i] = NUL;
566 idx++;
567 }
568
569
570 /* Skip to the start of the next extension string.
571 */
572
573 for ( base += i
574 ; (string[ base ] == ' ') && (string[ base ] != NUL)
575 ; base++ ) {
576 /* empty */ ;
577 }
578 }
579
580 extension_table = string_tab;
581 num_extensions = idx;
582}
583
584
585/**
586 * Determine of an extension is supported. The extension string table
587 * must have already be initialized by calling \c make_extension_table.
588 *
589 * \praram ext Extension to be tested.
590 * \return GL_TRUE of the extension is supported, GL_FALSE otherwise.
591 * \sa make_extension_table
592 */
593
594static GLboolean
595is_extension_supported( const char * ext )
596{
597 unsigned i;
598
599 for ( i = 0 ; i < num_extensions ; i++ ) {
600 if ( strcmp( ext, extension_table[i] ) == 0 ) {
601 return GL_TRUE;
602 }
603 }
604
605 return GL_FALSE;
606}
607
608
Brian Paul355da232001-03-23 22:46:26 +0000609int
610main(int argc, char *argv[])
611{
612 Display *dpy;
613 Window win;
614 GLXContext ctx;
615 char *dpyName = ":0";
Brian Paulad802332003-04-09 21:47:19 +0000616 int swap_interval = 1;
617 GLboolean do_swap_interval = GL_FALSE;
618 GLboolean force_get_rate = GL_FALSE;
Brian Paul355da232001-03-23 22:46:26 +0000619 GLboolean printInfo = GL_FALSE;
620 int i;
Brian Paulad802332003-04-09 21:47:19 +0000621 PFNGLXSWAPINTERVALMESAPROC set_swap_interval = NULL;
622 PFNGLXGETSWAPINTERVALMESAPROC get_swap_interval = NULL;
623
Brian Paul355da232001-03-23 22:46:26 +0000624
625 for (i = 1; i < argc; i++) {
Brian Paulad802332003-04-09 21:47:19 +0000626 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
Brian Paul355da232001-03-23 22:46:26 +0000627 dpyName = argv[i+1];
628 i++;
629 }
630 else if (strcmp(argv[i], "-info") == 0) {
631 printInfo = GL_TRUE;
632 }
Brian Paulad802332003-04-09 21:47:19 +0000633 else if (strcmp(argv[i], "-swap") == 0 && i + 1 < argc) {
634 swap_interval = atoi( argv[i+1] );
635 do_swap_interval = GL_TRUE;
636 i++;
637 }
638 else if (strcmp(argv[i], "-forcegetrate") == 0) {
639 /* This option was put in because some DRI drivers don't support the
640 * full GLX_OML_sync_control extension, but they do support
641 * glXGetMscRateOML.
642 */
643 force_get_rate = GL_TRUE;
644 }
645 else if (strcmp(argv[i], "-help") == 0) {
646 printf("Usage:\n");
647 printf(" gears [options]\n");
648 printf("Options:\n");
649 printf(" -help Print this information\n");
650 printf(" -display displayName Specify X display\n");
651 printf(" -info Display GL information\n");
652 printf(" -swap N Swap no more than once per N vertical refreshes\n");
Brian Paulebab6312003-04-09 22:50:52 +0000653 printf(" -forcegetrate Try to use glXGetMscRateOML function\n");
Brian Paulad802332003-04-09 21:47:19 +0000654 return 0;
655 }
Brian Paul355da232001-03-23 22:46:26 +0000656 }
657
658 dpy = XOpenDisplay(dpyName);
659 if (!dpy) {
660 printf("Error: couldn't open display %s\n", dpyName);
661 return -1;
662 }
663
664 make_window(dpy, "glxgears", 0, 0, 300, 300, &win, &ctx);
665 XMapWindow(dpy, win);
666 glXMakeCurrent(dpy, win, ctx);
667
Brian Paulad802332003-04-09 21:47:19 +0000668 make_extension_table( (char *) glXQueryExtensionsString(dpy,DefaultScreen(dpy)) );
669 has_OML_sync_control = is_extension_supported( "GLX_OML_sync_control" );
670 has_SGI_swap_control = is_extension_supported( "GLX_SGI_swap_control" );
671 has_MESA_swap_control = is_extension_supported( "GLX_MESA_swap_control" );
672
673 if ( has_MESA_swap_control ) {
674 set_swap_interval = (PFNGLXSWAPINTERVALMESAPROC) glXGetProcAddressARB( (const GLubyte *) "glXSwapIntervalMESA" );
675 get_swap_interval = (PFNGLXGETSWAPINTERVALMESAPROC) glXGetProcAddressARB( (const GLubyte *) "glXGetSwapIntervalMESA" );
676 }
677 else if ( has_SGI_swap_control ) {
678 set_swap_interval = (PFNGLXSWAPINTERVALMESAPROC) glXGetProcAddressARB( (const GLubyte *) "glXSwapIntervalSGI" );
679 }
680
681
Brian Paul355da232001-03-23 22:46:26 +0000682 if (printInfo) {
683 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
684 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
685 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
686 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
Brian Paulad802332003-04-09 21:47:19 +0000687 if ( has_OML_sync_control || force_get_rate ) {
688 show_refresh_rate( dpy );
689 }
690
691 if ( get_swap_interval != NULL ) {
692 printf("Default swap interval = %d\n", (*get_swap_interval)() );
693 }
694 }
695
696 if ( do_swap_interval ) {
697 if ( set_swap_interval != NULL ) {
698 if ( ((swap_interval == 0) && !has_MESA_swap_control)
699 || (swap_interval < 0) ) {
700 printf( "Swap interval must be non-negative or greater than zero "
701 "if GLX_MESA_swap_control is not supported.\n" );
702 }
703 else {
704 (*set_swap_interval)( swap_interval );
705 }
706
707 if ( printInfo && (get_swap_interval != NULL) ) {
708 printf("Current swap interval = %d\n", (*get_swap_interval)() );
709 }
710 }
711 else {
712 printf("Unable to set swap-interval. Neither GLX_SGI_swap_control "
713 "nor GLX_MESA_swap_control are supported.\n" );
714 }
Brian Paul355da232001-03-23 22:46:26 +0000715 }
716
717 init();
718
719 event_loop(dpy, win);
720
721 glXDestroyContext(dpy, ctx);
722 XDestroyWindow(dpy, win);
723 XCloseDisplay(dpy);
724
725 return 0;
726}