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