ftp://ftp.krokus.ru/pub/OpenBSD/distfiles/xscreensaver-5.01.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 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 static void
479 pinit(ModeInfo * mi, int zera)
480 {
481         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
482         int         X, Y, Z;
483
484     if (zera)
485       mi->polygon_count = 0;
486
487         glClearDepth(1.0);
488         glClearColor(0.0, 0.0, 0.0, 1.0);
489         glColor3f(1.0, 1.0, 1.0);
490
491         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
492         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
493         glLightfv(GL_LIGHT0, GL_POSITION, position0);
494         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
495         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
496         glLightfv(GL_LIGHT1, GL_POSITION, position1);
497         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
498         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
499         glEnable(GL_LIGHTING);
500         glEnable(GL_LIGHT0);
501         glEnable(GL_LIGHT1);
502         glEnable(GL_DEPTH_TEST);
503         glEnable(GL_NORMALIZE);
504         glEnable(GL_CULL_FACE);
505
506         glShadeModel(GL_SMOOTH);
507         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
508         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
509
510         if (zera) {
511                 pp->system_number = 1;
512                 glDrawBuffer(dbuf_p ? GL_BACK : GL_FRONT);
513                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
514                 (void) memset(pp->Cells, 0, sizeof (pp->Cells));
515                 for (X = 0; X < HCELLS; X++) {
516                         for (Y = 0; Y < VCELLS; Y++) {
517                                 pp->Cells[X][Y][0] = 1;
518                                 pp->Cells[X][Y][HCELLS - 1] = 1;
519                                 pp->Cells[0][Y][X] = 1;
520                                 pp->Cells[HCELLS - 1][Y][X] = 1;
521                         }
522                 }
523                 for (X = 0; X < HCELLS; X++) {
524                         for (Z = 0; Z < HCELLS; Z++) {
525                                 pp->Cells[X][0][Z] = 1;
526                                 pp->Cells[X][VCELLS - 1][Z] = 1;
527                         }
528                 }
529                 (void) memset(pp->usedcolors, 0, sizeof (pp->usedcolors));
530                 if ((pp->initial_rotation += 10.0) > 45.0) {
531                         pp->initial_rotation -= 90.0;
532                 }
533         }
534         pp->counter = 0;
535         pp->turncounter = 0;
536
537         if (!MI_IS_MONO(mi)) {
538                 int         collist[DEFINEDCOLORS];
539                 int         i, j, lower = 1000;
540
541                 /* Avoid repeating colors on the same screen unless necessary */
542                 for (i = 0; i < DEFINEDCOLORS; i++) {
543                         if (lower > pp->usedcolors[i])
544                                 lower = pp->usedcolors[i];
545                 }
546                 for (i = 0, j = 0; i < DEFINEDCOLORS; i++) {
547                         if (pp->usedcolors[i] == lower) {
548                                 collist[j] = i;
549                                 j++;
550                         }
551                 }
552                 i = collist[NRAND(j)];
553                 pp->usedcolors[i]++;
554                 switch (i) {
555                         case 0:
556                                 pp->system_color = MaterialRed;
557                                 break;
558                         case 1:
559                                 pp->system_color = MaterialGreen;
560                                 break;
561                         case 2:
562                                 pp->system_color = MaterialBlue;
563                                 break;
564                         case 3:
565                                 pp->system_color = MaterialCyan;
566                                 break;
567                         case 4:
568                                 pp->system_color = MaterialYellow;
569                                 break;
570                         case 5:
571                                 pp->system_color = MaterialMagenta;
572                                 break;
573                         case 6:
574                                 pp->system_color = MaterialWhite;
575                                 break;
576                 }
577         } else {
578                 pp->system_color = MaterialGray;
579         }
580
581         do {
582                 pp->PX = NRAND((HCELLS - 1)) + 1;
583                 pp->PY = NRAND((VCELLS - 1)) + 1;
584                 pp->PZ = NRAND((HCELLS - 1)) + 1;
585         } while (pp->Cells[pp->PX][pp->PY][pp->PZ] ||
586                  (pp->Cells[pp->PX + 1][pp->PY][pp->PZ] && pp->Cells[pp->PX - 1][pp->PY][pp->PZ] &&
587                   pp->Cells[pp->PX][pp->PY + 1][pp->PZ] && pp->Cells[pp->PX][pp->PY - 1][pp->PZ] &&
588                   pp->Cells[pp->PX][pp->PY][pp->PZ + 1] && pp->Cells[pp->PX][pp->PY][pp->PZ - 1]));
589         pp->Cells[pp->PX][pp->PY][pp->PZ] = 1;
590         pp->olddir = dirNone;
591
592         FindNeighbors(mi);
593
594         pp->nowdir = SelectNeighbor(mi);
595 }
596
597 ENTRYPOINT void
598 reshape_pipes(ModeInfo * mi, int width, int height)
599 {
600         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
601     pinit(mi, 1);
602
603         glViewport(0, 0, pp->WindW = (GLint) width, pp->WindH = (GLint) height);
604         glMatrixMode(GL_PROJECTION);
605         glLoadIdentity();
606         /*glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0); */
607         gluPerspective(65.0, (GLfloat) width / (GLfloat) height, 0.1, 20.0);
608         glMatrixMode(GL_MODELVIEW);
609
610   glClear(GL_COLOR_BUFFER_BIT);
611 }
612
613 ENTRYPOINT void
614 init_pipes (ModeInfo * mi)
615 {
616         int         screen = MI_SCREEN(mi);
617         pipesstruct *pp;
618
619         if (pipes == NULL) {
620                 if ((pipes = (pipesstruct *) calloc(MI_NUM_SCREENS(mi),
621                                               sizeof (pipesstruct))) == NULL)
622                         return;
623         }
624         pp = &pipes[screen];
625
626         pp->window = MI_WINDOW(mi);
627         if ((pp->glx_context = init_GL(mi)) != NULL) {
628
629                 reshape_pipes(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
630                 if (rotatepipes)
631                   pp->initial_rotation = NRAND(180); /* jwz */
632                 else
633                   pp->initial_rotation = -10.0;
634                 pinit(mi, 1);
635
636                 if (factory > 0) {
637                         pp->valve = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_BigValve);
638                         pp->bolts = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_Bolts3D);
639                         pp->betweenbolts = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_PipeBetweenBolts);
640
641                         pp->elbowbolts = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_ElbowBolts);
642                         pp->elbowcoins = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_ElbowCoins);
643
644                         pp->guagehead = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_GuageHead);
645                         pp->guageface = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_GuageFace);
646                         pp->guagedial = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_GuageDial);
647                         pp->guageconnector = BuildLWO(MI_IS_WIREFRAME(mi), &LWO_GuageConnector);
648                 }
649                 /* else they are all 0, thanks to calloc(). */
650
651                 if (MI_COUNT(mi) < 1 || MI_COUNT(mi) > NofSysTypes + 1) {
652                         pp->system_type = NRAND(NofSysTypes) + 1;
653                 } else {
654                         pp->system_type = MI_COUNT(mi);
655                 }
656
657                 if (MI_CYCLES(mi) > 0 && MI_CYCLES(mi) < 11) {
658                         pp->number_of_systems = MI_CYCLES(mi);
659                 } else {
660                         pp->number_of_systems = 5;
661                 }
662
663                 if (MI_SIZE(mi) < 10) {
664                         pp->system_length = 10;
665                 } else if (MI_SIZE(mi) > 1000) {
666                         pp->system_length = 1000;
667                 } else {
668                         pp->system_length = MI_SIZE(mi);
669                 }
670         } else {
671                 MI_CLEARWINDOW(mi);
672         }
673 }
674
675 ENTRYPOINT void
676 draw_pipes (ModeInfo * mi)
677 {
678         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
679
680         Display    *display = MI_DISPLAY(mi);
681         Window      window = MI_WINDOW(mi);
682
683         int         newdir;
684         int         OPX, OPY, OPZ;
685
686         if (!pp->glx_context)
687                 return;
688
689         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(pp->glx_context));
690
691     if (pp->reset) {
692       if (--pp->reset) {
693         /* Would be nice to fade to black here, by drawing successive quads
694            over the whole scene with gamma. */
695         return;
696       }
697       pinit(mi, 1);
698     }
699
700         glPushMatrix();
701
702         glTranslatef(0.0, 0.0, fisheye ? -3.8 : -4.8);
703         if (rotatepipes)
704                 glRotatef(pp->initial_rotation, 0.0, 1.0, 0.0);
705
706         if (!MI_IS_ICONIC(mi)) {
707                 /* Width/height ratio handled by gluPerspective() now. */
708                 glScalef(Scale4Window, Scale4Window, Scale4Window);
709         } else {
710                 glScalef(Scale4Iconic, Scale4Iconic, Scale4Iconic);
711         }
712
713         FindNeighbors(mi);
714
715         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pp->system_color);
716
717         /* If it's the begining of a system, draw a sphere */
718         if (pp->olddir == dirNone) {
719                 glPushMatrix();
720                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
721                 mySphere(0.6);
722                 glPopMatrix();
723         }
724         /* Check for stop conditions */
725         if (pp->ndirections == 0 || pp->counter > pp->system_length) {
726                 glPushMatrix();
727                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
728                 /* Finish the system with another sphere */
729                 mySphere(0.6);
730
731                 glPopMatrix();
732
733                 /* If the maximum number of system was drawn, restart (clearing the screen), */
734                 /* else start a new system. */
735                 if (++pp->system_number > pp->number_of_systems) {
736           /* pause doing nothing for N seconds before clearing the screen. */
737           int secs = 3;
738           pp->reset = secs * 1000000 / MI_PAUSE(mi);
739                 } else {
740                         pinit(mi, 0);
741                 }
742
743                 glPopMatrix();
744                 return;
745         }
746         pp->counter++;
747         pp->turncounter++;
748
749         /* Do will the direction change? if so, determine the new one */
750         newdir = pp->nowdir;
751         if (!pp->directions[newdir]) {  /* cannot proceed in the current direction */
752                 newdir = SelectNeighbor(mi);
753         } else {
754                 if (tightturns) {
755                         /* random change (20% chance) */
756                         if ((pp->counter > 1) && (NRAND(100) < 20)) {
757                                 newdir = SelectNeighbor(mi);
758                         }
759                 } else {
760                         /* Chance to turn increases after each length of pipe drawn */
761                         if ((pp->counter > 1) && NRAND(50) < NRAND(pp->turncounter + 1)) {
762                                 newdir = SelectNeighbor(mi);
763                                 pp->turncounter = 0;
764                         }
765                 }
766         }
767
768         /* Has the direction changed? */
769         if (newdir == pp->nowdir) {
770                 /* If not, draw the cell's center pipe */
771                 glPushMatrix();
772                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
773                 /* Chance of factory shape here, if enabled. */
774                 if ((pp->counter > 1) && (NRAND(100) < factory)) {
775                         MakeShape(mi, newdir);
776                 } else {
777                         MakeTube(mi, newdir);
778                 }
779                 glPopMatrix();
780         } else {
781                 /* If so, draw the cell's center elbow/sphere */
782                 int         sysT = pp->system_type;
783
784                 if (sysT == NofSysTypes + 1) {
785                         sysT = ((pp->system_number - 1) % NofSysTypes) + 1;
786                 }
787                 glPushMatrix();
788
789                 switch (sysT) {
790                         case 1:
791                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
792                                 mySphere(elbowradius);
793                                 break;
794                         case 2:
795                         case 3:
796                                 switch (pp->nowdir) {
797                                         case dirUP:
798                                                 switch (newdir) {
799                                                         case dirLEFT:
800                                                                 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);
801                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
802                                                                 break;
803                                                         case dirRIGHT:
804                                                                 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);
805                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
806                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
807                                                                 break;
808                                                         case dirFAR:
809                                                                 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));
810                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
811                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
812                                                                 break;
813                                                         case dirNEAR:
814                                                                 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));
815                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
816                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
817                                                                 break;
818                                                 }
819                                                 break;
820                                         case dirDOWN:
821                                                 switch (newdir) {
822                                                         case dirLEFT:
823                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
824                                                                 break;
825                                                         case dirRIGHT:
826                                                                 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);
827                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
828                                                                 break;
829                                                         case dirFAR:
830                                                                 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));
831                                                                 glRotatef(270.0, 0.0, 1.0, 0.0);
832                                                                 break;
833                                                         case dirNEAR:
834                                                                 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));
835                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
836                                                                 break;
837                                                 }
838                                                 break;
839                                         case dirLEFT:
840                                                 switch (newdir) {
841                                                         case dirUP:
842                                                                 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);
843                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
844                                                                 break;
845                                                         case dirDOWN:
846                                                                 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);
847                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
848                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
849                                                                 break;
850                                                         case dirFAR:
851                                                                 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));
852                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
853                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
854                                                                 break;
855                                                         case dirNEAR:
856                                                                 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));
857                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
858                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
859                                                                 break;
860                                                 }
861                                                 break;
862                                         case dirRIGHT:
863                                                 switch (newdir) {
864                                                         case dirUP:
865                                                                 glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
866                                                                 break;
867                                                         case dirDOWN:
868                                                                 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);
869                                                                 glRotatef(180.0, 1.0, 0.0, 0.0);
870                                                                 break;
871                                                         case dirFAR:
872                                                                 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));
873                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
874                                                                 break;
875                                                         case dirNEAR:
876                                                                 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));
877                                                                 glRotatef(90.0, 1.0, 0.0, 0.0);
878                                                                 break;
879                                                 }
880                                                 break;
881                                         case dirNEAR:
882                                                 switch (newdir) {
883                                                         case dirLEFT:
884                                                                 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));
885                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
886                                                                 break;
887                                                         case dirRIGHT:
888                                                                 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));
889                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
890                                                                 glRotatef(180.0, 0.0, 1.0, 0.0);
891                                                                 break;
892                                                         case dirUP:
893                                                                 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));
894                                                                 glRotatef(270.0, 0.0, 1.0, 0.0);
895                                                                 break;
896                                                         case dirDOWN:
897                                                                 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));
898                                                                 glRotatef(90.0, 0.0, 1.0, 0.0);
899                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
900                                                                 break;
901                                                 }
902                                                 break;
903                                         case dirFAR:
904                                                 switch (newdir) {
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(90.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, 1.0, 0.0, 0.0);
913                                                                 break;
914                                                         case dirLEFT:
915                                                                 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));
916                                                                 glRotatef(90.0, 1.0, 0.0, 0.0);
917                                                                 break;
918                                                         case dirRIGHT:
919                                                                 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));
920                                                                 glRotatef(270.0, 1.0, 0.0, 0.0);
921                                                                 glRotatef(180.0, 0.0, 0.0, 1.0);
922                                                                 break;
923                                                 }
924                                                 break;
925                                 }
926                                 myElbow(mi, (sysT == 2));
927                                 break;
928                 }
929                 glPopMatrix();
930         }
931
932         OPX = pp->PX;
933         OPY = pp->PY;
934         OPZ = pp->PZ;
935         pp->olddir = pp->nowdir;
936         pp->nowdir = newdir;
937         switch (pp->nowdir) {
938                 case dirUP:
939                         pp->PY++;
940                         break;
941                 case dirDOWN:
942                         pp->PY--;
943                         break;
944                 case dirLEFT:
945                         pp->PX--;
946                         break;
947                 case dirRIGHT:
948                         pp->PX++;
949                         break;
950                 case dirNEAR:
951                         pp->PZ++;
952                         break;
953                 case dirFAR:
954                         pp->PZ--;
955                         break;
956         }
957         pp->Cells[pp->PX][pp->PY][pp->PZ] = 1;
958
959         /* Cells'face pipe */
960         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);
961         MakeTube(mi, newdir);
962
963         glPopMatrix();
964
965         glFlush();
966
967     if (dbuf_p)
968       glXSwapBuffers(display, window);
969
970     if (mi->fps_p) do_fps (mi);
971 }
972
973 #ifndef STANDALONE
974 ENTRYPOINT void
975 change_pipes (ModeInfo * mi)
976 {
977         pipesstruct *pp = &pipes[MI_SCREEN(mi)];
978
979         if (!pp->glx_context)
980                 return;
981
982         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(pp->glx_context));
983         pinit(mi, 1);
984 }
985 #endif /* !STANDALONE */
986
987
988 ENTRYPOINT void
989 release_pipes (ModeInfo * mi)
990 {
991         if (pipes != NULL) {
992                 int         screen;
993
994                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
995                         pipesstruct *pp = &pipes[screen];
996
997                         if (pp->glx_context) {
998
999                                 /* Display lists MUST be freed while their glXContext is current. */
1000                                 glXMakeCurrent(MI_DISPLAY(mi), pp->window, *(pp->glx_context));
1001
1002                                 if (pp->valve)
1003                                         glDeleteLists(pp->valve, 1);
1004                                 if (pp->bolts)
1005                                         glDeleteLists(pp->bolts, 1);
1006                                 if (pp->betweenbolts)
1007                                         glDeleteLists(pp->betweenbolts, 1);
1008
1009                                 if (pp->elbowbolts)
1010                                         glDeleteLists(pp->elbowbolts, 1);
1011                                 if (pp->elbowcoins)
1012                                         glDeleteLists(pp->elbowcoins, 1);
1013
1014                                 if (pp->guagehead)
1015                                         glDeleteLists(pp->guagehead, 1);
1016                                 if (pp->guageface)
1017                                         glDeleteLists(pp->guageface, 1);
1018                                 if (pp->guagedial)
1019                                         glDeleteLists(pp->guagedial, 1);
1020                                 if (pp->guageconnector)
1021                                         glDeleteLists(pp->guageconnector, 1);
1022                         }
1023                 }
1024
1025                 (void) free((void *) pipes);
1026                 pipes = NULL;
1027         }
1028         FreeAllGL(mi);
1029 }
1030
1031 XSCREENSAVER_MODULE ("Pipes", pipes)
1032
1033 #endif