2 * engine.c - GL representation of a 4 stroke engine
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
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
25 #define DEFAULTS "*delay: 30000 \n" \
26 "*showFPS: False \n" \
27 "*titleFont: -*-helvetica-medium-r-normal-*-180-*\n" \
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 */
37 #include "gltrackball.h"
39 /* lifted from lament.c */
40 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
41 #define RANDSIGN() ((random() & 1) ? 1 : -1)
46 #define DEF_ENGINE "(none)"
47 #define DEF_TITLES "False"
48 #define DEF_SPIN "True"
49 #define DEF_MOVE "True"
52 #define countof(x) (sizeof((x))/sizeof((*x)))
54 static char *which_engine;
57 static Bool do_titles;
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" },
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},
76 ENTRYPOINT ModeSpecOpt engine_opts = {countof(opts), opts, countof(vars), vars, NULL};
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};
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
95 #define MOVE_MULT 0.05
97 #define RAND_RANGE(min, max) ((min) + (max - min) * f_rand())
101 GLXContext *glx_context;
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 */
110 trackball_state *trackball;
123 float sin_table[TWOREV];
124 float cos_table[TWOREV];
125 float tan_table[TWOREV];
128 GLfloat boom_lpos[4];
129 GLfloat boom_d, boom_wd;
132 GLfloat viewer[3], lookat[3];
135 GLfloat ln[730], yp[730], ang[730];
139 GLuint shaft_list, piston_list;
140 int shaft_polys, piston_polys;
144 static Engine *engine = NULL;
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};
154 static GLvoid normal(GLfloat [], GLfloat [], GLfloat [],
155 GLfloat *, GLfloat *, GLfloat *);
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):
165 * |= firing order is 1-6-2-4-3-5 in this diagram
168 * We renumber these using our scheme but preserve the effective firing order:
171 * |= firing order is 4-1-2-5-0-3 in this diagram
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
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:
182 * cylinder firing order angle
183 * -------- ------------ -----
197 int pistonAngle[12]; /* twelve cylinders should suffice... */
198 int speed; /* step size in degrees for engine speed */
199 const char *engineName; /* currently unused */
202 static const engine_type engines[] = {
203 { 3, 0, { 0, 240, 480, 0, 0, 0,
204 0, 0, 0, 0, 0, 0 }, 12,
206 { 4, 0, { 0, 180, 540, 360, 0, 0,
207 0, 0, 0, 0, 0, 0 }, 12,
209 { 4, 180, { 0, 360, 180, 540, 0, 0,
210 0, 0, 0, 0, 0, 0 }, 12,
212 { 5, 0, { 0, 576, 144, 432, 288, 0,
213 0, 0, 0, 0, 0, 0 }, 12,
215 { 6, 0, { 0, 240, 480, 120, 600, 360,
216 0, 0, 0, 0, 0, 0 }, 12,
218 { 6, 90, { 0, 360, 480, 120, 240, 600,
219 0, 0, 0, 0, 0, 0 }, 12,
221 { 6, 180, { 0, 360, 240, 600, 480, 120,
222 0, 0, 0, 0, 0, 0 }, 12,
224 { 8, 90, { 0, 450, 90, 180, 270, 360,
225 540, 630, 0, 0, 0, 0 }, 15,
227 {10, 90, { 0, 72, 432, 504, 288, 360,
228 144, 216, 576, 648, 0, 0 }, 12,
230 {12, 60, { 0, 300, 240, 540, 480, 60,
231 120, 420, 600, 180, 360, 660 }, 12,
235 /* this define is just a little shorter way of referring to members of the
238 #define ENG engines[e->engineType]
240 /* given a number of cylinders and an included angle, finds matching engine */
242 find_engine(char *name)
247 if (!name || !*name || !strcasecmp (name, "(none)"))
248 return (random() % countof(engines));
250 for (s = name; *s; s++)
251 if (*s == '-' || *s == '_') *s = ' ';
253 for (i = 0; i < countof(engines); i++) {
254 if (!strcasecmp(name, engines[i].engineName))
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");
268 fprintf (stderr, ", V)\n");
273 /* we use trig tables to speed things up - 200 calls to sin()
274 in one frame can be a bit harsh..
277 static void make_tables(Engine *e)
282 f = ONEREV / (M_PI * 2);
283 for (i = 0 ; i <= TWOREV ; i++) {
284 e->sin_table[i] = sin(i/f);
286 for (i = 0 ; i <= TWOREV ; i++) {
287 e->cos_table[i] = cos(i/f);
289 for (i = 0 ; i <= TWOREV ; i++) {
290 e->tan_table[i] = tan(i/f);
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) */
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)
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 Z1, Y1, Z2, Y2, xl;
307 GLfloat y2c[TWOREV], z2c[TWOREV];
311 nsegs = outer*(MAX(e->win_w, e->win_h)/200);
312 nsegs = MAX(nsegs, 6);
313 nsegs = MAX(nsegs, 40);
318 z1 = e->cos_table[sangle]*outer+z; y1 = e->sin_table[sangle] * outer+y;
319 Z1 = e->cos_table[sangle] * inner+z; Y1 = e->sin_table[sangle]*inner+y ;
323 if (inner < outer && endcaps < 3) tube = 1;
327 for (a = sangle ; a <= angle || b <= angle ; a+= step) {
328 y2=outer*(float)e->sin_table[a]+y;
329 z2=outer*(float)e->cos_table[a]+z;
331 y2c[a] = y2; z2c[a] = z2; /* cache for later */
333 Y2=inner*(float)e->sin_table[a]+y;
334 Z2=inner*(float)e->cos_table[a]+z;
336 glNormal3f(0, y1, z1);
338 glVertex3f(xl,y1,z1);
339 glNormal3f(0, y2, z2);
340 glVertex3f(xl,y2,z2);
343 if (a == sangle && angle - sangle < ONEREV) {
345 glVertex3f(x, Y1, Z1);
348 glVertex3f(x, y1, z1);
349 glVertex3f(xl, y1, z1);
351 glVertex3f(xl, Z1, Z1);
353 glVertex3f(xl, y, z);
358 glNormal3f(-1, 0, 0); /* left end */
359 glVertex3f(x, y1, z1);
360 glVertex3f(x, y2, z2);
361 glVertex3f(x, Y2, Z2);
362 glVertex3f(x, Y1, Z1);
366 glNormal3f(0, -Y1, -Z1); /* inner surface */
367 glVertex3f(x, Y1, Z1);
368 glVertex3f(xl, Y1, Z1);
369 glNormal3f(0, -Y2, -Z2);
370 glVertex3f(xl, Y2, Z2);
371 glVertex3f(x, Y2, Z2);
375 glNormal3f(1, 0, 0); /* right end */
376 glVertex3f(xl, y1, z1);
377 glVertex3f(xl, y2, z2);
378 glVertex3f(xl, Y2, Z2);
379 glVertex3f(xl, Y1, Z1);
390 if (angle - sangle < ONEREV) {
392 GLfloat v1[3], v2[3], v3[3];
393 v1[0] = x; v1[1] = y; v1[2] = z;
394 v2[0] = x; v2[1] = y1; v2[2] = z1;
395 v3[0] = xl; v3[1] = y1; v3[2] = z1;
396 normal(&v2[0], &v1[0], &v3[0], &nx, &ny, &nz);
398 glNormal3f(nx, ny, nz);
400 glVertex3f(x, y1, z1);
401 glVertex3f(xl, y1, z1);
402 glVertex3f(xl, y, z);
412 } else if (endcaps == 2) {
413 start = end = length+0.01;
415 end = length+0.02; start = -0.01;
417 norm = (ex == length+0.01) ? -1 : 1;
424 for(ex = start ; ex <= end ; ex += length) {
425 z1 = outer*e->cos_table[sangle]+z;
426 y1 = y+e->sin_table[sangle]*outer;
428 glBegin(GL_TRIANGLES);
430 for (a = sangle ; a <= angle || b <= angle; a+= step) {
431 glNormal3f(norm, 0, 0);
432 glVertex3f(x+ex,y, z);
433 glVertex3f(x+ex,y1,z1);
434 glVertex3f(x+ex,y2c[a],z2c[a]);
436 y1 = y2c[a]; z1 = z2c[a];
447 /* this is just a convenience function to make a solid rod */
448 static int rod (Engine *e, GLfloat x, GLfloat y, GLfloat z, float length, float diameter)
450 return cylinder(e, x, y, z, length, diameter, diameter, 3, 0, ONEREV);
453 static GLvoid normal(GLfloat v1[], GLfloat v2[], GLfloat v3[],
454 GLfloat *nx, GLfloat *ny, GLfloat *nz)
456 GLfloat x, y, z, X, Y, Z;
473 static int Rect(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h,
481 yh = y+h; xw = x+w; zt = z - t;
483 glBegin(GL_QUADS); /* front */
486 glVertex3f(x, yh, z);
487 glVertex3f(xw, yh, z);
488 glVertex3f(xw, y, z);
491 glNormal3f(0, 0, -1);
492 glVertex3f(x, y, zt);
493 glVertex3f(x, yh, zt);
494 glVertex3f(xw, yh, zt);
495 glVertex3f(xw, y, zt);
499 glVertex3f(x, yh, z);
500 glVertex3f(x, yh, zt);
501 glVertex3f(xw, yh, zt);
502 glVertex3f(xw, yh, z);
505 glNormal3f(0, -1, 0);
507 glVertex3f(x, y, zt);
508 glVertex3f(xw, y, zt);
509 glVertex3f(xw, y, z);
512 glNormal3f(-1, 0, 0);
514 glVertex3f(x, y, zt);
515 glVertex3f(x, yh, zt);
516 glVertex3f(x, yh, z);
520 glVertex3f(xw, y, z);
521 glVertex3f(xw, y, zt);
522 glVertex3f(xw, yh, zt);
523 glVertex3f(xw, yh, z);
529 static int makepiston(Engine *e)
532 GLfloat colour[] = {0.6, 0.6, 0.6, 1.0};
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 */
549 static int CrankBit(Engine *e, GLfloat x)
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);
557 static int boom(Engine *e, GLfloat x, GLfloat y, int s)
560 int flameOut = 720/ENG.speed/ENG.cylinders;
562 if (e->boom_time == 0 && s) {
563 e->boom_red[0] = e->boom_red[1] = 0;
567 } else if (e->boom_time == 0 && !s) {
569 } else if (e->boom_time >= 8 && e->boom_time < flameOut && !s) {
571 e->boom_red[0] -= 0.2; e->boom_red[1] -= 0.1;
573 } else if (e->boom_time >= flameOut) {
575 glDisable(GL_LIGHT1);
578 e->boom_red[0] += 0.2; e->boom_red[1] += 0.1;
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 glLighti(GL_LIGHT1, GL_LINEAR_ATTENUATION, 1.3);
587 glLighti(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0);
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;
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);
601 static int display(Engine *e)
610 glEnable(GL_LIGHTING);
611 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
613 gluLookAt(e->viewer[0], e->viewer[1], e->viewer[2],
614 e->lookat[0], e->lookat[1], e->lookat[2],
617 glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
618 glLightfv(GL_LIGHT0, GL_SPECULAR, light_sp);
619 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_sp);
623 get_position (e->rot, &x, &y, &z, !e->button_down_p);
624 glTranslatef(x*16-9, y*14-7, z*16-10);
628 gltrackball_rotate (e->trackball);
629 get_rotation(e->rot, &x, &y, &z, !e->button_down_p);
630 glRotatef(x*ONEREV, 1.0, 0.0, 0.0);
631 glRotatef(y*ONEREV, 0.0, 1.0, 0.0);
632 glRotatef(x*ONEREV, 0.0, 0.0, 1.0);
635 /* So the rotation appears around the centre of the engine */
636 glTranslatef(-5, 0, 0);
640 glRotatef(e->display_a, 1, 0, 0);
641 glCallList(e->shaft_list);
642 polys += e->shaft_polys;
645 /* init the ln[] matrix for speed */
646 if (e->ln_init == 0) {
647 for (e->ln_init = 0 ; e->ln_init < 730 ; e->ln_init++) {
648 zb = e->sin_table[e->ln_init];
649 yb = e->cos_table[e->ln_init];
650 /* y ordinate of piston */
651 e->yp[e->ln_init] = yb + sqrt(25 - (zb*zb));
653 e->ln[e->ln_init] = sqrt(zb*zb + (yb-e->yp[e->ln_init])*(yb-e->yp[e->ln_init]));
654 /* angle of connecting rod */
655 e->ang[e->ln_init] = asin(zb/5)*57;
656 e->ang[e->ln_init] *= -1;
661 sides = (ENG.includedAngle == 0) ? 1 : 2;
662 for (half = 0; half < sides; half++, glRotatef(ENG.includedAngle,1,0,0))
665 /* glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white); */
666 for (j = 0; j < ENG.cylinders; j += sides)
668 b = (e->display_a + ENG.pistonAngle[j+half]) % ONEREV;
670 glTranslatef(e->crankWidth/2 + e->crankOffset*(j+half), e->yp[b]-0.3, 0);
671 glCallList(e->piston_list);
672 polys += e->piston_polys;
677 glRotatef(90, 0, 0, 1);
678 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
679 for (j = 0; j < ENG.cylinders; j += sides)
681 polys += cylinder(e, 8.5, -e->crankWidth/2-e->crankOffset*(j+half), 0,
682 0.5, 0.4, 0.3, 1, 0, ONEREV);
684 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
685 for (j = 0; j < ENG.cylinders; j += sides)
687 polys += rod(e, 8, -e->crankWidth/2-e->crankOffset*(j+half), 0, 0.5, 0.2);
688 polys += rod(e, 9, -e->crankWidth/2-e->crankOffset*(j+half), 0, 1, 0.15);
692 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
693 for (j = 0; j < ENG.cylinders; j += sides)
695 b = (e->display_a+HALFREV+ENG.pistonAngle[j+half]) % TWOREV;
697 glRotatef(e->ang[b], 0, 1, 0);
700 -e->crankWidth/2-e->crankOffset*(j+half),
708 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow_t);
710 glDepthMask(GL_FALSE);
711 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
712 rightSide = (sides > 1) ? 0 : 1.6;
714 polys += Rect(-e->crankWidth/2, -0.5, 1, 0.2, 9, 2);
716 polys += Rect(0.3+e->crankOffset*ENG.cylinders-rightSide, -0.5, 1, 0.2, 9, 2);
718 polys += Rect(-e->crankWidth/2+0.2, 8.3, 1,
719 e->crankWidth/2+0.1+e->crankOffset*ENG.cylinders-rightSide, 0.2, 2);
721 polys += Rect(-e->crankWidth/2+0.2, 3, 1,
722 e->crankWidth/2+0.1+e->crankOffset*ENG.cylinders-rightSide, 0.2, 0.2);
724 polys += Rect(-e->crankWidth/2+0.2, 3, -1+0.2,
725 e->crankWidth/2+0.1+e->crankOffset*ENG.cylinders-rightSide, 0.2, 0.2);
726 /* plates between cylinders */
727 for (j=0; j < ENG.cylinders - (sides == 1); j += sides)
728 polys += Rect(0.4+e->crankWidth+e->crankOffset*(j-half), 3, 1, 1, 5.3, 2);
729 glDepthMask(GL_TRUE);
733 /* see which of our plugs should fire now, if any */
734 for (j = 0; j < ENG.cylinders; j++)
736 if (0 == ((e->display_a + ENG.pistonAngle[j]) % TWOREV))
740 glRotatef(ENG.includedAngle,1,0,0);
741 glRotatef(90, 0, 0, 1);
742 polys += boom(e, 8, -e->crankWidth/2-e->crankOffset*j, 1);
748 if (e->lastPlug != j)
750 /* this code causes the last plug explosion to dim gradually */
752 glRotatef(ENG.includedAngle, 1, 0, 0);
753 glRotatef(90, 0, 0, 1);
754 polys += boom(e, 8, -e->crankWidth/2-e->crankOffset*e->lastPlug, 0);
758 e->display_a += ENG.speed;
759 if (e->display_a >= TWOREV)
766 static int makeshaft (Engine *e)
770 float crankThick = 0.2;
771 float crankDiam = 0.3;
773 e->shaft_list = glGenLists(1);
774 glNewList(e->shaft_list, GL_COMPILE);
776 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
777 /* draw the flywheel */
778 polys += cylinder(e, -2.5, 0, 0, 1, 3, 2.5, 0, 0, ONEREV);
779 polys += Rect(-2, -0.3, 2.8, 0.5, 0.6, 5.6);
780 polys += Rect(-2, -2.8, 0.3, 0.5, 5.6, 0.6);
782 /* now make each of the shaft bits between the cranks,
783 * starting from the flywheel end which is at X-coord 0.
784 * the first cranskhaft bit is always 2 units long
786 polys += rod(e, -2, 0, 0, 2, crankDiam);
788 /* Each crank is crankWidth units wide and the total width of a
789 * cylinder assembly is 3.3 units. For inline engines, there is just
790 * a single crank per cylinder width. For other engine
791 * configurations, there is a crank between each pair of adjacent
792 * cylinders on one side of the engine, so the crankOffset length is
795 e->crankOffset = 3.3;
796 if (ENG.includedAngle != 0)
798 for (j = 0; j < ENG.cylinders - 1; j++)
800 e->crankWidth - crankThick + e->crankOffset*j, 0, 0,
801 e->crankOffset - e->crankWidth + 2 * crankThick, crankDiam);
802 /* the last bit connects to the engine wall on the non-flywheel end */
803 polys += rod(e, e->crankWidth - crankThick + e->crankOffset*j, 0, 0, 0.9, crankDiam);
806 for (j = 0; j < ENG.cylinders; j++)
810 glRotatef(HALFREV+ENG.pistonAngle[j]+ENG.includedAngle,1,0,0);
812 glRotatef(HALFREV+ENG.pistonAngle[j],1,0,0);
814 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
815 polys += rod(e, e->crankOffset*j, -1.0, 0.0, e->crankWidth, crankDiam);
816 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
817 /* draw right part of crank */
818 polys += CrankBit(e, e->crankOffset*j);
819 /* draw left part of crank */
820 polys += CrankBit(e, e->crankWidth-crankThick+e->crankOffset*j);
828 ENTRYPOINT void reshape_engine(ModeInfo *mi, int width, int height)
830 Engine *e = &engine[MI_SCREEN(mi)];
831 glViewport(0,0,(GLint)width, (GLint) height);
832 glMatrixMode(GL_PROJECTION);
834 glFrustum(-1.0,1.0,-1.0,1.0,1.5,70.0);
835 glMatrixMode(GL_MODELVIEW);
841 ENTRYPOINT void init_engine(ModeInfo *mi)
843 int screen = MI_SCREEN(mi);
846 if (engine == NULL) {
847 if ((engine = (Engine *) calloc(MI_NUM_SCREENS(mi),
848 sizeof(Engine))) == NULL)
852 e->window = MI_WINDOW(mi);
854 e->x = e->y = e->z = e->a = e->an1 = e->nx = e->ny = e->nz =
855 e->dx = e->dy = e->dz = e->da = 0;
858 e->dx = (float)(random() % 1000)/30000;
859 e->dy = (float)(random() % 1000)/30000;
860 e->dz = (float)(random() % 1000)/30000;
862 e->viewer[0] = 0; e->viewer[1] = 2; e->viewer[2] = 18;
863 e->lookat[0] = 0; e->lookat[1] = 0; e->lookat[2] = 0;
867 e->da = (float)(random() % 1000)/125 - 4;
868 e->nx = (float)(random() % 100) / 100;
869 e->ny = (float)(random() % 100) / 100;
870 e->nz = (float)(random() % 100) / 100;
874 double spin_speed = 0.5;
875 double wander_speed = 0.01;
878 e->boom_red[3] = 0.9;
883 e->rot = make_rotator (spin ? spin_speed : 0,
884 spin ? spin_speed : 0,
885 spin ? spin_speed : 0,
887 move ? wander_speed : 0,
890 e->trackball = gltrackball_init ();
893 if ((e->glx_context = init_GL(mi)) != NULL) {
894 reshape_engine(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
898 glClearColor(0.0,0.0,0.0,0.0);
899 glShadeModel(GL_SMOOTH);
900 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
901 glEnable(GL_DEPTH_TEST);
902 glEnable(GL_LIGHTING);
904 glEnable(GL_NORMALIZE);
906 e->engineType = find_engine(which_engine);
908 e->engine_name = malloc(200);
909 sprintf (e->engine_name,
911 engines[e->engineType].engineName,
912 (engines[e->engineType].includedAngle == 0 ? "" :
913 engines[e->engineType].includedAngle == 180 ? "Flat " : "V"),
914 engines[e->engineType].cylinders,
915 (engines[e->engineType].includedAngle == 0 ? " Cylinder" : "")
918 e->shaft_polys = makeshaft(e);
919 e->piston_polys = makepiston(e);
920 load_font (mi->dpy, "titleFont", &e->xfont, &e->font_dlist);
924 engine_handle_event (ModeInfo *mi, XEvent *event)
926 Engine *e = &engine[MI_SCREEN(mi)];
928 if (event->xany.type == ButtonPress &&
929 event->xbutton.button == Button1)
931 e->button_down_p = True;
932 gltrackball_start (e->trackball,
933 event->xbutton.x, event->xbutton.y,
934 MI_WIDTH (mi), MI_HEIGHT (mi));
938 else if (event->xany.type == ButtonRelease &&
939 event->xbutton.button == Button1) {
940 e->button_down_p = False;
944 else if (event->xany.type == ButtonPress &&
945 (event->xbutton.button == Button4 ||
946 event->xbutton.button == Button5 ||
947 event->xbutton.button == Button6 ||
948 event->xbutton.button == Button7))
950 gltrackball_mousewheel (e->trackball, event->xbutton.button, 10,
951 !!event->xbutton.state);
954 else if (event->xany.type == MotionNotify &&
956 gltrackball_track (e->trackball,
957 event->xmotion.x, event->xmotion.y,
958 MI_WIDTH (mi), MI_HEIGHT (mi));
964 ENTRYPOINT void draw_engine(ModeInfo *mi)
966 Engine *e = &engine[MI_SCREEN(mi)];
967 Window w = MI_WINDOW(mi);
968 Display *disp = MI_DISPLAY(mi);
973 glXMakeCurrent(disp, w, *(e->glx_context));
976 mi->polygon_count = display(e);
979 print_gl_string (mi->dpy, e->xfont, e->font_dlist,
980 mi->xgwa.width, mi->xgwa.height,
981 10, mi->xgwa.height - 10,
982 e->engine_name, False);
984 if(mi->fps_p) do_fps(mi);
986 glXSwapBuffers(disp, w);
990 release_engine(ModeInfo *mi)
992 if (engine != NULL) {
993 (void) free((void *) engine);
999 XSCREENSAVER_MODULE ("Engine", engine)