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 "*suppressRotationAnimation: True\n" \
28 "*titleFont: -*-helvetica-medium-r-normal-*-*-180-*-*-*-*-*-*\n" \
30 # define free_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 */
39 #include "gltrackball.h"
41 /* lifted from lament.c */
42 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
43 #define RANDSIGN() ((random() & 1) ? 1 : -1)
48 #define DEF_ENGINE "(none)"
49 #define DEF_TITLES "False"
50 #define DEF_SPIN "True"
51 #define DEF_MOVE "True"
54 #define countof(x) (sizeof((x))/sizeof((*x)))
56 static char *which_engine;
59 static Bool do_titles;
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" },
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},
78 ENTRYPOINT ModeSpecOpt engine_opts = {countof(opts), opts, countof(vars), vars, NULL};
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};
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
97 #define MOVE_MULT 0.05
99 #define RAND_RANGE(min, max) ((min) + (max - min) * f_rand())
103 GLXContext *glx_context;
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 */
112 trackball_state *trackball;
114 texture_font_data *font_data;
124 float sin_table[TWOREV];
125 float cos_table[TWOREV];
126 float tan_table[TWOREV];
129 GLfloat boom_lpos[4];
130 GLfloat boom_d, boom_wd;
133 GLfloat viewer[3], lookat[3];
136 GLfloat ln[730], yp[730], ang[730];
140 GLuint shaft_list, piston_list;
141 int shaft_polys, piston_polys;
145 static Engine *engine = NULL;
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};
155 static GLvoid normal(GLfloat [], GLfloat [], GLfloat [],
156 GLfloat *, GLfloat *, GLfloat *);
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):
166 * |= firing order is 1-6-2-4-3-5 in this diagram
169 * We renumber these using our scheme but preserve the effective firing order:
172 * |= firing order is 4-1-2-5-0-3 in this diagram
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
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:
183 * cylinder firing order angle
184 * -------- ------------ -----
198 int pistonAngle[12]; /* twelve cylinders should suffice... */
199 int speed; /* step size in degrees for engine speed */
200 const char *engineName; /* currently unused */
203 static const engine_type engines[] = {
204 { 3, 0, { 0, 240, 480, 0, 0, 0,
205 0, 0, 0, 0, 0, 0 }, 12,
207 { 4, 0, { 0, 180, 540, 360, 0, 0,
208 0, 0, 0, 0, 0, 0 }, 12,
210 { 4, 180, { 0, 360, 180, 540, 0, 0,
211 0, 0, 0, 0, 0, 0 }, 12,
213 { 5, 0, { 0, 576, 144, 432, 288, 0,
214 0, 0, 0, 0, 0, 0 }, 12,
216 { 6, 0, { 0, 240, 480, 120, 600, 360,
217 0, 0, 0, 0, 0, 0 }, 12,
219 { 6, 90, { 0, 360, 480, 120, 240, 600,
220 0, 0, 0, 0, 0, 0 }, 12,
222 { 6, 180, { 0, 360, 240, 600, 480, 120,
223 0, 0, 0, 0, 0, 0 }, 12,
225 { 8, 90, { 0, 450, 90, 180, 270, 360,
226 540, 630, 0, 0, 0, 0 }, 15,
228 {10, 90, { 0, 72, 432, 504, 288, 360,
229 144, 216, 576, 648, 0, 0 }, 12,
231 {12, 60, { 0, 300, 240, 540, 480, 60,
232 120, 420, 600, 180, 360, 660 }, 12,
236 /* this define is just a little shorter way of referring to members of the
239 #define ENG engines[e->engineType]
241 /* given a number of cylinders and an included angle, finds matching engine */
243 find_engine(char *name)
248 if (!name || !*name || !strcasecmp (name, "(none)"))
249 return (random() % countof(engines));
251 for (s = name; *s; s++)
252 if (*s == '-' || *s == '_') *s = ' ';
254 for (i = 0; i < countof(engines); i++) {
255 if (!strcasecmp(name, engines[i].engineName))
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");
269 fprintf (stderr, ", V)\n");
274 /* we use trig tables to speed things up - 200 calls to sin()
275 in one frame can be a bit harsh..
278 static void make_tables(Engine *e)
283 f = ONEREV / (M_PI * 2);
284 for (i = 0 ; i < TWOREV ; i++) {
285 e->sin_table[i] = sin(i/f);
287 for (i = 0 ; i < TWOREV ; i++) {
288 e->cos_table[i] = cos(i/f);
290 for (i = 0 ; i < TWOREV ; i++) {
291 e->tan_table[i] = tan(i/f);
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) */
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)
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];
312 nsegs = outer*(MAX(e->win_w, e->win_h)/200);
313 nsegs = MAX(nsegs, 6);
314 nsegs = MAX(nsegs, 40);
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 ;
324 if (inner < outer && endcaps < 3) tube = 1;
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;
333 z2c[a] = z2; /* cache for later */
336 Y2=inner*(float)e->sin_table[a]+y;
337 Z2=inner*(float)e->cos_table[a]+z;
339 glNormal3f(0, y1, z1);
341 glVertex3f(xl,y1,z1);
342 glNormal3f(0, y2, z2);
343 glVertex3f(xl,y2,z2);
346 if (a == sangle && angle - sangle < ONEREV) {
348 glVertex3f(x, Y1, Z1);
351 glVertex3f(x, y1, z1);
352 glVertex3f(xl, y1, z1);
354 glVertex3f(xl, Z1, Z1);
356 glVertex3f(xl, y, z);
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);
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);
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);
393 if (angle - sangle < ONEREV) {
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);
401 glNormal3f(nx, ny, nz);
403 glVertex3f(x, y1, z1);
404 glVertex3f(xl, y1, z1);
405 glVertex3f(xl, y, z);
415 } else if (endcaps == 2) {
416 start = end = length+0.01;
418 end = length+0.02; start = -0.01;
420 norm = (ex == length+0.01) ? -1 : 1;
427 for(ex = start ; ex <= end ; ex += length) {
428 z1 = outer*e->cos_table[sangle]+z;
429 y1 = y+e->sin_table[sangle]*outer;
431 glBegin(GL_TRIANGLES);
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]);
439 y1 = y2c[a]; z1 = z2c[a];
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)
453 return cylinder(e, x, y, z, length, diameter, diameter, 3, 0, ONEREV);
456 static GLvoid normal(GLfloat v1[], GLfloat v2[], GLfloat v3[],
457 GLfloat *nx, GLfloat *ny, GLfloat *nz)
459 GLfloat x, y, z, X, Y, Z;
476 static int Rect(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h,
484 yh = y+h; xw = x+w; zt = z - t;
486 glBegin(GL_QUADS); /* front */
489 glVertex3f(x, yh, z);
490 glVertex3f(xw, yh, z);
491 glVertex3f(xw, y, z);
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);
502 glVertex3f(x, yh, z);
503 glVertex3f(x, yh, zt);
504 glVertex3f(xw, yh, zt);
505 glVertex3f(xw, yh, z);
508 glNormal3f(0, -1, 0);
510 glVertex3f(x, y, zt);
511 glVertex3f(xw, y, zt);
512 glVertex3f(xw, y, z);
515 glNormal3f(-1, 0, 0);
517 glVertex3f(x, y, zt);
518 glVertex3f(x, yh, zt);
519 glVertex3f(x, yh, z);
523 glVertex3f(xw, y, z);
524 glVertex3f(xw, y, zt);
525 glVertex3f(xw, yh, zt);
526 glVertex3f(xw, yh, z);
532 static int makepiston(Engine *e)
535 GLfloat colour[] = {0.6, 0.6, 0.6, 1.0};
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 */
553 static int CrankBit(Engine *e, GLfloat x)
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);
561 static int boom(Engine *e, GLfloat x, GLfloat y, int s)
564 int flameOut = 720/ENG.speed/ENG.cylinders;
566 if (e->boom_time == 0 && s) {
567 e->boom_red[0] = e->boom_red[1] = 0;
571 } else if (e->boom_time == 0 && !s) {
573 } else if (e->boom_time >= 8 && e->boom_time < flameOut && !s) {
575 e->boom_red[0] -= 0.2; e->boom_red[1] -= 0.1;
577 } else if (e->boom_time >= flameOut) {
579 glDisable(GL_LIGHT1);
582 e->boom_red[0] += 0.2; e->boom_red[1] += 0.1;
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);
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;
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);
605 static int display(ModeInfo *mi)
607 Engine *e = &engine[MI_SCREEN(mi)];
615 glEnable(GL_LIGHTING);
616 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
618 gluLookAt(e->viewer[0], e->viewer[1], e->viewer[2],
619 e->lookat[0], e->lookat[1], e->lookat[2],
623 # ifdef HAVE_MOBILE /* Keep it the same relative size when rotated. */
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);
632 glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
633 glLightfv(GL_LIGHT0, GL_SPECULAR, light_sp);
634 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_sp);
638 get_position (e->rot, &x, &y, &z, !e->button_down_p);
639 glTranslatef(x*16-9, y*14-7, z*16-10);
645 gltrackball_rotate (e->trackball);
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);
653 /* So the rotation appears around the centre of the engine */
654 glTranslatef(-5, 0, 0);
658 glRotatef(e->display_a, 1, 0, 0);
659 glCallList(e->shaft_list);
660 polys += e->shaft_polys;
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));
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;
679 sides = (ENG.includedAngle == 0) ? 1 : 2;
680 for (half = 0; half < sides; half++, glRotatef(ENG.includedAngle,1,0,0))
683 /* glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white); */
684 for (j = 0; j < ENG.cylinders; j += sides)
686 b = (e->display_a + ENG.pistonAngle[j+half]) % ONEREV;
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;
695 glRotatef(90, 0, 0, 1);
696 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
697 for (j = 0; j < ENG.cylinders; j += sides)
699 polys += cylinder(e, 8.5, -e->crankWidth/2-e->crankOffset*(j+half), 0,
700 0.5, 0.4, 0.3, 1, 0, ONEREV);
702 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
703 for (j = 0; j < ENG.cylinders; j += sides)
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);
710 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
711 for (j = 0; j < ENG.cylinders; j += sides)
713 b = (e->display_a+HALFREV+ENG.pistonAngle[j+half]) % TWOREV;
715 glRotatef(e->ang[b], 0, 1, 0);
718 -e->crankWidth/2-e->crankOffset*(j+half),
726 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow_t);
728 glDepthMask(GL_FALSE);
729 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
730 rightSide = (sides > 1) ? 0 : 1.6;
732 polys += Rect(-e->crankWidth/2, -0.5, 1, 0.2, 9, 2);
734 polys += Rect(0.3+e->crankOffset*ENG.cylinders-rightSide, -0.5, 1, 0.2, 9, 2);
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);
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);
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);
751 /* see which of our plugs should fire now, if any */
752 for (j = 0; j < ENG.cylinders; j++)
754 if (0 == ((e->display_a + ENG.pistonAngle[j]) % TWOREV))
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);
766 if (e->lastPlug != j)
768 /* this code causes the last plug explosion to dim gradually */
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);
776 e->display_a += ENG.speed;
777 if (e->display_a >= TWOREV)
784 static int makeshaft (Engine *e)
788 float crankThick = 0.2;
789 float crankDiam = 0.3;
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);
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);
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
805 polys += rod(e, -2, 0, 0, 2, crankDiam);
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
814 e->crankOffset = 3.3;
815 if (ENG.includedAngle != 0)
817 for (j = 0; j < ENG.cylinders - 1; j++)
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);
825 for (j = 0; j < ENG.cylinders; j++)
829 glRotatef(HALFREV+ENG.pistonAngle[j]+ENG.includedAngle,1,0,0);
831 glRotatef(HALFREV+ENG.pistonAngle[j],1,0,0);
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);
847 ENTRYPOINT void reshape_engine(ModeInfo *mi, int width, int height)
849 Engine *e = &engine[MI_SCREEN(mi)];
850 double h = (GLfloat) height / (GLfloat) width;
853 if (width > height * 5) { /* tiny window: show middle */
854 height = width * 9/16;
856 h = height / (GLfloat) width;
859 glViewport(0, y, width, height);
860 glMatrixMode(GL_PROJECTION);
862 gluPerspective(40, 1/h, 1.5, 70.0);
863 glMatrixMode(GL_MODELVIEW);
869 ENTRYPOINT void init_engine(ModeInfo *mi)
871 int screen = MI_SCREEN(mi);
876 e->window = MI_WINDOW(mi);
878 e->x = e->y = e->z = e->a = e->an1 = e->nx = e->ny = e->nz =
879 e->dx = e->dy = e->dz = e->da = 0;
882 e->dx = (float)(random() % 1000)/30000;
883 e->dy = (float)(random() % 1000)/30000;
884 e->dz = (float)(random() % 1000)/30000;
886 e->viewer[0] = 0; e->viewer[1] = 2; e->viewer[2] = 18;
887 e->lookat[0] = 0; e->lookat[1] = 0; e->lookat[2] = 0;
891 e->da = (float)(random() % 1000)/125 - 4;
892 e->nx = (float)(random() % 100) / 100;
893 e->ny = (float)(random() % 100) / 100;
894 e->nz = (float)(random() % 100) / 100;
898 double spin_speed = 0.5;
899 double wander_speed = 0.01;
902 e->boom_red[3] = 0.9;
907 e->rot = make_rotator (spin ? spin_speed : 0,
908 spin ? spin_speed : 0,
909 spin ? spin_speed : 0,
911 move ? wander_speed : 0,
914 e->trackball = gltrackball_init (True);
917 if (!e->glx_context && /* re-initting breaks print_texture_label */
918 (e->glx_context = init_GL(mi)) != NULL) {
919 reshape_engine(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
923 glShadeModel(GL_SMOOTH);
924 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
925 glEnable(GL_DEPTH_TEST);
926 glEnable(GL_LIGHTING);
928 glEnable(GL_NORMALIZE);
930 e->engineType = find_engine(which_engine);
933 e->engine_name = malloc(200);
934 sprintf (e->engine_name,
936 engines[e->engineType].engineName,
937 (engines[e->engineType].includedAngle == 0 ? "" :
938 engines[e->engineType].includedAngle == 180 ? "Flat " : "V"),
939 engines[e->engineType].cylinders,
940 (engines[e->engineType].includedAngle == 0 ? " Cylinder" : "")
943 e->shaft_polys = makeshaft(e);
944 e->piston_polys = makepiston(e);
947 e->font_data = load_texture_font (mi->dpy, "titleFont");
951 engine_handle_event (ModeInfo *mi, XEvent *event)
953 Engine *e = &engine[MI_SCREEN(mi)];
955 if (event->xany.type == ButtonPress &&
956 event->xbutton.button == Button1)
960 else if (event->xany.type == ButtonRelease &&
961 event->xbutton.button == Button1) {
965 if (gltrackball_event_handler (event, e->trackball,
966 MI_WIDTH (mi), MI_HEIGHT (mi),
969 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
971 which_engine = NULL; /* randomize */
979 ENTRYPOINT void draw_engine(ModeInfo *mi)
981 Engine *e = &engine[MI_SCREEN(mi)];
982 Window w = MI_WINDOW(mi);
983 Display *disp = MI_DISPLAY(mi);
988 glXMakeCurrent(disp, w, *(e->glx_context));
991 mi->polygon_count = display(mi);
995 print_texture_label (mi->dpy, e->font_data,
996 mi->xgwa.width, mi->xgwa.height,
999 if(mi->fps_p) do_fps(mi);
1001 glXSwapBuffers(disp, w);
1004 XSCREENSAVER_MODULE ("Engine", engine)