http://ftp.x.org/contrib/applications/xscreensaver-3.09.tar.gz
[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 cage_opts                      xlockmore_opts
86 # define DEFAULTS                       "*cycles:               1       \n"                     \
87                                                         "*delay:                1000    \n"                     \
88                                                         "*wireframe:    False   \n"
89 # include "xlockmore.h"         /* from the xscreensaver distribution */
90 #else /* !STANDALONE */
91 # include "xlock.h"             /* from the xlockmore distribution */
92
93 #endif /* !STANDALONE */
94
95 #ifdef USE_GL
96
97
98 #include <GL/glu.h>
99 #include "e_textures.h"
100
101 ModeSpecOpt cage_opts =
102 {0, NULL, 0, NULL, NULL};
103
104 #ifdef USE_MODULES
105 ModStruct   cage_description =
106 {"cage", "init_cage", "draw_cage", "release_cage",
107  "draw_cage", "change_cage", NULL, &cage_opts,
108  1000, 1, 1, 1, 1.0, 4, "",
109  "Shows the Impossible Cage, an Escher-like GL scene", 0, NULL};
110
111 #endif
112
113 #define Scale4Window               0.3
114 #define Scale4Iconic               0.4
115
116 #define sqr(A)                     ((A)*(A))
117
118 #ifndef Pi
119 #define Pi                         M_PI
120 #endif
121
122 /*************************************************************************/
123
124 typedef struct {
125         GLint       WindH, WindW;
126         GLfloat     step;
127         int         AreObjectsDefined[1];
128         GLXContext *glx_context;
129 } cagestruct;
130
131 static float front_shininess[] =
132 {60.0};
133 static float front_specular[] =
134 {0.7, 0.7, 0.7, 1.0};
135 static float ambient[] =
136 {0.0, 0.0, 0.0, 1.0};
137 static float diffuse[] =
138 {1.0, 1.0, 1.0, 1.0};
139 static float position0[] =
140 {1.0, 1.0, 1.0, 0.0};
141 static float position1[] =
142 {-1.0, -1.0, 1.0, 0.0};
143 static float lmodel_ambient[] =
144 {0.5, 0.5, 0.5, 1.0};
145 static float lmodel_twoside[] =
146 {GL_TRUE};
147
148 static float MaterialWhite[] =
149 {0.7, 0.7, 0.7, 1.0};
150
151 static cagestruct *cage = NULL;
152 static GLuint objects;
153
154 #define ObjWoodPlank    0
155
156 #define PlankWidth      3.0
157 #define PlankHeight     0.35
158 #define PlankThickness  0.15
159
160 static void
161 draw_woodplank(cagestruct * cp)
162 {
163         if (!cp->AreObjectsDefined[ObjWoodPlank]) {
164                 glNewList(objects + ObjWoodPlank, GL_COMPILE_AND_EXECUTE);
165                 glBegin(GL_QUADS);
166                 glNormal3f(0, 0, 1);
167                 glTexCoord2f(0, 0);
168                 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
169                 glTexCoord2f(1, 0);
170                 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
171                 glTexCoord2f(1, 1);
172                 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
173                 glTexCoord2f(0, 1);
174                 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
175                 glNormal3f(0, 0, -1);
176                 glTexCoord2f(0, 0);
177                 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
178                 glTexCoord2f(1, 0);
179                 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
180                 glTexCoord2f(1, 1);
181                 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
182                 glTexCoord2f(0, 1);
183                 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
184                 glNormal3f(0, 1, 0);
185                 glTexCoord2f(0, 0);
186                 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
187                 glTexCoord2f(1, 0);
188                 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
189                 glTexCoord2f(1, 1);
190                 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
191                 glTexCoord2f(0, 1);
192                 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
193                 glNormal3f(0, -1, 0);
194                 glTexCoord2f(0, 0);
195                 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
196                 glTexCoord2f(1, 0);
197                 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
198                 glTexCoord2f(1, 1);
199                 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
200                 glTexCoord2f(0, 1);
201                 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
202                 glNormal3f(1, 0, 0);
203                 glTexCoord2f(0, 0);
204                 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
205                 glTexCoord2f(1, 0);
206                 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
207                 glTexCoord2f(1, 1);
208                 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
209                 glTexCoord2f(0, 1);
210                 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
211                 glNormal3f(-1, 0, 0);
212                 glTexCoord2f(0, 0);
213                 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
214                 glTexCoord2f(1, 0);
215                 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
216                 glTexCoord2f(1, 1);
217                 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
218                 glTexCoord2f(0, 1);
219                 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
220                 glEnd();
221                 glEndList();
222                 cp->AreObjectsDefined[ObjWoodPlank] = 1;
223 #ifdef DEBUG_LISTS
224                 (void) printf("WoodPlank drawn SLOWLY\n");
225 #endif
226         } else {
227                 glCallList(objects + ObjWoodPlank);
228 #ifdef DEBUG_LISTS
229                 (void) printf("WoodPlank drawn quickly\n");
230 #endif
231         }
232 }
233
234 static void
235 draw_impossiblecage(cagestruct * cp)
236 {
237         glPushMatrix();
238         glRotatef(90, 0, 1, 0);
239         glTranslatef(0.0, PlankHeight - PlankWidth, -PlankThickness - PlankWidth);
240         draw_woodplank(cp);
241         glPopMatrix();
242         glPushMatrix();
243         glRotatef(90, 0, 0, 1);
244         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - PlankThickness);
245         draw_woodplank(cp);
246         glPopMatrix();
247         glPushMatrix();
248         glRotatef(90, 0, 1, 0);
249         glTranslatef(0.0, PlankWidth - PlankHeight, -PlankThickness - PlankWidth);
250         draw_woodplank(cp);
251         glPopMatrix();
252         glPushMatrix();
253         glTranslatef(0.0, PlankWidth - PlankHeight, 3 * PlankThickness - PlankWidth);
254         draw_woodplank(cp);
255         glPopMatrix();
256         glPushMatrix();
257         glRotatef(90, 0, 0, 1);
258         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - PlankThickness);
259         draw_woodplank(cp);
260         glPopMatrix();
261         glPushMatrix();
262         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - 3 * PlankThickness);
263         draw_woodplank(cp);
264         glPopMatrix();
265         glPushMatrix();
266         glTranslatef(0.0, PlankHeight - PlankWidth, 3 * PlankThickness - PlankWidth);
267         draw_woodplank(cp);
268         glPopMatrix();
269         glPushMatrix();
270         glRotatef(90, 0, 0, 1);
271         glTranslatef(0.0, PlankHeight - PlankWidth, PlankThickness - PlankWidth);
272         draw_woodplank(cp);
273         glPopMatrix();
274         glPushMatrix();
275         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - 3 * PlankThickness);
276         draw_woodplank(cp);
277         glPopMatrix();
278         glPushMatrix();
279         glRotatef(90, 0, 1, 0);
280         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth + PlankThickness);
281         draw_woodplank(cp);
282         glPopMatrix();
283         glPushMatrix();
284         glRotatef(90, 0, 0, 1);
285         glTranslatef(0.0, PlankWidth - PlankHeight, PlankThickness - PlankWidth);
286         draw_woodplank(cp);
287         glPopMatrix();
288         glPushMatrix();
289         glRotatef(90, 0, 1, 0);
290         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth + PlankThickness);
291         draw_woodplank(cp);
292         glPopMatrix();
293 }
294
295 static void
296 reshape(ModeInfo * mi, int width, int height)
297 {
298         cagestruct *cp = &cage[MI_SCREEN(mi)];
299
300         glViewport(0, 0, cp->WindW = (GLint) width, cp->WindH = (GLint) height);
301         glMatrixMode(GL_PROJECTION);
302         glLoadIdentity();
303         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
304         glMatrixMode(GL_MODELVIEW);
305         if (width >= 1024) {
306                 glLineWidth(3);
307                 glPointSize(3);
308         } else if (width >= 512) {
309                 glLineWidth(2);
310                 glPointSize(2);
311         } else {
312                 glLineWidth(1);
313                 glPointSize(1);
314         }
315         cp->AreObjectsDefined[ObjWoodPlank] = 0;
316 }
317
318 static void
319 pinit(void)
320 {
321         glClearDepth(1.0);
322         glClearColor(0.0, 0.0, 0.0, 1.0);
323
324         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
325         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
326         glLightfv(GL_LIGHT0, GL_POSITION, position0);
327         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
328         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
329         glLightfv(GL_LIGHT1, GL_POSITION, position1);
330         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
331         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
332         glEnable(GL_LIGHTING);
333         glEnable(GL_LIGHT0);
334         glEnable(GL_LIGHT1);
335         glEnable(GL_NORMALIZE);
336         glFrontFace(GL_CCW);
337         glCullFace(GL_BACK);
338
339         /* cage */
340         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
341         glShadeModel(GL_FLAT);
342         glDisable(GL_DEPTH_TEST);
343         glEnable(GL_TEXTURE_2D);
344         glEnable(GL_CULL_FACE);
345
346         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
347         gluBuild2DMipmaps(GL_TEXTURE_2D, 3, WoodTextureWidth, WoodTextureHeight,
348                           GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
349         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
350         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
351         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
352         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
353         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
354
355         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
356         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
357 }
358
359 void
360 init_cage(ModeInfo * mi)
361 {
362         int         screen = MI_SCREEN(mi);
363         cagestruct *cp;
364
365         if (cage == NULL) {
366                 if ((cage = (cagestruct *) calloc(MI_NUM_SCREENS(mi),
367                                                sizeof (cagestruct))) == NULL)
368                         return;
369         }
370         cp = &cage[screen];
371         cp->step = NRAND(90);
372
373         if ((cp->glx_context = init_GL(mi)) != NULL) {
374
375                 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
376                 glDrawBuffer(GL_BACK);
377                 if (!glIsList(objects))
378                         objects = glGenLists(1);
379                 pinit();
380         } else {
381                 MI_CLEARWINDOW(mi);
382         }
383 }
384
385 void
386 draw_cage(ModeInfo * mi)
387 {
388         cagestruct *cp = &cage[MI_SCREEN(mi)];
389
390         Display    *display = MI_DISPLAY(mi);
391         Window      window = MI_WINDOW(mi);
392
393         MI_IS_DRAWN(mi) = True;
394
395         if (!cp->glx_context)
396                 return;
397
398         glXMakeCurrent(display, window, *(cp->glx_context));
399
400         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
401
402         glPushMatrix();
403
404         glTranslatef(0.0, 0.0, -10.0);
405
406         if (!MI_IS_ICONIC(mi)) {
407                 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
408         } else {
409                 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
410         }
411
412         /* cage */
413         glRotatef(cp->step * 100, 0, 0, 1);
414         glRotatef(25 + cos(cp->step * 5) * 6, 1, 0, 0);
415         glRotatef(204.5 - sin(cp->step * 5) * 8, 0, 1, 0);
416         draw_impossiblecage(cp);
417
418         glPopMatrix();
419
420         glFlush();
421
422         glXSwapBuffers(display, window);
423
424         cp->step += 0.025;
425 }
426
427 void
428 change_cage(ModeInfo * mi)
429 {
430         cagestruct *cp = &cage[MI_SCREEN(mi)];
431
432         if (!cp->glx_context)
433                 return;
434
435         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
436         pinit();
437 }
438
439 void
440 release_cage(ModeInfo * mi)
441 {
442         if (cage != NULL) {
443                 (void) free((void *) cage);
444                 cage = NULL;
445         }
446         if (glIsList(objects)) {
447                 glDeleteLists(objects, 1);
448         }
449         FreeAllGL(mi);
450 }
451
452 #endif