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