Merge branch '7.8'
[mesa.git] / progs / xdemos / glxgears.c
1 /*
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 *
26 * See usage() below for command line options.
27 */
28
29
30 #include <math.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <X11/Xlib.h>
35 #include <X11/keysym.h>
36 #include <GL/gl.h>
37 #include <GL/glx.h>
38 #include <GL/glxext.h>
39
40 #ifndef GLX_MESA_swap_control
41 #define GLX_MESA_swap_control 1
42 typedef int (*PFNGLXGETSWAPINTERVALMESAPROC)(void);
43 #endif
44
45
46 #define BENCHMARK
47
48 #ifdef BENCHMARK
49
50 /* XXX this probably isn't very portable */
51
52 #include <sys/time.h>
53 #include <unistd.h>
54
55 /* return current time (in seconds) */
56 static double
57 current_time(void)
58 {
59 struct timeval tv;
60 #ifdef __VMS
61 (void) gettimeofday(&tv, NULL );
62 #else
63 struct timezone tz;
64 (void) gettimeofday(&tv, &tz);
65 #endif
66 return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
67 }
68
69 #else /*BENCHMARK*/
70
71 /* dummy */
72 static double
73 current_time(void)
74 {
75 /* update this function for other platforms! */
76 static double t = 0.0;
77 static int warn = 1;
78 if (warn) {
79 fprintf(stderr, "Warning: current_time() not implemented!!\n");
80 warn = 0;
81 }
82 return t += 1.0;
83 }
84
85 #endif /*BENCHMARK*/
86
87
88
89 #ifndef M_PI
90 #define M_PI 3.14159265
91 #endif
92
93
94 /** Event handler results: */
95 #define NOP 0
96 #define EXIT 1
97 #define DRAW 2
98
99 static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
100 static GLint gear1, gear2, gear3;
101 static GLfloat angle = 0.0;
102
103 static GLboolean fullscreen = GL_FALSE; /* Create a single fullscreen window */
104 static GLboolean stereo = GL_FALSE; /* Enable stereo. */
105 static GLboolean animate = GL_TRUE; /* Animation */
106 static GLfloat eyesep = 5.0; /* Eye separation. */
107 static GLfloat fix_point = 40.0; /* Fixation point distance. */
108 static GLfloat left, right, asp; /* Stereo frustum params. */
109
110
111 /*
112 *
113 * Draw a gear wheel. You'll probably want to call this function when
114 * building a display list since we do a lot of trig here.
115 *
116 * Input: inner_radius - radius of hole at center
117 * outer_radius - radius at center of teeth
118 * width - width of gear
119 * teeth - number of teeth
120 * tooth_depth - depth of tooth
121 */
122 static void
123 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
124 GLint teeth, GLfloat tooth_depth)
125 {
126 GLint i;
127 GLfloat r0, r1, r2;
128 GLfloat angle, da;
129 GLfloat u, v, len;
130
131 r0 = inner_radius;
132 r1 = outer_radius - tooth_depth / 2.0;
133 r2 = outer_radius + tooth_depth / 2.0;
134
135 da = 2.0 * M_PI / teeth / 4.0;
136
137 glShadeModel(GL_FLAT);
138
139 glNormal3f(0.0, 0.0, 1.0);
140
141 /* draw front face */
142 glBegin(GL_QUAD_STRIP);
143 for (i = 0; i <= teeth; i++) {
144 angle = i * 2.0 * M_PI / teeth;
145 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
146 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
147 if (i < teeth) {
148 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
149 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
150 width * 0.5);
151 }
152 }
153 glEnd();
154
155 /* draw front sides of teeth */
156 glBegin(GL_QUADS);
157 da = 2.0 * M_PI / teeth / 4.0;
158 for (i = 0; i < teeth; i++) {
159 angle = i * 2.0 * M_PI / teeth;
160
161 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
162 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
163 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
164 width * 0.5);
165 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
166 width * 0.5);
167 }
168 glEnd();
169
170 glNormal3f(0.0, 0.0, -1.0);
171
172 /* draw back face */
173 glBegin(GL_QUAD_STRIP);
174 for (i = 0; i <= teeth; i++) {
175 angle = i * 2.0 * M_PI / teeth;
176 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
177 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
178 if (i < teeth) {
179 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
180 -width * 0.5);
181 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
182 }
183 }
184 glEnd();
185
186 /* draw back sides of teeth */
187 glBegin(GL_QUADS);
188 da = 2.0 * M_PI / teeth / 4.0;
189 for (i = 0; i < teeth; i++) {
190 angle = i * 2.0 * M_PI / teeth;
191
192 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
193 -width * 0.5);
194 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
195 -width * 0.5);
196 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
197 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
198 }
199 glEnd();
200
201 /* draw outward faces of teeth */
202 glBegin(GL_QUAD_STRIP);
203 for (i = 0; i < teeth; i++) {
204 angle = i * 2.0 * M_PI / teeth;
205
206 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
207 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
208 u = r2 * cos(angle + da) - r1 * cos(angle);
209 v = r2 * sin(angle + da) - r1 * sin(angle);
210 len = sqrt(u * u + v * v);
211 u /= len;
212 v /= len;
213 glNormal3f(v, -u, 0.0);
214 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
215 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
216 glNormal3f(cos(angle), sin(angle), 0.0);
217 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
218 width * 0.5);
219 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
220 -width * 0.5);
221 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
222 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
223 glNormal3f(v, -u, 0.0);
224 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
225 width * 0.5);
226 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
227 -width * 0.5);
228 glNormal3f(cos(angle), sin(angle), 0.0);
229 }
230
231 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
232 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
233
234 glEnd();
235
236 glShadeModel(GL_SMOOTH);
237
238 /* draw inside radius cylinder */
239 glBegin(GL_QUAD_STRIP);
240 for (i = 0; i <= teeth; i++) {
241 angle = i * 2.0 * M_PI / teeth;
242 glNormal3f(-cos(angle), -sin(angle), 0.0);
243 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
244 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
245 }
246 glEnd();
247 }
248
249
250 static void
251 draw(void)
252 {
253 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
254
255 glPushMatrix();
256 glRotatef(view_rotx, 1.0, 0.0, 0.0);
257 glRotatef(view_roty, 0.0, 1.0, 0.0);
258 glRotatef(view_rotz, 0.0, 0.0, 1.0);
259
260 glPushMatrix();
261 glTranslatef(-3.0, -2.0, 0.0);
262 glRotatef(angle, 0.0, 0.0, 1.0);
263 glCallList(gear1);
264 glPopMatrix();
265
266 glPushMatrix();
267 glTranslatef(3.1, -2.0, 0.0);
268 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
269 glCallList(gear2);
270 glPopMatrix();
271
272 glPushMatrix();
273 glTranslatef(-3.1, 4.2, 0.0);
274 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
275 glCallList(gear3);
276 glPopMatrix();
277
278 glPopMatrix();
279 }
280
281
282 static void
283 draw_gears(void)
284 {
285 if (stereo) {
286 /* First left eye. */
287 glDrawBuffer(GL_BACK_LEFT);
288
289 glMatrixMode(GL_PROJECTION);
290 glLoadIdentity();
291 glFrustum(left, right, -asp, asp, 5.0, 60.0);
292
293 glMatrixMode(GL_MODELVIEW);
294
295 glPushMatrix();
296 glTranslated(+0.5 * eyesep, 0.0, 0.0);
297 draw();
298 glPopMatrix();
299
300 /* Then right eye. */
301 glDrawBuffer(GL_BACK_RIGHT);
302
303 glMatrixMode(GL_PROJECTION);
304 glLoadIdentity();
305 glFrustum(-right, -left, -asp, asp, 5.0, 60.0);
306
307 glMatrixMode(GL_MODELVIEW);
308
309 glPushMatrix();
310 glTranslated(-0.5 * eyesep, 0.0, 0.0);
311 draw();
312 glPopMatrix();
313 }
314 else {
315 draw();
316 }
317 }
318
319
320 /** Draw single frame, do SwapBuffers, compute FPS */
321 static void
322 draw_frame(Display *dpy, Window win)
323 {
324 static int frames = 0;
325 static double tRot0 = -1.0, tRate0 = -1.0;
326 double dt, t = current_time();
327
328 if (tRot0 < 0.0)
329 tRot0 = t;
330 dt = t - tRot0;
331 tRot0 = t;
332
333 if (animate) {
334 /* advance rotation for next frame */
335 angle += 70.0 * dt; /* 70 degrees per second */
336 if (angle > 3600.0)
337 angle -= 3600.0;
338 }
339
340 draw_gears();
341 glXSwapBuffers(dpy, win);
342
343 frames++;
344
345 if (tRate0 < 0.0)
346 tRate0 = t;
347 if (t - tRate0 >= 5.0) {
348 GLfloat seconds = t - tRate0;
349 GLfloat fps = frames / seconds;
350 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
351 fps);
352 tRate0 = t;
353 frames = 0;
354 }
355 }
356
357
358 /* new window size or exposure */
359 static void
360 reshape(int width, int height)
361 {
362 glViewport(0, 0, (GLint) width, (GLint) height);
363
364 if (stereo) {
365 GLfloat w;
366
367 asp = (GLfloat) height / (GLfloat) width;
368 w = fix_point * (1.0 / 5.0);
369
370 left = -5.0 * ((w - 0.5 * eyesep) / fix_point);
371 right = 5.0 * ((w + 0.5 * eyesep) / fix_point);
372 }
373 else {
374 GLfloat h = (GLfloat) height / (GLfloat) width;
375
376 glMatrixMode(GL_PROJECTION);
377 glLoadIdentity();
378 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
379 }
380
381 glMatrixMode(GL_MODELVIEW);
382 glLoadIdentity();
383 glTranslatef(0.0, 0.0, -40.0);
384 }
385
386
387
388 static void
389 init(void)
390 {
391 static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
392 static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
393 static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
394 static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
395
396 glLightfv(GL_LIGHT0, GL_POSITION, pos);
397 glEnable(GL_CULL_FACE);
398 glEnable(GL_LIGHTING);
399 glEnable(GL_LIGHT0);
400 glEnable(GL_DEPTH_TEST);
401
402 /* make the gears */
403 gear1 = glGenLists(1);
404 glNewList(gear1, GL_COMPILE);
405 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
406 gear(1.0, 4.0, 1.0, 20, 0.7);
407 glEndList();
408
409 gear2 = glGenLists(1);
410 glNewList(gear2, GL_COMPILE);
411 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
412 gear(0.5, 2.0, 2.0, 10, 0.7);
413 glEndList();
414
415 gear3 = glGenLists(1);
416 glNewList(gear3, GL_COMPILE);
417 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
418 gear(1.3, 2.0, 0.5, 10, 0.7);
419 glEndList();
420
421 glEnable(GL_NORMALIZE);
422 }
423
424
425 /**
426 * Remove window border/decorations.
427 */
428 static void
429 no_border( Display *dpy, Window w)
430 {
431 static const unsigned MWM_HINTS_DECORATIONS = (1 << 1);
432 static const int PROP_MOTIF_WM_HINTS_ELEMENTS = 5;
433
434 typedef struct
435 {
436 unsigned long flags;
437 unsigned long functions;
438 unsigned long decorations;
439 long inputMode;
440 unsigned long status;
441 } PropMotifWmHints;
442
443 PropMotifWmHints motif_hints;
444 Atom prop, proptype;
445 unsigned long flags = 0;
446
447 /* setup the property */
448 motif_hints.flags = MWM_HINTS_DECORATIONS;
449 motif_hints.decorations = flags;
450
451 /* get the atom for the property */
452 prop = XInternAtom( dpy, "_MOTIF_WM_HINTS", True );
453 if (!prop) {
454 /* something went wrong! */
455 return;
456 }
457
458 /* not sure this is correct, seems to work, XA_WM_HINTS didn't work */
459 proptype = prop;
460
461 XChangeProperty( dpy, w, /* display, window */
462 prop, proptype, /* property, type */
463 32, /* format: 32-bit datums */
464 PropModeReplace, /* mode */
465 (unsigned char *) &motif_hints, /* data */
466 PROP_MOTIF_WM_HINTS_ELEMENTS /* nelements */
467 );
468 }
469
470
471 /*
472 * Create an RGB, double-buffered window.
473 * Return the window and context handles.
474 */
475 static void
476 make_window( Display *dpy, const char *name,
477 int x, int y, int width, int height,
478 Window *winRet, GLXContext *ctxRet)
479 {
480 int attribs[] = { GLX_RGBA,
481 GLX_RED_SIZE, 1,
482 GLX_GREEN_SIZE, 1,
483 GLX_BLUE_SIZE, 1,
484 GLX_DOUBLEBUFFER,
485 GLX_DEPTH_SIZE, 1,
486 None };
487 int stereoAttribs[] = { GLX_RGBA,
488 GLX_RED_SIZE, 1,
489 GLX_GREEN_SIZE, 1,
490 GLX_BLUE_SIZE, 1,
491 GLX_DOUBLEBUFFER,
492 GLX_DEPTH_SIZE, 1,
493 GLX_STEREO,
494 None };
495 int scrnum;
496 XSetWindowAttributes attr;
497 unsigned long mask;
498 Window root;
499 Window win;
500 GLXContext ctx;
501 XVisualInfo *visinfo;
502
503 scrnum = DefaultScreen( dpy );
504 root = RootWindow( dpy, scrnum );
505
506 if (fullscreen) {
507 x = 0; y = 0;
508 width = DisplayWidth( dpy, scrnum );
509 height = DisplayHeight( dpy, scrnum );
510 }
511
512 if (stereo)
513 visinfo = glXChooseVisual( dpy, scrnum, stereoAttribs );
514 else
515 visinfo = glXChooseVisual( dpy, scrnum, attribs );
516 if (!visinfo) {
517 if (stereo) {
518 printf("Error: couldn't get an RGB, "
519 "Double-buffered, Stereo visual\n");
520 } else
521 printf("Error: couldn't get an RGB, Double-buffered visual\n");
522 exit(1);
523 }
524
525 /* window attributes */
526 attr.background_pixel = 0;
527 attr.border_pixel = 0;
528 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
529 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
530 /* XXX this is a bad way to get a borderless window! */
531 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
532
533 win = XCreateWindow( dpy, root, x, y, width, height,
534 0, visinfo->depth, InputOutput,
535 visinfo->visual, mask, &attr );
536
537 if (fullscreen)
538 no_border(dpy, win);
539
540 /* set hints and properties */
541 {
542 XSizeHints sizehints;
543 sizehints.x = x;
544 sizehints.y = y;
545 sizehints.width = width;
546 sizehints.height = height;
547 sizehints.flags = USSize | USPosition;
548 XSetNormalHints(dpy, win, &sizehints);
549 XSetStandardProperties(dpy, win, name, name,
550 None, (char **)NULL, 0, &sizehints);
551 }
552
553 ctx = glXCreateContext( dpy, visinfo, NULL, True );
554 if (!ctx) {
555 printf("Error: glXCreateContext failed\n");
556 exit(1);
557 }
558
559 XFree(visinfo);
560
561 *winRet = win;
562 *ctxRet = ctx;
563 }
564
565
566 /**
567 * Determine whether or not a GLX extension is supported.
568 */
569 static int
570 is_glx_extension_supported(Display *dpy, const char *query)
571 {
572 const int scrnum = DefaultScreen(dpy);
573 const char *glx_extensions = NULL;
574 const size_t len = strlen(query);
575 const char *ptr;
576
577 if (glx_extensions == NULL) {
578 glx_extensions = glXQueryExtensionsString(dpy, scrnum);
579 }
580
581 ptr = strstr(glx_extensions, query);
582 return ((ptr != NULL) && ((ptr[len] == ' ') || (ptr[len] == '\0')));
583 }
584
585
586 /**
587 * Attempt to determine whether or not the display is synched to vblank.
588 */
589 static void
590 query_vsync(Display *dpy, GLXDrawable drawable)
591 {
592 int interval = 0;
593
594 #if defined(GLX_EXT_swap_control)
595 if (is_glx_extension_supported(dpy, "GLX_EXT_swap_control")) {
596 unsigned int tmp = -1;
597 glXQueryDrawable(dpy, drawable, GLX_SWAP_INTERVAL_EXT, &tmp);
598 interval = tmp;
599 } else
600 #endif
601 if (is_glx_extension_supported(dpy, "GLX_MESA_swap_control")) {
602 PFNGLXGETSWAPINTERVALMESAPROC pglXGetSwapIntervalMESA =
603 (PFNGLXGETSWAPINTERVALMESAPROC)
604 glXGetProcAddressARB((const GLubyte *) "glXGetSwapIntervalMESA");
605
606 interval = (*pglXGetSwapIntervalMESA)();
607 } else if (is_glx_extension_supported(dpy, "GLX_SGI_swap_control")) {
608 /* The default swap interval with this extension is 1. Assume that it
609 * is set to the default.
610 *
611 * Many Mesa-based drivers default to 0, but all of these drivers also
612 * export GLX_MESA_swap_control. In that case, this branch will never
613 * be taken, and the correct result should be reported.
614 */
615 interval = 1;
616 }
617
618
619 if (interval > 0) {
620 printf("Running synchronized to the vertical refresh. The framerate should be\n");
621 if (interval == 1) {
622 printf("approximately the same as the monitor refresh rate.\n");
623 } else if (interval > 1) {
624 printf("approximately 1/%d the monitor refresh rate.\n",
625 interval);
626 }
627 }
628 }
629
630 /**
631 * Handle one X event.
632 * \return NOP, EXIT or DRAW
633 */
634 static int
635 handle_event(Display *dpy, Window win, XEvent *event)
636 {
637 (void) dpy;
638 (void) win;
639
640 switch (event->type) {
641 case Expose:
642 return DRAW;
643 case ConfigureNotify:
644 reshape(event->xconfigure.width, event->xconfigure.height);
645 break;
646 case KeyPress:
647 {
648 char buffer[10];
649 int r, code;
650 code = XLookupKeysym(&event->xkey, 0);
651 if (code == XK_Left) {
652 view_roty += 5.0;
653 }
654 else if (code == XK_Right) {
655 view_roty -= 5.0;
656 }
657 else if (code == XK_Up) {
658 view_rotx += 5.0;
659 }
660 else if (code == XK_Down) {
661 view_rotx -= 5.0;
662 }
663 else {
664 r = XLookupString(&event->xkey, buffer, sizeof(buffer),
665 NULL, NULL);
666 if (buffer[0] == 27) {
667 /* escape */
668 return EXIT;
669 }
670 else if (buffer[0] == 'a' || buffer[0] == 'A') {
671 animate = !animate;
672 }
673 }
674 return DRAW;
675 }
676 }
677 return NOP;
678 }
679
680
681 static void
682 event_loop(Display *dpy, Window win)
683 {
684 while (1) {
685 int op;
686 while (!animate || XPending(dpy) > 0) {
687 XEvent event;
688 XNextEvent(dpy, &event);
689 op = handle_event(dpy, win, &event);
690 if (op == EXIT)
691 return;
692 else if (op == DRAW)
693 break;
694 }
695
696 draw_frame(dpy, win);
697 }
698 }
699
700
701 static void
702 usage(void)
703 {
704 printf("Usage:\n");
705 printf(" -display <displayname> set the display to run on\n");
706 printf(" -stereo run in stereo mode\n");
707 printf(" -fullscreen run in fullscreen mode\n");
708 printf(" -info display OpenGL renderer info\n");
709 printf(" -geometry WxH+X+Y window geometry\n");
710 }
711
712
713 int
714 main(int argc, char *argv[])
715 {
716 unsigned int winWidth = 300, winHeight = 300;
717 int x = 0, y = 0;
718 Display *dpy;
719 Window win;
720 GLXContext ctx;
721 char *dpyName = NULL;
722 GLboolean printInfo = GL_FALSE;
723 int i;
724
725 for (i = 1; i < argc; i++) {
726 if (strcmp(argv[i], "-display") == 0) {
727 dpyName = argv[i+1];
728 i++;
729 }
730 else if (strcmp(argv[i], "-info") == 0) {
731 printInfo = GL_TRUE;
732 }
733 else if (strcmp(argv[i], "-stereo") == 0) {
734 stereo = GL_TRUE;
735 }
736 else if (strcmp(argv[i], "-fullscreen") == 0) {
737 fullscreen = GL_TRUE;
738 }
739 else if (i < argc-1 && strcmp(argv[i], "-geometry") == 0) {
740 XParseGeometry(argv[i+1], &x, &y, &winWidth, &winHeight);
741 i++;
742 }
743 else {
744 usage();
745 return -1;
746 }
747 }
748
749 dpy = XOpenDisplay(dpyName);
750 if (!dpy) {
751 printf("Error: couldn't open display %s\n",
752 dpyName ? dpyName : getenv("DISPLAY"));
753 return -1;
754 }
755
756 make_window(dpy, "glxgears", x, y, winWidth, winHeight, &win, &ctx);
757 XMapWindow(dpy, win);
758 glXMakeCurrent(dpy, win, ctx);
759 query_vsync(dpy, win);
760
761 if (printInfo) {
762 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
763 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
764 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
765 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
766 }
767
768 init();
769
770 /* Set initial projection/viewing transformation.
771 * We can't be sure we'll get a ConfigureNotify event when the window
772 * first appears.
773 */
774 reshape(winWidth, winHeight);
775
776 event_loop(dpy, win);
777
778 glDeleteLists(gear1, 1);
779 glDeleteLists(gear2, 1);
780 glDeleteLists(gear3, 1);
781 glXMakeCurrent(dpy, None, NULL);
782 glXDestroyContext(dpy, ctx);
783 XDestroyWindow(dpy, win);
784 XCloseDisplay(dpy);
785
786 return 0;
787 }