1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* gears --- 3D gear wheels */
5 static const char sccsid[] = "@(#)gears.c 4.07 97/11/24 xlockmore";
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
22 * 09-Feb-01: "Planetary" gear system added by jwz@jwz.org.
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 # define DEFAULTS "*count: 1000000 \n" \
41 "*showFPS: False \n" \
42 "*wireframe: False \n"
43 # include "xlockmore.h" /* from the xscreensaver distribution */
44 # define refresh_gears 0
45 #else /* !STANDALONE */
46 # include "xlock.h" /* from the xlockmore distribution */
47 #endif /* !STANDALONE */
52 #include "gltrackball.h"
55 #define countof(x) (sizeof((x))/sizeof((*x)))
57 #define DEF_MODE "random"
58 #define DEF_SPIN "True"
60 static char *mode_str;
63 static XrmOptionDescRec opts[] = {
64 {"-mode", ".gears.mode", XrmoptionSepArg, 0 },
65 {"-planetary", ".gears.mode", XrmoptionNoArg, "planetary" },
66 {"-simple", ".gears.mode", XrmoptionNoArg, "simple" },
67 {"-spin", ".gears.spin", XrmoptionNoArg, "true" },
68 {"+spin", ".gears.spin", XrmoptionNoArg, "false" },
71 static argtype vars[] = {
72 {&mode_str, "mode", "Mode", DEF_MODE, t_String},
73 {&spin, "spin", "Spin", DEF_SPIN, t_Bool},
76 ENTRYPOINT ModeSpecOpt gears_opts = {countof(opts), opts, countof(vars), vars, NULL};
79 ModStruct gears_description =
80 {"gears", "init_gears", "draw_gears", "release_gears",
81 "draw_gears", "init_gears", NULL, &gears_opts,
82 1000, 1, 2, 1, 4, 1.0, "",
83 "Shows GL's gears", 0, NULL};
87 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
90 # define TUBE_FACES 20 /* how densely to render tubes */
97 GLuint gear1, gear2, gear3;
98 GLuint gear_inner, gear_outer;
101 GLXContext *glx_context;
104 trackball_state *trackball;
109 static gearsstruct *gears = NULL;
112 * Draw a gear wheel. You'll probably want to call this function when
113 * building a display list since we do a lot of trig here.
115 * Input: inner_radius - radius of hole at center
116 * outer_radius - radius at center of teeth
117 * width - width of gear
118 * teeth - number of teeth
119 * tooth_depth - depth of tooth
120 * wire - true for wireframe mode
123 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
124 GLint teeth, GLfloat tooth_depth, Bool wire, Bool invert)
134 r1 = outer_radius - tooth_depth / 2.0;
135 r2 = outer_radius + tooth_depth / 2.0;
141 r2 = inner_radius + tooth_depth / 2.0;
142 r1 = outer_radius - tooth_depth / 2.0;
146 da = 2.0 * M_PI / teeth / 4.0;
148 glShadeModel(GL_FLAT);
150 /* This subroutine got kind of messy when I added all the checks
151 * for wireframe mode. A much cleaner solution that I sometimes
152 * use is to have a variable hold the value GL_LINE_LOOP when
153 * in wireframe mode, or hold the value GL_POLYGON otherwise.
154 * Then I just call glBegin(that_variable), give my polygon
155 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
156 * integrate that trick here.
161 glNormal3f(0.0, 0.0, 1.0);
163 /* draw front face */
165 glBegin(GL_QUAD_STRIP);
166 for (i = 0; i <= teeth; i++) {
169 angle = i * 2.0 * M_PI / teeth;
170 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
171 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
173 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
174 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
176 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
177 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
184 /* draw front sides of teeth */
187 da = 2.0 * M_PI / teeth / 4.0;
188 for (i = 0; i < teeth; i++) {
189 angle = i * 2.0 * M_PI / teeth;
192 glBegin(GL_LINE_LOOP);
193 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
194 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
195 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
196 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
205 glNormal3f(0.0, 0.0, -1.0);
209 glBegin(GL_QUAD_STRIP);
210 for (i = 0; i <= teeth; i++) {
211 angle = i * 2.0 * M_PI / teeth;
214 glVertex3f(r1 * cos(angle), r1 * sin(angle), -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(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
220 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
221 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
228 /* draw back sides of teeth */
231 da = 2.0 * M_PI / teeth / 4.0;
232 for (i = 0; i < teeth; i++) {
233 angle = i * 2.0 * M_PI / teeth;
236 glBegin(GL_LINE_LOOP);
237 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
238 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
239 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
240 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
248 /* draw outward faces of teeth */
250 glBegin(GL_QUAD_STRIP);
251 for (i = 0; i <= teeth; i++) {
252 angle = i * 2.0 * M_PI / teeth;
255 u = r2 * cos(angle + da) - r1 * cos(angle);
256 v = r2 * sin(angle + da) - r1 * sin(angle);
258 u = r2 * cos(angle + da + M_PI/2) - r1 * cos(angle + M_PI/2);
259 v = r2 * sin(angle + da + M_PI/2) - r1 * sin(angle + M_PI/2);
262 len = sqrt(u * u + v * v);
265 glNormal3f(v, -u, 0.0);
269 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
270 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
272 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
273 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
276 glNormal3f(cos(angle), sin(angle), 0.0);
278 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
280 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
281 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
284 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
285 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
287 u = r1 * cos(angle + 3 * da + M_PI/2) - r2 * cos(angle + 2 * da + M_PI/2);
288 v = r1 * sin(angle + 3 * da + M_PI/2) - r2 * sin(angle + 2 * da + M_PI/2);
291 glNormal3f(v, -u, 0.0);
293 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
294 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
297 glNormal3f(cos(angle), sin(angle), 0.0);
299 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
306 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
307 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
311 glShadeModel(GL_SMOOTH);
313 /* draw inside radius cylinder */
315 glBegin(GL_QUAD_STRIP);
316 for (i = 0; i <= teeth; i++) {
317 angle = i * 2.0 * M_PI / teeth;
322 glNormal3f(-cos(angle), -sin(angle), 0.0);
324 glNormal3f(cos(angle), sin(angle), 0.0);
326 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
327 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
329 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
330 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), -width * 0.5);
331 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
332 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
343 unit_tube (Bool wire)
346 int faces = TUBE_FACES;
347 GLfloat step = M_PI * 2 / faces;
356 glBegin(wire ? GL_LINES : GL_QUAD_STRIP);
358 glBegin(wire ? GL_LINES : GL_QUADS);
361 for (i = 0, th = 0; i <= faces; i++)
363 GLfloat x = cos (th);
364 GLfloat y = sin (th);
366 glVertex3f(x, 0.0, y);
367 glVertex3f(x, 1.0, y);
373 glVertex3f(x, 1.0, y);
374 glVertex3f(x, 0.0, y);
381 for (z = 0; z <= 1; z++)
383 glFrontFace(z == 0 ? GL_CCW : GL_CW);
384 glNormal3f(0, (z == 0 ? -1 : 1), 0);
385 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
386 if (! wire) glVertex3f(0, z, 0);
387 for (i = 0, th = 0; i <= faces; i++)
389 GLfloat x = cos (th);
390 GLfloat y = sin (th);
400 tube (GLfloat x1, GLfloat y1, GLfloat z1,
401 GLfloat x2, GLfloat y2, GLfloat z2,
402 GLfloat diameter, GLfloat cap_size,
405 GLfloat length, angle, a, b, c;
407 if (diameter <= 0) abort();
413 length = sqrt (a*a + b*b + c*c);
414 angle = acos (a / length);
417 glTranslatef(x1, y1, z1);
418 glScalef (length, length, length);
420 if (c == 0 && b == 0)
421 glRotatef (angle / (M_PI / 180), 0, 1, 0);
423 glRotatef (angle / (M_PI / 180), 0, -c, b);
425 glRotatef (-90, 0, 0, 1);
426 glScalef (diameter/length, 1, diameter/length);
428 /* extend the endpoints of the tube by the cap size in both directions */
431 GLfloat c = cap_size/length;
432 glTranslatef (0, -c, 0);
433 glScalef (1, 1+c+c, 1);
442 ctube (GLfloat diameter, GLfloat width, Bool wire)
451 GLfloat width1, GLfloat height1,
452 GLfloat width2, GLfloat height2,
455 glShadeModel(GL_FLAT);
457 #if 0 /* don't need these - they're embedded in other objects */
460 glNormal3f(-1, 0, 0);
461 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
462 glVertex3f(-length/2, -width1/2, -height1/2);
463 glVertex3f(-length/2, width1/2, -height1/2);
464 glVertex3f(-length/2, width1/2, height1/2);
465 glVertex3f(-length/2, -width1/2, height1/2);
471 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
472 glVertex3f(length/2, -width2/2, -height2/2);
473 glVertex3f(length/2, width2/2, -height2/2);
474 glVertex3f(length/2, width2/2, height2/2);
475 glVertex3f(length/2, -width2/2, height2/2);
481 glNormal3f(0, 0, -1);
482 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
483 glVertex3f(-length/2, -width1/2, -height1/2);
484 glVertex3f(-length/2, width1/2, -height1/2);
485 glVertex3f( length/2, width2/2, -height2/2);
486 glVertex3f( length/2, -width2/2, -height2/2);
492 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
493 glVertex3f(-length/2, -width1/2, height1/2);
494 glVertex3f(-length/2, width1/2, height1/2);
495 glVertex3f( length/2, width2/2, height2/2);
496 glVertex3f( length/2, -width2/2, height2/2);
501 glNormal3f(0, -1, 0);
502 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
503 glVertex3f(-length/2, -width1/2, -height1/2);
504 glVertex3f(-length/2, -width1/2, height1/2);
505 glVertex3f( length/2, -width2/2, height2/2);
506 glVertex3f( length/2, -width2/2, -height2/2);
512 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
513 glVertex3f(-length/2, width1/2, -height1/2);
514 glVertex3f(-length/2, width1/2, height1/2);
515 glVertex3f( length/2, width2/2, height2/2);
516 glVertex3f( length/2, width2/2, -height2/2);
526 gearsstruct *gp = &gears[MI_SCREEN(mi)];
527 int wire = MI_IS_WIREFRAME(mi);
530 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
532 glClear(GL_COLOR_BUFFER_BIT);
537 gltrackball_rotate (gp->trackball);
542 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
543 glRotatef (x * 360, 1.0, 0.0, 0.0);
544 glRotatef (y * 360, 0.0, 1.0, 0.0);
545 glRotatef (z * 360, 0.0, 0.0, 1.0);
548 if (!gp->planetary_p) {
550 glTranslatef(-3.0, -2.0, 0.0);
551 glRotatef(gp->angle, 0.0, 0.0, 1.0);
552 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
553 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
554 glCallList(gp->gear1);
558 glTranslatef(3.1, -2.0, 0.0);
559 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
560 glCallList(gp->gear2);
564 glTranslatef(-3.1, 4.2, 0.0);
565 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
566 glCallList(gp->gear3);
569 } else { /* gp->planetary_p */
571 glScalef(0.8, 0.8, 0.8);
574 glTranslatef(0.0, 4.2, 0.0);
575 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
576 glCallList(gp->gear1);
580 glRotatef(120, 0.0, 0.0, 1.0);
581 glTranslatef(0.0, 4.2, 0.0);
582 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
583 glCallList(gp->gear2);
587 glRotatef(240, 0.0, 0.0, 1.0);
588 glTranslatef(0.0, 4.2, 0.0);
589 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
590 glCallList(gp->gear3);
594 glTranslatef(0.0, 0.0, 0.0);
595 glRotatef(-gp->angle, 0.0, 0.0, 1.0);
596 glCallList(gp->gear_inner);
600 glTranslatef(0.0, 0.0, 0.0);
601 glRotatef((gp->angle / 3.0) - 7.5, 0.0, 0.0, 1.0);
602 glCallList(gp->gear_outer);
606 glTranslatef(0.0, 0.0, 0.0);
607 glCallList(gp->armature);
616 /* new window size or exposure */
618 reshape_gears (ModeInfo *mi, int width, int height)
620 GLfloat h = (GLfloat) height / (GLfloat) width;
622 glViewport(0, 0, (GLint) width, (GLint) height);
623 glMatrixMode(GL_PROJECTION);
625 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
626 glMatrixMode(GL_MODELVIEW);
628 glTranslatef(0.0, 0.0, -40.0);
630 /* The depth buffer will be cleared, if needed, before the
631 * next frame. Right now we just want to black the screen.
633 glClear(GL_COLOR_BUFFER_BIT);
641 gearsstruct *gp = &gears[MI_SCREEN(mi)];
642 static const GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
643 static const GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
644 static const GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
645 static const GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
646 static const GLfloat gray[4] = {0.5, 0.5, 0.5, 1.0};
647 static const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
648 int wire = MI_IS_WIREFRAME(mi);
649 int mono = MI_IS_MONO(mi);
652 glLightfv(GL_LIGHT0, GL_POSITION, pos);
653 glEnable(GL_CULL_FACE);
654 glEnable(GL_LIGHTING);
656 glEnable(GL_DEPTH_TEST);
660 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
661 * 2nd time mode is run it is Grayscale on PseudoColor.
662 * The code below forces monochrome on TrueColor.
664 if (MI_IS_MONO(mi)) {
665 red[0] = red[1] = red[2] = 1.0;
666 green[0] = green[1] = green[2] = 1.0;
667 blue[0] = blue[1] = blue[2] = 1.0;
673 if (! gp->planetary_p) {
675 gp->gear1 = glGenLists(1);
676 glNewList(gp->gear1, GL_COMPILE);
684 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
686 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
689 gear(1.0, 4.0, 1.0, 20, 0.7, wire, False);
692 gp->gear2 = glGenLists(1);
693 glNewList(gp->gear2, GL_COMPILE);
701 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
703 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
705 gear(0.5, 2.0, 2.0, 10, 0.7, wire, False);
708 gp->gear3 = glGenLists(1);
709 glNewList(gp->gear3, GL_COMPILE);
717 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
719 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
721 gear(1.3, 2.0, 0.5, 10, 0.7, wire, False);
724 glEnable(GL_NORMALIZE);
726 } else { /* gp->planetary_p */
728 gp->gear1 = glGenLists(1);
729 glNewList(gp->gear1, GL_COMPILE);
737 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
739 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
741 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
744 gp->gear2 = glGenLists(1);
745 glNewList(gp->gear2, GL_COMPILE);
753 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
755 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
757 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
760 gp->gear3 = glGenLists(1);
761 glNewList(gp->gear3, GL_COMPILE);
769 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
771 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
773 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
776 glEnable(GL_NORMALIZE);
779 gp->gear_inner = glGenLists(1);
780 glNewList(gp->gear_inner, GL_COMPILE);
788 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
790 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
792 gear(1.0, 2.0, 2.0, 12, 0.7, wire, False);
795 glEnable(GL_NORMALIZE);
798 gp->gear_outer = glGenLists(1);
799 glNewList(gp->gear_outer, GL_COMPILE);
807 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
809 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
811 gear(5.7, 7.0, 2.0, 36, 0.7, wire, True);
813 /* put some nubs on the outer ring, so we can tell how it's moving */
815 glTranslatef(7.0, 0, 0);
816 glRotatef(90, 0, 1, 0);
818 ctube(0.5, 0.5, wire); /* nub 1 */
822 glRotatef(120, 0, 0, 1);
823 glTranslatef(7.0, 0, 0);
824 glRotatef(90, 0, 1, 0);
825 ctube(0.5, 0.5, wire); /* nub 2 */
829 glRotatef(240, 0, 0, 1);
830 glTranslatef(7.0, 0, 0);
831 glRotatef(90, 0, 1, 0);
832 ctube(0.5, 0.5, wire); /* nub 3 */
838 glEnable(GL_NORMALIZE);
840 gp->armature = glGenLists(1);
841 glNewList(gp->armature, GL_COMPILE);
849 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
851 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
854 glTranslatef(0, 0, 1.5);
855 ctube(0.5, 10, wire); /* center axle */
858 glTranslatef(0.0, 4.2, -1);
859 ctube(0.5, 3, wire); /* axle 1 */
860 glTranslatef(0, 0, 1.8);
861 ctube(0.7, 0.7, wire);
865 glRotatef(120, 0.0, 0.0, 1.0);
866 glTranslatef(0.0, 4.2, -1);
867 ctube(0.5, 3, wire); /* axle 2 */
868 glTranslatef(0, 0, 1.8);
869 ctube(0.7, 0.7, wire);
873 glRotatef(240, 0.0, 0.0, 1.0);
874 glTranslatef(0.0, 4.2, -1);
875 ctube(0.5, 3, wire); /* axle 3 */
876 glTranslatef(0, 0, 1.8);
877 ctube(0.7, 0.7, wire);
880 glTranslatef(0, 0, 1.5); /* center disk */
884 glRotatef(270, 0, 0, 1);
885 glRotatef(-10, 0, 1, 0);
886 glTranslatef(-2.2, 0, 0);
887 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 1 */
891 glRotatef(30, 0, 0, 1);
892 glRotatef(-10, 0, 1, 0);
893 glTranslatef(-2.2, 0, 0);
894 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 2 */
898 glRotatef(150, 0, 0, 1);
899 glRotatef(-10, 0, 1, 0);
900 glTranslatef(-2.2, 0, 0);
901 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 3 */
906 glEnable(GL_NORMALIZE);
912 gears_handle_event (ModeInfo *mi, XEvent *event)
914 gearsstruct *gp = &gears[MI_SCREEN(mi)];
916 if (event->xany.type == ButtonPress &&
917 event->xbutton.button == Button1)
919 gp->button_down_p = True;
920 gltrackball_start (gp->trackball,
921 event->xbutton.x, event->xbutton.y,
922 MI_WIDTH (mi), MI_HEIGHT (mi));
925 else if (event->xany.type == ButtonRelease &&
926 event->xbutton.button == Button1)
928 gp->button_down_p = False;
931 else if (event->xany.type == ButtonPress &&
932 (event->xbutton.button == Button4 ||
933 event->xbutton.button == Button5))
935 gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
936 !!event->xbutton.state);
939 else if (event->xany.type == MotionNotify &&
942 gltrackball_track (gp->trackball,
943 event->xmotion.x, event->xmotion.y,
944 MI_WIDTH (mi), MI_HEIGHT (mi));
953 init_gears (ModeInfo * mi)
955 int screen = MI_SCREEN(mi);
958 /* Boolean rgba, doublebuffer, cmap_installed; */
962 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
963 sizeof (gearsstruct))) == NULL)
968 if (mode_str && !strcasecmp (mode_str, "planetary"))
969 gp->planetary_p = True;
970 else if (mode_str && !strcasecmp (mode_str, "simple"))
971 gp->planetary_p = False;
972 else if (!mode_str || !*mode_str || !strcasecmp (mode_str, "random"))
973 gp->planetary_p = !(random() % 2);
975 fprintf (stderr, "%s: mode must be planetary, simple, or random",
978 gp->window = MI_WINDOW(mi);
980 gp->rot = make_rotator (1, 1, 1, 1, 0, True);
981 gp->trackball = gltrackball_init ();
983 if ((gp->glx_context = init_GL(mi)) != NULL) {
984 reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
992 draw_gears (ModeInfo * mi)
994 gearsstruct *gp = &gears[MI_SCREEN(mi)];
995 Display *display = MI_DISPLAY(mi);
996 Window window = MI_WINDOW(mi);
997 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
1002 if (!gp->glx_context)
1005 glDrawBuffer(GL_BACK);
1007 glXMakeCurrent(display, window, *(gp->glx_context));
1010 /* let's do something so we don't get bored */
1011 gp->angle = (int) (gp->angle + angle_incr) % 360;
1013 if (mi->fps_p) do_fps (mi);
1015 glXSwapBuffers(display, window);
1019 release_gears (ModeInfo * mi)
1021 if (gears != NULL) {
1024 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1025 gearsstruct *gp = &gears[screen];
1027 if (gp->glx_context) {
1028 /* Display lists MUST be freed while their glXContext is current. */
1029 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1031 if (glIsList(gp->gear1))
1032 glDeleteLists(gp->gear1, 1);
1033 if (glIsList(gp->gear2))
1034 glDeleteLists(gp->gear2, 1);
1035 if (glIsList(gp->gear3))
1036 glDeleteLists(gp->gear3, 1);
1037 if (glIsList(gp->gear_inner))
1038 glDeleteLists(gp->gear_inner, 1);
1039 if (glIsList(gp->gear_outer))
1040 glDeleteLists(gp->gear_outer, 1);
1044 (void) free((void *) gears);
1051 XSCREENSAVER_MODULE ("Gears", gears)
1053 /*********************************************************/