2 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
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
26 * Command line options:
27 * -info print GL implementation information
28 * -stereo use stereo enabled GLX visual
38 #include <X11/keysym.h>
47 /* XXX this probably isn't very portable */
52 /* return current time (in seconds) */
58 (void) gettimeofday(&tv
, NULL
);
61 (void) gettimeofday(&tv
, &tz
);
63 return (double) tv
.tv_sec
+ tv
.tv_usec
/ 1000000.0;
72 /* update this function for other platforms! */
73 static double t
= 0.0;
76 fprintf(stderr
, "Warning: current_time() not implemented!!\n");
87 #define M_PI 3.14159265
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;
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. */
104 * Draw a gear wheel. You'll probably want to call this function when
105 * building a display list since we do a lot of trig here.
107 * Input: inner_radius - radius of hole at center
108 * outer_radius - radius at center of teeth
109 * width - width of gear
110 * teeth - number of teeth
111 * tooth_depth - depth of tooth
114 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
115 GLint teeth
, GLfloat tooth_depth
)
123 r1
= outer_radius
- tooth_depth
/ 2.0;
124 r2
= outer_radius
+ tooth_depth
/ 2.0;
126 da
= 2.0 * M_PI
/ teeth
/ 4.0;
128 glShadeModel(GL_FLAT
);
130 glNormal3f(0.0, 0.0, 1.0);
132 /* draw front face */
133 glBegin(GL_QUAD_STRIP
);
134 for (i
= 0; i
<= teeth
; i
++) {
135 angle
= i
* 2.0 * M_PI
/ teeth
;
136 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
137 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
139 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
140 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
146 /* draw front sides of teeth */
148 da
= 2.0 * M_PI
/ teeth
/ 4.0;
149 for (i
= 0; i
< teeth
; i
++) {
150 angle
= i
* 2.0 * M_PI
/ teeth
;
152 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
153 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
154 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
156 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
161 glNormal3f(0.0, 0.0, -1.0);
164 glBegin(GL_QUAD_STRIP
);
165 for (i
= 0; i
<= teeth
; i
++) {
166 angle
= i
* 2.0 * M_PI
/ teeth
;
167 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
168 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
170 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
172 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
177 /* draw back sides of teeth */
179 da
= 2.0 * M_PI
/ teeth
/ 4.0;
180 for (i
= 0; i
< teeth
; i
++) {
181 angle
= i
* 2.0 * M_PI
/ teeth
;
183 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
185 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
187 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
188 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
192 /* draw outward faces of teeth */
193 glBegin(GL_QUAD_STRIP
);
194 for (i
= 0; i
< teeth
; i
++) {
195 angle
= i
* 2.0 * M_PI
/ teeth
;
197 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
198 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
199 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
200 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
201 len
= sqrt(u
* u
+ v
* v
);
204 glNormal3f(v
, -u
, 0.0);
205 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
206 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
207 glNormal3f(cos(angle
), sin(angle
), 0.0);
208 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
210 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
212 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
213 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
214 glNormal3f(v
, -u
, 0.0);
215 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
217 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
219 glNormal3f(cos(angle
), sin(angle
), 0.0);
222 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
223 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
227 glShadeModel(GL_SMOOTH
);
229 /* draw inside radius cylinder */
230 glBegin(GL_QUAD_STRIP
);
231 for (i
= 0; i
<= teeth
; i
++) {
232 angle
= i
* 2.0 * M_PI
/ teeth
;
233 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
234 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
235 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
244 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
247 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
248 glRotatef(view_roty
, 0.0, 1.0, 0.0);
249 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
252 glTranslatef(-3.0, -2.0, 0.0);
253 glRotatef(angle
, 0.0, 0.0, 1.0);
258 glTranslatef(3.1, -2.0, 0.0);
259 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
264 glTranslatef(-3.1, 4.2, 0.0);
265 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
276 /* First left eye. */
277 glDrawBuffer(GL_BACK_LEFT
);
279 glMatrixMode(GL_PROJECTION
);
281 glFrustum(left
, right
, -asp
, asp
, 5.0, 60.0);
283 glMatrixMode(GL_MODELVIEW
);
286 glTranslated(+0.5 * eyesep
, 0.0, 0.0);
290 /* Then right eye. */
291 glDrawBuffer(GL_BACK_RIGHT
);
293 glMatrixMode(GL_PROJECTION
);
295 glFrustum(-right
, -left
, -asp
, asp
, 5.0, 60.0);
297 glMatrixMode(GL_MODELVIEW
);
300 glTranslated(-0.5 * eyesep
, 0.0, 0.0);
308 /* new window size or exposure */
310 reshape(int width
, int height
)
312 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
317 asp
= (GLfloat
) height
/ (GLfloat
) width
;
318 w
= fix_point
* (1.0 / 5.0);
320 left
= -5.0 * ((w
- 0.5 * eyesep
) / fix_point
);
321 right
= 5.0 * ((w
+ 0.5 * eyesep
) / fix_point
);
323 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
325 glMatrixMode(GL_PROJECTION
);
327 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
330 glMatrixMode(GL_MODELVIEW
);
332 glTranslatef(0.0, 0.0, -40.0);
340 static GLfloat pos
[4] = { 5.0, 5.0, 10.0, 0.0 };
341 static GLfloat red
[4] = { 0.8, 0.1, 0.0, 1.0 };
342 static GLfloat green
[4] = { 0.0, 0.8, 0.2, 1.0 };
343 static GLfloat blue
[4] = { 0.2, 0.2, 1.0, 1.0 };
345 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
346 glEnable(GL_CULL_FACE
);
347 glEnable(GL_LIGHTING
);
349 glEnable(GL_DEPTH_TEST
);
352 gear1
= glGenLists(1);
353 glNewList(gear1
, GL_COMPILE
);
354 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
355 gear(1.0, 4.0, 1.0, 20, 0.7);
358 gear2
= glGenLists(1);
359 glNewList(gear2
, GL_COMPILE
);
360 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
361 gear(0.5, 2.0, 2.0, 10, 0.7);
364 gear3
= glGenLists(1);
365 glNewList(gear3
, GL_COMPILE
);
366 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
367 gear(1.3, 2.0, 0.5, 10, 0.7);
370 glEnable(GL_NORMALIZE
);
375 * Create an RGB, double-buffered window.
376 * Return the window and context handles.
379 make_window( Display
*dpy
, const char *name
,
380 int x
, int y
, int width
, int height
,
381 Window
*winRet
, GLXContext
*ctxRet
)
383 int attribs
[] = { GLX_RGBA
,
390 int stereoAttribs
[] = { GLX_RGBA
,
399 XSetWindowAttributes attr
;
404 XVisualInfo
*visinfo
;
406 scrnum
= DefaultScreen( dpy
);
407 root
= RootWindow( dpy
, scrnum
);
411 width
= DisplayWidth( dpy
, scrnum
);
412 height
= DisplayHeight( dpy
, scrnum
);
416 visinfo
= glXChooseVisual( dpy
, scrnum
, stereoAttribs
);
418 visinfo
= glXChooseVisual( dpy
, scrnum
, attribs
);
421 printf("Error: couldn't get an RGB, "
422 "Double-buffered, Stereo visual\n");
424 printf("Error: couldn't get an RGB, Double-buffered visual\n");
428 /* window attributes */
429 attr
.background_pixel
= 0;
430 attr
.border_pixel
= 0;
431 attr
.colormap
= XCreateColormap( dpy
, root
, visinfo
->visual
, AllocNone
);
432 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
433 attr
.override_redirect
= fullscreen
;
434 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
| CWOverrideRedirect
;
436 win
= XCreateWindow( dpy
, root
, 0, 0, width
, height
,
437 0, visinfo
->depth
, InputOutput
,
438 visinfo
->visual
, mask
, &attr
);
440 /* set hints and properties */
442 XSizeHints sizehints
;
445 sizehints
.width
= width
;
446 sizehints
.height
= height
;
447 sizehints
.flags
= USSize
| USPosition
;
448 XSetNormalHints(dpy
, win
, &sizehints
);
449 XSetStandardProperties(dpy
, win
, name
, name
,
450 None
, (char **)NULL
, 0, &sizehints
);
453 ctx
= glXCreateContext( dpy
, visinfo
, NULL
, True
);
455 printf("Error: glXCreateContext failed\n");
467 event_loop(Display
*dpy
, Window win
)
470 while (XPending(dpy
) > 0) {
472 XNextEvent(dpy
, &event
);
473 switch (event
.type
) {
475 /* we'll redraw below */
477 case ConfigureNotify
:
478 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
484 code
= XLookupKeysym(&event
.xkey
, 0);
485 if (code
== XK_Left
) {
488 else if (code
== XK_Right
) {
491 else if (code
== XK_Up
) {
494 else if (code
== XK_Down
) {
498 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
500 if (buffer
[0] == 27) {
510 static int frames
= 0;
511 static double tRot0
= -1.0, tRate0
= -1.0;
512 double dt
, t
= current_time();
518 /* advance rotation for next frame */
519 angle
+= 70.0 * dt
; /* 70 degrees per second */
524 glXSwapBuffers(dpy
, win
);
530 if (t
- tRate0
>= 5.0) {
531 GLfloat seconds
= t
- tRate0
;
532 GLfloat fps
= frames
/ seconds
;
533 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames
, seconds
,
547 printf(" -display <displayname> set the display to run on\n");
548 printf(" -stereo run in stereo mode\n");
549 printf(" -fullscreen run in fullscreen mode\n");
550 printf(" -info display OpenGL renderer info\n");
555 main(int argc
, char *argv
[])
557 const int winWidth
= 300, winHeight
= 300;
561 char *dpyName
= NULL
;
562 GLboolean printInfo
= GL_FALSE
;
565 for (i
= 1; i
< argc
; i
++) {
566 if (strcmp(argv
[i
], "-display") == 0) {
570 else if (strcmp(argv
[i
], "-info") == 0) {
573 else if (strcmp(argv
[i
], "-stereo") == 0) {
576 else if (strcmp(argv
[i
], "-fullscreen") == 0) {
577 fullscreen
= GL_TRUE
;
585 dpy
= XOpenDisplay(dpyName
);
587 printf("Error: couldn't open display %s\n",
588 dpyName
? dpyName
: getenv("DISPLAY"));
592 make_window(dpy
, "glxgears", 0, 0, winWidth
, winHeight
, &win
, &ctx
);
593 XMapWindow(dpy
, win
);
594 glXMakeCurrent(dpy
, win
, ctx
);
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
));
605 /* Set initial projection/viewing transformation.
606 * We can't be sure we'll get a ConfigureNotify event when the window
609 reshape(winWidth
, winHeight
);
611 event_loop(dpy
, win
);
613 glDeleteLists(gear1
, 1);
614 glDeleteLists(gear2
, 1);
615 glDeleteLists(gear3
, 1);
616 glXDestroyContext(dpy
, ctx
);
617 XDestroyWindow(dpy
, win
);