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