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
37 #include <X11/keysym.h>
46 /* XXX this probably isn't very portable */
51 /* return current time (in seconds) */
57 (void) gettimeofday(&tv
, NULL
);
60 (void) gettimeofday(&tv
, &tz
);
62 return (int) tv
.tv_sec
;
79 #define M_PI 3.14159265
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;
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.
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
100 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
101 GLint teeth
, GLfloat tooth_depth
)
109 r1
= outer_radius
- tooth_depth
/ 2.0;
110 r2
= outer_radius
+ tooth_depth
/ 2.0;
112 da
= 2.0 * M_PI
/ teeth
/ 4.0;
114 glShadeModel(GL_FLAT
);
116 glNormal3f(0.0, 0.0, 1.0);
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);
125 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
126 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
132 /* draw front sides of teeth */
134 da
= 2.0 * M_PI
/ teeth
/ 4.0;
135 for (i
= 0; i
< teeth
; i
++) {
136 angle
= i
* 2.0 * M_PI
/ teeth
;
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
),
142 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
147 glNormal3f(0.0, 0.0, -1.0);
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);
156 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
158 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
163 /* draw back sides of teeth */
165 da
= 2.0 * M_PI
/ teeth
/ 4.0;
166 for (i
= 0; i
< teeth
; i
++) {
167 angle
= i
* 2.0 * M_PI
/ teeth
;
169 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
171 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
173 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
174 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
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
;
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
);
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
),
196 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
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
),
203 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
205 glNormal3f(cos(angle
), sin(angle
), 0.0);
208 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
209 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
213 glShadeModel(GL_SMOOTH
);
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);
230 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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);
238 glTranslatef(-3.0, -2.0, 0.0);
239 glRotatef(angle
, 0.0, 0.0, 1.0);
244 glTranslatef(3.1, -2.0, 0.0);
245 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
250 glTranslatef(-3.1, 4.2, 0.0);
251 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
259 /* new window size or exposure */
261 reshape(int width
, int height
)
263 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
265 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
266 glMatrixMode(GL_PROJECTION
);
268 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
269 glMatrixMode(GL_MODELVIEW
);
271 glTranslatef(0.0, 0.0, -40.0);
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 };
283 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
284 glEnable(GL_CULL_FACE
);
285 glEnable(GL_LIGHTING
);
287 glEnable(GL_DEPTH_TEST
);
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);
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);
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);
308 glEnable(GL_NORMALIZE
);
313 * Create an RGB, double-buffered window.
314 * Return the window and context handles.
317 make_window( Display
*dpy
, const char *name
,
318 int x
, int y
, int width
, int height
,
319 Window
*winRet
, GLXContext
*ctxRet
)
321 int attrib
[] = { GLX_RGBA
,
329 XSetWindowAttributes attr
;
334 XVisualInfo
*visinfo
;
336 scrnum
= DefaultScreen( dpy
);
337 root
= RootWindow( dpy
, scrnum
);
339 visinfo
= glXChooseVisual( dpy
, scrnum
, attrib
);
341 printf("Error: couldn't get an RGB, Double-buffered visual\n");
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
;
352 win
= XCreateWindow( dpy
, root
, 0, 0, width
, height
,
353 0, visinfo
->depth
, InputOutput
,
354 visinfo
->visual
, mask
, &attr
);
356 /* set hints and properties */
358 XSizeHints sizehints
;
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
);
369 ctx
= glXCreateContext( dpy
, visinfo
, NULL
, True
);
371 printf("Error: glXCreateContext failed\n");
383 event_loop(Display
*dpy
, Window win
)
386 while (XPending(dpy
) > 0) {
388 XNextEvent(dpy
, &event
);
389 switch (event
.type
) {
391 /* we'll redraw below */
393 case ConfigureNotify
:
394 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
400 code
= XLookupKeysym(&event
.xkey
, 0);
401 if (code
== XK_Left
) {
404 else if (code
== XK_Right
) {
407 else if (code
== XK_Up
) {
410 else if (code
== XK_Down
) {
414 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
416 if (buffer
[0] == 27) {
429 glXSwapBuffers(dpy
, win
);
434 static int frames
= 0;
435 int t
= current_time();
443 GLfloat seconds
= t
- t0
;
444 GLfloat fps
= frames
/ seconds
;
445 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames
, seconds
,
456 main(int argc
, char *argv
[])
461 char *dpyName
= ":0";
462 GLboolean printInfo
= GL_FALSE
;
465 for (i
= 1; i
< argc
; i
++) {
466 if (strcmp(argv
[i
], "-display") == 0) {
470 else if (strcmp(argv
[i
], "-info") == 0) {
475 dpy
= XOpenDisplay(dpyName
);
477 printf("Error: couldn't open display %s\n", dpyName
);
481 make_window(dpy
, "glxgears", 0, 0, 300, 300, &win
, &ctx
);
482 XMapWindow(dpy
, win
);
483 glXMakeCurrent(dpy
, win
, ctx
);
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
));
494 event_loop(dpy
, win
);
496 glXDestroyContext(dpy
, ctx
);
497 XDestroyWindow(dpy
, win
);