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