ARB prog parser: fix parameter binding type
[mesa.git] / progs / xdemos / glxgears_fbconfig.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 * \file glxgears_fbconfig.c
24 * Yet-another-version of gears. Originally ported to GLX by Brian Paul on
25 * 23 March 2001. Modified to use fbconfigs by Ian Romanick on 10 Feb 2004.
26 *
27 * Command line options:
28 * -info print GL implementation information
29 *
30 * \author Brian Paul
31 * \author Ian Romanick <idr@us.ibm.com>
32 */
33
34
35 #define GLX_GLXEXT_PROTOTYPES
36
37 #include <math.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <X11/Xlib.h>
42 #include <X11/keysym.h>
43 #include <GL/gl.h>
44 #include <GL/glx.h>
45 #include <GL/glxext.h>
46 #include <assert.h>
47 #include "pbutil.h"
48
49 /* I had to use the SGIX versions of these because for some reason glxext.h
50 * doesn't define the core versions if GLX_VERSION_1_3 is defined, and glx.h
51 * doesn't define them at all. One or both header files is clearly broken.
52 */
53 static PFNGLXCHOOSEFBCONFIGSGIXPROC choose_fbconfig = NULL;
54 static PFNGLXGETVISUALFROMFBCONFIGSGIXPROC get_visual_from_fbconfig = NULL;
55 static PFNGLXCREATECONTEXTWITHCONFIGSGIXPROC create_new_context = NULL;
56
57 #define BENCHMARK
58
59 #ifdef BENCHMARK
60
61 /* XXX this probably isn't very portable */
62
63 #include <sys/time.h>
64 #include <unistd.h>
65
66 /* return current time (in seconds) */
67 static int
68 current_time(void)
69 {
70 struct timeval tv;
71 #ifdef __VMS
72 (void) gettimeofday(&tv, NULL );
73 #else
74 struct timezone tz;
75 (void) gettimeofday(&tv, &tz);
76 #endif
77 return (int) tv.tv_sec;
78 }
79
80 #else /*BENCHMARK*/
81
82 /* dummy */
83 static int
84 current_time(void)
85 {
86 return 0;
87 }
88
89 #endif /*BENCHMARK*/
90
91
92
93 #ifndef M_PI
94 #define M_PI 3.14159265
95 #endif
96
97
98 static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
99 static GLint gear1, gear2, gear3;
100 static GLfloat angle = 0.0;
101
102
103 /*
104 *
105 * Draw a gear wheel. You'll probably want to call this function when
106 * building a display list since we do a lot of trig here.
107 *
108 * Input: inner_radius - radius of hole at center
109 * outer_radius - radius at center of teeth
110 * width - width of gear
111 * teeth - number of teeth
112 * tooth_depth - depth of tooth
113 */
114 static void
115 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
116 GLint teeth, GLfloat tooth_depth)
117 {
118 GLint i;
119 GLfloat r0, r1, r2;
120 GLfloat angle, da;
121 GLfloat u, v, len;
122
123 r0 = inner_radius;
124 r1 = outer_radius - tooth_depth / 2.0;
125 r2 = outer_radius + tooth_depth / 2.0;
126
127 da = 2.0 * M_PI / teeth / 4.0;
128
129 glShadeModel(GL_FLAT);
130
131 glNormal3f(0.0, 0.0, 1.0);
132
133 /* draw front face */
134 glBegin(GL_QUAD_STRIP);
135 for (i = 0; i <= teeth; i++) {
136 angle = i * 2.0 * M_PI / teeth;
137 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
138 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
139 if (i < teeth) {
140 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
141 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
142 width * 0.5);
143 }
144 }
145 glEnd();
146
147 /* draw front sides of teeth */
148 glBegin(GL_QUADS);
149 da = 2.0 * M_PI / teeth / 4.0;
150 for (i = 0; i < teeth; i++) {
151 angle = i * 2.0 * M_PI / teeth;
152
153 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
154 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
155 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
156 width * 0.5);
157 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
158 width * 0.5);
159 }
160 glEnd();
161
162 glNormal3f(0.0, 0.0, -1.0);
163
164 /* draw back face */
165 glBegin(GL_QUAD_STRIP);
166 for (i = 0; i <= teeth; i++) {
167 angle = i * 2.0 * M_PI / teeth;
168 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
169 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
170 if (i < teeth) {
171 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
172 -width * 0.5);
173 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
174 }
175 }
176 glEnd();
177
178 /* draw back sides of teeth */
179 glBegin(GL_QUADS);
180 da = 2.0 * M_PI / teeth / 4.0;
181 for (i = 0; i < teeth; i++) {
182 angle = i * 2.0 * M_PI / teeth;
183
184 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
185 -width * 0.5);
186 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
187 -width * 0.5);
188 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
189 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
190 }
191 glEnd();
192
193 /* draw outward faces of teeth */
194 glBegin(GL_QUAD_STRIP);
195 for (i = 0; i < teeth; i++) {
196 angle = i * 2.0 * M_PI / teeth;
197
198 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
199 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
200 u = r2 * cos(angle + da) - r1 * cos(angle);
201 v = r2 * sin(angle + da) - r1 * sin(angle);
202 len = sqrt(u * u + v * v);
203 u /= len;
204 v /= len;
205 glNormal3f(v, -u, 0.0);
206 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
207 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
208 glNormal3f(cos(angle), sin(angle), 0.0);
209 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
210 width * 0.5);
211 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
212 -width * 0.5);
213 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
214 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
215 glNormal3f(v, -u, 0.0);
216 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
217 width * 0.5);
218 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
219 -width * 0.5);
220 glNormal3f(cos(angle), sin(angle), 0.0);
221 }
222
223 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
224 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
225
226 glEnd();
227
228 glShadeModel(GL_SMOOTH);
229
230 /* draw inside radius cylinder */
231 glBegin(GL_QUAD_STRIP);
232 for (i = 0; i <= teeth; i++) {
233 angle = i * 2.0 * M_PI / teeth;
234 glNormal3f(-cos(angle), -sin(angle), 0.0);
235 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
236 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
237 }
238 glEnd();
239 }
240
241
242 static void
243 draw(void)
244 {
245 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
246
247 glPushMatrix();
248 glRotatef(view_rotx, 1.0, 0.0, 0.0);
249 glRotatef(view_roty, 0.0, 1.0, 0.0);
250 glRotatef(view_rotz, 0.0, 0.0, 1.0);
251
252 glPushMatrix();
253 glTranslatef(-3.0, -2.0, 0.0);
254 glRotatef(angle, 0.0, 0.0, 1.0);
255 glCallList(gear1);
256 glPopMatrix();
257
258 glPushMatrix();
259 glTranslatef(3.1, -2.0, 0.0);
260 glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
261 glCallList(gear2);
262 glPopMatrix();
263
264 glPushMatrix();
265 glTranslatef(-3.1, 4.2, 0.0);
266 glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
267 glCallList(gear3);
268 glPopMatrix();
269
270 glPopMatrix();
271 }
272
273
274 /* new window size or exposure */
275 static void
276 reshape(int width, int height)
277 {
278 GLfloat h = (GLfloat) height / (GLfloat) width;
279
280 glViewport(0, 0, (GLint) width, (GLint) height);
281 glMatrixMode(GL_PROJECTION);
282 glLoadIdentity();
283 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
284 glMatrixMode(GL_MODELVIEW);
285 glLoadIdentity();
286 glTranslatef(0.0, 0.0, -40.0);
287 }
288
289
290 static void
291 init(void)
292 {
293 static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
294 static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
295 static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
296 static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
297
298 glLightfv(GL_LIGHT0, GL_POSITION, pos);
299 glEnable(GL_CULL_FACE);
300 glEnable(GL_LIGHTING);
301 glEnable(GL_LIGHT0);
302 glEnable(GL_DEPTH_TEST);
303
304 /* make the gears */
305 gear1 = glGenLists(1);
306 glNewList(gear1, GL_COMPILE);
307 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
308 gear(1.0, 4.0, 1.0, 20, 0.7);
309 glEndList();
310
311 gear2 = glGenLists(1);
312 glNewList(gear2, GL_COMPILE);
313 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
314 gear(0.5, 2.0, 2.0, 10, 0.7);
315 glEndList();
316
317 gear3 = glGenLists(1);
318 glNewList(gear3, GL_COMPILE);
319 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
320 gear(1.3, 2.0, 0.5, 10, 0.7);
321 glEndList();
322
323 glEnable(GL_NORMALIZE);
324 }
325
326
327 /**
328 * Initialize fbconfig related function pointers.
329 */
330 static void
331 init_fbconfig_functions(Display *dpy, int scrnum)
332 {
333 const char * glx_extensions;
334 const char * match;
335 static const char ext_name[] = "GLX_SGIX_fbconfig";
336 const size_t len = strlen( ext_name );
337 int major;
338 int minor;
339 GLboolean ext_version_supported;
340 GLboolean glx_1_3_supported;
341
342
343 /* Determine if GLX 1.3 or greater is supported.
344 */
345 glXQueryVersion(dpy, & major, & minor);
346 glx_1_3_supported = (major == 1) && (minor >= 3);
347
348 /* Determine if GLX_SGIX_fbconfig is supported.
349 */
350 glx_extensions = glXQueryExtensionsString(dpy, scrnum);
351 match = strstr( glx_extensions, ext_name );
352
353 ext_version_supported = (match != NULL)
354 && ((match[len] == '\0') || (match[len] == ' '));
355
356 printf( "GLX 1.3 is %ssupported.\n",
357 (glx_1_3_supported) ? "" : "not " );
358 printf( "%s is %ssupported.\n",
359 ext_name, (ext_version_supported) ? "" : "not " );
360
361 if ( glx_1_3_supported ) {
362 choose_fbconfig = (PFNGLXCHOOSEFBCONFIGSGIXPROC) glXGetProcAddressARB(
363 (GLubyte *) "glXChooseFBConfig");
364 get_visual_from_fbconfig = (PFNGLXGETVISUALFROMFBCONFIGSGIXPROC) glXGetProcAddressARB(
365 (GLubyte *) "glXGetVisualFromFBConfig");
366 create_new_context = (PFNGLXCREATECONTEXTWITHCONFIGSGIXPROC) glXGetProcAddressARB(
367 (GLubyte *) "glXCreateNewContext");
368 }
369 else if ( ext_version_supported ) {
370 choose_fbconfig = (PFNGLXCHOOSEFBCONFIGSGIXPROC) glXGetProcAddressARB(
371 (GLubyte *) "glXChooseFBConfigSGIX");
372 get_visual_from_fbconfig = (PFNGLXGETVISUALFROMFBCONFIGSGIXPROC) glXGetProcAddressARB(
373 (GLubyte *) "glXGetVisualFromFBConfigSGIX");
374 create_new_context = (PFNGLXCREATECONTEXTWITHCONFIGSGIXPROC) glXGetProcAddressARB(
375 (GLubyte *) "glXCreateContextWithConfigSGIX");
376 }
377 else {
378 printf( "This demo requires either GLX 1.3 or %s be supported.\n",
379 ext_name );
380 exit(1);
381 }
382
383 if ( choose_fbconfig == NULL ) {
384 printf( "glXChooseFBConfig not found!\n" );
385 exit(1);
386 }
387
388 if ( get_visual_from_fbconfig == NULL ) {
389 printf( "glXGetVisualFromFBConfig not found!\n" );
390 exit(1);
391 }
392
393 if ( create_new_context == NULL ) {
394 printf( "glXCreateNewContext not found!\n" );
395 exit(1);
396 }
397 }
398
399
400 /*
401 * Create an RGB, double-buffered window.
402 * Return the window and context handles.
403 */
404 static void
405 make_window( Display *dpy, const char *name,
406 int x, int y, int width, int height,
407 Window *winRet, GLXContext *ctxRet)
408 {
409 int attrib[] = { GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
410 GLX_RENDER_TYPE, GLX_RGBA_BIT,
411 GLX_RED_SIZE, 1,
412 GLX_GREEN_SIZE, 1,
413 GLX_BLUE_SIZE, 1,
414 GLX_DOUBLEBUFFER, GL_TRUE,
415 GLX_DEPTH_SIZE, 1,
416 None };
417 GLXFBConfig * fbconfig;
418 int num_configs;
419 int scrnum;
420 int i;
421 XSetWindowAttributes attr;
422 unsigned long mask;
423 Window root;
424 Window win;
425 GLXContext ctx;
426 XVisualInfo *visinfo;
427
428 scrnum = DefaultScreen( dpy );
429 root = RootWindow( dpy, scrnum );
430
431 init_fbconfig_functions(dpy, scrnum);
432 fbconfig = (*choose_fbconfig)(dpy, scrnum, attrib, & num_configs);
433 if (fbconfig == NULL) {
434 printf("Error: couldn't get an RGB, Double-buffered visual\n");
435 exit(1);
436 }
437
438 printf("\nThe following fbconfigs meet the requirements. The first one "
439 "will be used.\n\n");
440 for ( i = 0 ; i < num_configs ; i++ ) {
441 PrintFBConfigInfo(dpy, scrnum, fbconfig[i], GL_TRUE);
442 }
443
444 /* window attributes */
445 visinfo = (*get_visual_from_fbconfig)(dpy, fbconfig[0]);
446 assert(visinfo != NULL);
447 attr.background_pixel = 0;
448 attr.border_pixel = 0;
449 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
450 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
451 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
452
453 win = XCreateWindow( dpy, root, 0, 0, width, height,
454 0, visinfo->depth, InputOutput,
455 visinfo->visual, mask, &attr );
456
457 /* set hints and properties */
458 {
459 XSizeHints sizehints;
460 sizehints.x = x;
461 sizehints.y = y;
462 sizehints.width = width;
463 sizehints.height = height;
464 sizehints.flags = USSize | USPosition;
465 XSetNormalHints(dpy, win, &sizehints);
466 XSetStandardProperties(dpy, win, name, name,
467 None, (char **)NULL, 0, &sizehints);
468 }
469
470 ctx = (*create_new_context)(dpy, fbconfig[0], GLX_RGBA_TYPE, NULL, GL_TRUE);
471 if (!ctx) {
472 printf("Error: glXCreateNewContext failed\n");
473 exit(1);
474 }
475
476 XFree(fbconfig);
477
478 *winRet = win;
479 *ctxRet = ctx;
480 }
481
482
483 static void
484 event_loop(Display *dpy, Window win)
485 {
486 while (1) {
487 while (XPending(dpy) > 0) {
488 XEvent event;
489 XNextEvent(dpy, &event);
490 switch (event.type) {
491 case Expose:
492 /* we'll redraw below */
493 break;
494 case ConfigureNotify:
495 reshape(event.xconfigure.width, event.xconfigure.height);
496 break;
497 case KeyPress:
498 {
499 char buffer[10];
500 int r, code;
501 code = XLookupKeysym(&event.xkey, 0);
502 if (code == XK_Left) {
503 view_roty += 5.0;
504 }
505 else if (code == XK_Right) {
506 view_roty -= 5.0;
507 }
508 else if (code == XK_Up) {
509 view_rotx += 5.0;
510 }
511 else if (code == XK_Down) {
512 view_rotx -= 5.0;
513 }
514 else {
515 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
516 NULL, NULL);
517 if (buffer[0] == 27) {
518 /* escape */
519 return;
520 }
521 }
522 }
523 }
524 }
525
526 /* next frame */
527 angle += 2.0;
528
529 draw();
530 glXSwapBuffers(dpy, win);
531
532 /* calc framerate */
533 {
534 static int t0 = -1;
535 static int frames = 0;
536 int t = current_time();
537
538 if (t0 < 0)
539 t0 = t;
540
541 frames++;
542
543 if (t - t0 >= 5.0) {
544 GLfloat seconds = t - t0;
545 GLfloat fps = frames / seconds;
546 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
547 fps);
548 t0 = t;
549 frames = 0;
550 }
551 }
552 }
553 }
554
555
556 int
557 main(int argc, char *argv[])
558 {
559 Display *dpy;
560 Window win;
561 GLXContext ctx;
562 const char *dpyName = NULL;
563 GLboolean printInfo = GL_FALSE;
564 int i;
565
566 for (i = 1; i < argc; i++) {
567 if (strcmp(argv[i], "-display") == 0) {
568 dpyName = argv[i+1];
569 i++;
570 }
571 else if (strcmp(argv[i], "-info") == 0) {
572 printInfo = GL_TRUE;
573 }
574 }
575
576 dpy = XOpenDisplay(dpyName);
577 if (!dpy) {
578 printf("Error: couldn't open display %s\n", XDisplayName(dpyName));
579 return -1;
580 }
581
582 make_window(dpy, "glxgears", 0, 0, 300, 300, &win, &ctx);
583 XMapWindow(dpy, win);
584 glXMakeCurrent(dpy, win, ctx);
585
586 if (printInfo) {
587 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
588 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
589 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
590 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
591 }
592
593 init();
594
595 event_loop(dpy, win);
596
597 glXDestroyContext(dpy, ctx);
598 XDestroyWindow(dpy, win);
599 XCloseDisplay(dpy);
600
601 return 0;
602 }