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
, &tz
);
58 return (int) tv
.tv_sec
;
75 #define M_PI 3.14159265
79 static GLfloat view_rotx
= 20.0, view_roty
= 30.0, view_rotz
= 0.0;
80 static GLint gear1
, gear2
, gear3
;
81 static GLfloat angle
= 0.0;
86 * Draw a gear wheel. You'll probably want to call this function when
87 * building a display list since we do a lot of trig here.
89 * Input: inner_radius - radius of hole at center
90 * outer_radius - radius at center of teeth
91 * width - width of gear
92 * teeth - number of teeth
93 * tooth_depth - depth of tooth
96 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
97 GLint teeth
, GLfloat tooth_depth
)
105 r1
= outer_radius
- tooth_depth
/ 2.0;
106 r2
= outer_radius
+ tooth_depth
/ 2.0;
108 da
= 2.0 * M_PI
/ teeth
/ 4.0;
110 glShadeModel(GL_FLAT
);
112 glNormal3f(0.0, 0.0, 1.0);
114 /* draw front face */
115 glBegin(GL_QUAD_STRIP
);
116 for (i
= 0; i
<= teeth
; i
++) {
117 angle
= i
* 2.0 * M_PI
/ teeth
;
118 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
119 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
121 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
122 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
128 /* draw front sides of teeth */
130 da
= 2.0 * M_PI
/ teeth
/ 4.0;
131 for (i
= 0; i
< teeth
; i
++) {
132 angle
= i
* 2.0 * M_PI
/ teeth
;
134 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
135 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
136 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
138 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
143 glNormal3f(0.0, 0.0, -1.0);
146 glBegin(GL_QUAD_STRIP
);
147 for (i
= 0; i
<= teeth
; i
++) {
148 angle
= i
* 2.0 * M_PI
/ teeth
;
149 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
150 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
152 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
154 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
159 /* draw back sides of teeth */
161 da
= 2.0 * M_PI
/ teeth
/ 4.0;
162 for (i
= 0; i
< teeth
; i
++) {
163 angle
= i
* 2.0 * M_PI
/ teeth
;
165 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
167 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
169 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
170 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
174 /* draw outward faces of teeth */
175 glBegin(GL_QUAD_STRIP
);
176 for (i
= 0; i
< teeth
; i
++) {
177 angle
= i
* 2.0 * M_PI
/ teeth
;
179 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
180 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
181 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
182 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
183 len
= sqrt(u
* u
+ v
* v
);
186 glNormal3f(v
, -u
, 0.0);
187 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
188 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
189 glNormal3f(cos(angle
), sin(angle
), 0.0);
190 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
192 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
194 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
195 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
196 glNormal3f(v
, -u
, 0.0);
197 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
199 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
201 glNormal3f(cos(angle
), sin(angle
), 0.0);
204 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
205 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
209 glShadeModel(GL_SMOOTH
);
211 /* draw inside radius cylinder */
212 glBegin(GL_QUAD_STRIP
);
213 for (i
= 0; i
<= teeth
; i
++) {
214 angle
= i
* 2.0 * M_PI
/ teeth
;
215 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
216 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
217 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
226 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
229 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
230 glRotatef(view_roty
, 0.0, 1.0, 0.0);
231 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
234 glTranslatef(-3.0, -2.0, 0.0);
235 glRotatef(angle
, 0.0, 0.0, 1.0);
240 glTranslatef(3.1, -2.0, 0.0);
241 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
246 glTranslatef(-3.1, 4.2, 0.0);
247 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
255 /* new window size or exposure */
257 reshape(int width
, int height
)
259 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
261 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
262 glMatrixMode(GL_PROJECTION
);
264 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
265 glMatrixMode(GL_MODELVIEW
);
267 glTranslatef(0.0, 0.0, -40.0);
274 static GLfloat pos
[4] = { 5.0, 5.0, 10.0, 0.0 };
275 static GLfloat red
[4] = { 0.8, 0.1, 0.0, 1.0 };
276 static GLfloat green
[4] = { 0.0, 0.8, 0.2, 1.0 };
277 static GLfloat blue
[4] = { 0.2, 0.2, 1.0, 1.0 };
279 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
280 glEnable(GL_CULL_FACE
);
281 glEnable(GL_LIGHTING
);
283 glEnable(GL_DEPTH_TEST
);
286 gear1
= glGenLists(1);
287 glNewList(gear1
, GL_COMPILE
);
288 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
289 gear(1.0, 4.0, 1.0, 20, 0.7);
292 gear2
= glGenLists(1);
293 glNewList(gear2
, GL_COMPILE
);
294 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
295 gear(0.5, 2.0, 2.0, 10, 0.7);
298 gear3
= glGenLists(1);
299 glNewList(gear3
, GL_COMPILE
);
300 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
301 gear(1.3, 2.0, 0.5, 10, 0.7);
304 glEnable(GL_NORMALIZE
);
309 * Create an RGB, double-buffered window.
310 * Return the window and context handles.
313 make_window( Display
*dpy
, const char *name
,
314 int x
, int y
, int width
, int height
,
315 Window
*winRet
, GLXContext
*ctxRet
)
317 int attrib
[] = { GLX_RGBA
,
325 XSetWindowAttributes attr
;
330 XVisualInfo
*visinfo
;
332 scrnum
= DefaultScreen( dpy
);
333 root
= RootWindow( dpy
, scrnum
);
335 visinfo
= glXChooseVisual( dpy
, scrnum
, attrib
);
337 printf("Error: couldn't get an RGB, Double-buffered visual\n");
341 /* window attributes */
342 attr
.background_pixel
= 0;
343 attr
.border_pixel
= 0;
344 attr
.colormap
= XCreateColormap( dpy
, root
, visinfo
->visual
, AllocNone
);
345 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
346 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
348 win
= XCreateWindow( dpy
, root
, 0, 0, width
, height
,
349 0, visinfo
->depth
, InputOutput
,
350 visinfo
->visual
, mask
, &attr
);
352 /* set hints and properties */
354 XSizeHints sizehints
;
357 sizehints
.width
= width
;
358 sizehints
.height
= height
;
359 sizehints
.flags
= USSize
| USPosition
;
360 XSetNormalHints(dpy
, win
, &sizehints
);
361 XSetStandardProperties(dpy
, win
, name
, name
,
362 None
, (char **)NULL
, 0, &sizehints
);
365 ctx
= glXCreateContext( dpy
, visinfo
, NULL
, True
);
367 printf("Error: glXCreateContext failed\n");
379 event_loop(Display
*dpy
, Window win
)
382 while (XPending(dpy
) > 0) {
384 XNextEvent(dpy
, &event
);
385 switch (event
.type
) {
387 /* we'll redraw below */
389 case ConfigureNotify
:
390 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
396 code
= XLookupKeysym(&event
.xkey
, 0);
397 if (code
== XK_Left
) {
400 else if (code
== XK_Right
) {
403 else if (code
== XK_Up
) {
406 else if (code
== XK_Down
) {
410 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
412 if (buffer
[0] == 27) {
425 glXSwapBuffers(dpy
, win
);
430 static int frames
= 0;
431 int t
= current_time();
439 GLfloat seconds
= t
- t0
;
440 GLfloat fps
= frames
/ seconds
;
441 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames
, seconds
,
452 main(int argc
, char *argv
[])
457 char *dpyName
= ":0";
458 GLboolean printInfo
= GL_FALSE
;
461 for (i
= 1; i
< argc
; i
++) {
462 if (strcmp(argv
[i
], "-display") == 0) {
466 else if (strcmp(argv
[i
], "-info") == 0) {
471 dpy
= XOpenDisplay(dpyName
);
473 printf("Error: couldn't open display %s\n", dpyName
);
477 make_window(dpy
, "glxgears", 0, 0, 300, 300, &win
, &ctx
);
478 XMapWindow(dpy
, win
);
479 glXMakeCurrent(dpy
, win
, ctx
);
482 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
483 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
484 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
485 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
490 event_loop(dpy
, win
);
492 glXDestroyContext(dpy
, ctx
);
493 XDestroyWindow(dpy
, win
);