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