ftp://ftp.uni-heidelberg.de/pub/X11/contrib/applications/xscreensaver-2.07.tar.gz
[xscreensaver] / hacks / glx / pipes.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * pipes.c - Shows 3D selfbuiding pipe system (xlock Version)
3  */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)pipes.c       4.04 97/07/28 xlockmore";
6 #endif
7 /* Permission to use, copy, modify, and distribute this software and its
8  * documentation for any purpose and without fee is hereby granted,
9  * provided that the above copyright notice appear in all copies and that
10  * both that copyright notice and this permission notice appear in
11  * supporting documentation.
12  *
13  * This file is provided AS IS with no warranties of any kind.  The author
14  * shall have no liability with respect to the infringement of copyrights,
15  * trade secrets or any patents by this file or any part thereof.  In no
16  * event will the author be liable for any lost revenue or profits or
17  * other special, indirect and consequential damages.
18  *
19  * This program was inspired on a WindowsNT(R)'s screen saver. It was written 
20  * from scratch and it was not based on any other source code.
21  *
22  * ==========================================================================
23  * The routine myElbow is derivated from the doughnut routine from the MesaGL
24  * library (more especifically the Mesaaux library) written by Brian Paul.
25  * ==========================================================================
26  *
27  * Thanks goes to Brian Paul for making it possible and inexpensive to use
28  * OpenGL at home.
29  *
30  * Since I'm not a native english speaker, my apologies for any gramatical
31  * mistake.
32  *
33  * My e-mail addresses are
34  *
35  * vianna@cat.cbpf.br 
36  *         and
37  * marcelo@venus.rdc.puc-rio.br
38  *
39  * Marcelo F. Vianna (Apr-09-1997)
40  *
41  * Revision History:
42  * 29-Apr-97: Factory equipment by Ed Mackey.  Productive day today, eh?
43  * 29-Apr-97: Less tight turns Jeff Epler <jepler@inetnebr.com>
44  * 29-Apr-97: Efficiency speed-ups by Marcelo F. Vianna
45  */
46
47 /*-
48  * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
49  * otherwise caddr_t is not defined correctly
50  */
51
52 #include <X11/Intrinsic.h>
53
54 #ifdef STANDALONE
55 # define PROGCLASS                                      "Pipes"
56 # define HACK_INIT                                      init_pipes
57 # define HACK_DRAW                                      draw_pipes
58 # define pipes_opts                                     xlockmore_opts
59 # define DEFAULTS       "*count:                2       \n"                     \
60                                         "*cycles:               5       \n"                     \
61                                         "*size:                 500     \n"                     \
62                                         "*delay:                100     \n"                     \
63                                         "*fisheye:              True    \n"                     \
64                                         "*tightturns:   False   \n"                     \
65                                         "*rotatepipes:  True    \n"                     \
66                                         "*noBuffer:             True    \n"
67 # include "xlockmore.h"                         /* from the xscreensaver distribution */
68 #else  /* !STANDALONE */
69 # include "xlock.h"                                     /* from the xlockmore distribution */
70 #endif /* !STANDALONE */
71
72 #ifdef USE_GL
73
74 #include <GL/glu.h>
75 #include "buildlwo.h"
76
77 #define DEF_FACTORY     "2"
78 #define DEF_FISHEYE     "True"
79 #define DEF_TIGHTTURNS  "False"
80 #define DEF_ROTATEPIPES "True"
81 #define NofSysTypes     3
82
83 static int  factory;
84 static Bool fisheye, tightturns, rotatepipes;
85
86 static XrmOptionDescRec opts[] =
87 {
88         {"-factory", ".pipes.factory", XrmoptionSepArg, (caddr_t) NULL},
89         {"-fisheye", ".pipes.fisheye", XrmoptionNoArg, (caddr_t) "on"},
90         {"+fisheye", ".pipes.fisheye", XrmoptionNoArg, (caddr_t) "off"},
91         {"-tightturns", ".pipes.tightturns", XrmoptionNoArg, (caddr_t) "on"},
92         {"+tightturns", ".pipes.tightturns", XrmoptionNoArg, (caddr_t) "off"},
93       {"-rotatepipes", ".pipes.rotatepipes", XrmoptionNoArg, (caddr_t) "on"},
94       {"+rotatepipes", ".pipes.rotatepipes", XrmoptionNoArg, (caddr_t) "off"}
95 };
96 static argtype vars[] =
97 {
98         {(caddr_t *) & factory, "factory", "Factory", DEF_FACTORY, t_Int},
99         {(caddr_t *) & fisheye, "fisheye", "Fisheye", DEF_FISHEYE, t_Bool},
100         {(caddr_t *) & tightturns, "tightturns", "Tightturns", DEF_TIGHTTURNS, t_Bool},
101         {(caddr_t *) & rotatepipes, "rotatepipes", "Rotatepipes", DEF_ROTATEPIPES, t_Bool}
102 };
103 static OptionStruct desc[] =
104 {
105         {"-factory num", "how much extra equipment in pipes (0 for none)"},
106         {"-/+fisheye", "turn on/off zoomed-in view of pipes"},
107         {"-/+tightturns", "turn on/off tight turns"},
108         {"-/+rotatepipes", "turn on/off pipe system rotation per screenful"}
109 };
110
111 ModeSpecOpt pipes_opts =
112 {7, opts, 4, vars, desc};
113
114 #define Scale4Window               0.1
115 #define Scale4Iconic               0.07
116
117 #define one_third                  0.3333333333333333333
118
119 #define dirNone -1
120 #define dirUP 0
121 #define dirDOWN 1
122 #define dirLEFT 2
123 #define dirRIGHT 3
124 #define dirNEAR 4
125 #define dirFAR 5
126
127 #define HCELLS 33
128 #define VCELLS 25
129 #define DEFINEDCOLORS 7
130 #define elbowradius 0.5
131
132 /*************************************************************************/
133
134 typedef struct {
135 #if defined( MESA ) && defined( SLOW )
136         int         flip;
137 #endif
138         GLint       WindH, WindW;
139         int         Cells[HCELLS][VCELLS][HCELLS];
140         int         usedcolors[DEFINEDCOLORS];
141         int         directions[6];
142         int         ndirections;
143         int         nowdir, olddir;
144         int         system_number;
145         int         counter;
146         int         PX, PY, PZ;
147         int         number_of_systems;
148         int         system_type;
149         int         system_length;
150         int         turncounter;
151         float      *system_color;
152         GLfloat     initial_rotation;
153         GLuint      valve, bolts, betweenbolts, elbowbolts, elbowcoins;
154         GLuint      guagehead, guageface, guagedial, guageconnector;
155         GLXContext  glx_context;
156 } pipesstruct;
157
158 extern struct lwo LWO_BigValve, LWO_PipeBetweenBolts, LWO_Bolts3D;
159 extern struct lwo LWO_GuageHead, LWO_GuageFace, LWO_GuageDial, LWO_GuageConnector;
160 extern struct lwo LWO_ElbowBolts, LWO_ElbowCoins;
161
162 static float front_shininess[] =
163 {60.0};
164 static float front_specular[] =
165 {0.7, 0.7, 0.7, 1.0};
166 static float ambient0[] =
167 {0.4, 0.4, 0.4, 1.0};
168 static float diffuse0[] =
169 {1.0, 1.0, 1.0, 1.0};
170 static float ambient1[] =
171 {0.2, 0.2, 0.2, 1.0};
172 static float diffuse1[] =
173 {0.5, 0.5, 0.5, 1.0};
174 static float position0[] =
175 {1.0, 1.0, 1.0, 0.0};
176 static float position1[] =
177 {-1.0, -1.0, 1.0, 0.0};
178 static float lmodel_ambient[] =
179 {0.5, 0.5, 0.5, 1.0};
180 static float lmodel_twoside[] =
181 {GL_TRUE};
182
183 static float MaterialRed[] =
184 {0.7, 0.0, 0.0, 1.0};
185 static float MaterialGreen[] =
186 {0.1, 0.5, 0.2, 1.0};
187 static float MaterialBlue[] =
188 {0.0, 0.0, 0.7, 1.0};
189 static float MaterialCyan[] =
190 {0.2, 0.5, 0.7, 1.0};
191 static float MaterialYellow[] =
192 {0.7, 0.7, 0.0, 1.0};
193 static float MaterialMagenta[] =
194 {0.6, 0.2, 0.5, 1.0};
195 static float MaterialWhite[] =
196 {0.7, 0.7, 0.7, 1.0};
197 static float MaterialGray[] =
198 {0.2, 0.2, 0.2, 1.0};
199
200 static pipesstruct *pipes = NULL;
201
202
203 static void
204 MakeTube(int direction)
205 {
206         float       an;
207         float       SINan_3, COSan_3;
208
209         /*dirUP    = 00000000 */
210         /*dirDOWN  = 00000001 */
211         /*dirLEFT  = 00000010 */
212         /*dirRIGHT = 00000011 */
213         /*dirNEAR  = 00000100 */
214         /*dirFAR   = 00000101 */
215
216         glRotatef(90.0, (direction & 6) ? 0.0 : 1.0, (direction & 2) ? 1.0 : 0.0, 0.0);
217
218         glBegin(GL_QUAD_STRIP);
219         for (an = 0.0; an <= 2.0 * M_PI; an += M_PI / 12.0) {
220                 glNormal3f((COSan_3 = cos(an) / 3.0), (SINan_3 = sin(an) / 3.0), 0.0);
221                 glVertex3f(COSan_3, SINan_3, one_third);
222                 glVertex3f(COSan_3, SINan_3, -one_third);
223         }
224         glEnd();
225 }
226
227 static void
228 mySphere(float radius)
229 {
230         GLUquadricObj *quadObj;
231
232         quadObj = gluNewQuadric();
233         gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
234         gluSphere(quadObj, radius, 16, 16);
235         gluDeleteQuadric(quadObj);
236 }
237
238 static void
239 myElbow(ModeInfo * mi, int bolted)
240 {
241 #define nsides 25
242 #define rings 25
243 #define r one_third
244 #define R one_third
245
246         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
247
248         int         i, j;
249         GLfloat     p0[3], p1[3], p2[3], p3[3];
250         GLfloat     n0[3], n1[3], n2[3], n3[3];
251         GLfloat     COSphi, COSphi1, COStheta, COStheta1;
252         GLfloat     _SINtheta, _SINtheta1;
253
254         for (i = 0; i <= rings / 4; i++) {
255                 GLfloat     theta, theta1;
256
257                 theta = (GLfloat) i *2.0 * M_PI / rings;
258
259                 theta1 = (GLfloat) (i + 1) * 2.0 * M_PI / rings;
260                 for (j = 0; j < nsides; j++) {
261                         GLfloat     phi, phi1;
262
263                         phi = (GLfloat) j *2.0 * M_PI / nsides;
264
265                         phi1 = (GLfloat) (j + 1) * 2.0 * M_PI / nsides;
266
267                         p0[0] = (COStheta = cos(theta)) * (R + r * (COSphi = cos(phi)));
268                         p0[1] = (_SINtheta = -sin(theta)) * (R + r * COSphi);
269
270                         p1[0] = (COStheta1 = cos(theta1)) * (R + r * COSphi);
271                         p1[1] = (_SINtheta1 = -sin(theta1)) * (R + r * COSphi);
272
273                         p2[0] = COStheta1 * (R + r * (COSphi1 = cos(phi1)));
274                         p2[1] = _SINtheta1 * (R + r * COSphi1);
275
276                         p3[0] = COStheta * (R + r * COSphi1);
277                         p3[1] = _SINtheta * (R + r * COSphi1);
278
279                         n0[0] = COStheta * COSphi;
280                         n0[1] = _SINtheta * COSphi;
281
282                         n1[0] = COStheta1 * COSphi;
283                         n1[1] = _SINtheta1 * COSphi;
284
285                         n2[0] = COStheta1 * COSphi1;
286                         n2[1] = _SINtheta1 * COSphi1;
287
288                         n3[0] = COStheta * COSphi1;
289                         n3[1] = _SINtheta * COSphi1;
290
291                         p0[2] = p1[2] = r * (n0[2] = n1[2] = sin(phi));
292                         p2[2] = p3[2] = r * (n2[2] = n3[2] = sin(phi1));
293
294                         glBegin(GL_QUADS);
295                         glNormal3fv(n3);
296                         glVertex3fv(p3);
297                         glNormal3fv(n2);
298                         glVertex3fv(p2);
299                         glNormal3fv(n1);
300                         glVertex3fv(p1);
301                         glNormal3fv(n0);
302                         glVertex3fv(p0);
303                         glEnd();
304                 }
305         }
306
307         if (factory > 0 && bolted) {
308                 /* Bolt the elbow onto the pipe system */
309                 glFrontFace(GL_CW);
310                 glPushMatrix();
311                 glRotatef(90.0, 0.0, 0.0, -1.0);
312                 glRotatef(90.0, 0.0, 1.0, 0.0);
313                 glTranslatef(0.0, one_third, one_third);
314                 glCallList(pp->elbowcoins);
315                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
316                 glCallList(pp->elbowbolts);
317                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pp->system_color);
318                 glPopMatrix();
319                 glFrontFace(GL_CCW);
320         }
321 #undef r
322 #undef R
323 #undef nsides
324 #undef rings
325 }
326
327 static void
328 FindNeighbors(ModeInfo * mi)
329 {
330         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
331
332         pp->ndirections = 0;
333         pp->directions[dirUP] = (!pp->Cells[pp->PX][pp->PY + 1][pp->PZ]) ? 1 : 0;
334         pp->ndirections += pp->directions[dirUP];
335         pp->directions[dirDOWN] = (!pp->Cells[pp->PX][pp->PY - 1][pp->PZ]) ? 1 : 0;
336         pp->ndirections += pp->directions[dirDOWN];
337         pp->directions[dirLEFT] = (!pp->Cells[pp->PX - 1][pp->PY][pp->PZ]) ? 1 : 0;
338         pp->ndirections += pp->directions[dirLEFT];
339         pp->directions[dirRIGHT] = (!pp->Cells[pp->PX + 1][pp->PY][pp->PZ]) ? 1 : 0;
340         pp->ndirections += pp->directions[dirRIGHT];
341         pp->directions[dirFAR] = (!pp->Cells[pp->PX][pp->PY][pp->PZ - 1]) ? 1 : 0;
342         pp->ndirections += pp->directions[dirFAR];
343         pp->directions[dirNEAR] = (!pp->Cells[pp->PX][pp->PY][pp->PZ + 1]) ? 1 : 0;
344         pp->ndirections += pp->directions[dirNEAR];
345 }
346
347 static int
348 SelectNeighbor(ModeInfo * mi)
349 {
350         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
351         int         dirlist[6];
352         int         i, j;
353
354         for (i = 0, j = 0; i < 6; i++) {
355                 if (pp->directions[i]) {
356                         dirlist[j] = i;
357                         j++;
358                 }
359         }
360
361         return dirlist[NRAND(pp->ndirections)];
362 }
363
364 static void
365 MakeValve(ModeInfo * mi, int newdir)
366 {
367         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
368
369         /* There is a glPopMatrix() right after this subroutine returns. */
370         switch (newdir) {
371                 case dirUP:
372                 case dirDOWN:
373                         glRotatef(90.0, 1.0, 0.0, 0.0);
374                         glRotatef(NRAND(3) * 90.0, 0.0, 0.0, 1.0);
375                         break;
376                 case dirLEFT:
377                 case dirRIGHT:
378                         glRotatef(90.0, 0.0, -1.0, 0.0);
379                         glRotatef((NRAND(3) * 90.0) - 90.0, 0.0, 0.0, 1.0);
380                         break;
381                 case dirNEAR:
382                 case dirFAR:
383                         glRotatef(NRAND(4) * 90.0, 0.0, 0.0, 1.0);
384                         break;
385         }
386         glFrontFace(GL_CW);
387         glCallList(pp->betweenbolts);
388         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
389         glCallList(pp->bolts);
390         if (!MI_WIN_IS_MONO(mi)) {
391                 if (pp->system_color == MaterialRed) {
392                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, NRAND(2) ? MaterialYellow : MaterialBlue);
393                 } else if (pp->system_color == MaterialBlue) {
394                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, NRAND(2) ? MaterialRed : MaterialYellow);
395                 } else if (pp->system_color == MaterialYellow) {
396                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, NRAND(2) ? MaterialBlue : MaterialRed);
397                 } else {
398                         switch ((NRAND(3))) {
399                                 case 0:
400                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialRed);
401                                         break;
402                                 case 1:
403                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlue);
404                                         break;
405                                 case 2:
406                                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
407                         }
408                 }
409         }
410         glRotatef((GLfloat) (NRAND(90)), 1.0, 0.0, 0.0);
411         glCallList(pp->valve);
412         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pp->system_color);
413         glFrontFace(GL_CCW);
414 }
415
416 static int
417 MakeGuage(ModeInfo * mi, int newdir)
418 {
419         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
420
421         /* Can't have a guage on a vertical pipe. */
422         if ((newdir == dirUP) || (newdir == dirDOWN))
423                 return (0);
424
425         /* Is there space above this pipe for a guage? */
426         if (!pp->directions[dirUP])
427                 return (0);
428
429         /* Yes!  Mark the space as used. */
430         pp->Cells[pp->PX][pp->PY + 1][pp->PZ] = 1;
431
432         glFrontFace(GL_CW);
433         glPushMatrix();
434         if ((newdir == dirLEFT) || (newdir == dirRIGHT))
435                 glRotatef(90.0, 0.0, 1.0, 0.0);
436         glCallList(pp->betweenbolts);
437         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray);
438         glCallList(pp->bolts);
439         glPopMatrix();
440
441         glCallList(pp->guageconnector);
442         glPushMatrix();
443         glTranslatef(0.0, 1.33333, 0.0);
444         /* Do not change the above to 1 + ONE_THIRD, because */
445         /* the object really is centered on 1.3333300000. */
446         glRotatef(NRAND(270) + 45.0, 0.0, 0.0, -1.0);
447         /* Random rotation for the dial.  I love it. */
448         glCallList(pp->guagedial);
449         glPopMatrix();
450
451         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pp->system_color);
452         glCallList(pp->guagehead);
453
454         /* GuageFace is drawn last, in case of low-res depth buffers. */
455         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
456         glCallList(pp->guageface);
457
458         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pp->system_color);
459         glFrontFace(GL_CCW);
460
461         return (1);
462 }
463
464 static void
465 MakeShape(ModeInfo * mi, int newdir)
466 {
467         switch (NRAND(2)) {
468                 case 1:
469                         if (!MakeGuage(mi, newdir))
470                                 MakeTube(newdir);
471                         break;
472                 default:
473                         MakeValve(mi, newdir);
474                         break;
475         }
476 }
477
478 static void pinit(ModeInfo * mi, int zera);
479
480 void
481 draw_pipes(ModeInfo * mi)
482 {
483         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
484
485         Display    *display = MI_DISPLAY(mi);
486         Window      window = MI_WINDOW(mi);
487
488         int         newdir;
489         int         OPX, OPY, OPZ;
490
491         glXMakeCurrent(display, window, pp->glx_context);
492
493 #if defined( MESA ) && defined( SLOW )
494         glDrawBuffer(GL_BACK);
495 #else
496         glDrawBuffer(GL_FRONT);
497 #endif
498         glPushMatrix();
499
500         glTranslatef(0.0, 0.0, fisheye ? -3.8 : -4.8);
501         if (rotatepipes)
502                 glRotatef(pp->initial_rotation, 0.0, 1.0, 0.0);
503
504         if (!MI_WIN_IS_ICONIC(mi)) {
505                 /* Width/height ratio handled by gluPerspective() now. */
506                 glScalef(Scale4Window, Scale4Window, Scale4Window);
507         } else {
508                 glScalef(Scale4Iconic, Scale4Iconic, Scale4Iconic);
509         }
510
511         FindNeighbors(mi);
512
513         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pp->system_color);
514
515         /* If it's the begining of a system, draw a sphere */
516         if (pp->olddir == dirNone) {
517                 glPushMatrix();
518                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
519                 mySphere(0.6);
520                 glPopMatrix();
521         }
522         /* Check for stop conditions */
523         if (pp->ndirections == 0 || pp->counter > pp->system_length) {
524                 glPushMatrix();
525                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
526                 /* Finish the system with another sphere */
527                 mySphere(0.6);
528 #if defined( MESA ) && defined( SLOW )
529                 glXSwapBuffers(display, window);
530 #endif
531                 glPopMatrix();
532
533                 /* If the maximum number of system was drawn, restart (clearing the screen), */
534                 /* else start a new system. */
535                 if (++pp->system_number > pp->number_of_systems) {
536                         (void) sleep(1);
537                         pinit(mi, 1);
538                 } else {
539                         pinit(mi, 0);
540                 }
541
542                 glPopMatrix();
543                 return;
544         }
545         pp->counter++;
546         pp->turncounter++;
547
548         /* Do will the direction change? if so, determine the new one */
549         newdir = pp->nowdir;
550         if (!pp->directions[newdir]) {  /* cannot proceed in the current direction */
551                 newdir = SelectNeighbor(mi);
552         } else {
553                 if (tightturns) {
554                         /* random change (20% chance) */
555                         if ((pp->counter > 1) && (NRAND(100) < 20)) {
556                                 newdir = SelectNeighbor(mi);
557                         }
558                 } else {
559                         /* Chance to turn increases after each length of pipe drawn */
560                         if ((pp->counter > 1) && NRAND(50) < NRAND(pp->turncounter + 1)) {
561                                 newdir = SelectNeighbor(mi);
562                                 pp->turncounter = 0;
563                         }
564                 }
565         }
566
567         /* Has the direction changed? */
568         if (newdir == pp->nowdir) {
569                 /* If not, draw the cell's center pipe */
570                 glPushMatrix();
571                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
572                 /* Chance of factory shape here, if enabled. */
573                 if ((pp->counter > 1) && (NRAND(100) < factory)) {
574                         MakeShape(mi, newdir);
575                 } else {
576                         MakeTube(newdir);
577                 }
578                 glPopMatrix();
579         } else {
580                 /* If so, draw the cell's center elbow/sphere */
581                 int         sysT = pp->system_type;
582
583                 if (sysT == NofSysTypes + 1) {
584                         sysT = ((pp->system_number - 1) % NofSysTypes) + 1;
585                 }
586                 glPushMatrix();
587
588                 switch (sysT) {
589                         case 1:
590                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
591                                 mySphere(elbowradius);
592                                 break;
593                         case 2:
594                         case 3:
595                                 switch (pp->nowdir) {
596                                         case dirUP:
597                                                 switch (newdir) {
598                                                         case dirLEFT:
599                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
600                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
601                                                                 break;
602                                                         case dirRIGHT:
603                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
604                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
605                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
606                                                                 break;
607                                                         case dirFAR:
608                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
609                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
610                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
611                                                                 break;
612                                                         case dirNEAR:
613                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
614                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
615                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
616                                                                 break;
617                                                 }
618                                                 break;
619                                         case dirDOWN:
620                                                 switch (newdir) {
621                                                         case dirLEFT:
622                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
623                                                                 break;
624                                                         case dirRIGHT:
625                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
626                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
627                                                                 break;
628                                                         case dirFAR:
629                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
630                                                                 glRotatef(270.0, 0.0, 1.0, 0.0);
631                                                                 break;
632                                                         case dirNEAR:
633                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
634                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
635                                                                 break;
636                                                 }
637                                                 break;
638                                         case dirLEFT:
639                                                 switch (newdir) {
640                                                         case dirUP:
641                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
642                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
643                                                                 break;
644                                                         case dirDOWN:
645                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
646                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
647                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
648                                                                 break;
649                                                         case dirFAR:
650                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
651                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
652                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
653                                                                 break;
654                                                         case dirNEAR:
655                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
656                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
657                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
658                                                                 break;
659                                                 }
660                                                 break;
661                                         case dirRIGHT:
662                                                 switch (newdir) {
663                                                         case dirUP:
664                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
665                                                                 break;
666                                                         case dirDOWN:
667                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
668                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
669                                                                 break;
670                                                         case dirFAR:
671                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
672                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
673                                                                 break;
674                                                         case dirNEAR:
675                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
676                                                                 glRotatef(90.0, 1.0, 0.0, 0.0);
677                                                                 break;
678                                                 }
679                                                 break;
680                                         case dirNEAR:
681                                                 switch (newdir) {
682                                                         case dirLEFT:
683                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
684                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
685                                                                 break;
686                                                         case dirRIGHT:
687                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
688                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
689                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
690                                                                 break;
691                                                         case dirUP:
692                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
693                                                                 glRotatef(270.0, 0.0, 1.0, 0.0);
694                                                                 break;
695                                                         case dirDOWN:
696                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
697                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
698                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
699                                                                 break;
700                                                 }
701                                                 break;
702                                         case dirFAR:
703                                                 switch (newdir) {
704                                                         case dirUP:
705                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
706                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
707                                                                 break;
708                                                         case dirDOWN:
709                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
710                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
711                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
712                                                                 break;
713                                                         case dirLEFT:
714                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
715                                                                 glRotatef(90.0, 1.0, 0.0, 0.0);
716                                                                 break;
717                                                         case dirRIGHT:
718                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
719                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
720                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
721                                                                 break;
722                                                 }
723                                                 break;
724                                 }
725                                 myElbow(mi, (sysT == 2));
726                                 break;
727                 }
728                 glPopMatrix();
729         }
730
731         OPX = pp->PX;
732         OPY = pp->PY;
733         OPZ = pp->PZ;
734         pp->olddir = pp->nowdir;
735         pp->nowdir = newdir;
736         switch (pp->nowdir) {
737                 case dirUP:
738                         pp->PY++;
739                         break;
740                 case dirDOWN:
741                         pp->PY--;
742                         break;
743                 case dirLEFT:
744                         pp->PX--;
745                         break;
746                 case dirRIGHT:
747                         pp->PX++;
748                         break;
749                 case dirNEAR:
750                         pp->PZ++;
751                         break;
752                 case dirFAR:
753                         pp->PZ--;
754                         break;
755         }
756         pp->Cells[pp->PX][pp->PY][pp->PZ] = 1;
757
758         /* Cells'face pipe */
759         glTranslatef(((pp->PX + OPX) / 2.0 - 16) / 3.0 * 4.0, ((pp->PY + OPY) / 2.0 - 12) / 3.0 * 4.0, ((pp->PZ + OPZ) / 2.0 - 16) / 3.0 * 4.0);
760         MakeTube(newdir);
761
762         glPopMatrix();
763
764         glFlush();
765
766 #if defined( MESA ) && defined( SLOW )
767         pp->flip = !pp->flip;
768         if (pp->flip)
769                 glXSwapBuffers(display, window);
770 #endif
771 }
772
773 static void
774 reshape(ModeInfo * mi, int width, int height)
775 {
776         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
777
778         glViewport(0, 0, pp->WindW = (GLint) width, pp->WindH = (GLint) height);
779         glMatrixMode(GL_PROJECTION);
780         glLoadIdentity();
781         /*glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0); */
782         gluPerspective(65.0, (GLfloat) width / (GLfloat) height, 0.1, 20.0);
783         glMatrixMode(GL_MODELVIEW);
784 }
785
786 static void
787 pinit(ModeInfo * mi, int zera)
788 {
789         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
790         int         X, Y, Z;
791
792         glClearDepth(1.0);
793         glClearColor(0.0, 0.0, 0.0, 1.0);
794         glColor3f(1.0, 1.0, 1.0);
795
796         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
797         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
798         glLightfv(GL_LIGHT0, GL_POSITION, position0);
799         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
800         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
801         glLightfv(GL_LIGHT1, GL_POSITION, position1);
802         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
803         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
804         glEnable(GL_LIGHTING);
805         glEnable(GL_LIGHT0);
806         glEnable(GL_LIGHT1);
807         glEnable(GL_DEPTH_TEST);
808         glEnable(GL_NORMALIZE);
809         glEnable(GL_CULL_FACE);
810
811         glShadeModel(GL_SMOOTH);
812         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
813         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
814
815         if (zera) {
816                 pp->system_number = 1;
817                 glDrawBuffer(GL_FRONT_AND_BACK);
818                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
819                 (void) memset(pp->Cells, 0, sizeof (pp->Cells));
820                 for (X = 0; X < HCELLS; X++) {
821                         for (Y = 0; Y < VCELLS; Y++) {
822                                 pp->Cells[X][Y][0] = 1;
823                                 pp->Cells[X][Y][HCELLS - 1] = 1;
824                                 pp->Cells[0][Y][X] = 1;
825                                 pp->Cells[HCELLS - 1][Y][X] = 1;
826                         }
827                 }
828                 for (X = 0; X < HCELLS; X++) {
829                         for (Z = 0; Z < HCELLS; Z++) {
830                                 pp->Cells[X][0][Z] = 1;
831                                 pp->Cells[X][VCELLS - 1][Z] = 1;
832                         }
833                 }
834                 (void) memset(pp->usedcolors, 0, sizeof (pp->usedcolors));
835                 if ((pp->initial_rotation += 10.0) > 45.0) {
836                         pp->initial_rotation -= 90.0;
837                 }
838         }
839         pp->counter = 0;
840         pp->turncounter = 0;
841
842         if (!MI_WIN_IS_MONO(mi)) {
843                 int         collist[DEFINEDCOLORS];
844                 int         i, j, lower = 1000;
845
846                 /* Avoid repeating colors on the same screen unless necessary */
847                 for (i = 0; i < DEFINEDCOLORS; i++) {
848                         if (lower > pp->usedcolors[i])
849                                 lower = pp->usedcolors[i];
850                 }
851                 for (i = 0, j = 0; i < DEFINEDCOLORS; i++) {
852                         if (pp->usedcolors[i] == lower) {
853                                 collist[j] = i;
854                                 j++;
855                         }
856                 }
857                 i = collist[NRAND(j)];
858                 pp->usedcolors[i]++;
859                 switch (i) {
860                         case 0:
861                                 pp->system_color = MaterialRed;
862                                 break;
863                         case 1:
864                                 pp->system_color = MaterialGreen;
865                                 break;
866                         case 2:
867                                 pp->system_color = MaterialBlue;
868                                 break;
869                         case 3:
870                                 pp->system_color = MaterialCyan;
871                                 break;
872                         case 4:
873                                 pp->system_color = MaterialYellow;
874                                 break;
875                         case 5:
876                                 pp->system_color = MaterialMagenta;
877                                 break;
878                         case 6:
879                                 pp->system_color = MaterialWhite;
880                                 break;
881                 }
882         } else {
883                 pp->system_color = MaterialGray;
884         }
885
886         do {
887                 pp->PX = NRAND((HCELLS - 1)) + 1;
888                 pp->PY = NRAND((VCELLS - 1)) + 1;
889                 pp->PZ = NRAND((HCELLS - 1)) + 1;
890         } while (pp->Cells[pp->PX][pp->PY][pp->PZ] ||
891                  (pp->Cells[pp->PX + 1][pp->PY][pp->PZ] && pp->Cells[pp->PX - 1][pp->PY][pp->PZ] &&
892                   pp->Cells[pp->PX][pp->PY + 1][pp->PZ] && pp->Cells[pp->PX][pp->PY - 1][pp->PZ] &&
893                   pp->Cells[pp->PX][pp->PY][pp->PZ + 1] && pp->Cells[pp->PX][pp->PY][pp->PZ - 1]));
894         pp->Cells[pp->PX][pp->PY][pp->PZ] = 1;
895         pp->olddir = dirNone;
896
897         FindNeighbors(mi);
898
899         pp->nowdir = SelectNeighbor(mi);
900 }
901
902 void
903 init_pipes(ModeInfo * mi)
904 {
905         int         screen = MI_SCREEN(mi);
906         pipesstruct *pp;
907
908         if (pipes == NULL) {
909                 if ((pipes = (pipesstruct *) calloc(MI_NUM_SCREENS(mi),
910                                               sizeof (pipesstruct))) == NULL)
911                         return;
912         }
913         pp = &pipes[screen];
914
915         pp->glx_context = init_GL(mi);
916
917         reshape(mi, MI_WIN_WIDTH(mi), MI_WIN_HEIGHT(mi));
918         pp->initial_rotation = -10.0;
919         pinit(mi, 1);
920
921         if (factory > 0) {
922                 pp->valve = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_BigValve);
923                 pp->bolts = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_Bolts3D);
924                 pp->betweenbolts = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_PipeBetweenBolts);
925
926                 pp->elbowbolts = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_ElbowBolts);
927                 pp->elbowcoins = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_ElbowCoins);
928
929                 pp->guagehead = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_GuageHead);
930                 pp->guageface = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_GuageFace);
931                 pp->guagedial = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_GuageDial);
932                 pp->guageconnector = BuildLWO(MI_WIN_IS_WIREFRAME(mi), &LWO_GuageConnector);
933         }
934         /* else they are all 0, thanks to calloc(). */
935
936         if (MI_BATCHCOUNT(mi) < 1 || MI_BATCHCOUNT(mi) > NofSysTypes + 1) {
937                 pp->system_type = NRAND(NofSysTypes) + 1;
938         } else {
939                 pp->system_type = MI_BATCHCOUNT(mi);
940         }
941
942         if (MI_CYCLES(mi) > 0 && MI_CYCLES(mi) < 11) {
943                 pp->number_of_systems = MI_CYCLES(mi);
944         } else {
945                 pp->number_of_systems = 5;
946         }
947
948         if (MI_SIZE(mi) < 10) {
949                 pp->system_length = 10;
950         } else if (MI_SIZE(mi) > 1000) {
951                 pp->system_length = 1000;
952         } else {
953                 pp->system_length = MI_SIZE(mi);
954         }
955
956 }
957
958 void
959 change_pipes(ModeInfo * mi)
960 {
961         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
962
963         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), pp->glx_context);
964         pinit(mi, 1);
965 }
966
967 void
968 release_pipes(ModeInfo * mi)
969 {
970         if (pipes != NULL) {
971                 int         screen;
972
973                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
974                         pipesstruct *pp = &pipes[screen];
975
976                         /* Display lists MUST be freed while their glXContext is current. */
977                         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), pp->glx_context);
978
979                         if (pp->valve)
980                                 glDeleteLists(pp->valve, 1);
981                         if (pp->bolts)
982                                 glDeleteLists(pp->bolts, 1);
983                         if (pp->betweenbolts)
984                                 glDeleteLists(pp->betweenbolts, 1);
985
986                         if (pp->elbowbolts)
987                                 glDeleteLists(pp->elbowbolts, 1);
988                         if (pp->elbowcoins)
989                                 glDeleteLists(pp->elbowcoins, 1);
990
991                         if (pp->guagehead)
992                                 glDeleteLists(pp->guagehead, 1);
993                         if (pp->guageface)
994                                 glDeleteLists(pp->guageface, 1);
995                         if (pp->guagedial)
996                                 glDeleteLists(pp->guagedial, 1);
997                         if (pp->guageconnector)
998                                 glDeleteLists(pp->guageconnector, 1);
999                 }
1000
1001                 /* Don't destroy the glXContext.  init_GL does that. */
1002
1003                 (void) free((void *) pipes);
1004                 pipes = NULL;
1005         }
1006 }
1007
1008 #endif