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 * 09-Feb-01: "Planetary" gear system added by jwz@jwz.org.
24 * 10-May-97: Compatible with xscreensaver
25 * 22-Mar-97: Added support for -mono mode, and monochrome X servers.
26 * Ed Mackey, emackey@netaxs.com
27 * 13-Mar-97: Memory leak fix by Tom Schmidt <tschmidt@micron.com>
28 * 1996: "written" by Danny Sung <dannys@ucla.edu>
29 * Based on 3-D gear wheels by Brian Paul which is in the public domain.
33 * PURIFY 3.0a on SunOS4 reports an unitialized memory read on each of
34 * the glCallList() functions below when using MesaGL 2.1. This has
35 * been fixed in MesaGL 2.2 and later releases.
39 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
40 * otherwise caddr_t is not defined correctly
43 #include <X11/Intrinsic.h>
46 # define PROGCLASS "Gears"
47 # define HACK_INIT init_gears
48 # define HACK_DRAW draw_gears
49 # define HACK_RESHAPE reshape_gears
50 # define gears_opts xlockmore_opts
51 # define DEFAULTS "*count: 1 \n" \
54 "*planetary: False \n" \
55 "*showFPS: False \n" \
56 "*wireframe: False \n"
57 # include "xlockmore.h" /* from the xscreensaver distribution */
58 #else /* !STANDALONE */
59 # include "xlock.h" /* from the xlockmore distribution */
60 #endif /* !STANDALONE */
65 #define countof(x) (sizeof((x))/sizeof((*x)))
67 #define DEF_PLANETARY "False"
71 static XrmOptionDescRec opts[] = {
72 {"-planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "true" },
73 {"+planetary", ".gears.planetary", XrmoptionNoArg, (caddr_t) "false" },
76 static argtype vars[] = {
77 {(caddr_t *) &planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
80 ModeSpecOpt gears_opts = {countof(opts), opts, countof(vars), vars, NULL};
83 ModStruct gears_description =
84 {"gears", "init_gears", "draw_gears", "release_gears",
85 "draw_gears", "init_gears", NULL, &gears_opts,
86 1000, 1, 2, 1, 4, 1.0, "",
87 "Shows GL's gears", 0, NULL};
93 GLfloat rotx, roty, rotz; /* current object rotation */
94 GLfloat dx, dy, dz; /* current rotational velocity */
95 GLfloat ddx, ddy, ddz; /* current rotational acceleration */
96 GLfloat d_max; /* max velocity */
98 GLuint gear1, gear2, gear3;
99 GLuint gear_inner, gear_outer;
102 GLXContext *glx_context;
106 static gearsstruct *gears = NULL;
109 * Draw a gear wheel. You'll probably want to call this function when
110 * building a display list since we do a lot of trig here.
112 * Input: inner_radius - radius of hole at center
113 * outer_radius - radius at center of teeth
114 * width - width of gear
115 * teeth - number of teeth
116 * tooth_depth - depth of tooth
117 * wire - true for wireframe mode
120 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
121 GLint teeth, GLfloat tooth_depth, Bool wire, Bool invert)
131 r1 = outer_radius - tooth_depth / 2.0;
132 r2 = outer_radius + tooth_depth / 2.0;
138 r2 = inner_radius + tooth_depth / 2.0;
139 r1 = outer_radius - tooth_depth / 2.0;
143 da = 2.0 * M_PI / teeth / 4.0;
145 glShadeModel(GL_FLAT);
147 /* This subroutine got kind of messy when I added all the checks
148 * for wireframe mode. A much cleaner solution that I sometimes
149 * use is to have a variable hold the value GL_LINE_LOOP when
150 * in wireframe mode, or hold the value GL_POLYGON otherwise.
151 * Then I just call glBegin(that_variable), give my polygon
152 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
153 * integrate that trick here.
158 glNormal3f(0.0, 0.0, 1.0);
160 /* draw front face */
162 glBegin(GL_QUAD_STRIP);
163 for (i = 0; i <= teeth; i++) {
166 angle = i * 2.0 * M_PI / teeth;
167 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
168 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
170 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
171 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
173 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
174 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
181 /* draw front sides of teeth */
184 da = 2.0 * M_PI / teeth / 4.0;
185 for (i = 0; i < teeth; i++) {
186 angle = i * 2.0 * M_PI / teeth;
189 glBegin(GL_LINE_LOOP);
190 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
191 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
192 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
193 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
202 glNormal3f(0.0, 0.0, -1.0);
206 glBegin(GL_QUAD_STRIP);
207 for (i = 0; i <= teeth; i++) {
208 angle = i * 2.0 * M_PI / teeth;
211 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
212 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
214 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
215 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
217 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
218 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
225 /* draw back sides of teeth */
228 da = 2.0 * M_PI / teeth / 4.0;
229 for (i = 0; i < teeth; i++) {
230 angle = i * 2.0 * M_PI / teeth;
233 glBegin(GL_LINE_LOOP);
234 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
235 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
236 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
237 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
245 /* draw outward faces of teeth */
247 glBegin(GL_QUAD_STRIP);
248 for (i = 0; i <= teeth; i++) {
249 angle = i * 2.0 * M_PI / teeth;
252 u = r2 * cos(angle + da) - r1 * cos(angle);
253 v = r2 * sin(angle + da) - r1 * sin(angle);
255 u = r2 * cos(angle + da + M_PI/2) - r1 * cos(angle + M_PI/2);
256 v = r2 * sin(angle + da + M_PI/2) - r1 * sin(angle + M_PI/2);
259 len = sqrt(u * u + v * v);
262 glNormal3f(v, -u, 0.0);
266 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
267 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
269 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
270 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
273 glNormal3f(cos(angle), sin(angle), 0.0);
275 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
277 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
278 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
281 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
282 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
284 u = r1 * cos(angle + 3 * da + M_PI/2) - r2 * cos(angle + 2 * da + M_PI/2);
285 v = r1 * sin(angle + 3 * da + M_PI/2) - r2 * sin(angle + 2 * da + M_PI/2);
288 glNormal3f(v, -u, 0.0);
290 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
291 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
294 glNormal3f(cos(angle), sin(angle), 0.0);
296 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
303 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
304 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
308 glShadeModel(GL_SMOOTH);
310 /* draw inside radius cylinder */
312 glBegin(GL_QUAD_STRIP);
313 for (i = 0; i <= teeth; i++) {
314 angle = i * 2.0 * M_PI / teeth;
319 glNormal3f(-cos(angle), -sin(angle), 0.0);
321 glNormal3f(cos(angle), sin(angle), 0.0);
323 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
324 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
326 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
327 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), -width * 0.5);
328 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
329 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
341 tube(GLfloat radius, GLfloat width, GLint facets, Bool wire)
344 GLfloat da = 2.0 * M_PI / facets / 4.0;
348 /* draw bottom of tube */
350 glShadeModel(GL_FLAT);
354 glBegin(GL_TRIANGLE_FAN);
355 glVertex3f(0, 0, width * 0.5);
356 for (i = 0; i <= facets; i++) {
357 GLfloat angle = i * 2.0 * M_PI / facets;
358 glVertex3f(radius * cos(angle), radius * sin(angle), width * 0.5);
363 /* draw top of tube */
365 glShadeModel(GL_FLAT);
366 glNormal3f(0, 0, -1);
370 glBegin(GL_TRIANGLE_FAN);
371 glVertex3f(0, 0, -width * 0.5);
372 for (i = 0; i <= facets; i++) {
373 GLfloat angle = i * 2.0 * M_PI / facets;
374 glVertex3f(radius * cos(angle), radius * sin(angle), -width * 0.5);
379 /* draw side of tube */
381 glShadeModel(GL_SMOOTH);
384 glBegin(GL_QUAD_STRIP);
386 for (i = 0; i <= facets; i++) {
387 GLfloat angle = i * 2.0 * M_PI / facets;
392 glNormal3f(cos(angle), sin(angle), 0.0);
394 glVertex3f(radius * cos(angle), radius * sin(angle), -width * 0.5);
395 glVertex3f(radius * cos(angle), radius * sin(angle), width * 0.5);
398 glVertex3f(radius * cos(angle), radius * sin(angle), -width * 0.5);
399 glVertex3f(radius * cos(angle + 4 * da), radius * sin(angle + 4 * da), -width * 0.5);
400 glVertex3f(radius * cos(angle), radius * sin(angle), width * 0.5);
401 glVertex3f(radius * cos(angle + 4 * da), radius * sin(angle + 4 * da), width * 0.5);
415 GLfloat width1, GLfloat height1,
416 GLfloat width2, GLfloat height2,
419 glShadeModel(GL_FLAT);
421 #if 0 /* don't need these - they're embedded in other objects */
424 glNormal3f(-1, 0, 0);
425 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
426 glVertex3f(-length/2, -width1/2, -height1/2);
427 glVertex3f(-length/2, width1/2, -height1/2);
428 glVertex3f(-length/2, width1/2, height1/2);
429 glVertex3f(-length/2, -width1/2, height1/2);
435 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
436 glVertex3f(length/2, -width2/2, -height2/2);
437 glVertex3f(length/2, width2/2, -height2/2);
438 glVertex3f(length/2, width2/2, height2/2);
439 glVertex3f(length/2, -width2/2, height2/2);
445 glNormal3f(0, 0, -1);
446 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
447 glVertex3f(-length/2, -width1/2, -height1/2);
448 glVertex3f(-length/2, width1/2, -height1/2);
449 glVertex3f( length/2, width2/2, -height2/2);
450 glVertex3f( length/2, -width2/2, -height2/2);
456 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
457 glVertex3f(-length/2, -width1/2, height1/2);
458 glVertex3f(-length/2, width1/2, height1/2);
459 glVertex3f( length/2, width2/2, height2/2);
460 glVertex3f( length/2, -width2/2, height2/2);
465 glNormal3f(0, -1, 0);
466 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
467 glVertex3f(-length/2, -width1/2, -height1/2);
468 glVertex3f(-length/2, -width1/2, height1/2);
469 glVertex3f( length/2, -width2/2, height2/2);
470 glVertex3f( length/2, -width2/2, -height2/2);
476 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
477 glVertex3f(-length/2, width1/2, -height1/2);
478 glVertex3f(-length/2, width1/2, height1/2);
479 glVertex3f( length/2, width2/2, height2/2);
480 glVertex3f( length/2, width2/2, -height2/2);
490 gearsstruct *gp = &gears[MI_SCREEN(mi)];
491 int wire = MI_IS_WIREFRAME(mi);
494 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
496 glClear(GL_COLOR_BUFFER_BIT);
502 GLfloat x = gp->rotx;
503 GLfloat y = gp->roty;
504 GLfloat z = gp->rotz;
505 if (x < 0) x = 1 - (x + 1);
506 if (y < 0) y = 1 - (y + 1);
507 if (z < 0) z = 1 - (z + 1);
508 glRotatef(x * 360, 1.0, 0.0, 0.0);
509 glRotatef(y * 360, 0.0, 1.0, 0.0);
510 glRotatef(z * 360, 0.0, 0.0, 1.0);
515 glTranslatef(-3.0, -2.0, 0.0);
516 glRotatef(gp->angle, 0.0, 0.0, 1.0);
517 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
518 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
519 glCallList(gp->gear1);
523 glTranslatef(3.1, -2.0, 0.0);
524 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
525 glCallList(gp->gear2);
529 glTranslatef(-3.1, 4.2, 0.0);
530 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
531 glCallList(gp->gear3);
534 } else { /* planetary */
536 glScalef(0.8, 0.8, 0.8);
539 glTranslatef(0.0, 4.2, 0.0);
540 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
541 glCallList(gp->gear1);
545 glRotatef(120, 0.0, 0.0, 1.0);
546 glTranslatef(0.0, 4.2, 0.0);
547 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
548 glCallList(gp->gear2);
552 glRotatef(240, 0.0, 0.0, 1.0);
553 glTranslatef(0.0, 4.2, 0.0);
554 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
555 glCallList(gp->gear3);
559 glTranslatef(0.0, 0.0, 0.0);
560 glRotatef(-gp->angle, 0.0, 0.0, 1.0);
561 glCallList(gp->gear_inner);
565 glTranslatef(0.0, 0.0, 0.0);
566 glRotatef((gp->angle / 3.0) - 7.5, 0.0, 0.0, 1.0);
567 glCallList(gp->gear_outer);
571 glTranslatef(0.0, 0.0, 0.0);
572 glCallList(gp->armature);
581 /* new window size or exposure */
583 reshape_gears(ModeInfo *mi, int width, int height)
585 GLfloat h = (GLfloat) height / (GLfloat) width;
587 glViewport(0, 0, (GLint) width, (GLint) height);
588 glMatrixMode(GL_PROJECTION);
590 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
591 glMatrixMode(GL_MODELVIEW);
593 glTranslatef(0.0, 0.0, -40.0);
595 /* The depth buffer will be cleared, if needed, before the
596 * next frame. Right now we just want to black the screen.
598 glClear(GL_COLOR_BUFFER_BIT);
606 gearsstruct *gp = &gears[MI_SCREEN(mi)];
607 static GLfloat pos[4] =
608 {5.0, 5.0, 10.0, 1.0};
609 static GLfloat red[4] =
610 {0.8, 0.1, 0.0, 1.0};
611 static GLfloat green[4] =
612 {0.0, 0.8, 0.2, 1.0};
613 static GLfloat blue[4] =
614 {0.2, 0.2, 1.0, 1.0};
615 static GLfloat gray[4] =
616 {0.5, 0.5, 0.5, 1.0};
617 static GLfloat white[4] =
618 {1.0, 1.0, 1.0, 1.0};
619 int wire = MI_IS_WIREFRAME(mi);
620 int mono = MI_IS_MONO(mi);
623 glLightfv(GL_LIGHT0, GL_POSITION, pos);
624 glEnable(GL_CULL_FACE);
625 glEnable(GL_LIGHTING);
627 glEnable(GL_DEPTH_TEST);
631 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
632 * 2nd time mode is run it is Grayscale on PseudoColor.
633 * The code below forces monochrome on TrueColor.
635 if (MI_IS_MONO(mi)) {
636 red[0] = red[1] = red[2] = 1.0;
637 green[0] = green[1] = green[2] = 1.0;
638 blue[0] = blue[1] = blue[2] = 1.0;
646 gp->gear1 = glGenLists(1);
647 glNewList(gp->gear1, GL_COMPILE);
655 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
657 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
660 gear(1.0, 4.0, 1.0, 20, 0.7, wire, False);
663 gp->gear2 = glGenLists(1);
664 glNewList(gp->gear2, GL_COMPILE);
672 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
674 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
676 gear(0.5, 2.0, 2.0, 10, 0.7, wire, False);
679 gp->gear3 = glGenLists(1);
680 glNewList(gp->gear3, GL_COMPILE);
688 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
690 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
692 gear(1.3, 2.0, 0.5, 10, 0.7, wire, False);
695 glEnable(GL_NORMALIZE);
697 } else { /* planetary */
699 gp->gear1 = glGenLists(1);
700 glNewList(gp->gear1, GL_COMPILE);
708 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
710 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
712 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
715 gp->gear2 = glGenLists(1);
716 glNewList(gp->gear2, GL_COMPILE);
724 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
726 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
728 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
731 gp->gear3 = glGenLists(1);
732 glNewList(gp->gear3, GL_COMPILE);
740 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
742 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
744 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
747 glEnable(GL_NORMALIZE);
750 gp->gear_inner = glGenLists(1);
751 glNewList(gp->gear_inner, GL_COMPILE);
759 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
761 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
763 gear(1.0, 2.0, 2.0, 12, 0.7, wire, False);
766 glEnable(GL_NORMALIZE);
769 gp->gear_outer = glGenLists(1);
770 glNewList(gp->gear_outer, GL_COMPILE);
778 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
780 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
782 gear(5.7, 7.0, 2.0, 36, 0.7, wire, True);
784 /* put some nubs on the outer ring, so we can tell how it's moving */
786 glTranslatef(7.0, 0, 0);
787 glRotatef(90, 0, 1, 0);
788 tube(0.5, 0.5, 10, wire); /* nub 1 */
792 glRotatef(120, 0, 0, 1);
793 glTranslatef(7.0, 0, 0);
794 glRotatef(90, 0, 1, 0);
795 tube(0.5, 0.5, 10, wire); /* nub 2 */
799 glRotatef(240, 0, 0, 1);
800 glTranslatef(7.0, 0, 0);
801 glRotatef(90, 0, 1, 0);
802 tube(0.5, 0.5, 10, wire); /* nub 3 */
808 glEnable(GL_NORMALIZE);
810 gp->armature = glGenLists(1);
811 glNewList(gp->armature, GL_COMPILE);
819 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
821 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
824 glTranslatef(0, 0, 1.5);
826 tube(0.5, 10, 15, wire); /* center axle */
829 glTranslatef(0.0, 4.2, -1);
830 tube(0.5, 3, 15, wire); /* axle 1 */
831 glTranslatef(0, 0, 1.8);
832 tube(0.7, 0.7, 15, wire);
836 glRotatef(120, 0.0, 0.0, 1.0);
837 glTranslatef(0.0, 4.2, -1);
838 tube(0.5, 3, 15, wire); /* axle 2 */
839 glTranslatef(0, 0, 1.8);
840 tube(0.7, 0.7, 15, wire);
844 glRotatef(240, 0.0, 0.0, 1.0);
845 glTranslatef(0.0, 4.2, -1);
846 tube(0.5, 3, 15, wire); /* axle 3 */
847 glTranslatef(0, 0, 1.8);
848 tube(0.7, 0.7, 15, wire);
851 glTranslatef(0, 0, 1.5); /* center disk */
852 tube(1.5, 2, 20, wire);
855 glRotatef(270, 0, 0, 1);
856 glRotatef(-10, 0, 1, 0);
857 glTranslatef(-2.2, 0, 0);
858 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 1 */
862 glRotatef(30, 0, 0, 1);
863 glRotatef(-10, 0, 1, 0);
864 glTranslatef(-2.2, 0, 0);
865 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 2 */
869 glRotatef(150, 0, 0, 1);
870 glRotatef(-10, 0, 1, 0);
871 glTranslatef(-2.2, 0, 0);
872 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 3 */
877 glEnable(GL_NORMALIZE);
882 /* lifted from lament.c */
883 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
884 #define RANDSIGN() ((random() & 1) ? 1 : -1)
887 rotate(GLfloat *pos, GLfloat *v, GLfloat *dv, GLfloat max_v)
902 if (ppos < 0) abort();
903 if (ppos > 1.0) abort();
904 *pos = (*pos > 0 ? ppos : -ppos);
910 if (*v > max_v || *v < -max_v)
914 /* If it stops, start it going in the other direction. */
921 /* keep going in the same direction */
936 /* Alter direction of rotational acceleration randomly. */
937 if (! (random() % 120))
940 /* Change acceleration very occasionally. */
941 if (! (random() % 200))
945 else if (random() & 1)
954 init_gears(ModeInfo * mi)
956 int screen = MI_SCREEN(mi);
959 /* Boolean rgba, doublebuffer, cmap_installed; */
963 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
964 sizeof (gearsstruct))) == NULL)
969 gp->window = MI_WINDOW(mi);
971 gp->rotx = frand(1.0) * RANDSIGN();
972 gp->roty = frand(1.0) * RANDSIGN();
973 gp->rotz = frand(1.0) * RANDSIGN();
975 /* bell curve from 0-1.5 degrees, avg 0.75 */
976 gp->dx = (frand(1) + frand(1) + frand(1)) / (360*2);
977 gp->dy = (frand(1) + frand(1) + frand(1)) / (360*2);
978 gp->dz = (frand(1) + frand(1) + frand(1)) / (360*2);
980 gp->d_max = gp->dx * 2;
982 gp->ddx = 0.00006 + frand(0.00003);
983 gp->ddy = 0.00006 + frand(0.00003);
984 gp->ddz = 0.00006 + frand(0.00003);
990 if ((gp->glx_context = init_GL(mi)) != NULL) {
991 reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
999 draw_gears(ModeInfo * mi)
1001 gearsstruct *gp = &gears[MI_SCREEN(mi)];
1002 Display *display = MI_DISPLAY(mi);
1003 Window window = MI_WINDOW(mi);
1004 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
1009 if (!gp->glx_context)
1012 glDrawBuffer(GL_BACK);
1014 glXMakeCurrent(display, window, *(gp->glx_context));
1017 /* let's do something so we don't get bored */
1018 gp->angle = (int) (gp->angle + angle_incr) % 360;
1020 rotate(&gp->rotx, &gp->dx, &gp->ddx, gp->d_max);
1021 rotate(&gp->roty, &gp->dy, &gp->ddy, gp->d_max);
1022 rotate(&gp->rotz, &gp->dz, &gp->ddz, gp->d_max);
1024 if (mi->fps_p) do_fps (mi);
1026 glXSwapBuffers(display, window);
1030 release_gears(ModeInfo * mi)
1032 if (gears != NULL) {
1035 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1036 gearsstruct *gp = &gears[screen];
1038 if (gp->glx_context) {
1039 /* Display lists MUST be freed while their glXContext is current. */
1040 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1042 if (glIsList(gp->gear1))
1043 glDeleteLists(gp->gear1, 1);
1044 if (glIsList(gp->gear2))
1045 glDeleteLists(gp->gear2, 1);
1046 if (glIsList(gp->gear3))
1047 glDeleteLists(gp->gear3, 1);
1048 if (glIsList(gp->gear_inner))
1049 glDeleteLists(gp->gear_inner, 1);
1050 if (glIsList(gp->gear_outer))
1051 glDeleteLists(gp->gear_outer, 1);
1055 (void) free((void *) gears);
1062 /*********************************************************/