ftp://updates.redhat.com/enterprise/2.1AS/en/os/SRPMS/xscreensaver-3.33-4.rhel21...
[xscreensaver] / hacks / glx / cage.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* cage --- the Impossible Cage, an Escher like scene. */
3
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)cage.c        4.07 98/01/04 xlockmore";
6
7 #endif
8
9 #undef DEBUG_LISTS
10
11 /*-
12  * Permission to use, copy, modify, and distribute this software and its
13  * documentation for any purpose and without fee is hereby granted,
14  * provided that the above copyright notice appear in all copies and that
15  * both that copyright notice and this permission notice appear in
16  * supporting documentation.
17  *
18  * This file is provided AS IS with no warranties of any kind.  The author
19  * shall have no liability with respect to the infringement of copyrights,
20  * trade secrets or any patents by this file or any part thereof.  In no
21  * event will the author be liable for any lost revenue or profits or
22  * other special, indirect and consequential damages.
23  *
24  * The RotateAroundU() routine was adapted from the book
25  *    "Computer Graphics Principles and Practice 
26  *     Foley - vanDam - Feiner - Hughes
27  *     Second Edition" Pag. 227, exercise 5.15.
28  * 
29  * This mode shows some interesting scenes that are impossible OR very
30  * wierd to build in the real universe. Much of the scenes are inspirated
31  * on Mauritz Cornelis Escher's works which derivated the mode's name.
32  * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
33  * say he was a mathematician.
34  *
35  * Thanks goes to Brian Paul for making it possible and inexpensive to use 
36  * OpenGL at home.
37  *
38  * Since I'm not a native English speaker, my apologies for any grammatical
39  * mistake.
40  *
41  * My e-mail address is
42  * m-vianna@usa.net
43  *
44  * Marcelo F. Vianna (Jun-01-1997)
45  *
46  * Revision History:
47  * 01-Jan-98: Mode separated from escher and renamed
48  * 08-Jun-97: New scene implemented: "Impossible Cage" based in a M.C. Escher's
49  *            painting with the same name (quite similar). The first GL mode
50  *            to use texture mapping.
51  *            The "Impossible Cage" scene doesn't use DEPTH BUFFER, the 
52  *            wood planks are drawn consistently using GL_CULL_FACE, and
53  *            the painter's algorithm is used to sort the planks.
54  *            Marcelo F. Vianna.
55  * 07-Jun-97: Speed ups in Moebius Strip using GL_CULL_FACE.
56  *            Marcelo F. Vianna.
57  * 03-Jun-97: Initial Release (Only one scene: "Moebius Strip")
58  *            The Moebius Strip scene was inspirated in a M.C. Escher's
59  *            painting named Moebius Strip II in wich ants walk across a
60  *            Moebius Strip path, sometimes meeting each other and sometimes
61  *            being in "opposite faces" (note that the moebius strip has
62  *            only one face and one edge).
63  *            Marcelo F. Vianna.
64  *
65  */
66
67 /*-
68  * Texture mapping is only available on RGBA contexts, Mono and color index
69  * visuals DO NOT support texture mapping in OpenGL.
70  *
71  * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
72  * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
73  * is not officially supported for both OpenGL and Mesa, but seems to not crash
74  * Mesa.
75  *
76  * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
77  */
78
79 #include <X11/Intrinsic.h>
80
81 #ifdef STANDALONE
82 # define PROGCLASS                      "Cage"
83 # define HACK_INIT                      init_cage
84 # define HACK_DRAW                      draw_cage
85 # define HACK_RESHAPE                   reshape_cage
86 # define cage_opts                      xlockmore_opts
87 # define DEFAULTS                       "*cycles:               1       \n"                     \
88                                                         "*delay:                25000   \n"                     \
89                                                         "*showFPS:      False   \n"                     \
90                                                         "*wireframe:    False   \n"
91 # include "xlockmore.h"         /* from the xscreensaver distribution */
92 #else /* !STANDALONE */
93 # include "xlock.h"             /* from the xlockmore distribution */
94
95 #endif /* !STANDALONE */
96
97 #ifdef USE_GL
98
99
100 #include <GL/glu.h>
101 #include "e_textures.h"
102
103 ModeSpecOpt cage_opts =
104 {0, NULL, 0, NULL, NULL};
105
106 #ifdef USE_MODULES
107 ModStruct   cage_description =
108 {"cage", "init_cage", "draw_cage", "release_cage",
109  "draw_cage", "change_cage", NULL, &cage_opts,
110  1000, 1, 1, 1, 1.0, 4, "",
111  "Shows the Impossible Cage, an Escher-like GL scene", 0, NULL};
112
113 #endif
114
115 #define Scale4Window               0.3
116 #define Scale4Iconic               0.4
117
118 #define sqr(A)                     ((A)*(A))
119
120 #ifndef Pi
121 #define Pi                         M_PI
122 #endif
123
124 /*************************************************************************/
125
126 typedef struct {
127         GLint       WindH, WindW;
128         GLfloat     step;
129         int         AreObjectsDefined[1];
130         GLXContext *glx_context;
131 } cagestruct;
132
133 static float front_shininess[] =
134 {60.0};
135 static float front_specular[] =
136 {0.7, 0.7, 0.7, 1.0};
137 static float ambient[] =
138 {0.0, 0.0, 0.0, 1.0};
139 static float diffuse[] =
140 {1.0, 1.0, 1.0, 1.0};
141 static float position0[] =
142 {1.0, 1.0, 1.0, 0.0};
143 static float position1[] =
144 {-1.0, -1.0, 1.0, 0.0};
145 static float lmodel_ambient[] =
146 {0.5, 0.5, 0.5, 1.0};
147 static float lmodel_twoside[] =
148 {GL_TRUE};
149
150 static float MaterialWhite[] =
151 {0.7, 0.7, 0.7, 1.0};
152
153 static cagestruct *cage = NULL;
154 static GLuint objects;
155
156 #define ObjWoodPlank    0
157
158 #define PlankWidth      3.0
159 #define PlankHeight     0.35
160 #define PlankThickness  0.15
161
162 static void
163 draw_woodplank(cagestruct * cp)
164 {
165         if (!cp->AreObjectsDefined[ObjWoodPlank]) {
166                 glNewList(objects + ObjWoodPlank, GL_COMPILE_AND_EXECUTE);
167                 glBegin(GL_QUADS);
168                 glNormal3f(0, 0, 1);
169                 glTexCoord2f(0, 0);
170                 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
171                 glTexCoord2f(1, 0);
172                 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
173                 glTexCoord2f(1, 1);
174                 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
175                 glTexCoord2f(0, 1);
176                 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
177                 glNormal3f(0, 0, -1);
178                 glTexCoord2f(0, 0);
179                 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
180                 glTexCoord2f(1, 0);
181                 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
182                 glTexCoord2f(1, 1);
183                 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
184                 glTexCoord2f(0, 1);
185                 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
186                 glNormal3f(0, 1, 0);
187                 glTexCoord2f(0, 0);
188                 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
189                 glTexCoord2f(1, 0);
190                 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
191                 glTexCoord2f(1, 1);
192                 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
193                 glTexCoord2f(0, 1);
194                 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
195                 glNormal3f(0, -1, 0);
196                 glTexCoord2f(0, 0);
197                 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
198                 glTexCoord2f(1, 0);
199                 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
200                 glTexCoord2f(1, 1);
201                 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
202                 glTexCoord2f(0, 1);
203                 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
204                 glNormal3f(1, 0, 0);
205                 glTexCoord2f(0, 0);
206                 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
207                 glTexCoord2f(1, 0);
208                 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
209                 glTexCoord2f(1, 1);
210                 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
211                 glTexCoord2f(0, 1);
212                 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
213                 glNormal3f(-1, 0, 0);
214                 glTexCoord2f(0, 0);
215                 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
216                 glTexCoord2f(1, 0);
217                 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
218                 glTexCoord2f(1, 1);
219                 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
220                 glTexCoord2f(0, 1);
221                 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
222                 glEnd();
223                 glEndList();
224                 cp->AreObjectsDefined[ObjWoodPlank] = 1;
225 #ifdef DEBUG_LISTS
226                 (void) printf("WoodPlank drawn SLOWLY\n");
227 #endif
228         } else {
229                 glCallList(objects + ObjWoodPlank);
230 #ifdef DEBUG_LISTS
231                 (void) printf("WoodPlank drawn quickly\n");
232 #endif
233         }
234 }
235
236 static void
237 draw_impossiblecage(cagestruct * cp)
238 {
239         glPushMatrix();
240         glRotatef(90, 0, 1, 0);
241         glTranslatef(0.0, PlankHeight - PlankWidth, -PlankThickness - PlankWidth);
242         draw_woodplank(cp);
243         glPopMatrix();
244         glPushMatrix();
245         glRotatef(90, 0, 0, 1);
246         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - PlankThickness);
247         draw_woodplank(cp);
248         glPopMatrix();
249         glPushMatrix();
250         glRotatef(90, 0, 1, 0);
251         glTranslatef(0.0, PlankWidth - PlankHeight, -PlankThickness - PlankWidth);
252         draw_woodplank(cp);
253         glPopMatrix();
254         glPushMatrix();
255         glTranslatef(0.0, PlankWidth - PlankHeight, 3 * PlankThickness - PlankWidth);
256         draw_woodplank(cp);
257         glPopMatrix();
258         glPushMatrix();
259         glRotatef(90, 0, 0, 1);
260         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - PlankThickness);
261         draw_woodplank(cp);
262         glPopMatrix();
263         glPushMatrix();
264         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - 3 * PlankThickness);
265         draw_woodplank(cp);
266         glPopMatrix();
267         glPushMatrix();
268         glTranslatef(0.0, PlankHeight - PlankWidth, 3 * PlankThickness - PlankWidth);
269         draw_woodplank(cp);
270         glPopMatrix();
271         glPushMatrix();
272         glRotatef(90, 0, 0, 1);
273         glTranslatef(0.0, PlankHeight - PlankWidth, PlankThickness - PlankWidth);
274         draw_woodplank(cp);
275         glPopMatrix();
276         glPushMatrix();
277         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - 3 * PlankThickness);
278         draw_woodplank(cp);
279         glPopMatrix();
280         glPushMatrix();
281         glRotatef(90, 0, 1, 0);
282         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth + PlankThickness);
283         draw_woodplank(cp);
284         glPopMatrix();
285         glPushMatrix();
286         glRotatef(90, 0, 0, 1);
287         glTranslatef(0.0, PlankWidth - PlankHeight, PlankThickness - PlankWidth);
288         draw_woodplank(cp);
289         glPopMatrix();
290         glPushMatrix();
291         glRotatef(90, 0, 1, 0);
292         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth + PlankThickness);
293         draw_woodplank(cp);
294         glPopMatrix();
295 }
296
297 void
298 reshape_cage(ModeInfo * mi, int width, int height)
299 {
300         cagestruct *cp = &cage[MI_SCREEN(mi)];
301
302         glViewport(0, 0, cp->WindW = (GLint) width, cp->WindH = (GLint) height);
303         glMatrixMode(GL_PROJECTION);
304         glLoadIdentity();
305         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
306         glMatrixMode(GL_MODELVIEW);
307         if (width >= 1024) {
308                 glLineWidth(3);
309                 glPointSize(3);
310         } else if (width >= 512) {
311                 glLineWidth(2);
312                 glPointSize(2);
313         } else {
314                 glLineWidth(1);
315                 glPointSize(1);
316         }
317         cp->AreObjectsDefined[ObjWoodPlank] = 0;
318 }
319
320 static void
321 pinit(void)
322 {
323         glClearDepth(1.0);
324         glClearColor(0.0, 0.0, 0.0, 1.0);
325
326         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
327         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
328         glLightfv(GL_LIGHT0, GL_POSITION, position0);
329         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
330         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
331         glLightfv(GL_LIGHT1, GL_POSITION, position1);
332         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
333         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
334         glEnable(GL_LIGHTING);
335         glEnable(GL_LIGHT0);
336         glEnable(GL_LIGHT1);
337         glEnable(GL_NORMALIZE);
338         glFrontFace(GL_CCW);
339         glCullFace(GL_BACK);
340
341         /* cage */
342         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
343         glShadeModel(GL_FLAT);
344         glDisable(GL_DEPTH_TEST);
345         glEnable(GL_TEXTURE_2D);
346         glEnable(GL_CULL_FACE);
347
348         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
349         gluBuild2DMipmaps(GL_TEXTURE_2D, 3, WoodTextureWidth, WoodTextureHeight,
350                           GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
351         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
352         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
353         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
354         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
355         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
356
357         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
358         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
359 }
360
361 void
362 init_cage(ModeInfo * mi)
363 {
364         int         screen = MI_SCREEN(mi);
365         cagestruct *cp;
366
367         if (cage == NULL) {
368                 if ((cage = (cagestruct *) calloc(MI_NUM_SCREENS(mi),
369                                                sizeof (cagestruct))) == NULL)
370                         return;
371         }
372         cp = &cage[screen];
373         cp->step = NRAND(90);
374
375         if ((cp->glx_context = init_GL(mi)) != NULL) {
376
377                 reshape_cage(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
378                 glDrawBuffer(GL_BACK);
379                 if (!glIsList(objects))
380                         objects = glGenLists(1);
381                 pinit();
382         } else {
383                 MI_CLEARWINDOW(mi);
384         }
385 }
386
387 void
388 draw_cage(ModeInfo * mi)
389 {
390         cagestruct *cp = &cage[MI_SCREEN(mi)];
391
392         Display    *display = MI_DISPLAY(mi);
393         Window      window = MI_WINDOW(mi);
394
395         MI_IS_DRAWN(mi) = True;
396
397         if (!cp->glx_context)
398                 return;
399
400         glXMakeCurrent(display, window, *(cp->glx_context));
401
402         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
403
404         glPushMatrix();
405
406         glTranslatef(0.0, 0.0, -10.0);
407
408         if (!MI_IS_ICONIC(mi)) {
409                 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
410         } else {
411                 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
412         }
413
414         /* cage */
415         glRotatef(cp->step * 100, 0, 0, 1);
416         glRotatef(25 + cos(cp->step * 5) * 6, 1, 0, 0);
417         glRotatef(204.5 - sin(cp->step * 5) * 8, 0, 1, 0);
418         draw_impossiblecage(cp);
419
420         glPopMatrix();
421
422     if (mi->fps_p) do_fps (mi);
423         glFlush();
424
425         glXSwapBuffers(display, window);
426
427         cp->step += 0.025;
428 }
429
430 void
431 change_cage(ModeInfo * mi)
432 {
433         cagestruct *cp = &cage[MI_SCREEN(mi)];
434
435         if (!cp->glx_context)
436                 return;
437
438         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
439         pinit();
440 }
441
442 void
443 release_cage(ModeInfo * mi)
444 {
445         if (cage != NULL) {
446                 (void) free((void *) cage);
447                 cage = NULL;
448         }
449         if (glIsList(objects)) {
450                 glDeleteLists(objects, 1);
451         }
452         FreeAllGL(mi);
453 }
454
455 #endif