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