Merge branch 'mesa_7_5_branch'
[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 * Command line options:
27 * -info print GL implementation information
28 * -stereo use stereo enabled GLX visual
29 *
30 */
31
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <X11/Xlib.h>
38 #include <X11/keysym.h>
39 #include <GL/gl.h>
40 #include <GL/glx.h>
41
42 #ifndef GLX_MESA_swap_control
43 #define GLX_MESA_swap_control 1
44 typedef int (*PFNGLXGETSWAPINTERVALMESAPROC)(void);
45 #endif
46
47
48 static int is_glx_extension_supported(Display *dpy, const char *query);
49
50 static void query_vsync(Display *dpy);
51
52 #define BENCHMARK
53
54 #ifdef BENCHMARK
55
56 /* XXX this probably isn't very portable */
57
58 #include <sys/time.h>
59 #include <unistd.h>
60
61 /* return current time (in seconds) */
62 static double
63 current_time(void)
64 {
65 struct timeval tv;
66 #ifdef __VMS
67 (void) gettimeofday(&tv, NULL );
68 #else
69 struct timezone tz;
70 (void) gettimeofday(&tv, &tz);
71 #endif
72 return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
73 }
74
75 #else /*BENCHMARK*/
76
77 /* dummy */
78 static double
79 current_time(void)
80 {
81 /* update this function for other platforms! */
82 static double t = 0.0;
83 static int warn = 1;
84 if (warn) {
85 fprintf(stderr, "Warning: current_time() not implemented!!\n");
86 warn = 0;
87 }
88 return t += 1.0;
89 }
90
91 #endif /*BENCHMARK*/
92
93
94
95 #ifndef M_PI
96 #define M_PI 3.14159265
97 #endif
98
99
100 /** Event handler results: */
101 #define NOP 0
102 #define EXIT 1
103 #define DRAW 2
104
105 static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
106 static GLint gear1, gear2, gear3;
107 static GLfloat angle = 0.0;
108
109 static GLboolean fullscreen = GL_FALSE; /* Create a single fullscreen window */
110 static GLboolean stereo = GL_FALSE; /* Enable stereo. */
111 static GLboolean animate = GL_TRUE; /* Animation */
112 static GLfloat eyesep = 5.0; /* Eye separation. */
113 static GLfloat fix_point = 40.0; /* Fixation point distance. */
114 static GLfloat left, right, asp; /* Stereo frustum params. */
115
116
117 /*
118 *
119 * Draw a gear wheel. You'll probably want to call this function when
120 * building a display list since we do a lot of trig here.
121 *
122 * Input: inner_radius - radius of hole at center
123 * outer_radius - radius at center of teeth
124 * width - width of gear
125 * teeth - number of teeth
126 * tooth_depth - depth of tooth
127 */
128 static void
129 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
130 GLint teeth, GLfloat tooth_depth)
131 {
132 GLint i;
133 GLfloat r0, r1, r2;
134 GLfloat angle, da;
135 GLfloat u, v, len;
136
137 r0 = inner_radius;
138 r1 = outer_radius - tooth_depth / 2.0;
139 r2 = outer_radius + tooth_depth / 2.0;
140
141 da = 2.0 * M_PI / teeth / 4.0;
142
143 glShadeModel(GL_FLAT);
144
145 glNormal3f(0.0, 0.0, 1.0);
146
147 /* draw front face */
148 glBegin(GL_QUAD_STRIP);
149 for (i = 0; i <= teeth; i++) {
150 angle = i * 2.0 * M_PI / teeth;
151 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
152 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
153 if (i < teeth) {
154 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
155 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
156 width * 0.5);
157 }
158 }
159 glEnd();
160
161 /* draw front sides of teeth */
162 glBegin(GL_QUADS);
163 da = 2.0 * M_PI / teeth / 4.0;
164 for (i = 0; i < teeth; i++) {
165 angle = i * 2.0 * M_PI / teeth;
166
167 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
168 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
169 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
170 width * 0.5);
171 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
172 width * 0.5);
173 }
174 glEnd();
175
176 glNormal3f(0.0, 0.0, -1.0);
177
178 /* draw back face */
179 glBegin(GL_QUAD_STRIP);
180 for (i = 0; i <= teeth; i++) {
181 angle = i * 2.0 * M_PI / teeth;
182 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
183 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
184 if (i < teeth) {
185 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
186 -width * 0.5);
187 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
188 }
189 }
190 glEnd();
191
192 /* draw back sides of teeth */
193 glBegin(GL_QUADS);
194 da = 2.0 * M_PI / teeth / 4.0;
195 for (i = 0; i < teeth; i++) {
196 angle = i * 2.0 * M_PI / teeth;
197
198 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
199 -width * 0.5);
200 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
201 -width * 0.5);
202 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
203 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
204 }
205 glEnd();
206
207 /* draw outward faces of teeth */
208 glBegin(GL_QUAD_STRIP);
209 for (i = 0; i < teeth; i++) {
210 angle = i * 2.0 * M_PI / teeth;
211
212 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
213 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
214 u = r2 * cos(angle + da) - r1 * cos(angle);
215 v = r2 * sin(angle + da) - r1 * sin(angle);
216 len = sqrt(u * u + v * v);
217 u /= len;
218 v /= len;
219 glNormal3f(v, -u, 0.0);
220 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
221 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
222 glNormal3f(cos(angle), sin(angle), 0.0);
223 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
224 width * 0.5);
225 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
226 -width * 0.5);
227 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
228 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
229 glNormal3f(v, -u, 0.0);
230 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
231 width * 0.5);
232 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
233 -width * 0.5);
234 glNormal3f(cos(angle), sin(angle), 0.0);
235 }
236
237 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
238 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
239
240 glEnd();
241
242 glShadeModel(GL_SMOOTH);
243
244 /* draw inside radius cylinder */
245 glBegin(GL_QUAD_STRIP);
246 for (i = 0; i <= teeth; i++) {
247 angle = i * 2.0 * M_PI / teeth;
248 glNormal3f(-cos(angle), -sin(angle), 0.0);
249 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
250 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
251 }
252 glEnd();
253 }
254
255
256 static void
257 draw(void)
258 {
259 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
260
261 glPushMatrix();
262 glRotatef(view_rotx, 1.0, 0.0, 0.0);
263 glRotatef(view_roty, 0.0, 1.0, 0.0);
264 glRotatef(view_rotz, 0.0, 0.0, 1.0);
265
266 glPushMatrix();
267 glTranslatef(-3.0, -2.0, 0.0);
268 glRotatef(angle, 0.0, 0.0, 1.0);
269 glCallList(gear1);
270 glPopMatrix();
271
272 glPushMatrix();
273 glTranslatef(3.1, -2.0, 0.0);
274 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
275 glCallList(gear2);
276 glPopMatrix();
277
278 glPushMatrix();
279 glTranslatef(-3.1, 4.2, 0.0);
280 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
281 glCallList(gear3);
282 glPopMatrix();
283
284 glPopMatrix();
285 }
286
287
288 static void
289 draw_gears(void)
290 {
291 if (stereo) {
292 /* First left eye. */
293 glDrawBuffer(GL_BACK_LEFT);
294
295 glMatrixMode(GL_PROJECTION);
296 glLoadIdentity();
297 glFrustum(left, right, -asp, asp, 5.0, 60.0);
298
299 glMatrixMode(GL_MODELVIEW);
300
301 glPushMatrix();
302 glTranslated(+0.5 * eyesep, 0.0, 0.0);
303 draw();
304 glPopMatrix();
305
306 /* Then right eye. */
307 glDrawBuffer(GL_BACK_RIGHT);
308
309 glMatrixMode(GL_PROJECTION);
310 glLoadIdentity();
311 glFrustum(-right, -left, -asp, asp, 5.0, 60.0);
312
313 glMatrixMode(GL_MODELVIEW);
314
315 glPushMatrix();
316 glTranslated(-0.5 * eyesep, 0.0, 0.0);
317 draw();
318 glPopMatrix();
319 }
320 else {
321 draw();
322 }
323 }
324
325
326 /** Draw single frame, do SwapBuffers, compute FPS */
327 static void
328 draw_frame(Display *dpy, Window win)
329 {
330 static int frames = 0;
331 static double tRot0 = -1.0, tRate0 = -1.0;
332 double dt, t = current_time();
333
334 if (tRot0 < 0.0)
335 tRot0 = t;
336 dt = t - tRot0;
337 tRot0 = t;
338
339 if (animate) {
340 /* advance rotation for next frame */
341 angle += 70.0 * dt; /* 70 degrees per second */
342 if (angle > 3600.0)
343 angle -= 3600.0;
344 }
345
346 draw_gears();
347 glXSwapBuffers(dpy, win);
348
349 frames++;
350
351 if (tRate0 < 0.0)
352 tRate0 = t;
353 if (t - tRate0 >= 5.0) {
354 GLfloat seconds = t - tRate0;
355 GLfloat fps = frames / seconds;
356 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
357 fps);
358 tRate0 = t;
359 frames = 0;
360 }
361 }
362
363
364 /* new window size or exposure */
365 static void
366 reshape(int width, int height)
367 {
368 glViewport(0, 0, (GLint) width, (GLint) height);
369
370 if (stereo) {
371 GLfloat w;
372
373 asp = (GLfloat) height / (GLfloat) width;
374 w = fix_point * (1.0 / 5.0);
375
376 left = -5.0 * ((w - 0.5 * eyesep) / fix_point);
377 right = 5.0 * ((w + 0.5 * eyesep) / fix_point);
378 }
379 else {
380 GLfloat h = (GLfloat) height / (GLfloat) width;
381
382 glMatrixMode(GL_PROJECTION);
383 glLoadIdentity();
384 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
385 }
386
387 glMatrixMode(GL_MODELVIEW);
388 glLoadIdentity();
389 glTranslatef(0.0, 0.0, -40.0);
390 }
391
392
393
394 static void
395 init(void)
396 {
397 static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
398 static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
399 static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
400 static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
401
402 glLightfv(GL_LIGHT0, GL_POSITION, pos);
403 glEnable(GL_CULL_FACE);
404 glEnable(GL_LIGHTING);
405 glEnable(GL_LIGHT0);
406 glEnable(GL_DEPTH_TEST);
407
408 /* make the gears */
409 gear1 = glGenLists(1);
410 glNewList(gear1, GL_COMPILE);
411 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
412 gear(1.0, 4.0, 1.0, 20, 0.7);
413 glEndList();
414
415 gear2 = glGenLists(1);
416 glNewList(gear2, GL_COMPILE);
417 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
418 gear(0.5, 2.0, 2.0, 10, 0.7);
419 glEndList();
420
421 gear3 = glGenLists(1);
422 glNewList(gear3, GL_COMPILE);
423 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
424 gear(1.3, 2.0, 0.5, 10, 0.7);
425 glEndList();
426
427 glEnable(GL_NORMALIZE);
428 }
429
430
431 /**
432 * Remove window border/decorations.
433 */
434 static void
435 no_border( Display *dpy, Window w)
436 {
437 static const unsigned MWM_HINTS_DECORATIONS = (1 << 1);
438 static const int PROP_MOTIF_WM_HINTS_ELEMENTS = 5;
439
440 typedef struct
441 {
442 unsigned long flags;
443 unsigned long functions;
444 unsigned long decorations;
445 long inputMode;
446 unsigned long status;
447 } PropMotifWmHints;
448
449 PropMotifWmHints motif_hints;
450 Atom prop, proptype;
451 unsigned long flags = 0;
452
453 /* setup the property */
454 motif_hints.flags = MWM_HINTS_DECORATIONS;
455 motif_hints.decorations = flags;
456
457 /* get the atom for the property */
458 prop = XInternAtom( dpy, "_MOTIF_WM_HINTS", True );
459 if (!prop) {
460 /* something went wrong! */
461 return;
462 }
463
464 /* not sure this is correct, seems to work, XA_WM_HINTS didn't work */
465 proptype = prop;
466
467 XChangeProperty( dpy, w, /* display, window */
468 prop, proptype, /* property, type */
469 32, /* format: 32-bit datums */
470 PropModeReplace, /* mode */
471 (unsigned char *) &motif_hints, /* data */
472 PROP_MOTIF_WM_HINTS_ELEMENTS /* nelements */
473 );
474 }
475
476
477 /*
478 * Create an RGB, double-buffered window.
479 * Return the window and context handles.
480 */
481 static void
482 make_window( Display *dpy, const char *name,
483 int x, int y, int width, int height,
484 Window *winRet, GLXContext *ctxRet)
485 {
486 int attribs[] = { GLX_RGBA,
487 GLX_RED_SIZE, 1,
488 GLX_GREEN_SIZE, 1,
489 GLX_BLUE_SIZE, 1,
490 GLX_DOUBLEBUFFER,
491 GLX_DEPTH_SIZE, 1,
492 None };
493 int stereoAttribs[] = { GLX_RGBA,
494 GLX_RED_SIZE, 1,
495 GLX_GREEN_SIZE, 1,
496 GLX_BLUE_SIZE, 1,
497 GLX_DOUBLEBUFFER,
498 GLX_DEPTH_SIZE, 1,
499 GLX_STEREO,
500 None };
501 int scrnum;
502 XSetWindowAttributes attr;
503 unsigned long mask;
504 Window root;
505 Window win;
506 GLXContext ctx;
507 XVisualInfo *visinfo;
508
509 scrnum = DefaultScreen( dpy );
510 root = RootWindow( dpy, scrnum );
511
512 if (fullscreen) {
513 x = 0; y = 0;
514 width = DisplayWidth( dpy, scrnum );
515 height = DisplayHeight( dpy, scrnum );
516 }
517
518 if (stereo)
519 visinfo = glXChooseVisual( dpy, scrnum, stereoAttribs );
520 else
521 visinfo = glXChooseVisual( dpy, scrnum, attribs );
522 if (!visinfo) {
523 if (stereo) {
524 printf("Error: couldn't get an RGB, "
525 "Double-buffered, Stereo visual\n");
526 } else
527 printf("Error: couldn't get an RGB, Double-buffered visual\n");
528 exit(1);
529 }
530
531 /* window attributes */
532 attr.background_pixel = 0;
533 attr.border_pixel = 0;
534 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
535 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
536 /* XXX this is a bad way to get a borderless window! */
537 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
538
539 win = XCreateWindow( dpy, root, x, y, width, height,
540 0, visinfo->depth, InputOutput,
541 visinfo->visual, mask, &attr );
542
543 if (fullscreen)
544 no_border(dpy, win);
545
546 /* set hints and properties */
547 {
548 XSizeHints sizehints;
549 sizehints.x = x;
550 sizehints.y = y;
551 sizehints.width = width;
552 sizehints.height = height;
553 sizehints.flags = USSize | USPosition;
554 XSetNormalHints(dpy, win, &sizehints);
555 XSetStandardProperties(dpy, win, name, name,
556 None, (char **)NULL, 0, &sizehints);
557 }
558
559 ctx = glXCreateContext( dpy, visinfo, NULL, True );
560 if (!ctx) {
561 printf("Error: glXCreateContext failed\n");
562 exit(1);
563 }
564
565 XFree(visinfo);
566
567 *winRet = win;
568 *ctxRet = ctx;
569 }
570
571
572 /**
573 * Determine whether or not a GLX extension is supported.
574 */
575 int
576 is_glx_extension_supported(Display *dpy, const char *query)
577 {
578 const int scrnum = DefaultScreen(dpy);
579 const char *glx_extensions = NULL;
580 const size_t len = strlen(query);
581 const char *ptr;
582
583 if (glx_extensions == NULL) {
584 glx_extensions = glXQueryExtensionsString(dpy, scrnum);
585 }
586
587 ptr = strstr(glx_extensions, query);
588 return ((ptr != NULL) && ((ptr[len] == ' ') || (ptr[len] == '\0')));
589 }
590
591
592 /**
593 * Attempt to determine whether or not the display is synched to vblank.
594 */
595 void
596 query_vsync(Display *dpy)
597 {
598 int interval = 0;
599
600
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);
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 glXDestroyContext(dpy, ctx);
782 XDestroyWindow(dpy, win);
783 XCloseDisplay(dpy);
784
785 return 0;
786 }