58bf4a53d54defa16c07e18a102d6f36a92a444c
[xscreensaver] / hacks / glx / engine.c
1 /*
2  * engine.c - GL representation of a 4 stroke engine
3  *
4  * version 1.01
5  *
6  * Copyright (C) 2001 Ben Buxton (bb@cactii.net)
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that
11  * copyright notice and this permission notice appear in supporting
12  * documentation.  No representations are made about the suitability of this
13  * software for any purpose.  It is provided "as is" without express or
14  * implied warranty.
15  */
16
17
18 #include <X11/Intrinsic.h>
19
20 #ifdef STANDALONE
21 # define PROGCLASS                                      "Engine"
22 # define HACK_INIT                                      init_engine
23 # define HACK_DRAW                                      draw_engine
24 # define HACK_HANDLE_EVENT                              engine_handle_event
25 # define HACK_RESHAPE                           reshape_engine
26 # define EVENT_MASK                                     PointerMotionMask
27 # define engine_opts                                     xlockmore_opts
28 /* insert defaults here */
29
30 #define DEFAULTS        "*delay:       10000       \n" \
31                         "*showFPS:       False       \n" \
32                         "*move:       True       \n" \
33                         "*spin:       True       \n" \
34                         "*rotatespeed:      1\n" \
35
36 # include "xlockmore.h"                         /* from the xscreensaver distribution */
37 #else  /* !STANDALONE */
38 # include "xlock.h"                                     /* from the xlockmore distribution */
39 #endif /* !STANDALONE */
40
41 #include "rotator.h"
42 #include "gltrackball.h"
43
44 /* lifted from lament.c */
45 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
46 #define RANDSIGN() ((random() & 1) ? 1 : -1)
47
48
49 #ifdef USE_GL
50
51 #include <GL/glu.h>
52
53
54 static int rotatespeed;
55 static int move;
56 static int movepaused = 0;
57 static int spin;
58
59 #undef countof
60 #define countof(x) (sizeof((x))/sizeof((*x)))
61
62 static XrmOptionDescRec opts[] = {
63   {"-rotate-speed", ".engine.rotatespeed", XrmoptionSepArg, "1" },
64   {"-move", ".engine.move", XrmoptionNoArg, (caddr_t) "true" },
65   {"+move", ".engine.move", XrmoptionNoArg, (caddr_t) "false" },
66   {"-spin", ".engine.spin", XrmoptionNoArg, (caddr_t) "true" },
67   {"+spin", ".engine.spin", XrmoptionNoArg, (caddr_t) "false" },
68 };
69
70 static argtype vars[] = {
71   {(caddr_t *) &rotatespeed, "rotatespeed", "Rotatespeed", "1", t_Int},
72   {(caddr_t *) &move, "move", "Move", "True", t_Bool},
73   {(caddr_t *) &spin, "spin", "Spin", "True", t_Bool},
74 };
75
76 ModeSpecOpt engine_opts = {countof(opts), opts, countof(vars), vars, NULL};
77
78 #ifdef USE_MODULES
79 ModStruct   engine_description =
80 {"engine", "init_engine", "draw_engine", "release_engine",
81  "draw_engine", "init_engine", NULL, &engine_opts,
82  1000, 1, 2, 1, 4, 1.0, "",
83  "A four stroke engine", 0, NULL};
84
85 #endif
86
87
88 typedef struct {
89   GLXContext *glx_context;
90   Window window;
91   GLfloat x, y, z; /* position */
92   GLfloat dx, dy, dz; /* position */
93   GLfloat an1, an2, an3; /* internal angle */
94   GLfloat nx, ny, nz; /* spin vector */
95   GLfloat a; /* spin angle */
96   GLfloat da; /* spin speed */
97   rotator *rot;
98   trackball_state *trackball;
99   Bool button_down_p;
100 } Engine;
101
102 static Engine *engine = NULL;
103
104 #include <math.h>
105 #include <sys/time.h>
106 #include <stdio.h>
107 #include <stdlib.h>
108
109 #ifndef M_PI
110 #define M_PI 3.14159265
111 #endif
112
113 #define MOVE_MULT 0.05
114
115 #define RAND_RANGE(min, max) ((min) + (max - min) * f_rand())
116
117 int win_w, win_h;
118
119 static GLfloat viewer[] = {0.0, 0.0, 30.0};
120 static GLfloat lookat[] = {0.0, 0.0, 0.0};
121 static GLfloat lightpos[] = {7.0, 7.0, 12, 1.0};
122 GLfloat light_sp[] = {0.8, 0.8, 0.8, 0.5};
123 static GLfloat red[] = {1.0, 0, 0, 1.0};
124 static GLfloat green[] = {0.0, 1, 0, 1.0};
125 static GLfloat blue[] = {0, 0, 1, 1.0};
126 static GLfloat white[] = {1.0, 1, 1, 1.0};
127 static GLfloat yellow_t[] = {1, 1, 0, 0.4};
128
129 void circle(float, int,int);
130 GLvoid normal(GLfloat [], GLfloat [], GLfloat [], 
131                   GLfloat *, GLfloat *, GLfloat *);
132
133 float sin_table[720];
134 float cos_table[720];
135 float tan_table[720];
136
137 /* we use trig tables to speed things up - 200 calls to sin()
138  in one frame can be a bit harsh..
139 */
140
141 void make_tables(void) {
142 int i;
143 float f;
144
145   f = 360 / (M_PI * 2);
146   for (i = 0 ; i <= 720 ; i++) {
147     sin_table[i] = sin(i/f);
148   }
149   for (i = 0 ; i <= 720 ; i++) {
150     cos_table[i] = cos(i/f);
151   }
152   for (i = 0 ; i <= 720 ; i++) {
153     tan_table[i] = tan(i/f);
154   }
155 }
156
157 /* if inner and outer are the same, we draw a cylinder, not a tube */
158 /* for a tube, endcaps is 0 (none), 1 (left), 2 (right) or 3(both) */
159 /* angle is how far around the axis to go (up to 360) */
160
161 void cylinder (GLfloat x, GLfloat y, GLfloat z, float length, float outer, float inner, int endcaps, int sang,
162                  int eang) {
163 int a; /* current angle around cylinder */
164 int b = 0; /* previous */
165 int angle, norm, step, sangle;
166 float z1, y1, z2, y2, ex=0;
167 float y3, z3;
168 float Z1, Y1, Z2, Y2, xl, Y3, Z3;
169 GLfloat y2c[720], z2c[720];
170 GLfloat ony, onz; /* previous normals */
171 int nsegs, tube = 0;
172
173   glPushMatrix();
174   nsegs = outer*(MAX(win_w, win_h)/200);
175   nsegs = MAX(nsegs, 6);
176   nsegs = MAX(nsegs, 40);
177   if (nsegs % 2)
178      nsegs += 1;
179   sangle = sang;
180   angle = eang;
181   ony = onz = 0;
182   z1 = cos_table[sangle]*outer+z; y1 = sin_table[sangle] * outer+y;
183   Z1 = cos_table[sangle] * inner+z; Y1 = sin_table[sangle]*inner+y ; 
184   Z2 = z;
185   Y2 = y;
186   xl = x + length;
187   if (inner < outer && endcaps < 3) tube = 1;
188   step = 360/nsegs;
189
190   glBegin(GL_QUADS);
191   for (a = sangle ; a <= angle || b <= angle ; a+= step) {
192     y2=outer*(float)sin_table[a]+y;
193     z2=outer*(float)cos_table[a]+z;
194     y3=outer*(float)sin_table[a+step]+y;
195     z3=outer*(float)cos_table[a+step]+z;
196     if (endcaps)
197        y2c[a] = y2; z2c[a] = z2; /* cache for later */
198     if (tube) {
199       Y2=inner*(float)sin_table[a]+y;
200       Z2=inner*(float)cos_table[a]+z;
201       Y3=inner*(float)sin_table[a+step]+y;
202       Z3=inner*(float)cos_table[a+step]+z;
203     }
204     glNormal3f(0, y1, z1);
205     glVertex3f(x,y1,z1);
206     glVertex3f(xl,y1,z1);
207     glNormal3f(0, y2, z2);
208     glVertex3f(xl,y2,z2);
209     glVertex3f(x,y2,z2);
210     if (a == sangle && angle - sangle < 360) {
211       if (tube)
212         glVertex3f(x, Y1, Z1);
213       else
214         glVertex3f(x, y, z);
215       glVertex3f(x, y1, z1);
216       glVertex3f(xl, y1, z1);
217       if (tube)
218         glVertex3f(xl, Z1, Z1);
219       else
220         glVertex3f(xl, y, z);
221     }
222     if (tube) {
223       if (endcaps != 1) {
224         glNormal3f(-1, 0, 0); /* left end */
225         glVertex3f(x, y1, z1);
226         glVertex3f(x, y2, z2);
227         glVertex3f(x, Y2, Z2);
228         glVertex3f(x, Y1, Z1);
229       }
230
231       glNormal3f(0, -Y1, -Z1); /* inner surface */
232       glVertex3f(x, Y1, Z1);
233       glVertex3f(xl, Y1, Z1);
234       glNormal3f(0, -Y2, -Z2);
235       glVertex3f(xl, Y2, Z2);
236       glVertex3f(x, Y2, Z2);
237
238       if (endcaps != 2) {
239         glNormal3f(1, 0, 0); /* right end */
240         glVertex3f(xl, y1, z1);
241         glVertex3f(xl, y2, z2);
242         glVertex3f(xl, Y2, Z2);
243         glVertex3f(xl, Y1, Z1);
244       }
245     }
246
247     z1=z2; y1=y2;
248     Z1=Z2; Y1=Y2;
249     b = a;
250   }
251   glEnd();
252
253   if (angle - sangle < 360) {
254     GLfloat nx, ny, nz;
255     GLfloat v1[3], v2[3], v3[3];
256     v1[0] = x; v1[1] = y; v1[2] = z;
257     v2[0] = x; v2[1] = y1; v2[2] = z1;
258     v3[0] = xl; v3[1] = y1; v3[2] = z1;
259     normal(&v2[0], &v1[0], &v3[0], &nx, &ny, &nz);
260     glBegin(GL_QUADS);
261     glNormal3f(nx, ny, nz);
262     glVertex3f(x, y, z);
263     glVertex3f(x, y1, z1);
264     glVertex3f(xl, y1, z1);
265     glVertex3f(xl, y, z);
266     glEnd();
267   }
268   if (endcaps) {
269     GLfloat end, start;
270     if (tube) {
271       if (endcaps == 1) {
272         end = 0;
273         start = 0;
274       } else if (endcaps == 2) {
275         start = end = length+0.01;
276       } else {
277         end = length+0.02; start = -0.01;
278       }
279       norm = (ex == length+0.01) ? -1 : 1;
280     } else  {
281       end = length;
282       start = 0;
283       norm = -1;
284     }
285
286     for(ex = start ; ex <= end ; ex += length) {
287       z1 = outer*cos_table[sangle]+z;
288       y1 = y+sin_table[sangle]*outer;
289       step = 360/nsegs;
290       glBegin(GL_TRIANGLES);
291       b = 0;
292       for (a = sangle ; a <= angle || b <= angle; a+= step) {
293           glNormal3f(norm, 0, 0);
294           glVertex3f(x+ex,y, z);
295           glVertex3f(x+ex,y1,z1);
296           glVertex3f(x+ex,y2c[a],z2c[a]);
297         y1 = y2c[a]; z1 = z2c[a];
298         b = a;
299       }
300       if (!tube) norm = 1;
301       glEnd();
302     }
303   }
304   glPopMatrix();
305 }
306
307 GLvoid normal(GLfloat v1[], GLfloat v2[], GLfloat v3[], 
308                   GLfloat *nx, GLfloat *ny, GLfloat *nz)
309 {
310    GLfloat x, y, z, X, Y, Z;
311
312    x = v2[0]-v1[0];
313    y = v2[1]-v1[1];
314    z = v2[2]-v1[2];
315    X = v3[0]-v1[0];
316    Y = v3[1]-v1[1];
317    Z = v3[2]-v1[2];
318
319    *nx = Y*z - Z*y;
320    *ny = Z*x - X*z;
321    *nz = X*y - Y*x;
322
323
324
325
326
327 void circle(float radius, int segments, int half) {
328 float x1 = 0, x2 = 0;
329 float y1 = 0, y2 = 0;
330 int i, t, s;
331
332   if (half) {
333     t = 270; s = 90;
334     x1 = radius, y1 = 0;
335   } else {
336     t = 360, s = 0;
337   }
338   glBegin(GL_TRIANGLES);
339   glNormal3f(1, 0, 0);
340   for(i=s;i<=t;i+=10)
341   {
342     float angle=i;
343     x2=radius*(float)cos_table[(int)angle];
344     y2=radius*(float)sin_table[(int)angle];
345     glVertex3f(0,0,0);
346     glVertex3f(x1,y1,0);
347     glVertex3f(x2,y2,0);
348     x1=x2;
349     y1=y2;
350   }
351   glEnd();
352 }
353
354 void ring(GLfloat inner, GLfloat outer, int nsegs) {
355 GLfloat z1, z2, y1, y2;
356 GLfloat Z1, Z2, Y1, Y2;
357 int i;
358
359   z1 = inner; y1 = 0;
360   Z1 = outer; Y1 = 0;
361   glBegin(GL_QUADS);
362   glNormal3f(1, 0, 0);
363   for(i=0; i <=360 ; i+= 360/nsegs)
364   {
365     float angle=i;
366     z2=inner*(float)sin_table[(int)angle];
367     y2=inner*(float)cos_table[(int)angle];
368     Z2=outer*(float)sin_table[(int)angle];
369     Y2=outer*(float)cos_table[(int)angle];
370     glVertex3f(0, Y1, Z1);
371     glVertex3f(0, y1, z1);
372     glVertex3f(0, y2, z2);
373     glVertex3f(0, Y2, Z2);
374     z1=z2;
375     y1=y2;
376     Z1=Z2;
377     Y1=Y2;
378   }
379   glEnd();
380 }
381
382 void Rect(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h,
383             GLfloat t) {
384 GLfloat yh;
385 GLfloat xw;
386 GLfloat zt;
387
388   yh = y+h; xw = x+w; zt = z - t;
389
390   glBegin(GL_QUADS); /* front */
391     glNormal3f(0, 0, 1);
392     glVertex3f(x, y, z);
393     glVertex3f(x, yh, z);
394     glVertex3f(xw, yh, z);
395     glVertex3f(xw, y, z);
396   /* back */
397     glNormal3f(0, 0, -1);
398     glVertex3f(x, y, zt);
399     glVertex3f(x, yh, zt);
400     glVertex3f(xw, yh, zt);
401     glVertex3f(xw, y, zt);
402   /* top */
403     glNormal3f(0, 1, 0);
404     glVertex3f(x, yh, z);
405     glVertex3f(x, yh, zt);
406     glVertex3f(xw, yh, zt);
407     glVertex3f(xw, yh, z);
408   /* bottom */
409     glNormal3f(0, -1, 0);
410     glVertex3f(x, y, z);
411     glVertex3f(x, y, zt);
412     glVertex3f(xw, y, zt);
413     glVertex3f(xw, y, z);
414   /* left */
415     glNormal3f(-1, 0, 0);
416     glVertex3f(x, y, z);
417     glVertex3f(x, y, zt);
418     glVertex3f(x, yh, zt);
419     glVertex3f(x, yh, z);
420   /* right */
421     glNormal3f(1, 0, 0);
422     glVertex3f(xw, y, z);
423     glVertex3f(xw, y, zt);
424     glVertex3f(xw, yh, zt);
425     glVertex3f(xw, yh, z);
426   glEnd();
427 }
428
429 void makepiston(void) {
430 GLfloat colour[] = {0.6, 0.6, 0.6, 1.0};
431 int i;
432   
433   i = glGenLists(1);
434   glNewList(i, GL_COMPILE);
435   glRotatef(90, 0, 0, 1);
436   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colour);
437   glMaterialfv(GL_FRONT, GL_SPECULAR, colour);
438   glMateriali(GL_FRONT, GL_SHININESS, 20);
439   cylinder(0, 0, 0, 2, 1, 0.7, 2, 0, 360); /* body */
440   colour[0] = colour[1] = colour[2] = 0.2;
441   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colour);
442   cylinder(1.6, 0, 0, 0.1, 1.05, 1.05, 0, 0, 360); /* ring */
443   cylinder(1.8, 0, 0, 0.1, 1.05, 1.05, 0, 0, 360); /* ring */
444   glEndList();
445 }
446
447 void CrankBit(GLfloat x, GLfloat y, GLfloat z) {
448   Rect(x, y, z, 0.2, 1.8, 1);
449 }
450
451 void boom(GLfloat x, GLfloat y, int s) {
452 static GLfloat red[] = {0, 0, 0, 0.9};
453 static GLfloat lpos[] = {0, 0, 0, 1};
454 static GLfloat d = 0, wd;
455 static int time = 0;
456
457   if (time == 0 && s) {
458     red[0] = red[1] = 0;
459     d = 0.05;
460     time++;
461     glEnable(GL_LIGHT1); 
462   } else if (time == 0 && !s) {
463     return;
464   } else if (time >= 8 && time < 16 && !s) {
465     time++;
466     red[0] -= 0.2; red[1] -= 0.1;
467     d-= 0.04;
468   } else if (time >= 16) {
469     time = 0;
470     glDisable(GL_LIGHT1);
471     return;
472   } else {
473     red[0] += 0.2; red[1] += 0.1;
474     d+= 0.04;
475     time++;
476   }
477   lpos[0] = x; lpos[1] = y-d;
478   glLightfv(GL_LIGHT1, GL_POSITION, lpos);
479   glLightfv(GL_LIGHT1, GL_DIFFUSE, red);
480   glLightfv(GL_LIGHT1, GL_SPECULAR, red);
481   glLighti(GL_LIGHT1, GL_LINEAR_ATTENUATION, 1.3);
482   glLighti(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0);
483
484   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
485   glPushMatrix();
486   glRotatef(90, 0, 0, 1);
487   wd = d*3;
488   if (wd > 0.7) wd = 0.7;
489   glEnable(GL_BLEND);
490   glDepthMask(GL_FALSE);
491   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
492   cylinder(y-d, -x, 0, d, wd, wd, 1, 0, 360);
493   glDepthMask(GL_TRUE);
494   glDisable(GL_BLEND);
495   glPopMatrix();
496 }
497
498 void display(Engine *e) {
499
500 static int a = 0;
501 GLfloat zb, yb;
502 static GLfloat ln[730], yp[730], ang[730];
503 static int ln_init = 0;
504 static int spark;
505
506   glEnable(GL_LIGHTING);
507   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
508   glLoadIdentity();
509   gluLookAt(viewer[0], viewer[1], viewer[2], lookat[0], lookat[1], lookat[2], 0.0, 1.0, 0.0);
510   glPushMatrix();
511   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
512   glLightfv(GL_LIGHT0, GL_SPECULAR, light_sp);
513   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_sp);
514
515   if (move) {
516     double x, y, z;
517     get_position (e->rot, &x, &y, &z, !e->button_down_p);
518     glTranslatef(x*16-9, y*14-7, z*16-10);
519   }
520   if (spin) {
521     double x, y, z;
522     gltrackball_rotate (e->trackball);
523     get_rotation(e->rot, &x, &y, &z, !e->button_down_p);
524     glRotatef(x*360, 1.0, 0.0, 0.0);
525     glRotatef(y*360, 0.0, 1.0, 0.0);
526     glRotatef(x*360, 0.0, 0.0, 1.0);
527   }
528
529 /* So the rotation appears around the centre of the engine */
530   glTranslatef(-5, 0, 0); 
531
532 /* crankshaft */
533   glPushMatrix();
534   glRotatef(a, 1, 0, 0);
535   glCallList(1);
536   glPopMatrix();
537
538   /* init the ln[] matrix for speed */
539   if (ln_init == 0) {
540     for (ln_init = 0 ; ln_init < 730 ; ln_init++) {
541       zb = sin_table[ln_init];
542       yb = cos_table[ln_init];
543       yp[ln_init] = yb + sqrt(25 - (zb*zb)); /* y ordinate of piston */
544       ln[ln_init] = sqrt(zb*zb + (yb-yp[ln_init])*(yb-yp[ln_init])); /* length of rod */
545       ang[ln_init] = asin(zb/5)*57; /* angle of connecting rod */
546       ang[ln_init] *= -1;
547     }
548   }
549
550   zb = sin_table[a];
551   yb = cos_table[a];
552
553 /* pistons */
554   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
555   glPushMatrix();
556   glTranslatef(0, yp[a]-0.3, 0);
557   glCallList(2);
558   glPopMatrix();
559   glPushMatrix();
560   glTranslatef(3.2, yp[(a > 180 ? a-180 : a+180)]-0.3, 0);
561   glCallList(2);
562   glPopMatrix();
563   glPushMatrix();
564   glTranslatef(6.5, yp[a]-0.3, 0);
565   glCallList(2);
566   glPopMatrix();
567   glPushMatrix();
568   glTranslatef(9.8, yp[(a > 180 ? a-180 : a+180)]-0.3, 0);
569   glCallList(2);
570   glPopMatrix();
571
572 /* spark plugs */
573   glPushMatrix();
574   glRotatef(90, 0, 0, 1);
575   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
576   cylinder(8.5, 0, 0, 0.5, 0.4, 0.3, 1, 0, 360);
577   cylinder(8.5, -3.2, 0, 0.5, 0.4, 0.3, 1, 0, 360);
578   cylinder(8.5, -6.5, 0, 0.5, 0.4, 0.3, 1, 0, 360);
579   cylinder(8.5, -9.8, 0, 0.5, 0.4, 0.3, 1, 0, 360);
580
581   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
582   cylinder(8, 0, 0, 0.5, 0.2, 0.2, 1, 0, 360);
583   cylinder(8, -3.2, 0, 0.5, 0.2, 0.2, 1, 0, 360);
584   cylinder(8, -6.5, 0, 0.5, 0.2, 0.2, 1, 0, 360);
585   cylinder(8, -9.8, 0, 0.5, 0.2, 0.2, 1, 0, 360);
586
587   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
588   cylinder(9, 0, 0, 1, 0.15, 0.15, 1, 0, 360);
589   cylinder(9, -3.2, 0, 1, 0.15, 0.15, 1, 0, 360);
590   cylinder(9, -6.5, 0, 1, 0.15, 0.15, 1, 0, 360);
591   cylinder(9, -9.8, 0, 1, 0.15, 0.15, 1, 0, 360);
592
593   glPopMatrix();
594
595   if (a == 0) spark = 1 - spark;
596
597   if (spark == 0) {
598     if (a == 0)
599       boom(0, 8, 1);
600     else if (a == 180) 
601       boom(3.2, 8, 1);
602     if (a < 180)
603       boom(0, 8, 0);
604     else if (a < 360)
605       boom(3.2, 8, 0);
606   } else {
607     if (a == 0)
608       boom(6.5, 8, 1);
609     else if (a == 180) 
610       boom(9.8, 8, 1);
611     if (a < 180)
612       boom(6.5, 8, 0);
613     else if (a < 360)
614       boom(9.8, 8, 0);
615   }
616
617
618
619   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
620   glPushMatrix();
621   cylinder(-0.8, yb, zb, 1.6, 0.3, 0.3, 1, 0, 365);
622   cylinder(5.7, yb, zb, 1.7, 0.3, 0.3, 1, 0, 365);
623
624   cylinder(2.4, -yb, -zb, 1.7, 0.3, 0.3, 1, 0, 365);
625   cylinder(9.0, -yb, -zb, 1.7, 0.3, 0.3, 1, 0, 365);
626   glPopMatrix();
627
628  /* rod */
629   glPushMatrix();
630   glRotatef(90, 0, 0, 1);
631   glRotatef(ang[a], 0, -1, 0);
632   cylinder(yb, 0, zb, ln[a], 0.2, 0.2, 0, 0, 365);
633   cylinder(yb, -6.4, zb, ln[a], 0.2, 0.2, 0, 0, 365);
634   glPopMatrix();
635
636   glPushMatrix();
637   glRotatef(90, 0, 0, 1);
638   glRotatef(ang[a+180], 0, -1, 0);
639   cylinder(-yb, -3.2, -zb, ln[a], 0.2, 0.2, 0, 0, 365);
640   cylinder(-yb, -9.7, -zb, ln[a], 0.2, 0.2, 0, 0, 365);
641   glPopMatrix();
642
643
644   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow_t);
645   glEnable(GL_BLEND);
646   glDepthMask(GL_FALSE);
647   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
648
649   Rect(-1, 8.3, 1, 12, 0.2, 2);
650   Rect(-1.2, -0.5, 1, 0.2, 9, 2);
651
652   Rect(1.4, 3, 1, 0.6, 5.3, 2);
653   Rect(4.4, 3, 1, 1, 5.3, 2);
654   Rect(7.7, 3, 1, 0.8, 5.3, 2);
655   Rect(10.8, -0.5, 1, 0.2, 8.8, 2);
656
657   Rect(-1, 2.8, 1, 12, 0.2, 0.2); 
658   Rect(-1, 2.8, -1, 12, 0.2, 0.2); 
659
660   glDepthMask(GL_TRUE);
661   glDisable(GL_BLEND);
662
663
664   a+=10; if (a >= 360) a = 0;
665   glPopMatrix();
666   glFlush();
667 }
668
669 void makeshaft (void) {
670 int i;
671   i = glGenLists(1);
672   glNewList(i, GL_COMPILE);
673   cylinder(10.4, 0, 0, 2, 0.3, 0.3, 1, 0, 365);
674   cylinder(7.1, 0, 0, 2, 0.3, 0.3, 0, 0, 365);
675   cylinder(3.8, 0, 0, 2, 0.3, 0.3, 0, 0, 365);
676   cylinder(0.5, 0, 0, 2, 0.3, 0.3, 0, 0, 365);
677   cylinder(-1.5, 0, 0, 1, 0.3, 0.3, 1, 0, 365);
678
679   cylinder(12.4, 0, 0, 1, 3, 2.5, 0, 0, 365);
680   Rect(12.4, -0.3, 2.8, 0.5, 0.6, 5.6);
681   Rect(12.4, -2.8, 0.3, 0.5, 5.6, 0.6);
682
683   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
684   CrankBit(0.5, -0.4, 0.5);
685   cylinder(0.5, 0.5, 0, 0.2, 2, 2, 1, 240, 300);
686   CrankBit(-0.7, -0.4, 0.5);
687   cylinder(-0.7, 0.5, 0, 0.2, 2, 2, 1, 240, 300);
688
689   CrankBit(2.5, -1.4, 0.5);
690   cylinder(2.5, -0.5, 0, 0.2, 2, 2, 1, 60, 120);
691   CrankBit(3.8, -1.4, 0.5);
692   cylinder(3.8, -0.5, 0, 0.2, 2, 2, 1, 60, 120);
693
694   CrankBit(5.8, -0.4, 0.5);
695   cylinder(5.8, 0.5, 0, 0.2, 2, 2, 1, 240, 300);
696   CrankBit(7.1, -0.4, 0.5);
697   cylinder(7.1, 0.5, 0, 0.2, 2, 2, 1, 240, 300);
698
699   CrankBit(9.1, -1.4, 0.5);
700   cylinder(9.1, -0.5, 0, 0.2, 2, 2, 1, 60, 120);
701   CrankBit(10.4, -1.4, 0.5);
702   cylinder(10.4, -0.5, 0, 0.2, 2, 2, 1, 60, 120);
703
704   glEndList();
705
706 }
707
708 void reshape_engine(ModeInfo *mi, int width, int height)
709 {
710
711  glViewport(0,0,(GLint)width, (GLint) height);
712  glMatrixMode(GL_PROJECTION);
713  glLoadIdentity();
714  glFrustum(-1.0,1.0,-1.0,1.0,1.5,70.0);
715  glMatrixMode(GL_MODELVIEW);
716  win_h = height; win_w = width;
717 }
718
719
720 void init_engine(ModeInfo *mi)
721 {
722 int screen = MI_SCREEN(mi);
723 Engine *e;
724
725  if (engine == NULL) {
726    if ((engine = (Engine *) calloc(MI_NUM_SCREENS(mi),
727                                         sizeof(Engine))) == NULL)
728           return;
729  }
730  e = &engine[screen];
731  e->window = MI_WINDOW(mi);
732
733  e->x = e->y = e->z = e->a = e->an1 = e->nx = e->ny = e->nz = 
734  e->dx = e->dy = e->dz = e->da = 0;
735
736  if (move) {
737    e->dx = (float)(random() % 1000)/30000;
738    e->dy = (float)(random() % 1000)/30000;
739    e->dz = (float)(random() % 1000)/30000;
740  } else {
741   viewer[0] = 0; viewer[1] = 6; viewer[2] = 15;
742   lookat[0] = 0; lookat[1] = 4; lookat[2] = 0; 
743  }
744  if (spin) {
745    e->da = (float)(random() % 1000)/125 - 4;
746    e->nx = (float)(random() % 100) / 100;
747    e->ny = (float)(random() % 100) / 100;
748    e->nz = (float)(random() % 100) / 100;
749  }
750
751  {
752    double spin_speed = 1.0;
753    double wander_speed = 0.03;
754
755  e->rot = make_rotator (spin ? spin_speed : 0,
756                         spin ? spin_speed : 0,
757                         spin ? spin_speed : 0,
758                         1.0,
759                         move ? wander_speed : 0,
760                         True);
761
762     e->trackball = gltrackball_init ();
763  }
764
765  if ((e->glx_context = init_GL(mi)) != NULL) {
766       reshape_engine(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
767  } else {
768      MI_CLEARWINDOW(mi);
769  }
770  glClearColor(0.0,0.0,0.0,0.0);
771  glShadeModel(GL_SMOOTH);
772  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
773  glEnable(GL_DEPTH_TEST);
774  glEnable(GL_LIGHTING);
775  glEnable(GL_LIGHT0);
776  glEnable(GL_NORMALIZE);
777  make_tables();
778  makeshaft();
779  makepiston();
780 }
781
782 Bool engine_handle_event (ModeInfo *mi, XEvent *event) {
783    Engine *e = &engine[MI_SCREEN(mi)];
784
785    if (event->xany.type == ButtonPress &&
786        event->xbutton.button & Button1)
787    {
788        e->button_down_p = True;
789        gltrackball_start (e->trackball,
790                           event->xbutton.x, event->xbutton.y,
791                           MI_WIDTH (mi), MI_HEIGHT (mi));
792        movepaused = 1;
793        return True;
794    }
795    else if (event->xany.type == ButtonRelease &&
796             event->xbutton.button & Button1) {
797        e->button_down_p = False;
798        movepaused = 0;
799        return True;
800    }
801    else if (event->xany.type == MotionNotify &&
802             e->button_down_p) {
803       gltrackball_track (e->trackball,
804                          event->xmotion.x, event->xmotion.y,
805                          MI_WIDTH (mi), MI_HEIGHT (mi));
806       return True;
807    }
808   return False;
809 }
810
811 void draw_engine(ModeInfo *mi) {
812 Engine *e = &engine[MI_SCREEN(mi)];
813 Window w = MI_WINDOW(mi);
814 Display *disp = MI_DISPLAY(mi);
815
816   if (!e->glx_context)
817       return;
818
819   glXMakeCurrent(disp, w, *(e->glx_context));
820
821
822   display(e);
823
824   if(mi->fps_p) do_fps(mi);
825   glFinish(); 
826   glXSwapBuffers(disp, w);
827 }
828
829 void release_engine(ModeInfo *mi) {
830
831   if (engine != NULL) {
832    (void) free((void *) engine);
833    engine = NULL;
834   }
835   FreeAllGL(MI);
836 }
837
838 #endif