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