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 #include <X11/Intrinsic.h>
28 # define PROGCLASS "Engine"
29 # define HACK_INIT init_engine
30 # define HACK_DRAW draw_engine
31 # define HACK_HANDLE_EVENT engine_handle_event
32 # define HACK_RESHAPE reshape_engine
33 # define EVENT_MASK PointerMotionMask
34 # define engine_opts xlockmore_opts
35 /* insert defaults here */
37 #define DEF_ENGINE "(none)"
39 #define DEFAULTS "*delay: 10000 \n" \
40 "*showFPS: False \n" \
43 "*engine: " DEF_ENGINE "\n" \
45 # include "xlockmore.h" /* from the xscreensaver distribution */
46 #else /* !STANDALONE */
47 # include "xlock.h" /* from the xlockmore distribution */
48 #endif /* !STANDALONE */
51 #include "gltrackball.h"
53 /* lifted from lament.c */
54 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
55 #define RANDSIGN() ((random() & 1) ? 1 : -1)
66 #define countof(x) (sizeof((x))/sizeof((*x)))
68 static int engineType;
69 static char *which_engine;
71 static int movepaused = 0;
74 static XrmOptionDescRec opts[] = {
75 {"-engine", ".engine.engine", XrmoptionSepArg, DEF_ENGINE },
76 {"-move", ".engine.move", XrmoptionNoArg, (caddr_t) "true" },
77 {"+move", ".engine.move", XrmoptionNoArg, (caddr_t) "false" },
78 {"-spin", ".engine.spin", XrmoptionNoArg, (caddr_t) "true" },
79 {"+spin", ".engine.spin", XrmoptionNoArg, (caddr_t) "false" },
82 static argtype vars[] = {
83 {(caddr_t *) &which_engine, "engine", "Engine", DEF_ENGINE, t_String},
84 {(caddr_t *) &move, "move", "Move", "True", t_Bool},
85 {(caddr_t *) &spin, "spin", "Spin", "True", t_Bool},
88 ModeSpecOpt engine_opts = {countof(opts), opts, countof(vars), vars, NULL};
91 ModStruct engine_description =
92 {"engine", "init_engine", "draw_engine", "release_engine",
93 "draw_engine", "init_engine", NULL, &engine_opts,
94 1000, 1, 2, 1, 4, 1.0, "",
95 "A four stroke engine", 0, NULL};
100 GLXContext *glx_context;
102 GLfloat x, y, z; /* position */
103 GLfloat dx, dy, dz; /* position */
104 GLfloat an1, an2, an3; /* internal angle */
105 GLfloat nx, ny, nz; /* spin vector */
106 GLfloat a; /* spin angle */
107 GLfloat da; /* spin speed */
109 trackball_state *trackball;
113 static Engine *engine = NULL;
116 #include <sys/time.h>
121 #define M_PI 3.14159265
124 /* these defines are used to provide symbolic means
125 * by which to refer to various portions or multiples
126 * of a cyle in degrees
132 #define MOVE_MULT 0.05
134 #define RAND_RANGE(min, max) ((min) + (max - min) * f_rand())
137 float crankWidth = 1.5;
141 static GLfloat viewer[] = {0.0, 0.0, 30.0};
142 static GLfloat lookat[] = {0.0, 0.0, 0.0};
143 static GLfloat lightpos[] = {7.0, 7.0, 12, 1.0};
144 GLfloat light_sp[] = {0.8, 0.8, 0.8, 0.5};
145 static GLfloat red[] = {1.0, 0, 0, 1.0};
146 static GLfloat green[] = {0.0, 1, 0, 1.0};
147 static GLfloat blue[] = {0, 0, 1, 1.0};
148 static GLfloat white[] = {1.0, 1, 1, 1.0};
149 static GLfloat yellow_t[] = {1, 1, 0, 0.4};
151 GLvoid normal(GLfloat [], GLfloat [], GLfloat [],
152 GLfloat *, GLfloat *, GLfloat *);
154 float sin_table[TWOREV];
155 float cos_table[TWOREV];
156 float tan_table[TWOREV];
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 engine).
179 * Because these are all four-stroke engines, each piston goes through
180 * 720 degrees of rotation for each time the spark plug sparks, so in this case,
181 * 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 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[engineType]
240 /* given a number of cylinders and an included angle, finds matching engine */
241 int find_engine(const char *name)
245 if (!name || !*name || !strcasecmp (name, "(none)"))
246 return (random() % countof(engines));
248 for (i = 0; i < countof(engines); i++) {
249 if (!strcasecmp(name, engines[i].engineName))
253 fprintf (stderr, "%s: unknown engine type \"%s\"\n", progname, name);
254 fprintf (stderr, "%s: available models are:\n", progname);
255 for (i = 0; i < countof(engines); i++) {
256 fprintf (stderr, "\t %-13s (%d cylinders",
257 engines[i].engineName, engines[i].cylinders);
258 if (engines[i].includedAngle == 0)
259 fprintf (stderr, ")\n");
260 else if (engines[i].includedAngle == 180)
261 fprintf (stderr, ", flat)\n");
263 fprintf (stderr, ", V)\n");
268 /* we use trig tables to speed things up - 200 calls to sin()
269 in one frame can be a bit harsh..
272 void make_tables(void) {
276 f = ONEREV / (M_PI * 2);
277 for (i = 0 ; i <= TWOREV ; i++) {
278 sin_table[i] = sin(i/f);
280 for (i = 0 ; i <= TWOREV ; i++) {
281 cos_table[i] = cos(i/f);
283 for (i = 0 ; i <= TWOREV ; i++) {
284 tan_table[i] = tan(i/f);
288 /* if inner and outer are the same, we draw a cylinder, not a tube */
289 /* for a tube, endcaps is 0 (none), 1 (left), 2 (right) or 3(both) */
290 /* angle is how far around the axis to go (up to 360) */
292 void cylinder (GLfloat x, GLfloat y, GLfloat z,
293 float length, float outer, float inner, int endcaps, int sang, int eang) {
294 int a; /* current angle around cylinder */
295 int b = 0; /* previous */
296 int angle, norm, step, sangle;
297 float z1, y1, z2, y2, ex=0;
299 float Z1, Y1, Z2, Y2, xl, Y3, Z3;
300 GLfloat y2c[TWOREV], z2c[TWOREV];
301 GLfloat ony, onz; /* previous normals */
305 nsegs = outer*(MAX(win_w, win_h)/200);
306 nsegs = MAX(nsegs, 6);
307 nsegs = MAX(nsegs, 40);
313 z1 = cos_table[sangle]*outer+z; y1 = sin_table[sangle] * outer+y;
314 Z1 = cos_table[sangle] * inner+z; Y1 = sin_table[sangle]*inner+y ;
318 if (inner < outer && endcaps < 3) tube = 1;
322 for (a = sangle ; a <= angle || b <= angle ; a+= step) {
323 y2=outer*(float)sin_table[a]+y;
324 z2=outer*(float)cos_table[a]+z;
325 y3=outer*(float)sin_table[a+step]+y;
326 z3=outer*(float)cos_table[a+step]+z;
328 y2c[a] = y2; z2c[a] = z2; /* cache for later */
330 Y2=inner*(float)sin_table[a]+y;
331 Z2=inner*(float)cos_table[a]+z;
332 Y3=inner*(float)sin_table[a+step]+y;
333 Z3=inner*(float)cos_table[a+step]+z;
335 glNormal3f(0, y1, z1);
337 glVertex3f(xl,y1,z1);
338 glNormal3f(0, y2, z2);
339 glVertex3f(xl,y2,z2);
341 if (a == sangle && angle - sangle < ONEREV) {
343 glVertex3f(x, Y1, Z1);
346 glVertex3f(x, y1, z1);
347 glVertex3f(xl, y1, z1);
349 glVertex3f(xl, Z1, Z1);
351 glVertex3f(xl, y, z);
355 glNormal3f(-1, 0, 0); /* left end */
356 glVertex3f(x, y1, z1);
357 glVertex3f(x, y2, z2);
358 glVertex3f(x, Y2, Z2);
359 glVertex3f(x, Y1, Z1);
362 glNormal3f(0, -Y1, -Z1); /* inner surface */
363 glVertex3f(x, Y1, Z1);
364 glVertex3f(xl, Y1, Z1);
365 glNormal3f(0, -Y2, -Z2);
366 glVertex3f(xl, Y2, Z2);
367 glVertex3f(x, Y2, Z2);
370 glNormal3f(1, 0, 0); /* right end */
371 glVertex3f(xl, y1, z1);
372 glVertex3f(xl, y2, z2);
373 glVertex3f(xl, Y2, Z2);
374 glVertex3f(xl, Y1, Z1);
384 if (angle - sangle < ONEREV) {
386 GLfloat v1[3], v2[3], v3[3];
387 v1[0] = x; v1[1] = y; v1[2] = z;
388 v2[0] = x; v2[1] = y1; v2[2] = z1;
389 v3[0] = xl; v3[1] = y1; v3[2] = z1;
390 normal(&v2[0], &v1[0], &v3[0], &nx, &ny, &nz);
392 glNormal3f(nx, ny, nz);
394 glVertex3f(x, y1, z1);
395 glVertex3f(xl, y1, z1);
396 glVertex3f(xl, y, z);
405 } else if (endcaps == 2) {
406 start = end = length+0.01;
408 end = length+0.02; start = -0.01;
410 norm = (ex == length+0.01) ? -1 : 1;
417 for(ex = start ; ex <= end ; ex += length) {
418 z1 = outer*cos_table[sangle]+z;
419 y1 = y+sin_table[sangle]*outer;
421 glBegin(GL_TRIANGLES);
423 for (a = sangle ; a <= angle || b <= angle; a+= step) {
424 glNormal3f(norm, 0, 0);
425 glVertex3f(x+ex,y, z);
426 glVertex3f(x+ex,y1,z1);
427 glVertex3f(x+ex,y2c[a],z2c[a]);
428 y1 = y2c[a]; z1 = z2c[a];
438 /* this is just a convenience function to make a solid rod */
439 void rod (GLfloat x, GLfloat y, GLfloat z, float length, float diameter) {
440 cylinder(x, y, z, length, diameter, diameter, 3, 0, ONEREV);
443 GLvoid normal(GLfloat v1[], GLfloat v2[], GLfloat v3[],
444 GLfloat *nx, GLfloat *ny, GLfloat *nz)
446 GLfloat x, y, z, X, Y, Z;
463 void Rect(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h,
469 yh = y+h; xw = x+w; zt = z - t;
471 glBegin(GL_QUADS); /* front */
474 glVertex3f(x, yh, z);
475 glVertex3f(xw, yh, z);
476 glVertex3f(xw, y, z);
478 glNormal3f(0, 0, -1);
479 glVertex3f(x, y, zt);
480 glVertex3f(x, yh, zt);
481 glVertex3f(xw, yh, zt);
482 glVertex3f(xw, y, zt);
485 glVertex3f(x, yh, z);
486 glVertex3f(x, yh, zt);
487 glVertex3f(xw, yh, zt);
488 glVertex3f(xw, yh, z);
490 glNormal3f(0, -1, 0);
492 glVertex3f(x, y, zt);
493 glVertex3f(xw, y, zt);
494 glVertex3f(xw, y, z);
496 glNormal3f(-1, 0, 0);
498 glVertex3f(x, y, zt);
499 glVertex3f(x, yh, zt);
500 glVertex3f(x, yh, z);
503 glVertex3f(xw, y, z);
504 glVertex3f(xw, y, zt);
505 glVertex3f(xw, yh, zt);
506 glVertex3f(xw, yh, z);
510 void makepiston(void) {
511 GLfloat colour[] = {0.6, 0.6, 0.6, 1.0};
515 glNewList(i, GL_COMPILE);
516 glRotatef(90, 0, 0, 1);
517 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colour);
518 glMaterialfv(GL_FRONT, GL_SPECULAR, colour);
519 glMateriali(GL_FRONT, GL_SHININESS, 20);
520 cylinder(0, 0, 0, 2, 1, 0.7, 2, 0, ONEREV); /* body */
521 colour[0] = colour[1] = colour[2] = 0.2;
522 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colour);
523 cylinder(1.6, 0, 0, 0.1, 1.05, 1.05, 0, 0, ONEREV); /* ring */
524 cylinder(1.8, 0, 0, 0.1, 1.05, 1.05, 0, 0, ONEREV); /* ring */
528 void CrankBit(GLfloat x) {
529 Rect(x, -1.4, 0.5, 0.2, 1.8, 1);
530 cylinder(x, -0.5, 0, 0.2, 2, 2, 1, 60, 120);
533 void boom(GLfloat x, GLfloat y, int s) {
534 static GLfloat red[] = {0, 0, 0, 0.9};
535 static GLfloat lpos[] = {0, 0, 0, 1};
536 static GLfloat d = 0, wd;
537 int flameOut = 720/ENG.speed/ENG.cylinders;
540 if (time == 0 && s) {
545 } else if (time == 0 && !s) {
547 } else if (time >= 8 && time < flameOut && !s) {
549 red[0] -= 0.2; red[1] -= 0.1;
551 } else if (time >= flameOut) {
553 glDisable(GL_LIGHT1);
556 red[0] += 0.2; red[1] += 0.1;
560 lpos[0] = x-d; lpos[1] = y;
561 glLightfv(GL_LIGHT1, GL_POSITION, lpos);
562 glLightfv(GL_LIGHT1, GL_DIFFUSE, red);
563 glLightfv(GL_LIGHT1, GL_SPECULAR, red);
564 glLighti(GL_LIGHT1, GL_LINEAR_ATTENUATION, 1.3);
565 glLighti(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0);
567 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
569 if (wd > 0.7) wd = 0.7;
571 glDepthMask(GL_FALSE);
572 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
574 glDepthMask(GL_TRUE);
578 void display(Engine *e) {
582 static GLfloat ln[730], yp[730], ang[730];
583 static int ln_init = 0;
584 static int lastPlug = 0;
588 static float rightSide;
590 glEnable(GL_LIGHTING);
591 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
593 gluLookAt(viewer[0], viewer[1], viewer[2], lookat[0], lookat[1], lookat[2],
596 glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
597 glLightfv(GL_LIGHT0, GL_SPECULAR, light_sp);
598 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_sp);
602 get_position (e->rot, &x, &y, &z, !e->button_down_p);
603 glTranslatef(x*16-9, y*14-7, z*16-10);
607 gltrackball_rotate (e->trackball);
608 get_rotation(e->rot, &x, &y, &z, !e->button_down_p);
609 glRotatef(x*ONEREV, 1.0, 0.0, 0.0);
610 glRotatef(y*ONEREV, 0.0, 1.0, 0.0);
611 glRotatef(x*ONEREV, 0.0, 0.0, 1.0);
614 /* So the rotation appears around the centre of the engine */
615 glTranslatef(-5, 0, 0);
619 glRotatef(a, 1, 0, 0);
623 /* init the ln[] matrix for speed */
625 for (ln_init = 0 ; ln_init < 730 ; ln_init++) {
626 zb = sin_table[ln_init];
627 yb = cos_table[ln_init];
628 /* y ordinate of piston */
629 yp[ln_init] = yb + sqrt(25 - (zb*zb));
631 ln[ln_init] = sqrt(zb*zb + (yb-yp[ln_init])*(yb-yp[ln_init]));
632 /* angle of connecting rod */
633 ang[ln_init] = asin(zb/5)*57;
639 sides = (ENG.includedAngle == 0) ? 1 : 2;
640 for (half = 0; half < sides; half++, glRotatef(ENG.includedAngle,1,0,0))
643 /* glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white); */
644 for (j = 0; j < ENG.cylinders; j += sides)
646 b = (a + ENG.pistonAngle[j+half]) % ONEREV;
648 glTranslatef(crankWidth/2 + crankOffset*(j+half), yp[b]-0.3, 0);
654 glRotatef(90, 0, 0, 1);
655 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
656 for (j = 0; j < ENG.cylinders; j += sides)
658 cylinder(8.5, -crankWidth/2-crankOffset*(j+half), 0,
659 0.5, 0.4, 0.3, 1, 0, ONEREV);
661 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
662 for (j = 0; j < ENG.cylinders; j += sides)
664 rod(8, -crankWidth/2-crankOffset*(j+half), 0, 0.5, 0.2);
665 rod(9, -crankWidth/2-crankOffset*(j+half), 0, 1, 0.15);
669 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
670 for (j = 0; j < ENG.cylinders; j += sides)
672 b = (a+HALFREV+ENG.pistonAngle[j+half]) % TWOREV;
674 glRotatef(ang[b], 0, 1, 0);
675 rod(-cos_table[b], -crankWidth/2-crankOffset*(j+half), -sin_table[b],
682 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow_t);
684 glDepthMask(GL_FALSE);
685 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
686 rightSide = (sides > 1) ? 0 : 1.6;
688 Rect(-crankWidth/2, -0.5, 1, 0.2, 9, 2);
690 Rect(0.3+crankOffset*ENG.cylinders-rightSide, -0.5, 1, 0.2, 9, 2);
692 Rect(-crankWidth/2+0.2, 8.3, 1,
693 crankWidth/2+0.1+crankOffset*ENG.cylinders-rightSide, 0.2, 2);
695 Rect(-crankWidth/2+0.2, 3, 1,
696 crankWidth/2+0.1+crankOffset*ENG.cylinders-rightSide, 0.2, 0.2);
698 Rect(-crankWidth/2+0.2, 3, -1+0.2,
699 crankWidth/2+0.1+crankOffset*ENG.cylinders-rightSide, 0.2, 0.2);
700 /* plates between cylinders */
701 for (j=0; j < ENG.cylinders - (sides == 1); j += sides)
702 Rect(0.4+crankWidth+crankOffset*(j-half), 3, 1, 1, 5.3, 2);
703 glDepthMask(GL_TRUE);
707 /* see which of our plugs should fire now, if any */
708 for (j = 0; j < ENG.cylinders; j++)
710 if (0 == ((a + ENG.pistonAngle[j]) % TWOREV))
714 glRotatef(ENG.includedAngle,1,0,0);
715 glRotatef(90, 0, 0, 1);
716 boom(8, -crankWidth/2-crankOffset*j, 1);
724 /* this code causes the last plug explosion to dim gradually */
726 glRotatef(ENG.includedAngle, 1, 0, 0);
727 glRotatef(90, 0, 0, 1);
728 boom(8, -crankWidth/2-crankOffset*lastPlug, 0);
739 void makeshaft (void) {
742 const static float crankThick = 0.2;
743 const static float crankDiam = 0.3;
746 glNewList(i, GL_COMPILE);
748 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
749 /* draw the flywheel */
750 cylinder(-2.5, 0, 0, 1, 3, 2.5, 0, 0, ONEREV);
751 Rect(-2, -0.3, 2.8, 0.5, 0.6, 5.6);
752 Rect(-2, -2.8, 0.3, 0.5, 5.6, 0.6);
754 /* now make each of the shaft bits between the cranks,
755 * starting from the flywheel end which is at X-coord 0.
756 * the first cranskhaft bit is always 2 units long
758 rod(-2, 0, 0, 2, crankDiam);
760 /* Each crank is crankWidth units wide and the total width of a cylinder assembly
761 * is 3.3 units. For inline engines, there is just a single crank per cylinder
762 * width. For other engine configurations, there is a crank between each pair
763 * of adjacent cylinders on one side of the engine, so the crankOffset length is
767 if (ENG.includedAngle != 0)
769 for (j = 0; j < ENG.cylinders - 1; j++)
770 rod(crankWidth - crankThick + crankOffset*j, 0, 0,
771 crankOffset - crankWidth + 2 * crankThick, crankDiam);
772 /* the last bit connects to the engine wall on the non-flywheel end */
773 rod(crankWidth - crankThick + crankOffset*j, 0, 0, 0.9, crankDiam);
776 for (j = 0; j < ENG.cylinders; j++)
780 glRotatef(HALFREV+ENG.pistonAngle[j]+ENG.includedAngle,1,0,0);
782 glRotatef(HALFREV+ENG.pistonAngle[j],1,0,0);
784 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
785 rod(crankOffset*j, -1.0, 0.0, crankWidth, crankDiam);
786 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
787 /* draw right part of crank */
788 CrankBit(crankOffset*j);
789 /* draw left part of crank */
790 CrankBit(crankWidth-crankThick+crankOffset*j);
796 void reshape_engine(ModeInfo *mi, int width, int height)
799 glViewport(0,0,(GLint)width, (GLint) height);
800 glMatrixMode(GL_PROJECTION);
802 glFrustum(-1.0,1.0,-1.0,1.0,1.5,70.0);
803 glMatrixMode(GL_MODELVIEW);
804 win_h = height; win_w = width;
808 void init_engine(ModeInfo *mi)
810 int screen = MI_SCREEN(mi);
813 if (engine == NULL) {
814 if ((engine = (Engine *) calloc(MI_NUM_SCREENS(mi),
815 sizeof(Engine))) == NULL)
819 e->window = MI_WINDOW(mi);
821 e->x = e->y = e->z = e->a = e->an1 = e->nx = e->ny = e->nz =
822 e->dx = e->dy = e->dz = e->da = 0;
825 e->dx = (float)(random() % 1000)/30000;
826 e->dy = (float)(random() % 1000)/30000;
827 e->dz = (float)(random() % 1000)/30000;
829 viewer[0] = 0; viewer[1] = 2; viewer[2] = 18;
830 lookat[0] = 0; lookat[1] = 0; lookat[2] = 0;
834 e->da = (float)(random() % 1000)/125 - 4;
835 e->nx = (float)(random() % 100) / 100;
836 e->ny = (float)(random() % 100) / 100;
837 e->nz = (float)(random() % 100) / 100;
841 double spin_speed = 1.0;
842 double wander_speed = 0.03;
844 e->rot = make_rotator (spin ? spin_speed : 0,
845 spin ? spin_speed : 0,
846 spin ? spin_speed : 0,
848 move ? wander_speed : 0,
851 e->trackball = gltrackball_init ();
854 if ((e->glx_context = init_GL(mi)) != NULL) {
855 reshape_engine(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
859 glClearColor(0.0,0.0,0.0,0.0);
860 glShadeModel(GL_SMOOTH);
861 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
862 glEnable(GL_DEPTH_TEST);
863 glEnable(GL_LIGHTING);
865 glEnable(GL_NORMALIZE);
867 engineType = find_engine(which_engine);
872 Bool engine_handle_event (ModeInfo *mi, XEvent *event) {
873 Engine *e = &engine[MI_SCREEN(mi)];
875 if (event->xany.type == ButtonPress &&
876 event->xbutton.button & Button1)
878 e->button_down_p = True;
879 gltrackball_start (e->trackball,
880 event->xbutton.x, event->xbutton.y,
881 MI_WIDTH (mi), MI_HEIGHT (mi));
885 else if (event->xany.type == ButtonRelease &&
886 event->xbutton.button & Button1) {
887 e->button_down_p = False;
891 else if (event->xany.type == MotionNotify &&
893 gltrackball_track (e->trackball,
894 event->xmotion.x, event->xmotion.y,
895 MI_WIDTH (mi), MI_HEIGHT (mi));
901 void draw_engine(ModeInfo *mi) {
902 Engine *e = &engine[MI_SCREEN(mi)];
903 Window w = MI_WINDOW(mi);
904 Display *disp = MI_DISPLAY(mi);
909 glXMakeCurrent(disp, w, *(e->glx_context));
914 if(mi->fps_p) do_fps(mi);
916 glXSwapBuffers(disp, w);
919 void release_engine(ModeInfo *mi) {
921 if (engine != NULL) {
922 (void) free((void *) engine);