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