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