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