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