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