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