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 "*planetary: False \n" \
49 "*showFPS: False \n" \
50 "*wireframe: False \n"
51 # include "xlockmore.h" /* from the xscreensaver distribution */
52 #else /* !STANDALONE */
53 # include "xlock.h" /* from the xlockmore distribution */
54 #endif /* !STANDALONE */
59 #include "gltrackball.h"
62 #define countof(x) (sizeof((x))/sizeof((*x)))
64 #define DEF_PLANETARY "False"
68 static XrmOptionDescRec opts[] = {
69 {"-planetary", ".gears.planetary", XrmoptionNoArg, "true" },
70 {"+planetary", ".gears.planetary", XrmoptionNoArg, "false" },
73 static argtype vars[] = {
74 {&planetary, "planetary", "Planetary", DEF_PLANETARY, t_Bool},
77 ModeSpecOpt gears_opts = {countof(opts), opts, countof(vars), vars, NULL};
80 ModStruct gears_description =
81 {"gears", "init_gears", "draw_gears", "release_gears",
82 "draw_gears", "init_gears", NULL, &gears_opts,
83 1000, 1, 2, 1, 4, 1.0, "",
84 "Shows GL's gears", 0, NULL};
88 #define SMOOTH_TUBE /* whether to have smooth or faceted tubes */
91 # define TUBE_FACES 20 /* how densely to render tubes */
98 GLuint gear1, gear2, gear3;
99 GLuint gear_inner, gear_outer;
102 GLXContext *glx_context;
105 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);
541 get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
542 glRotatef (x * 360, 1.0, 0.0, 0.0);
543 glRotatef (y * 360, 0.0, 1.0, 0.0);
544 glRotatef (z * 360, 0.0, 0.0, 1.0);
549 glTranslatef(-3.0, -2.0, 0.0);
550 glRotatef(gp->angle, 0.0, 0.0, 1.0);
551 /* PURIFY 4.0.1 reports an unitialized memory read on the next line when using
552 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
553 glCallList(gp->gear1);
557 glTranslatef(3.1, -2.0, 0.0);
558 glRotatef(-2.0 * gp->angle - 9.0, 0.0, 0.0, 1.0);
559 glCallList(gp->gear2);
563 glTranslatef(-3.1, 4.2, 0.0);
564 glRotatef(-2.0 * gp->angle - 25.0, 0.0, 0.0, 1.0);
565 glCallList(gp->gear3);
568 } else { /* planetary */
570 glScalef(0.8, 0.8, 0.8);
573 glTranslatef(0.0, 4.2, 0.0);
574 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
575 glCallList(gp->gear1);
579 glRotatef(120, 0.0, 0.0, 1.0);
580 glTranslatef(0.0, 4.2, 0.0);
581 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
582 glCallList(gp->gear2);
586 glRotatef(240, 0.0, 0.0, 1.0);
587 glTranslatef(0.0, 4.2, 0.0);
588 glRotatef(gp->angle - 7.0, 0.0, 0.0, 1.0);
589 glCallList(gp->gear3);
593 glTranslatef(0.0, 0.0, 0.0);
594 glRotatef(-gp->angle, 0.0, 0.0, 1.0);
595 glCallList(gp->gear_inner);
599 glTranslatef(0.0, 0.0, 0.0);
600 glRotatef((gp->angle / 3.0) - 7.5, 0.0, 0.0, 1.0);
601 glCallList(gp->gear_outer);
605 glTranslatef(0.0, 0.0, 0.0);
606 glCallList(gp->armature);
615 /* new window size or exposure */
617 reshape_gears(ModeInfo *mi, int width, int height)
619 GLfloat h = (GLfloat) height / (GLfloat) width;
621 glViewport(0, 0, (GLint) width, (GLint) height);
622 glMatrixMode(GL_PROJECTION);
624 glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
625 glMatrixMode(GL_MODELVIEW);
627 glTranslatef(0.0, 0.0, -40.0);
629 /* The depth buffer will be cleared, if needed, before the
630 * next frame. Right now we just want to black the screen.
632 glClear(GL_COLOR_BUFFER_BIT);
640 gearsstruct *gp = &gears[MI_SCREEN(mi)];
641 static GLfloat pos[4] =
642 {5.0, 5.0, 10.0, 1.0};
643 static GLfloat red[4] =
644 {0.8, 0.1, 0.0, 1.0};
645 static GLfloat green[4] =
646 {0.0, 0.8, 0.2, 1.0};
647 static GLfloat blue[4] =
648 {0.2, 0.2, 1.0, 1.0};
649 static GLfloat gray[4] =
650 {0.5, 0.5, 0.5, 1.0};
651 static GLfloat white[4] =
652 {1.0, 1.0, 1.0, 1.0};
653 int wire = MI_IS_WIREFRAME(mi);
654 int mono = MI_IS_MONO(mi);
657 glLightfv(GL_LIGHT0, GL_POSITION, pos);
658 glEnable(GL_CULL_FACE);
659 glEnable(GL_LIGHTING);
661 glEnable(GL_DEPTH_TEST);
665 * Messes up on multiscreen Pseudocolor:0 StaticGray(monochrome):1
666 * 2nd time mode is run it is Grayscale on PseudoColor.
667 * The code below forces monochrome on TrueColor.
669 if (MI_IS_MONO(mi)) {
670 red[0] = red[1] = red[2] = 1.0;
671 green[0] = green[1] = green[2] = 1.0;
672 blue[0] = blue[1] = blue[2] = 1.0;
680 gp->gear1 = glGenLists(1);
681 glNewList(gp->gear1, GL_COMPILE);
689 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
691 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
694 gear(1.0, 4.0, 1.0, 20, 0.7, wire, False);
697 gp->gear2 = glGenLists(1);
698 glNewList(gp->gear2, GL_COMPILE);
706 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
708 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
710 gear(0.5, 2.0, 2.0, 10, 0.7, wire, False);
713 gp->gear3 = glGenLists(1);
714 glNewList(gp->gear3, GL_COMPILE);
722 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
724 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
726 gear(1.3, 2.0, 0.5, 10, 0.7, wire, False);
729 glEnable(GL_NORMALIZE);
731 } else { /* planetary */
733 gp->gear1 = glGenLists(1);
734 glNewList(gp->gear1, GL_COMPILE);
742 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
744 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
746 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
749 gp->gear2 = glGenLists(1);
750 glNewList(gp->gear2, GL_COMPILE);
758 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
760 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
762 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
765 gp->gear3 = glGenLists(1);
766 glNewList(gp->gear3, GL_COMPILE);
774 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
776 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
778 gear(1.3, 2.0, 2.0, 12, 0.7, wire, False);
781 glEnable(GL_NORMALIZE);
784 gp->gear_inner = glGenLists(1);
785 glNewList(gp->gear_inner, GL_COMPILE);
793 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
795 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
797 gear(1.0, 2.0, 2.0, 12, 0.7, wire, False);
800 glEnable(GL_NORMALIZE);
803 gp->gear_outer = glGenLists(1);
804 glNewList(gp->gear_outer, GL_COMPILE);
812 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
814 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
816 gear(5.7, 7.0, 2.0, 36, 0.7, wire, True);
818 /* put some nubs on the outer ring, so we can tell how it's moving */
820 glTranslatef(7.0, 0, 0);
821 glRotatef(90, 0, 1, 0);
823 ctube(0.5, 0.5, wire); /* nub 1 */
827 glRotatef(120, 0, 0, 1);
828 glTranslatef(7.0, 0, 0);
829 glRotatef(90, 0, 1, 0);
830 ctube(0.5, 0.5, wire); /* nub 2 */
834 glRotatef(240, 0, 0, 1);
835 glTranslatef(7.0, 0, 0);
836 glRotatef(90, 0, 1, 0);
837 ctube(0.5, 0.5, wire); /* nub 3 */
843 glEnable(GL_NORMALIZE);
845 gp->armature = glGenLists(1);
846 glNewList(gp->armature, GL_COMPILE);
854 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
856 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
859 glTranslatef(0, 0, 1.5);
860 ctube(0.5, 10, wire); /* center axle */
863 glTranslatef(0.0, 4.2, -1);
864 ctube(0.5, 3, wire); /* axle 1 */
865 glTranslatef(0, 0, 1.8);
866 ctube(0.7, 0.7, wire);
870 glRotatef(120, 0.0, 0.0, 1.0);
871 glTranslatef(0.0, 4.2, -1);
872 ctube(0.5, 3, wire); /* axle 2 */
873 glTranslatef(0, 0, 1.8);
874 ctube(0.7, 0.7, wire);
878 glRotatef(240, 0.0, 0.0, 1.0);
879 glTranslatef(0.0, 4.2, -1);
880 ctube(0.5, 3, wire); /* axle 3 */
881 glTranslatef(0, 0, 1.8);
882 ctube(0.7, 0.7, wire);
885 glTranslatef(0, 0, 1.5); /* center disk */
889 glRotatef(270, 0, 0, 1);
890 glRotatef(-10, 0, 1, 0);
891 glTranslatef(-2.2, 0, 0);
892 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 1 */
896 glRotatef(30, 0, 0, 1);
897 glRotatef(-10, 0, 1, 0);
898 glTranslatef(-2.2, 0, 0);
899 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 2 */
903 glRotatef(150, 0, 0, 1);
904 glRotatef(-10, 0, 1, 0);
905 glTranslatef(-2.2, 0, 0);
906 arm(4.0, 1.0, 0.5, 2.0, 1.0, wire); /* arm 3 */
911 glEnable(GL_NORMALIZE);
917 gears_handle_event (ModeInfo *mi, XEvent *event)
919 gearsstruct *gp = &gears[MI_SCREEN(mi)];
921 if (event->xany.type == ButtonPress &&
922 event->xbutton.button & Button1)
924 gp->button_down_p = True;
925 gltrackball_start (gp->trackball,
926 event->xbutton.x, event->xbutton.y,
927 MI_WIDTH (mi), MI_HEIGHT (mi));
930 else if (event->xany.type == ButtonRelease &&
931 event->xbutton.button & Button1)
933 gp->button_down_p = False;
936 else if (event->xany.type == MotionNotify &&
939 gltrackball_track (gp->trackball,
940 event->xmotion.x, event->xmotion.y,
941 MI_WIDTH (mi), MI_HEIGHT (mi));
950 init_gears(ModeInfo * mi)
952 int screen = MI_SCREEN(mi);
955 /* Boolean rgba, doublebuffer, cmap_installed; */
959 if ((gears = (gearsstruct *) calloc(MI_NUM_SCREENS(mi),
960 sizeof (gearsstruct))) == NULL)
965 gp->window = MI_WINDOW(mi);
967 gp->rot = make_rotator (1, 1, 1, 1, 0, True);
968 gp->trackball = gltrackball_init ();
970 if ((gp->glx_context = init_GL(mi)) != NULL) {
971 reshape_gears(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
979 draw_gears(ModeInfo * mi)
981 gearsstruct *gp = &gears[MI_SCREEN(mi)];
982 Display *display = MI_DISPLAY(mi);
983 Window window = MI_WINDOW(mi);
984 int angle_incr = MI_CYCLES(mi) ? MI_CYCLES(mi) : 2;
989 if (!gp->glx_context)
992 glDrawBuffer(GL_BACK);
994 glXMakeCurrent(display, window, *(gp->glx_context));
997 /* let's do something so we don't get bored */
998 gp->angle = (int) (gp->angle + angle_incr) % 360;
1000 if (mi->fps_p) do_fps (mi);
1002 glXSwapBuffers(display, window);
1006 release_gears(ModeInfo * mi)
1008 if (gears != NULL) {
1011 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
1012 gearsstruct *gp = &gears[screen];
1014 if (gp->glx_context) {
1015 /* Display lists MUST be freed while their glXContext is current. */
1016 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
1018 if (glIsList(gp->gear1))
1019 glDeleteLists(gp->gear1, 1);
1020 if (glIsList(gp->gear2))
1021 glDeleteLists(gp->gear2, 1);
1022 if (glIsList(gp->gear3))
1023 glDeleteLists(gp->gear3, 1);
1024 if (glIsList(gp->gear_inner))
1025 glDeleteLists(gp->gear_inner, 1);
1026 if (glIsList(gp->gear_outer))
1027 glDeleteLists(gp->gear_outer, 1);
1031 (void) free((void *) gears);
1038 /*********************************************************/