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 PROGCLASS "Gears"
39 # define HACK_INIT init_gears
40 # define HACK_DRAW draw_gears
41 # define HACK_RESHAPE reshape_gears
42 # define HACK_HANDLE_EVENT gears_handle_event
43 # define EVENT_MASK PointerMotionMask
44 # define gears_opts xlockmore_opts
45 # define DEFAULTS "*count: 1 \n" \
48 "*showFPS: False \n" \
49 "*wireframe: False \n"
50 # include "xlockmore.h" /* from the xscreensaver distribution */
51 #else /* !STANDALONE */
52 # include "xlock.h" /* from the xlockmore distribution */
53 #endif /* !STANDALONE */
58 #include "gltrackball.h"
61 #define countof(x) (sizeof((x))/sizeof((*x)))
63 #define DEF_PLANETARY "False"
67 static XrmOptionDescRec opts[] = {
68 {"-planetary", ".gears.planetary", XrmoptionNoArg, "true" },
69 {"+planetary", ".gears.planetary", XrmoptionNoArg, "false" },
72 static argtype vars[] = {
73 {&planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
76 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;
108 static gearsstruct *gears = NULL;
111 * Draw a gear wheel. You'll probably want to call this function when
112 * building a display list since we do a lot of trig here.
114 * Input: inner_radius - radius of hole at center
115 * outer_radius - radius at center of teeth
116 * width - width of gear
117 * teeth - number of teeth
118 * tooth_depth - depth of tooth
119 * wire - true for wireframe mode
122 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
123 GLint teeth, GLfloat tooth_depth, Bool wire, Bool invert)
133 r1 = outer_radius - tooth_depth / 2.0;
134 r2 = outer_radius + tooth_depth / 2.0;
140 r2 = inner_radius + tooth_depth / 2.0;
141 r1 = outer_radius - tooth_depth / 2.0;
145 da = 2.0 * M_PI / teeth / 4.0;
147 glShadeModel(GL_FLAT);
149 /* This subroutine got kind of messy when I added all the checks
150 * for wireframe mode. A much cleaner solution that I sometimes
151 * use is to have a variable hold the value GL_LINE_LOOP when
152 * in wireframe mode, or hold the value GL_POLYGON otherwise.
153 * Then I just call glBegin(that_variable), give my polygon
154 * coordinates, and glEnd(). Pretty neat eh? Too bad I couldn't
155 * integrate that trick here.
160 glNormal3f(0.0, 0.0, 1.0);
162 /* draw front face */
164 glBegin(GL_QUAD_STRIP);
165 for (i = 0; i <= teeth; i++) {
168 angle = i * 2.0 * M_PI / teeth;
169 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
170 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
172 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
173 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
175 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
176 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), width * 0.5);
183 /* draw front sides of teeth */
186 da = 2.0 * M_PI / teeth / 4.0;
187 for (i = 0; i < teeth; i++) {
188 angle = i * 2.0 * M_PI / teeth;
191 glBegin(GL_LINE_LOOP);
192 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
193 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
194 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
195 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
204 glNormal3f(0.0, 0.0, -1.0);
208 glBegin(GL_QUAD_STRIP);
209 for (i = 0; i <= teeth; i++) {
210 angle = i * 2.0 * M_PI / teeth;
213 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
214 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
216 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
217 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
219 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
220 glVertex3f(r1 * cos(angle + 4 * da), r1 * sin(angle + 4 * da), -width * 0.5);
227 /* draw back sides of teeth */
230 da = 2.0 * M_PI / teeth / 4.0;
231 for (i = 0; i < teeth; i++) {
232 angle = i * 2.0 * M_PI / teeth;
235 glBegin(GL_LINE_LOOP);
236 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
237 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
238 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
239 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
247 /* draw outward faces of teeth */
249 glBegin(GL_QUAD_STRIP);
250 for (i = 0; i <= teeth; i++) {
251 angle = i * 2.0 * M_PI / teeth;
254 u = r2 * cos(angle + da) - r1 * cos(angle);
255 v = r2 * sin(angle + da) - r1 * sin(angle);
257 u = r2 * cos(angle + da + M_PI/2) - r1 * cos(angle + M_PI/2);
258 v = r2 * sin(angle + da + M_PI/2) - r1 * sin(angle + M_PI/2);
261 len = sqrt(u * u + v * v);
264 glNormal3f(v, -u, 0.0);
268 glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
269 glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
271 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
272 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
275 glNormal3f(cos(angle), sin(angle), 0.0);
277 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
279 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
280 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
283 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
284 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
286 u = r1 * cos(angle + 3 * da + M_PI/2) - r2 * cos(angle + 2 * da + M_PI/2);
287 v = r1 * sin(angle + 3 * da + M_PI/2) - r2 * sin(angle + 2 * da + M_PI/2);
290 glNormal3f(v, -u, 0.0);
292 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
293 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
296 glNormal3f(cos(angle), sin(angle), 0.0);
298 glNormal3f(cos(angle + M_PI/2), sin(angle + M_PI/2), 0.0);
305 glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
306 glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
310 glShadeModel(GL_SMOOTH);
312 /* draw inside radius cylinder */
314 glBegin(GL_QUAD_STRIP);
315 for (i = 0; i <= teeth; i++) {
316 angle = i * 2.0 * M_PI / teeth;
321 glNormal3f(-cos(angle), -sin(angle), 0.0);
323 glNormal3f(cos(angle), sin(angle), 0.0);
325 glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
326 glVertex3f(r0 * cos(angle), r0 * sin(angle), 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);
330 glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
331 glVertex3f(r0 * cos(angle + 4 * da), r0 * sin(angle + 4 * da), width * 0.5);
342 unit_tube (Bool wire)
345 int faces = TUBE_FACES;
346 GLfloat step = M_PI * 2 / faces;
355 glBegin(wire ? GL_LINES : GL_QUAD_STRIP);
357 glBegin(wire ? GL_LINES : GL_QUADS);
360 for (i = 0, th = 0; i <= faces; i++)
362 GLfloat x = cos (th);
363 GLfloat y = sin (th);
365 glVertex3f(x, 0.0, y);
366 glVertex3f(x, 1.0, y);
372 glVertex3f(x, 1.0, y);
373 glVertex3f(x, 0.0, y);
380 for (z = 0; z <= 1; z++)
382 glFrontFace(z == 0 ? GL_CCW : GL_CW);
383 glNormal3f(0, (z == 0 ? -1 : 1), 0);
384 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
385 if (! wire) glVertex3f(0, z, 0);
386 for (i = 0, th = 0; i <= faces; i++)
388 GLfloat x = cos (th);
389 GLfloat y = sin (th);
399 tube (GLfloat x1, GLfloat y1, GLfloat z1,
400 GLfloat x2, GLfloat y2, GLfloat z2,
401 GLfloat diameter, GLfloat cap_size,
404 GLfloat length, angle, a, b, c;
406 if (diameter <= 0) abort();
412 length = sqrt (a*a + b*b + c*c);
413 angle = acos (a / length);
416 glTranslatef(x1, y1, z1);
417 glScalef (length, length, length);
419 if (c == 0 && b == 0)
420 glRotatef (angle / (M_PI / 180), 0, 1, 0);
422 glRotatef (angle / (M_PI / 180), 0, -c, b);
424 glRotatef (-90, 0, 0, 1);
425 glScalef (diameter/length, 1, diameter/length);
427 /* extend the endpoints of the tube by the cap size in both directions */
430 GLfloat c = cap_size/length;
431 glTranslatef (0, -c, 0);
432 glScalef (1, 1+c+c, 1);
441 ctube (GLfloat diameter, GLfloat width, Bool wire)
450 GLfloat width1, GLfloat height1,
451 GLfloat width2, GLfloat height2,
454 glShadeModel(GL_FLAT);
456 #if 0 /* don't need these - they're embedded in other objects */
459 glNormal3f(-1, 0, 0);
460 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
461 glVertex3f(-length/2, -width1/2, -height1/2);
462 glVertex3f(-length/2, width1/2, -height1/2);
463 glVertex3f(-length/2, width1/2, height1/2);
464 glVertex3f(-length/2, -width1/2, height1/2);
470 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
471 glVertex3f(length/2, -width2/2, -height2/2);
472 glVertex3f(length/2, width2/2, -height2/2);
473 glVertex3f(length/2, width2/2, height2/2);
474 glVertex3f(length/2, -width2/2, height2/2);
480 glNormal3f(0, 0, -1);
481 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
482 glVertex3f(-length/2, -width1/2, -height1/2);
483 glVertex3f(-length/2, width1/2, -height1/2);
484 glVertex3f( length/2, width2/2, -height2/2);
485 glVertex3f( length/2, -width2/2, -height2/2);
491 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
492 glVertex3f(-length/2, -width1/2, height1/2);
493 glVertex3f(-length/2, width1/2, height1/2);
494 glVertex3f( length/2, width2/2, height2/2);
495 glVertex3f( length/2, -width2/2, height2/2);
500 glNormal3f(0, -1, 0);
501 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
502 glVertex3f(-length/2, -width1/2, -height1/2);
503 glVertex3f(-length/2, -width1/2, height1/2);
504 glVertex3f( length/2, -width2/2, height2/2);
505 glVertex3f( length/2, -width2/2, -height2/2);
511 glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
512 glVertex3f(-length/2, width1/2, -height1/2);
513 glVertex3f(-length/2, width1/2, height1/2);
514 glVertex3f( length/2, width2/2, height2/2);
515 glVertex3f( length/2, width2/2, -height2/2);
525 gearsstruct *gp = &gears[MI_SCREEN(mi)];
526 int wire = MI_IS_WIREFRAME(mi);
529 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
531 glClear(GL_COLOR_BUFFER_BIT);
536 gltrackball_rotate (gp->trackball);
540 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
541 glRotatef (x * 360, 1.0, 0.0, 0.0);
542 glRotatef (y * 360, 0.0, 1.0, 0.0);
543 glRotatef (z * 360, 0.0, 0.0, 1.0);
548 glTranslatef(-3.0, -2.0, 0.0);
549 glRotatef(gp->angle, 0.0, 0.0, 1.0);
550 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
551 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
552 glCallList(gp->gear1);
556 glTranslatef(3.1, -2.0, 0.0);
557 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
558 glCallList(gp->gear2);
562 glTranslatef(-3.1, 4.2, 0.0);
563 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
564 glCallList(gp->gear3);
567 } else { /* planetary */
569 glScalef(0.8, 0.8, 0.8);
572 glTranslatef(0.0, 4.2, 0.0);
573 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
574 glCallList(gp->gear1);
578 glRotatef(120, 0.0, 0.0, 1.0);
579 glTranslatef(0.0, 4.2, 0.0);
580 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
581 glCallList(gp->gear2);
585 glRotatef(240, 0.0, 0.0, 1.0);
586 glTranslatef(0.0, 4.2, 0.0);
587 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
588 glCallList(gp->gear3);
592 glTranslatef(0.0, 0.0, 0.0);
593 glRotatef(-gp->angle, 0.0, 0.0, 1.0);
594 glCallList(gp->gear_inner);
598 glTranslatef(0.0, 0.0, 0.0);
599 glRotatef((gp->angle / 3.0) - 7.5, 0.0, 0.0, 1.0);
600 glCallList(gp->gear_outer);
604 glTranslatef(0.0, 0.0, 0.0);
605 glCallList(gp->armature);
614 /* new window size or exposure */
616 reshape_gears(ModeInfo *mi, int width, int height)
618 GLfloat h = (GLfloat) height / (GLfloat) width;
620 glViewport(0, 0, (GLint) width, (GLint) height);
621 glMatrixMode(GL_PROJECTION);
623 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
624 glMatrixMode(GL_MODELVIEW);
626 glTranslatef(0.0, 0.0, -40.0);
628 /* The depth buffer will be cleared, if needed, before the
629 * next frame. Right now we just want to black the screen.
631 glClear(GL_COLOR_BUFFER_BIT);
639 gearsstruct *gp = &gears[MI_SCREEN(mi)];
640 static GLfloat pos[4] =
641 {5.0, 5.0, 10.0, 1.0};
642 static GLfloat red[4] =
643 {0.8, 0.1, 0.0, 1.0};
644 static GLfloat green[4] =
645 {0.0, 0.8, 0.2, 1.0};
646 static GLfloat blue[4] =
647 {0.2, 0.2, 1.0, 1.0};
648 static GLfloat gray[4] =
649 {0.5, 0.5, 0.5, 1.0};
650 static GLfloat white[4] =
651 {1.0, 1.0, 1.0, 1.0};
652 int wire = MI_IS_WIREFRAME(mi);
653 int mono = MI_IS_MONO(mi);
656 glLightfv(GL_LIGHT0, GL_POSITION, pos);
657 glEnable(GL_CULL_FACE);
658 glEnable(GL_LIGHTING);
660 glEnable(GL_DEPTH_TEST);
664 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
665 * 2nd time mode is run it is Grayscale on PseudoColor.
666 * The code below forces monochrome on TrueColor.
668 if (MI_IS_MONO(mi)) {
669 red[0] = red[1] = red[2] = 1.0;
670 green[0] = green[1] = green[2] = 1.0;
671 blue[0] = blue[1] = blue[2] = 1.0;
679 gp->gear1 = glGenLists(1);
680 glNewList(gp->gear1, GL_COMPILE);
688 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
690 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
693 gear(1.0, 4.0, 1.0, 20, 0.7, wire, False);
696 gp->gear2 = glGenLists(1);
697 glNewList(gp->gear2, GL_COMPILE);
705 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
707 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
709 gear(0.5, 2.0, 2.0, 10, 0.7, wire, False);
712 gp->gear3 = glGenLists(1);
713 glNewList(gp->gear3, GL_COMPILE);
721 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
723 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
725 gear(1.3, 2.0, 0.5, 10, 0.7, wire, False);
728 glEnable(GL_NORMALIZE);
730 } else { /* planetary */
732 gp->gear1 = glGenLists(1);
733 glNewList(gp->gear1, GL_COMPILE);
741 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
743 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
745 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
748 gp->gear2 = glGenLists(1);
749 glNewList(gp->gear2, GL_COMPILE);
757 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
759 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
761 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
764 gp->gear3 = glGenLists(1);
765 glNewList(gp->gear3, GL_COMPILE);
773 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
775 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
777 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
780 glEnable(GL_NORMALIZE);
783 gp->gear_inner = glGenLists(1);
784 glNewList(gp->gear_inner, GL_COMPILE);
792 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
794 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
796 gear(1.0, 2.0, 2.0, 12, 0.7, wire, False);
799 glEnable(GL_NORMALIZE);
802 gp->gear_outer = glGenLists(1);
803 glNewList(gp->gear_outer, GL_COMPILE);
811 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
813 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
815 gear(5.7, 7.0, 2.0, 36, 0.7, wire, True);
817 /* put some nubs on the outer ring, so we can tell how it's moving */
819 glTranslatef(7.0, 0, 0);
820 glRotatef(90, 0, 1, 0);
822 ctube(0.5, 0.5, wire); /* nub 1 */
826 glRotatef(120, 0, 0, 1);
827 glTranslatef(7.0, 0, 0);
828 glRotatef(90, 0, 1, 0);
829 ctube(0.5, 0.5, wire); /* nub 2 */
833 glRotatef(240, 0, 0, 1);
834 glTranslatef(7.0, 0, 0);
835 glRotatef(90, 0, 1, 0);
836 ctube(0.5, 0.5, wire); /* nub 3 */
842 glEnable(GL_NORMALIZE);
844 gp->armature = glGenLists(1);
845 glNewList(gp->armature, GL_COMPILE);
853 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
855 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
858 glTranslatef(0, 0, 1.5);
859 ctube(0.5, 10, wire); /* center axle */
862 glTranslatef(0.0, 4.2, -1);
863 ctube(0.5, 3, wire); /* axle 1 */
864 glTranslatef(0, 0, 1.8);
865 ctube(0.7, 0.7, wire);
869 glRotatef(120, 0.0, 0.0, 1.0);
870 glTranslatef(0.0, 4.2, -1);
871 ctube(0.5, 3, wire); /* axle 2 */
872 glTranslatef(0, 0, 1.8);
873 ctube(0.7, 0.7, wire);
877 glRotatef(240, 0.0, 0.0, 1.0);
878 glTranslatef(0.0, 4.2, -1);
879 ctube(0.5, 3, wire); /* axle 3 */
880 glTranslatef(0, 0, 1.8);
881 ctube(0.7, 0.7, wire);
884 glTranslatef(0, 0, 1.5); /* center disk */
888 glRotatef(270, 0, 0, 1);
889 glRotatef(-10, 0, 1, 0);
890 glTranslatef(-2.2, 0, 0);
891 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 1 */
895 glRotatef(30, 0, 0, 1);
896 glRotatef(-10, 0, 1, 0);
897 glTranslatef(-2.2, 0, 0);
898 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 2 */
902 glRotatef(150, 0, 0, 1);
903 glRotatef(-10, 0, 1, 0);
904 glTranslatef(-2.2, 0, 0);
905 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 3 */
910 glEnable(GL_NORMALIZE);
916 gears_handle_event (ModeInfo *mi, XEvent *event)
918 gearsstruct *gp = &gears[MI_SCREEN(mi)];
920 if (event->xany.type == ButtonPress &&
921 event->xbutton.button == Button1)
923 gp->button_down_p = True;
924 gltrackball_start (gp->trackball,
925 event->xbutton.x, event->xbutton.y,
926 MI_WIDTH (mi), MI_HEIGHT (mi));
929 else if (event->xany.type == ButtonRelease &&
930 event->xbutton.button == Button1)
932 gp->button_down_p = False;
935 else if (event->xany.type == ButtonPress &&
936 (event->xbutton.button == Button4 ||
937 event->xbutton.button == Button5))
939 gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
940 !!event->xbutton.state);
943 else if (event->xany.type == MotionNotify &&
946 gltrackball_track (gp->trackball,
947 event->xmotion.x, event->xmotion.y,
948 MI_WIDTH (mi), MI_HEIGHT (mi));
957 init_gears(ModeInfo * mi)
959 int screen = MI_SCREEN(mi);
962 /* Boolean rgba, doublebuffer, cmap_installed; */
966 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
967 sizeof (gearsstruct))) == NULL)
972 gp->window = MI_WINDOW(mi);
974 gp->rot = make_rotator (1, 1, 1, 1, 0, True);
975 gp->trackball = gltrackball_init ();
977 if ((gp->glx_context = init_GL(mi)) != NULL) {
978 reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
986 draw_gears(ModeInfo * mi)
988 gearsstruct *gp = &gears[MI_SCREEN(mi)];
989 Display *display = MI_DISPLAY(mi);
990 Window window = MI_WINDOW(mi);
991 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
996 if (!gp->glx_context)
999 glDrawBuffer(GL_BACK);
1001 glXMakeCurrent(display, window, *(gp->glx_context));
1004 /* let's do something so we don't get bored */
1005 gp->angle = (int) (gp->angle + angle_incr) % 360;
1007 if (mi->fps_p) do_fps (mi);
1009 glXSwapBuffers(display, window);
1013 release_gears(ModeInfo * mi)
1015 if (gears != NULL) {
1018 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1019 gearsstruct *gp = &gears[screen];
1021 if (gp->glx_context) {
1022 /* Display lists MUST be freed while their glXContext is current. */
1023 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1025 if (glIsList(gp->gear1))
1026 glDeleteLists(gp->gear1, 1);
1027 if (glIsList(gp->gear2))
1028 glDeleteLists(gp->gear2, 1);
1029 if (glIsList(gp->gear3))
1030 glDeleteLists(gp->gear3, 1);
1031 if (glIsList(gp->gear_inner))
1032 glDeleteLists(gp->gear_inner, 1);
1033 if (glIsList(gp->gear_outer))
1034 glDeleteLists(gp->gear_outer, 1);
1038 (void) free((void *) gears);
1045 /*********************************************************/