http://www.mirrorservice.org/sites/master.us.finkmirrors.net/distfiles/md5/fa43fdd68d...
[xscreensaver] / hacks / glx / engine.c
1 /*
2  * engine.c - GL representation of a 4 stroke engine
3  *
4  * version 2.00
5  *
6  * Copyright (C) 2001 Ben Buxton (bb@cactii.net)
7  * modified by Ed Beroset (beroset@mindspring.com)
8  *  new to 2.0 version is:
9  *  - command line argument to specify number of cylinders
10  *  - command line argument to specify included angle of engine
11  *  - removed broken command line argument to specify rotation speed
12  *  - included crankshaft shapes and firing orders for real engines
13  *    verified using the Bosch _Automotive Handbook_, 5th edition, pp 402,403
14  *
15  * Permission to use, copy, modify, distribute, and sell this software and its
16  * documentation for any purpose is hereby granted without fee, provided that
17  * the above copyright notice appear in all copies and that both that
18  * copyright notice and this permission notice appear in supporting
19  * documentation.  No representations are made about the suitability of this
20  * software for any purpose.  It is provided "as is" without express or
21  * implied warranty.
22  */
23
24
25 #include <X11/Intrinsic.h>
26
27 #ifdef STANDALONE
28 # define PROGCLASS                                      "Engine"
29 # define HACK_INIT                                      init_engine
30 # define HACK_DRAW                                      draw_engine
31 # define HACK_HANDLE_EVENT                              engine_handle_event
32 # define HACK_RESHAPE                                   reshape_engine
33 # define EVENT_MASK                                     PointerMotionMask
34 # define engine_opts                                    xlockmore_opts
35 /* insert defaults here */
36
37 #define DEF_ENGINE "(none)"
38
39 #define DEFAULTS        "*delay:           10000        \n" \
40                         "*showFPS:         False        \n" \
41                         "*move:            True         \n" \
42                         "*spin:            True         \n" \
43                         "*engine:        " DEF_ENGINE  "\n" \
44
45 # include "xlockmore.h"                         /* from the xscreensaver distribution */
46 #else  /* !STANDALONE */
47 # include "xlock.h"                                     /* from the xlockmore distribution */
48 #endif /* !STANDALONE */
49
50 #include "rotator.h"
51 #include "gltrackball.h"
52
53 /* lifted from lament.c */
54 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
55 #define RANDSIGN() ((random() & 1) ? 1 : -1)
56
57
58 #ifdef USE_GL
59
60 #include <GL/glu.h>
61
62
63
64
65 #undef countof
66 #define countof(x) (sizeof((x))/sizeof((*x)))
67
68 static int engineType;
69 static char *which_engine;
70 static int move;
71 static int movepaused = 0;
72 static int spin;
73
74 static XrmOptionDescRec opts[] = {
75   {"-engine", ".engine.engine", XrmoptionSepArg, DEF_ENGINE },
76   {"-move", ".engine.move", XrmoptionNoArg, (caddr_t) "true" },
77   {"+move", ".engine.move", XrmoptionNoArg, (caddr_t) "false" },
78   {"-spin", ".engine.spin", XrmoptionNoArg, (caddr_t) "true" },
79   {"+spin", ".engine.spin", XrmoptionNoArg, (caddr_t) "false" },
80 };
81
82 static argtype vars[] = {
83   {(caddr_t *) &which_engine, "engine", "Engine", DEF_ENGINE, t_String},
84   {(caddr_t *) &move, "move", "Move", "True", t_Bool},
85   {(caddr_t *) &spin, "spin", "Spin", "True", t_Bool},
86 };
87
88 ModeSpecOpt engine_opts = {countof(opts), opts, countof(vars), vars, NULL};
89
90 #ifdef USE_MODULES
91 ModStruct   engine_description =
92 {"engine", "init_engine", "draw_engine", "release_engine",
93  "draw_engine", "init_engine", NULL, &engine_opts,
94  1000, 1, 2, 1, 4, 1.0, "",
95  "A four stroke engine", 0, NULL};
96
97 #endif
98
99 typedef struct {
100   GLXContext *glx_context;
101   Window window;
102   GLfloat x, y, z; /* position */
103   GLfloat dx, dy, dz; /* position */
104   GLfloat an1, an2, an3; /* internal angle */
105   GLfloat nx, ny, nz; /* spin vector */
106   GLfloat a; /* spin angle */
107   GLfloat da; /* spin speed */
108   rotator *rot;
109   trackball_state *trackball;
110   Bool button_down_p;
111 } Engine;
112
113 static Engine *engine = NULL;
114
115 #include <math.h>
116 #include <sys/time.h>
117 #include <stdio.h>
118 #include <stdlib.h>
119
120 #ifndef M_PI
121 #define M_PI 3.14159265
122 #endif
123
124 /* these defines are used to provide symbolic means
125  * by which to refer to various portions or multiples
126  * of a cyle in degrees
127  */
128 #define HALFREV 180
129 #define ONEREV 360
130 #define TWOREV 720
131
132 #define MOVE_MULT 0.05
133
134 #define RAND_RANGE(min, max) ((min) + (max - min) * f_rand())
135
136 float crankOffset;
137 float crankWidth = 1.5;
138
139 int win_w, win_h;
140
141 static GLfloat viewer[] = {0.0, 0.0, 30.0};
142 static GLfloat lookat[] = {0.0, 0.0, 0.0};
143 static GLfloat lightpos[] = {7.0, 7.0, 12, 1.0};
144 GLfloat light_sp[] = {0.8, 0.8, 0.8, 0.5};
145 static GLfloat red[] = {1.0, 0, 0, 1.0};
146 static GLfloat green[] = {0.0, 1, 0, 1.0};
147 static GLfloat blue[] = {0, 0, 1, 1.0};
148 static GLfloat white[] = {1.0, 1, 1, 1.0};
149 static GLfloat yellow_t[] = {1, 1, 0, 0.4};
150
151 GLvoid normal(GLfloat [], GLfloat [], GLfloat [], 
152                   GLfloat *, GLfloat *, GLfloat *);
153
154 float sin_table[TWOREV];
155 float cos_table[TWOREV];
156 float tan_table[TWOREV];
157
158 /* 
159  * this table represents both the firing order and included angle of engine.
160  * To simplify things, we always number from 0 starting at the flywheel and 
161  * moving down the crankshaft toward the back of the engine.  This doesn't
162  * always match manufacturer's schemes.  For example, the Porsche 911 engine
163  * is a flat six with the following configuration (Porsche's numbering):
164  *
165  *    3  2  1
166  *  |=            firing order is 1-6-2-4-3-5 in this diagram
167  *     6  5  4
168  *
169  * We renumber these using our scheme but preserve the effective firing order:
170  *
171  *   0  2  4
172  * |=             firing order is 4-1-2-5-0-3 in this diagram
173  *    1  3  5
174  *
175  * To avoid going completely insane, we also reorder these so the newly 
176  * renumbered cylinder 0 is always first: 0-3-4-1-2-5
177  *   
178  * For a flat 6, the included angle is 180 degrees (0 would be a inline engine).
179  * Because these are all four-stroke engines, each piston goes through
180  * 720 degrees of rotation for each time the spark plug sparks, so in this case,
181  * we would use the following angles:
182  * cylinder     firing order     angle
183  * --------     ------------     -----
184  *    0               0             0
185  *    1               3           360
186  *    2               4           240
187  *    3               1           600
188  *    4               2           480
189  *    5               5           120
190  *
191  */
192
193 typedef struct 
194 {
195     int cylinders;
196     int includedAngle;
197     int pistonAngle[12];  /* twelve cylinders should suffice... */
198     int speed;          /*  step size in degrees for engine speed */
199     const char *engineName;  /* currently unused */
200 } engine_type;
201
202 engine_type engines[] = {
203     { 3,   0, { 0, 240, 480,   0,   0,   0,
204                 0,   0,   0,   0,   0,   0 }, 12,
205      "Honda Insight" },
206     { 4,   0, { 0, 180, 540, 360,   0,   0,
207                 0,   0,   0,   0,   0,   0 }, 12,
208      "BMW M3" },
209     { 4, 180, { 0, 360, 180, 540,   0,   0,
210                 0,   0,   0,   0,   0,   0 }, 12,
211      "VW Beetle" },
212     { 5,   0, { 0, 576, 144, 432, 288,   0,
213                 0,   0,   0,   0,   0,   0 }, 12,
214      "Audi Quattro" },
215     { 6,   0, { 0, 240, 480, 120, 600, 360,
216                 0,   0,   0,   0,   0,   0 }, 12,
217      "BMW M5" },
218     { 6,  90, { 0, 360, 480, 120, 240, 600,
219                 0,   0,   0,   0,   0,   0 }, 12,
220      "Subaru XT" },
221     { 6, 180, { 0, 360, 240, 600, 480, 120,
222                 0,   0,   0,   0,   0,   0 }, 12,
223      "Porsche 911" },
224     { 8,  90, { 0, 450,  90, 180, 270, 360,
225               540, 630,   0,   0,   0,   0 }, 15,
226      "Corvette Z06" },
227     {10,  90, { 0,  72, 432, 504, 288, 360,
228               144, 216, 576, 648,   0,   0 }, 12,
229      "Dodge Viper" },
230     {12,  60, { 0, 300, 240, 540, 480,  60,
231               120, 420, 600, 180, 360, 660 }, 12,
232      "Jaguar XKE" },
233 };
234
235 /* this define is just a little shorter way of referring to members of the 
236  * table above
237  */
238 #define ENG engines[engineType]
239
240 /* given a number of cylinders and an included angle, finds matching engine */
241 int find_engine(const char *name)
242 {
243   int i;
244
245   if (!name || !*name || !strcasecmp (name, "(none)"))
246     return (random() % countof(engines));
247
248   for (i = 0; i < countof(engines); i++) {
249     if (!strcasecmp(name, engines[i].engineName))
250       return i;
251   }
252
253   fprintf (stderr, "%s: unknown engine type \"%s\"\n", progname, name);
254   fprintf (stderr, "%s: available models are:\n", progname);
255   for (i = 0; i < countof(engines); i++) {
256     fprintf (stderr, "\t %-13s (%d cylinders",
257              engines[i].engineName, engines[i].cylinders);
258     if (engines[i].includedAngle == 0)
259       fprintf (stderr, ")\n");
260     else if (engines[i].includedAngle == 180)
261       fprintf (stderr, ", flat)\n");
262     else
263       fprintf (stderr, ", V)\n");
264   }
265   exit(1);
266 }
267
268 /* we use trig tables to speed things up - 200 calls to sin()
269  in one frame can be a bit harsh..
270 */
271
272 void make_tables(void) {
273 int i;
274 float f;
275
276   f = ONEREV / (M_PI * 2);
277   for (i = 0 ; i <= TWOREV ; i++) {
278     sin_table[i] = sin(i/f);
279   }
280   for (i = 0 ; i <= TWOREV ; i++) {
281     cos_table[i] = cos(i/f);
282   }
283   for (i = 0 ; i <= TWOREV ; i++) {
284     tan_table[i] = tan(i/f);
285   }
286 }
287
288 /* if inner and outer are the same, we draw a cylinder, not a tube */
289 /* for a tube, endcaps is 0 (none), 1 (left), 2 (right) or 3(both) */
290 /* angle is how far around the axis to go (up to 360) */
291
292 void cylinder (GLfloat x, GLfloat y, GLfloat z, 
293     float length, float outer, float inner, int endcaps, int sang, int eang) {
294 int a; /* current angle around cylinder */
295 int b = 0; /* previous */
296 int angle, norm, step, sangle;
297 float z1, y1, z2, y2, ex=0;
298 float y3, z3;
299 float Z1, Y1, Z2, Y2, xl, Y3, Z3;
300 GLfloat y2c[TWOREV], z2c[TWOREV];
301 GLfloat ony, onz; /* previous normals */
302 int nsegs, tube = 0;
303
304   glPushMatrix();
305   nsegs = outer*(MAX(win_w, win_h)/200);
306   nsegs = MAX(nsegs, 6);
307   nsegs = MAX(nsegs, 40);
308   if (nsegs % 2)
309      nsegs += 1;
310   sangle = sang;
311   angle = eang;
312   ony = onz = 0;
313   z1 = cos_table[sangle]*outer+z; y1 = sin_table[sangle] * outer+y;
314   Z1 = cos_table[sangle] * inner+z; Y1 = sin_table[sangle]*inner+y ; 
315   Z2 = z;
316   Y2 = y;
317   xl = x + length;
318   if (inner < outer && endcaps < 3) tube = 1;
319   step = ONEREV/nsegs;
320
321   glBegin(GL_QUADS);
322   for (a = sangle ; a <= angle || b <= angle ; a+= step) {
323     y2=outer*(float)sin_table[a]+y;
324     z2=outer*(float)cos_table[a]+z;
325     y3=outer*(float)sin_table[a+step]+y;
326     z3=outer*(float)cos_table[a+step]+z;
327     if (endcaps)
328        y2c[a] = y2; z2c[a] = z2; /* cache for later */
329     if (tube) {
330       Y2=inner*(float)sin_table[a]+y;
331       Z2=inner*(float)cos_table[a]+z;
332       Y3=inner*(float)sin_table[a+step]+y;
333       Z3=inner*(float)cos_table[a+step]+z;
334     }
335     glNormal3f(0, y1, z1);
336     glVertex3f(x,y1,z1);
337     glVertex3f(xl,y1,z1);
338     glNormal3f(0, y2, z2);
339     glVertex3f(xl,y2,z2);
340     glVertex3f(x,y2,z2);
341     if (a == sangle && angle - sangle < ONEREV) {
342       if (tube)
343         glVertex3f(x, Y1, Z1);
344       else
345         glVertex3f(x, y, z);
346       glVertex3f(x, y1, z1);
347       glVertex3f(xl, y1, z1);
348       if (tube)
349         glVertex3f(xl, Z1, Z1);
350       else
351         glVertex3f(xl, y, z);
352     }
353     if (tube) {
354       if (endcaps != 1) {
355         glNormal3f(-1, 0, 0); /* left end */
356         glVertex3f(x, y1, z1);
357         glVertex3f(x, y2, z2);
358         glVertex3f(x, Y2, Z2);
359         glVertex3f(x, Y1, Z1);
360       }
361
362       glNormal3f(0, -Y1, -Z1); /* inner surface */
363       glVertex3f(x, Y1, Z1);
364       glVertex3f(xl, Y1, Z1);
365       glNormal3f(0, -Y2, -Z2);
366       glVertex3f(xl, Y2, Z2);
367       glVertex3f(x, Y2, Z2);
368
369       if (endcaps != 2) {
370         glNormal3f(1, 0, 0); /* right end */
371         glVertex3f(xl, y1, z1);
372         glVertex3f(xl, y2, z2);
373         glVertex3f(xl, Y2, Z2);
374         glVertex3f(xl, Y1, Z1);
375       }
376     }
377
378     z1=z2; y1=y2;
379     Z1=Z2; Y1=Y2;
380     b = a;
381   }
382   glEnd();
383
384   if (angle - sangle < ONEREV) {
385     GLfloat nx, ny, nz;
386     GLfloat v1[3], v2[3], v3[3];
387     v1[0] = x; v1[1] = y; v1[2] = z;
388     v2[0] = x; v2[1] = y1; v2[2] = z1;
389     v3[0] = xl; v3[1] = y1; v3[2] = z1;
390     normal(&v2[0], &v1[0], &v3[0], &nx, &ny, &nz);
391     glBegin(GL_QUADS);
392     glNormal3f(nx, ny, nz);
393     glVertex3f(x, y, z);
394     glVertex3f(x, y1, z1);
395     glVertex3f(xl, y1, z1);
396     glVertex3f(xl, y, z);
397     glEnd();
398   }
399   if (endcaps) {
400     GLfloat end, start;
401     if (tube) {
402       if (endcaps == 1) {
403         end = 0;
404         start = 0;
405       } else if (endcaps == 2) {
406         start = end = length+0.01;
407       } else {
408         end = length+0.02; start = -0.01;
409       }
410       norm = (ex == length+0.01) ? -1 : 1;
411     } else  {
412       end = length;
413       start = 0;
414       norm = -1;
415     }
416
417     for(ex = start ; ex <= end ; ex += length) {
418       z1 = outer*cos_table[sangle]+z;
419       y1 = y+sin_table[sangle]*outer;
420       step = ONEREV/nsegs;
421       glBegin(GL_TRIANGLES);
422       b = 0;
423       for (a = sangle ; a <= angle || b <= angle; a+= step) {
424           glNormal3f(norm, 0, 0);
425           glVertex3f(x+ex,y, z);
426           glVertex3f(x+ex,y1,z1);
427           glVertex3f(x+ex,y2c[a],z2c[a]);
428         y1 = y2c[a]; z1 = z2c[a];
429         b = a;
430       }
431       if (!tube) norm = 1;
432       glEnd();
433     }
434   }
435   glPopMatrix();
436 }
437
438 /* this is just a convenience function to make a solid rod */
439 void rod (GLfloat x, GLfloat y, GLfloat z, float length, float diameter) {
440     cylinder(x, y, z, length, diameter, diameter, 3, 0, ONEREV);
441 }
442
443 GLvoid normal(GLfloat v1[], GLfloat v2[], GLfloat v3[], 
444                   GLfloat *nx, GLfloat *ny, GLfloat *nz)
445 {
446    GLfloat x, y, z, X, Y, Z;
447
448    x = v2[0]-v1[0];
449    y = v2[1]-v1[1];
450    z = v2[2]-v1[2];
451    X = v3[0]-v1[0];
452    Y = v3[1]-v1[1];
453    Z = v3[2]-v1[2];
454
455    *nx = Y*z - Z*y;
456    *ny = Z*x - X*z;
457    *nz = X*y - Y*x;
458
459
460
461
462
463 void Rect(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h,
464             GLfloat t) {
465 GLfloat yh;
466 GLfloat xw;
467 GLfloat zt;
468
469   yh = y+h; xw = x+w; zt = z - t;
470
471   glBegin(GL_QUADS); /* front */
472     glNormal3f(0, 0, 1);
473     glVertex3f(x, y, z);
474     glVertex3f(x, yh, z);
475     glVertex3f(xw, yh, z);
476     glVertex3f(xw, y, z);
477   /* back */
478     glNormal3f(0, 0, -1);
479     glVertex3f(x, y, zt);
480     glVertex3f(x, yh, zt);
481     glVertex3f(xw, yh, zt);
482     glVertex3f(xw, y, zt);
483   /* top */
484     glNormal3f(0, 1, 0);
485     glVertex3f(x, yh, z);
486     glVertex3f(x, yh, zt);
487     glVertex3f(xw, yh, zt);
488     glVertex3f(xw, yh, z);
489   /* bottom */
490     glNormal3f(0, -1, 0);
491     glVertex3f(x, y, z);
492     glVertex3f(x, y, zt);
493     glVertex3f(xw, y, zt);
494     glVertex3f(xw, y, z);
495   /* left */
496     glNormal3f(-1, 0, 0);
497     glVertex3f(x, y, z);
498     glVertex3f(x, y, zt);
499     glVertex3f(x, yh, zt);
500     glVertex3f(x, yh, z);
501   /* right */
502     glNormal3f(1, 0, 0);
503     glVertex3f(xw, y, z);
504     glVertex3f(xw, y, zt);
505     glVertex3f(xw, yh, zt);
506     glVertex3f(xw, yh, z);
507   glEnd();
508 }
509
510 void makepiston(void) {
511 GLfloat colour[] = {0.6, 0.6, 0.6, 1.0};
512 int i;
513   
514   i = glGenLists(1);
515   glNewList(i, GL_COMPILE);
516   glRotatef(90, 0, 0, 1);
517   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colour);
518   glMaterialfv(GL_FRONT, GL_SPECULAR, colour);
519   glMateriali(GL_FRONT, GL_SHININESS, 20);
520   cylinder(0, 0, 0, 2, 1, 0.7, 2, 0, ONEREV); /* body */
521   colour[0] = colour[1] = colour[2] = 0.2;
522   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colour);
523   cylinder(1.6, 0, 0, 0.1, 1.05, 1.05, 0, 0, ONEREV); /* ring */
524   cylinder(1.8, 0, 0, 0.1, 1.05, 1.05, 0, 0, ONEREV); /* ring */
525   glEndList();
526 }
527
528 void CrankBit(GLfloat x) {
529   Rect(x, -1.4, 0.5, 0.2, 1.8, 1);
530   cylinder(x, -0.5, 0, 0.2, 2, 2, 1, 60, 120);
531 }
532
533 void boom(GLfloat x, GLfloat y, int s) {
534 static GLfloat red[] = {0, 0, 0, 0.9};
535 static GLfloat lpos[] = {0, 0, 0, 1};
536 static GLfloat d = 0, wd;
537 int flameOut = 720/ENG.speed/ENG.cylinders;
538 static int time = 0;
539
540   if (time == 0 && s) {
541     red[0] = red[1] = 0;
542     d = 0.05;
543     time++;
544     glEnable(GL_LIGHT1); 
545   } else if (time == 0 && !s) {
546     return;
547   } else if (time >= 8 && time < flameOut && !s) {
548     time++;
549     red[0] -= 0.2; red[1] -= 0.1;
550     d-= 0.04;
551   } else if (time >= flameOut) {
552     time = 0;
553     glDisable(GL_LIGHT1);
554     return;
555   } else {
556     red[0] += 0.2; red[1] += 0.1;
557     d+= 0.04;
558     time++;
559   }
560   lpos[0] = x-d; lpos[1] = y;
561   glLightfv(GL_LIGHT1, GL_POSITION, lpos);
562   glLightfv(GL_LIGHT1, GL_DIFFUSE, red);
563   glLightfv(GL_LIGHT1, GL_SPECULAR, red);
564   glLighti(GL_LIGHT1, GL_LINEAR_ATTENUATION, 1.3);
565   glLighti(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0);
566
567   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
568   wd = d*3;
569   if (wd > 0.7) wd = 0.7;
570   glEnable(GL_BLEND);
571   glDepthMask(GL_FALSE);
572   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
573   rod(x, y, 0, d, wd);
574   glDepthMask(GL_TRUE);
575   glDisable(GL_BLEND);
576 }
577
578 void display(Engine *e) {
579
580 static int a = 0;
581 GLfloat zb, yb;
582 static GLfloat ln[730], yp[730], ang[730];
583 static int ln_init = 0;
584 static int lastPlug = 0;
585 int half;
586 int sides;
587 int j, b;
588 static float rightSide;
589
590   glEnable(GL_LIGHTING);
591   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
592   glLoadIdentity();
593   gluLookAt(viewer[0], viewer[1], viewer[2], lookat[0], lookat[1], lookat[2], 
594         0.0, 1.0, 0.0);
595   glPushMatrix();
596   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
597   glLightfv(GL_LIGHT0, GL_SPECULAR, light_sp);
598   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_sp);
599
600   if (move) {
601     double x, y, z;
602     get_position (e->rot, &x, &y, &z, !e->button_down_p);
603     glTranslatef(x*16-9, y*14-7, z*16-10);
604   }
605   if (spin) {
606     double x, y, z;
607     gltrackball_rotate (e->trackball);
608     get_rotation(e->rot, &x, &y, &z, !e->button_down_p);
609     glRotatef(x*ONEREV, 1.0, 0.0, 0.0);
610     glRotatef(y*ONEREV, 0.0, 1.0, 0.0);
611     glRotatef(x*ONEREV, 0.0, 0.0, 1.0);
612   }
613
614 /* So the rotation appears around the centre of the engine */
615   glTranslatef(-5, 0, 0); 
616
617 /* crankshaft */
618   glPushMatrix();
619   glRotatef(a, 1, 0, 0);
620   glCallList(1);
621   glPopMatrix();
622
623   /* init the ln[] matrix for speed */
624   if (ln_init == 0) {
625     for (ln_init = 0 ; ln_init < 730 ; ln_init++) {
626       zb = sin_table[ln_init];
627       yb = cos_table[ln_init];
628       /* y ordinate of piston */
629       yp[ln_init] = yb + sqrt(25 - (zb*zb)); 
630       /* length of rod */
631       ln[ln_init] = sqrt(zb*zb + (yb-yp[ln_init])*(yb-yp[ln_init])); 
632       /* angle of connecting rod */
633       ang[ln_init] = asin(zb/5)*57; 
634       ang[ln_init] *= -1;
635     }
636   }
637
638   glPushMatrix();
639   sides = (ENG.includedAngle == 0) ? 1 : 2;
640   for (half = 0; half < sides; half++, glRotatef(ENG.includedAngle,1,0,0))
641   {
642     /* pistons */
643       /* glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white); */
644       for (j = 0; j < ENG.cylinders; j += sides)
645       {
646         b = (a + ENG.pistonAngle[j+half]) % ONEREV;
647         glPushMatrix();
648         glTranslatef(crankWidth/2 + crankOffset*(j+half), yp[b]-0.3, 0);
649         glCallList(2);
650         glPopMatrix();
651       }
652     /* spark plugs */
653       glPushMatrix();
654       glRotatef(90, 0, 0, 1); 
655       glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
656       for (j = 0; j < ENG.cylinders; j += sides) 
657       {
658         cylinder(8.5, -crankWidth/2-crankOffset*(j+half), 0, 
659             0.5, 0.4, 0.3, 1, 0, ONEREV); 
660       }
661       glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
662       for (j = 0; j < ENG.cylinders; j += sides)
663       {
664         rod(8, -crankWidth/2-crankOffset*(j+half), 0, 0.5, 0.2); 
665         rod(9, -crankWidth/2-crankOffset*(j+half), 0, 1, 0.15); 
666       }   
667
668      /* rod */
669       glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
670       for (j = 0; j < ENG.cylinders; j += sides)
671       {
672           b = (a+HALFREV+ENG.pistonAngle[j+half]) % TWOREV; 
673           glPushMatrix();
674           glRotatef(ang[b], 0, 1, 0);
675           rod(-cos_table[b], -crankWidth/2-crankOffset*(j+half), -sin_table[b], 
676                 ln[b], 0.2);
677           glPopMatrix();
678       }
679       glPopMatrix();
680
681         /* engine block */
682       glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow_t);
683       glEnable(GL_BLEND);
684       glDepthMask(GL_FALSE);
685       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
686       rightSide = (sides > 1) ? 0 : 1.6;
687     /* left plate */
688       Rect(-crankWidth/2, -0.5,  1, 0.2, 9, 2);
689     /* right plate */
690       Rect(0.3+crankOffset*ENG.cylinders-rightSide, -0.5, 1, 0.2, 9, 2);
691     /* head plate */
692       Rect(-crankWidth/2+0.2, 8.3, 1, 
693             crankWidth/2+0.1+crankOffset*ENG.cylinders-rightSide, 0.2, 2);
694     /* front rail */
695       Rect(-crankWidth/2+0.2, 3, 1, 
696             crankWidth/2+0.1+crankOffset*ENG.cylinders-rightSide, 0.2, 0.2);
697     /* back rail */
698       Rect(-crankWidth/2+0.2, 3, -1+0.2, 
699             crankWidth/2+0.1+crankOffset*ENG.cylinders-rightSide, 0.2, 0.2);
700     /* plates between cylinders */
701       for (j=0; j < ENG.cylinders - (sides == 1); j += sides)
702         Rect(0.4+crankWidth+crankOffset*(j-half), 3, 1, 1, 5.3, 2);
703       glDepthMask(GL_TRUE);
704   }
705   glPopMatrix();
706
707         /* see which of our plugs should fire now, if any */
708   for (j = 0; j < ENG.cylinders; j++)
709   {
710     if (0 == ((a + ENG.pistonAngle[j]) % TWOREV))
711     {
712         glPushMatrix();
713         if (j & 1) 
714             glRotatef(ENG.includedAngle,1,0,0);
715         glRotatef(90, 0, 0, 1); 
716         boom(8, -crankWidth/2-crankOffset*j, 1); 
717         lastPlug = j;
718         glPopMatrix();
719     }
720   }
721
722   if (lastPlug != j)
723   {
724     /* this code causes the last plug explosion to dim gradually */
725     if (lastPlug & 1) 
726       glRotatef(ENG.includedAngle, 1, 0, 0);
727     glRotatef(90, 0, 0, 1); 
728     boom(8, -crankWidth/2-crankOffset*lastPlug, 0); 
729   }
730   glDisable(GL_BLEND);
731
732   a += ENG.speed; 
733   if (a >= TWOREV) 
734     a = 0;
735   glPopMatrix();
736   glFlush();
737 }
738
739 void makeshaft (void) {
740 int i;
741 int j;
742 const static float crankThick = 0.2;
743 const static float crankDiam = 0.3;
744
745   i = glGenLists(1);
746   glNewList(i, GL_COMPILE);
747
748   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
749   /* draw the flywheel */
750   cylinder(-2.5, 0, 0, 1, 3, 2.5, 0, 0, ONEREV);
751   Rect(-2, -0.3, 2.8, 0.5, 0.6, 5.6);
752   Rect(-2, -2.8, 0.3, 0.5, 5.6, 0.6);
753
754   /* now make each of the shaft bits between the cranks, 
755    * starting from the flywheel end which is at X-coord 0. 
756    * the first cranskhaft bit is always 2 units long 
757    */
758   rod(-2, 0, 0, 2, crankDiam);
759
760   /* Each crank is crankWidth units wide and the total width of a cylinder assembly
761    * is 3.3 units. For inline engines, there is just a single crank per cylinder
762    * width.  For other engine configurations, there is a crank between each pair
763    * of adjacent cylinders on one side of the engine, so the crankOffset length is 
764    * halved.
765    */
766   crankOffset = 3.3;
767   if (ENG.includedAngle != 0)
768     crankOffset /= 2;
769   for (j = 0; j < ENG.cylinders - 1; j++)
770     rod(crankWidth - crankThick + crankOffset*j, 0, 0, 
771         crankOffset - crankWidth + 2 * crankThick, crankDiam);
772   /* the last bit connects to the engine wall on the non-flywheel end */
773   rod(crankWidth - crankThick + crankOffset*j, 0, 0, 0.9, crankDiam);
774
775
776   for (j = 0; j < ENG.cylinders; j++)
777   {
778     glPushMatrix();
779     if (j & 1)
780         glRotatef(HALFREV+ENG.pistonAngle[j]+ENG.includedAngle,1,0,0);
781     else
782         glRotatef(HALFREV+ENG.pistonAngle[j],1,0,0);
783     /* draw wrist pin */
784     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
785     rod(crankOffset*j, -1.0, 0.0, crankWidth, crankDiam);
786     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
787     /* draw right part of crank */
788     CrankBit(crankOffset*j); 
789     /* draw left part of crank */
790     CrankBit(crankWidth-crankThick+crankOffset*j);
791     glPopMatrix();
792   }
793   glEndList();
794 }
795
796 void reshape_engine(ModeInfo *mi, int width, int height)
797 {
798
799  glViewport(0,0,(GLint)width, (GLint) height);
800  glMatrixMode(GL_PROJECTION);
801  glLoadIdentity();
802  glFrustum(-1.0,1.0,-1.0,1.0,1.5,70.0);
803  glMatrixMode(GL_MODELVIEW);
804  win_h = height; win_w = width;
805 }
806
807
808 void init_engine(ModeInfo *mi)
809 {
810 int screen = MI_SCREEN(mi);
811 Engine *e;
812
813  if (engine == NULL) {
814    if ((engine = (Engine *) calloc(MI_NUM_SCREENS(mi),
815                                         sizeof(Engine))) == NULL)
816           return;
817  }
818  e = &engine[screen];
819  e->window = MI_WINDOW(mi);
820
821  e->x = e->y = e->z = e->a = e->an1 = e->nx = e->ny = e->nz = 
822  e->dx = e->dy = e->dz = e->da = 0;
823
824  if (move) {
825    e->dx = (float)(random() % 1000)/30000;
826    e->dy = (float)(random() % 1000)/30000;
827    e->dz = (float)(random() % 1000)/30000;
828  } else {
829   viewer[0] = 0; viewer[1] = 2; viewer[2] = 18;
830   lookat[0] = 0; lookat[1] = 0; lookat[2] = 0; 
831
832  }
833  if (spin) {
834    e->da = (float)(random() % 1000)/125 - 4;
835    e->nx = (float)(random() % 100) / 100;
836    e->ny = (float)(random() % 100) / 100;
837    e->nz = (float)(random() % 100) / 100;
838  }
839
840  {
841    double spin_speed = 1.0;
842    double wander_speed = 0.03;
843
844  e->rot = make_rotator (spin ? spin_speed : 0,
845                         spin ? spin_speed : 0,
846                         spin ? spin_speed : 0,
847                         1.0,
848                         move ? wander_speed : 0,
849                         True);
850
851     e->trackball = gltrackball_init ();
852  }
853
854  if ((e->glx_context = init_GL(mi)) != NULL) {
855       reshape_engine(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
856  } else {
857      MI_CLEARWINDOW(mi);
858  }
859  glClearColor(0.0,0.0,0.0,0.0);
860  glShadeModel(GL_SMOOTH);
861  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
862  glEnable(GL_DEPTH_TEST);
863  glEnable(GL_LIGHTING);
864  glEnable(GL_LIGHT0);
865  glEnable(GL_NORMALIZE);
866  make_tables();
867  engineType = find_engine(which_engine);
868  makeshaft();
869  makepiston();
870 }
871
872 Bool engine_handle_event (ModeInfo *mi, XEvent *event) {
873    Engine *e = &engine[MI_SCREEN(mi)];
874
875    if (event->xany.type == ButtonPress &&
876        event->xbutton.button & Button1)
877    {
878        e->button_down_p = True;
879        gltrackball_start (e->trackball,
880                           event->xbutton.x, event->xbutton.y,
881                           MI_WIDTH (mi), MI_HEIGHT (mi));
882        movepaused = 1;
883        return True;
884    }
885    else if (event->xany.type == ButtonRelease &&
886             event->xbutton.button & Button1) {
887        e->button_down_p = False;
888        movepaused = 0;
889        return True;
890    }
891    else if (event->xany.type == MotionNotify &&
892             e->button_down_p) {
893       gltrackball_track (e->trackball,
894                          event->xmotion.x, event->xmotion.y,
895                          MI_WIDTH (mi), MI_HEIGHT (mi));
896       return True;
897    }
898   return False;
899 }
900
901 void draw_engine(ModeInfo *mi) {
902 Engine *e = &engine[MI_SCREEN(mi)];
903 Window w = MI_WINDOW(mi);
904 Display *disp = MI_DISPLAY(mi);
905
906   if (!e->glx_context)
907       return;
908
909   glXMakeCurrent(disp, w, *(e->glx_context));
910
911
912   display(e);
913
914   if(mi->fps_p) do_fps(mi);
915   glFinish(); 
916   glXSwapBuffers(disp, w);
917 }
918
919 void release_engine(ModeInfo *mi) {
920
921   if (engine != NULL) {
922    (void) free((void *) engine);
923    engine = NULL;
924   }
925   FreeAllGL(MI);
926 }
927
928 #endif