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