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