1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* gears --- 3D gear wheels */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)gears.c 4.07 97/11/24 xlockmore";
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
23 * 10-May-97: Compatible with xscreensaver
24 * 22-Mar-97: Added support for -mono mode, and monochrome X servers.
25 * Ed Mackey, emackey@netaxs.com
26 * 13-Mar-97: Memory leak fix by Tom Schmidt <tschmidt@micron.com>
27 * 1996: "written" by Danny Sung <dannys@ucla.edu>
28 * Based on 3-D gear wheels by Brian Paul which is in the public domain.
32 * PURIFY 3.0a on SunOS4 reports an unitialized memory read on each of
33 * the glCallList() functions below when using MesaGL 2.1. This has
34 * been fixed in MesaGL 2.2 and later releases.
38 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
39 * otherwise caddr_t is not defined correctly
42 #include <X11/Intrinsic.h>
45 # define PROGCLASS "Gears"
46 # define HACK_INIT init_gears
47 # define HACK_DRAW draw_gears
48 # define gears_opts xlockmore_opts
49 # define DEFAULTS "*count: 1 \n" \
52 "*wireframe: False \n"
53 # include "xlockmore.h" /* from the xscreensaver distribution */
54 #else /* !STANDALONE */
55 # include "xlock.h" /* from the xlockmore distribution */
56 #endif /* !STANDALONE */
60 ModeSpecOpt gears_opts =
61 {0, NULL, 0, NULL, NULL};
64 ModStruct gears_description =
65 {"gears", "init_gears", "draw_gears", "release_gears",
66 "draw_gears", "init_gears", NULL, &gears_opts,
67 1000, 1, 2, 1, 4, 1.0, "",
68 "Shows GL's gears", 0, NULL};
73 GLfloat view_rotx, view_roty, view_rotz;
74 GLuint gear1, gear2, gear3;
76 GLXContext *glx_context;
83 static gearsstruct *gears = NULL;
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
94 * wire - true for wireframe mode
97 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
98 GLint teeth, GLfloat tooth_depth, Bool wire)
106 r1 = outer_radius - tooth_depth / 2.0;
107 r2 = outer_radius + tooth_depth / 2.0;
109 da = 2.0 * M_PI / teeth / 4.0;
111 glShadeModel(GL_FLAT);
113 /* This subroutine got kind of messy when I added all the checks
114 * for wireframe mode. A much cleaner solution that I sometimes
115 * use is to have a variable hold the value GL_LINE_LOOP when
116 * in wireframe mode, or hold the value GL_POLYGON otherwise.
117 * Then I just call glBegin(that_variable), give my polygon
118 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
119 * integrate that trick here.
124 glNormal3f(0.0, 0.0, 1.0);
126 /* draw front face */
128 glBegin(GL_QUAD_STRIP);
129 for (i = 0; i <= teeth; i++) {
132 angle = i * 2.0 * M_PI / teeth;
133 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
134 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
136 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
137 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
139 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
140 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
147 /* draw front sides of teeth */
150 da = 2.0 * M_PI / teeth / 4.0;
151 for (i = 0; i < teeth; i++) {
152 angle = i * 2.0 * M_PI / teeth;
155 glBegin(GL_LINE_LOOP);
156 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
157 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
158 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
159 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
168 glNormal3f(0.0, 0.0, -1.0);
172 glBegin(GL_QUAD_STRIP);
173 for (i = 0; i <= teeth; i++) {
174 angle = i * 2.0 * M_PI / teeth;
177 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
178 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
180 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
181 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
183 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
184 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
191 /* draw back sides of teeth */
194 da = 2.0 * M_PI / teeth / 4.0;
195 for (i = 0; i < teeth; i++) {
196 angle = i * 2.0 * M_PI / teeth;
199 glBegin(GL_LINE_LOOP);
200 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
201 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
202 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
203 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
211 /* draw outward faces of teeth */
213 glBegin(GL_QUAD_STRIP);
214 for (i = 0; i < teeth; i++) {
215 angle = i * 2.0 * M_PI / teeth;
219 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
220 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
221 u = r2 * cos(angle + da) - r1 * cos(angle);
222 v = r2 * sin(angle + da) - r1 * sin(angle);
223 len = sqrt(u * u + v * v);
226 glNormal3f(v, -u, 0.0);
227 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
228 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
229 glNormal3f(cos(angle), sin(angle), 0.0);
230 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
231 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
232 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
233 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
234 glNormal3f(v, -u, 0.0);
235 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
236 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
237 glNormal3f(cos(angle), sin(angle), 0.0);
243 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
244 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
248 glShadeModel(GL_SMOOTH);
250 /* draw inside radius cylinder */
252 glBegin(GL_QUAD_STRIP);
253 for (i = 0; i <= teeth; i++) {
254 angle = i * 2.0 * M_PI / teeth;
257 glNormal3f(-cos(angle), -sin(angle), 0.0);
258 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
259 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
261 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
262 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), -width * 0.5);
263 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
264 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
276 gearsstruct *gp = &gears[MI_SCREEN(mi)];
277 int wire = MI_IS_WIREFRAME(mi);
280 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
282 glClear(GL_COLOR_BUFFER_BIT);
286 glRotatef(gp->view_rotx, 1.0, 0.0, 0.0);
287 glRotatef(gp->view_roty, 0.0, 1.0, 0.0);
288 glRotatef(gp->view_rotz, 0.0, 0.0, 1.0);
291 glTranslatef(-3.0, -2.0, 0.0);
292 glRotatef(gp->angle, 0.0, 0.0, 1.0);
293 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
294 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
295 glCallList(gp->gear1);
299 glTranslatef(3.1, -2.0, 0.0);
300 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
301 glCallList(gp->gear2);
305 glTranslatef(-3.1, 4.2, 0.0);
306 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
307 glCallList(gp->gear3);
315 /* new window size or exposure */
317 reshape(int width, int height)
319 GLfloat h = (GLfloat) height / (GLfloat) width;
321 glViewport(0, 0, (GLint) width, (GLint) height);
322 glMatrixMode(GL_PROJECTION);
324 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
325 glMatrixMode(GL_MODELVIEW);
327 glTranslatef(0.0, 0.0, -40.0);
329 /* The depth buffer will be cleared, if needed, before the
330 * next frame. Right now we just want to black the screen.
332 glClear(GL_COLOR_BUFFER_BIT);
340 gearsstruct *gp = &gears[MI_SCREEN(mi)];
341 static GLfloat pos[4] =
342 {5.0, 5.0, 10.0, 1.0};
343 static GLfloat red[4] =
344 {0.8, 0.1, 0.0, 1.0};
345 static GLfloat green[4] =
346 {0.0, 0.8, 0.2, 1.0};
347 static GLfloat blue[4] =
348 {0.2, 0.2, 1.0, 1.0};
349 static GLfloat gray[4] =
350 {0.5, 0.5, 0.5, 1.0};
351 static GLfloat white[4] =
352 {1.0, 1.0, 1.0, 1.0};
353 int wire = MI_IS_WIREFRAME(mi);
354 int mono = MI_IS_MONO(mi);
357 glLightfv(GL_LIGHT0, GL_POSITION, pos);
358 glEnable(GL_CULL_FACE);
359 glEnable(GL_LIGHTING);
361 glEnable(GL_DEPTH_TEST);
365 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
366 * 2nd time mode is run it is Grayscale on PseudoColor.
367 * The code below forces monochrome on TrueColor.
369 if (MI_IS_MONO(mi)) {
370 red[0] = red[1] = red[2] = 1.0;
371 green[0] = green[1] = green[2] = 1.0;
372 blue[0] = blue[1] = blue[2] = 1.0;
377 gp->gear1 = glGenLists(1);
378 glNewList(gp->gear1, GL_COMPILE);
386 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
388 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
390 gear(1.0, 4.0, 1.0, 20, 0.7, wire);
393 gp->gear2 = glGenLists(1);
394 glNewList(gp->gear2, GL_COMPILE);
402 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
404 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
406 gear(0.5, 2.0, 2.0, 10, 0.7, wire);
409 gp->gear3 = glGenLists(1);
410 glNewList(gp->gear3, GL_COMPILE);
418 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
420 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
422 gear(1.3, 2.0, 0.5, 10, 0.7, wire);
425 glEnable(GL_NORMALIZE);
429 init_gears(ModeInfo * mi)
431 int screen = MI_SCREEN(mi);
434 /* Boolean rgba, doublebuffer, cmap_installed; */
438 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
439 sizeof (gearsstruct))) == NULL)
444 gp->window = MI_WINDOW(mi);
445 gp->view_rotx = NRAND(360);
446 gp->view_roty = NRAND(360);
447 gp->view_rotz = NRAND(360);
448 gp->angle = NRAND(360);
450 if ((gp->glx_context = init_GL(mi)) != NULL) {
451 reshape(MI_WIDTH(mi), MI_HEIGHT(mi));
459 draw_gears(ModeInfo * mi)
461 gearsstruct *gp = &gears[MI_SCREEN(mi)];
462 Display *display = MI_DISPLAY(mi);
463 Window window = MI_WINDOW(mi);
464 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
465 int rot_incr = MI_COUNT(mi) ? MI_COUNT(mi) : 1;
467 if (!gp->glx_context)
470 glDrawBuffer(GL_BACK);
472 glXMakeCurrent(display, window, *(gp->glx_context));
475 /* let's do something so we don't get bored */
476 gp->angle = (int) (gp->angle + angle_incr) % 360;
477 gp->view_rotx = (int) (gp->view_rotx + rot_incr) % 360;
478 gp->view_roty = (int) (gp->view_roty + rot_incr) % 360;
479 gp->view_rotz = (int) (gp->view_rotz + rot_incr) % 360;
482 glXSwapBuffers(display, window);
486 release_gears(ModeInfo * mi)
491 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
492 gearsstruct *gp = &gears[screen];
494 if (gp->glx_context) {
495 /* Display lists MUST be freed while their glXContext is current. */
496 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
498 if (glIsList(gp->gear1))
499 glDeleteLists(gp->gear1, 1);
500 if (glIsList(gp->gear2))
501 glDeleteLists(gp->gear2, 1);
502 if (glIsList(gp->gear3))
503 glDeleteLists(gp->gear3, 1);
507 (void) free((void *) gears);
514 /*********************************************************/