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.
34 * PURIFY 3.0a on SunOS4 reports an unitialized memory read on each of
35 * the glCallList() functions below when using MesaGL 2.1. This has
36 * been fixed in MesaGL 2.2 and later releases.
40 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
41 * otherwise caddr_t is not defined correctly
44 #include <X11/Intrinsic.h>
47 # define PROGCLASS "Gears"
48 # define HACK_INIT init_gears
49 # define HACK_DRAW draw_gears
50 # define gears_opts xlockmore_opts
51 # define DEFAULTS "*count: 1 \n" \
54 "*wireframe: False \n"
55 # include "xlockmore.h" /* from the xscreensaver distribution */
56 #else /* !STANDALONE */
57 # include "xlock.h" /* from the xlockmore distribution */
58 #endif /* !STANDALONE */
62 ModeSpecOpt gears_opts = {
63 0, NULL, 0, NULL, NULL };
66 ModStruct gears_description =
67 {"gears", "init_gears", "draw_gears", "release_gears",
68 "draw_gears", "init_gears", NULL, &gears_opts,
69 1000, 1, 2, 1, 1.0, "",
70 "Shows GL's gears", 0, NULL};
75 GLfloat view_rotx, view_roty, view_rotz;
76 GLuint gear1, gear2, gear3;
78 GLXContext *glx_context;
85 static gearsstruct *gears = NULL;
88 * Draw a gear wheel. You'll probably want to call this function when
89 * building a display list since we do a lot of trig here.
91 * Input: inner_radius - radius of hole at center
92 * outer_radius - radius at center of teeth
93 * width - width of gear
94 * teeth - number of teeth
95 * tooth_depth - depth of tooth
96 * wire - true for wireframe mode
99 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
100 GLint teeth, GLfloat tooth_depth, Bool wire)
108 r1 = outer_radius - tooth_depth / 2.0;
109 r2 = outer_radius + tooth_depth / 2.0;
111 da = 2.0 * M_PI / teeth / 4.0;
113 glShadeModel(GL_FLAT);
115 /* This subroutine got kind of messy when I added all the checks
116 * for wireframe mode. A much cleaner solution that I sometimes
117 * use is to have a variable hold the value GL_LINE_LOOP when
118 * in wireframe mode, or hold the value GL_POLYGON otherwise.
119 * Then I just call glBegin(that_variable), give my polygon
120 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
121 * integrate that trick here.
126 glNormal3f(0.0, 0.0, 1.0);
128 /* draw front face */
130 glBegin(GL_QUAD_STRIP);
131 for (i = 0; i <= teeth; i++) {
134 angle = i * 2.0 * M_PI / teeth;
135 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
136 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
138 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
139 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
141 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
142 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
149 /* draw front sides of teeth */
152 da = 2.0 * M_PI / teeth / 4.0;
153 for (i = 0; i < teeth; i++) {
154 angle = i * 2.0 * M_PI / teeth;
157 glBegin(GL_LINE_LOOP);
158 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
159 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
160 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
161 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
170 glNormal3f(0.0, 0.0, -1.0);
174 glBegin(GL_QUAD_STRIP);
175 for (i = 0; i <= teeth; i++) {
176 angle = i * 2.0 * M_PI / teeth;
179 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
180 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
182 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
183 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
185 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
186 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
193 /* draw back sides of teeth */
196 da = 2.0 * M_PI / teeth / 4.0;
197 for (i = 0; i < teeth; i++) {
198 angle = i * 2.0 * M_PI / teeth;
201 glBegin(GL_LINE_LOOP);
202 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
203 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
204 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
205 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
213 /* draw outward faces of teeth */
215 glBegin(GL_QUAD_STRIP);
216 for (i = 0; i < teeth; i++) {
217 angle = i * 2.0 * M_PI / teeth;
221 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
222 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
223 u = r2 * cos(angle + da) - r1 * cos(angle);
224 v = r2 * sin(angle + da) - r1 * sin(angle);
225 len = sqrt(u * u + v * v);
228 glNormal3f(v, -u, 0.0);
229 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
230 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
231 glNormal3f(cos(angle), sin(angle), 0.0);
232 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
233 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
234 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
235 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
236 glNormal3f(v, -u, 0.0);
237 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
238 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
239 glNormal3f(cos(angle), sin(angle), 0.0);
245 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
246 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
250 glShadeModel(GL_SMOOTH);
252 /* draw inside radius cylinder */
254 glBegin(GL_QUAD_STRIP);
255 for (i = 0; i <= teeth; i++) {
256 angle = i * 2.0 * M_PI / teeth;
259 glNormal3f(-cos(angle), -sin(angle), 0.0);
260 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
261 glVertex3f(r0 * cos(angle), r0 * sin(angle), 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);
265 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
266 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
278 gearsstruct *gp = &gears[MI_SCREEN(mi)];
279 int wire = MI_WIN_IS_WIREFRAME(mi);
282 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
284 glClear(GL_COLOR_BUFFER_BIT);
288 glRotatef(gp->view_rotx, 1.0, 0.0, 0.0);
289 glRotatef(gp->view_roty, 0.0, 1.0, 0.0);
290 glRotatef(gp->view_rotz, 0.0, 0.0, 1.0);
293 glTranslatef(-3.0, -2.0, 0.0);
294 glRotatef(gp->angle, 0.0, 0.0, 1.0);
295 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
296 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
297 glCallList(gp->gear1);
301 glTranslatef(3.1, -2.0, 0.0);
302 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
303 glCallList(gp->gear2);
307 glTranslatef(-3.1, 4.2, 0.0);
308 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
309 glCallList(gp->gear3);
317 /* new window size or exposure */
319 reshape(int width, int height)
321 GLfloat h = (GLfloat) height / (GLfloat) width;
323 glViewport(0, 0, (GLint) width, (GLint) height);
324 glMatrixMode(GL_PROJECTION);
326 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
327 glMatrixMode(GL_MODELVIEW);
329 glTranslatef(0.0, 0.0, -40.0);
331 /* The depth buffer will be cleared, if needed, before the
332 * next frame. Right now we just want to black the screen.
334 glClear(GL_COLOR_BUFFER_BIT);
342 gearsstruct *gp = &gears[MI_SCREEN(mi)];
343 static GLfloat pos[4] =
344 {5.0, 5.0, 10.0, 1.0};
345 static GLfloat red[4] =
346 {0.8, 0.1, 0.0, 1.0};
347 static GLfloat green[4] =
348 {0.0, 0.8, 0.2, 1.0};
349 static GLfloat blue[4] =
350 {0.2, 0.2, 1.0, 1.0};
351 static GLfloat gray[4] =
352 {0.5, 0.5, 0.5, 1.0};
353 static GLfloat white[4] =
354 {1.0, 1.0, 1.0, 1.0};
355 int wire = MI_WIN_IS_WIREFRAME(mi);
356 int mono = MI_WIN_IS_MONO(mi);
359 glLightfv(GL_LIGHT0, GL_POSITION, pos);
360 glEnable(GL_CULL_FACE);
361 glEnable(GL_LIGHTING);
363 glEnable(GL_DEPTH_TEST);
367 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
368 * 2nd time mode is run it is Grayscale on PseudoColor.
369 * The code below forces monochrome on TrueColor.
371 if (MI_WIN_IS_MONO(mi)) {
372 red[0] = red[1] = red[2] = 1.0;
373 green[0] = green[1] = green[2] = 1.0;
374 blue[0] = blue[1] = blue[2] = 1.0;
379 gp->gear1 = glGenLists(1);
380 glNewList(gp->gear1, GL_COMPILE);
388 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
390 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
392 gear(1.0, 4.0, 1.0, 20, 0.7, wire);
395 gp->gear2 = glGenLists(1);
396 glNewList(gp->gear2, GL_COMPILE);
404 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
406 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
408 gear(0.5, 2.0, 2.0, 10, 0.7, wire);
411 gp->gear3 = glGenLists(1);
412 glNewList(gp->gear3, GL_COMPILE);
420 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
422 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
424 gear(1.3, 2.0, 0.5, 10, 0.7, wire);
427 glEnable(GL_NORMALIZE);
431 init_gears(ModeInfo * mi)
433 int screen = MI_SCREEN(mi);
436 /* Boolean rgba, doublebuffer, cmap_installed; */
440 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
441 sizeof (gearsstruct))) == NULL)
446 gp->window = MI_WINDOW(mi);
447 gp->view_rotx = NRAND(360);
448 gp->view_roty = NRAND(360);
449 gp->view_rotz = NRAND(360);
450 gp->angle = NRAND(360);
452 if ((gp->glx_context = init_GL(mi)) != NULL) {
453 reshape(MI_WIN_WIDTH(mi), MI_WIN_HEIGHT(mi));
461 draw_gears(ModeInfo * mi)
463 gearsstruct *gp = &gears[MI_SCREEN(mi)];
464 Display *display = MI_DISPLAY(mi);
465 Window window = MI_WINDOW(mi);
466 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
467 int rot_incr = MI_BATCHCOUNT(mi) ? MI_BATCHCOUNT(mi) : 1;
469 if (!gp->glx_context)
472 glDrawBuffer(GL_BACK);
474 glXMakeCurrent(display, window, *(gp->glx_context));
477 /* let's do something so we don't get bored */
478 gp->angle = (int) (gp->angle + angle_incr) % 360;
479 gp->view_rotx = (int) (gp->view_rotx + rot_incr) % 360;
480 gp->view_roty = (int) (gp->view_roty + rot_incr) % 360;
481 gp->view_rotz = (int) (gp->view_rotz + rot_incr) % 360;
484 glXSwapBuffers(display, window);
488 release_gears(ModeInfo * mi)
493 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
494 gearsstruct *gp = &gears[screen];
496 if (gp->glx_context) {
497 /* Display lists MUST be freed while their glXContext is current. */
498 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
500 if (glIsList(gp->gear1))
501 glDeleteLists(gp->gear1, 1);
502 if (glIsList(gp->gear2))
503 glDeleteLists(gp->gear2, 1);
504 if (glIsList(gp->gear3))
505 glDeleteLists(gp->gear3, 1);
509 (void) free((void *) gears);
516 /*********************************************************/