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 (int) tv
.tv_sec
;
80 #define M_PI 3.14159265
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;
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. */
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.
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
106 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
107 GLint teeth
, GLfloat tooth_depth
)
115 r1
= outer_radius
- tooth_depth
/ 2.0;
116 r2
= outer_radius
+ tooth_depth
/ 2.0;
118 da
= 2.0 * M_PI
/ teeth
/ 4.0;
120 glShadeModel(GL_FLAT
);
122 glNormal3f(0.0, 0.0, 1.0);
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);
131 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
132 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
138 /* draw front sides of teeth */
140 da
= 2.0 * M_PI
/ teeth
/ 4.0;
141 for (i
= 0; i
< teeth
; i
++) {
142 angle
= i
* 2.0 * M_PI
/ teeth
;
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
),
148 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
153 glNormal3f(0.0, 0.0, -1.0);
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);
162 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
164 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
169 /* draw back sides of teeth */
171 da
= 2.0 * M_PI
/ teeth
/ 4.0;
172 for (i
= 0; i
< teeth
; i
++) {
173 angle
= i
* 2.0 * M_PI
/ teeth
;
175 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
177 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
179 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
180 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
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
;
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
);
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
),
202 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
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
),
209 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
211 glNormal3f(cos(angle
), sin(angle
), 0.0);
214 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
215 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
219 glShadeModel(GL_SMOOTH
);
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);
236 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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);
244 glTranslatef(-3.0, -2.0, 0.0);
245 glRotatef(angle
, 0.0, 0.0, 1.0);
250 glTranslatef(3.1, -2.0, 0.0);
251 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
256 glTranslatef(-3.1, 4.2, 0.0);
257 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
268 /* First left eye. */
269 glDrawBuffer(GL_BACK_LEFT
);
271 glMatrixMode(GL_PROJECTION
);
273 glFrustum(left
, right
, -asp
, asp
, 5.0, 60.0);
275 glMatrixMode(GL_MODELVIEW
);
278 glTranslated(+0.5 * eyesep
, 0.0, 0.0);
282 /* Then right eye. */
283 glDrawBuffer(GL_BACK_RIGHT
);
285 glMatrixMode(GL_PROJECTION
);
287 glFrustum(-right
, -left
, -asp
, asp
, 5.0, 60.0);
289 glMatrixMode(GL_MODELVIEW
);
292 glTranslated(-0.5 * eyesep
, 0.0, 0.0);
300 /* new window size or exposure */
302 reshape(int width
, int height
)
304 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
309 asp
= (GLfloat
) height
/ (GLfloat
) width
;
310 w
= fix_point
* (1.0 / 5.0);
312 left
= -5.0 * ((w
- 0.5 * eyesep
) / fix_point
);
313 right
= 5.0 * ((w
+ 0.5 * eyesep
) / fix_point
);
315 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
317 glMatrixMode(GL_PROJECTION
);
319 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
322 glMatrixMode(GL_MODELVIEW
);
324 glTranslatef(0.0, 0.0, -40.0);
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 };
337 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
338 glEnable(GL_CULL_FACE
);
339 glEnable(GL_LIGHTING
);
341 glEnable(GL_DEPTH_TEST
);
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);
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);
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);
362 glEnable(GL_NORMALIZE
);
367 * Create an RGB, double-buffered window.
368 * Return the window and context handles.
371 make_window( Display
*dpy
, const char *name
,
372 int x
, int y
, int width
, int height
,
373 Window
*winRet
, GLXContext
*ctxRet
)
375 int attribs
[] = { GLX_RGBA
,
382 int stereoAttribs
[] = { GLX_RGBA
,
391 XSetWindowAttributes attr
;
396 XVisualInfo
*visinfo
;
398 scrnum
= DefaultScreen( dpy
);
399 root
= RootWindow( dpy
, scrnum
);
402 visinfo
= glXChooseVisual( dpy
, scrnum
, stereoAttribs
);
404 visinfo
= glXChooseVisual( dpy
, scrnum
, attribs
);
407 printf("Error: couldn't get an RGB, "
408 "Double-buffered, Stereo visual\n");
410 printf("Error: couldn't get an RGB, Double-buffered visual\n");
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
;
421 win
= XCreateWindow( dpy
, root
, 0, 0, width
, height
,
422 0, visinfo
->depth
, InputOutput
,
423 visinfo
->visual
, mask
, &attr
);
425 /* set hints and properties */
427 XSizeHints sizehints
;
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
);
438 ctx
= glXCreateContext( dpy
, visinfo
, NULL
, True
);
440 printf("Error: glXCreateContext failed\n");
452 event_loop(Display
*dpy
, Window win
)
455 while (XPending(dpy
) > 0) {
457 XNextEvent(dpy
, &event
);
458 switch (event
.type
) {
460 /* we'll redraw below */
462 case ConfigureNotify
:
463 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
469 code
= XLookupKeysym(&event
.xkey
, 0);
470 if (code
== XK_Left
) {
473 else if (code
== XK_Right
) {
476 else if (code
== XK_Up
) {
479 else if (code
== XK_Down
) {
483 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
485 if (buffer
[0] == 27) {
500 glXSwapBuffers(dpy
, win
);
505 static int frames
= 0;
506 int t
= current_time();
514 GLfloat seconds
= t
- t0
;
515 GLfloat fps
= frames
/ seconds
;
516 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames
, seconds
,
527 main(int argc
, char *argv
[])
532 char *dpyName
= NULL
;
533 GLboolean printInfo
= GL_FALSE
;
536 for (i
= 1; i
< argc
; i
++) {
537 if (strcmp(argv
[i
], "-display") == 0) {
541 else if (strcmp(argv
[i
], "-info") == 0) {
544 else if (strcmp(argv
[i
], "-stereo") == 0) {
548 printf("Warrning: unknown parameter: %s\n", argv
[i
]);
551 dpy
= XOpenDisplay(dpyName
);
553 printf("Error: couldn't open display %s\n",
554 dpyName
? dpyName
: getenv("DISPLAY"));
558 make_window(dpy
, "glxgears", 0, 0, 300, 300, &win
, &ctx
);
559 XMapWindow(dpy
, win
);
560 glXMakeCurrent(dpy
, win
, ctx
);
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
));
571 event_loop(dpy
, win
);
573 glXDestroyContext(dpy
, ctx
);
574 XDestroyWindow(dpy
, win
);