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