http://www.jwz.org/xscreensaver/xscreensaver-5.13.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 0
5 static const char sccsid[] = "@(#)cage.c        5.01 2001/03/01 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  * The RotateAroundU() routine was adapted from the book
22  *    "Computer Graphics Principles and Practice
23  *     Foley - vanDam - Feiner - Hughes
24  *     Second Edition" Pag. 227, exercise 5.15.
25  *
26  * This mode shows some interesting scenes that are impossible OR very
27  * wierd to build in the real universe. Much of the scenes are inspirated
28  * on Mauritz Cornelis Escher's works which derivated the mode's name.
29  * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
30  * say he was a mathematician.
31  *
32  * Thanks goes to Brian Paul for making it possible and inexpensive to use
33  * OpenGL at home.
34  *
35  * Since I'm not a native English speaker, my apologies for any grammatical
36  * mistakes.
37  *
38  * My e-mail address is
39  * mfvianna@centroin.com.br
40  *
41  * Marcelo F. Vianna (Jun-01-1997)
42  *
43  * Revision History:
44  * 05-Apr-2002: Removed all gllist uses (fix some bug with nvidia driver)
45  * 01-Mar-2001: Added FPS stuff E.Lassauge <lassauge@mail.dotcom.fr>
46  * 01-Nov-2000: Allocation checks
47  * 01-Jan-1998: Mode separated from escher and renamed
48  * 08-Jun-1997: New scene implemented: "Impossible Cage" based in a M.C.
49  *              Escher's painting with the same name (quite similar). The
50  *              first GL mode 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-1997: Speed ups in Moebius Strip using GL_CULL_FACE.
56  *              Marcelo F. Vianna.
57  * 03-Jun-1997: 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  * Texture mapping is only available on RGBA contexts, Mono and color index
68  * visuals DO NOT support texture mapping in OpenGL.
69  *
70  * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
71  * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
72  * is not officially supported for both OpenGL and Mesa, but seems to not crash
73  * Mesa.
74  *
75  * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
76  */
77
78 #ifdef STANDALONE
79 # define MODE_cage
80 # define DEFAULTS                       "*delay:                25000   \n"                     \
81                                                         "*showFPS:      False   \n"                     \
82                                                         "*wireframe:    False   \n"
83
84 # define refresh_cage 0
85 # define reshape_cage 0
86 # define cage_handle_event 0
87 # include "xlockmore.h"         /* from the xscreensaver distribution */
88 #else /* !STANDALONE */
89 # include "xlock.h"             /* from the xlockmore distribution */
90 #endif /* !STANDALONE */
91
92 #ifdef MODE_cage
93
94 #include "e_textures.h"
95
96 ENTRYPOINT ModeSpecOpt cage_opts =
97 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
98
99 #ifdef USE_MODULES
100 ModStruct   cage_description =
101 {"cage", "init_cage", "draw_cage", "release_cage",
102  "draw_cage", "change_cage", (char *) NULL, &cage_opts,
103  25000, 1, 1, 1, 1.0, 4, "",
104  "Shows the Impossible Cage, an Escher-like GL scene", 0, NULL};
105
106 #endif
107
108 #define Scale4Window               0.3
109 #define Scale4Iconic               0.4
110
111 #define sqr(A)                     ((A)*(A))
112
113 #ifndef Pi
114 #define Pi                         M_PI
115 #endif
116
117 #define ObjWoodPlank    0
118 #define MaxObj          1
119
120 /*************************************************************************/
121
122 typedef struct {
123         GLint       WindH, WindW;
124         GLfloat     step;
125         GLXContext *glx_context;
126 } cagestruct;
127
128 static const float front_shininess[] = {60.0};
129 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
130 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
131 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
132 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
133 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
134 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
135 static const float lmodel_twoside[] = {GL_TRUE};
136
137 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
138
139 static cagestruct *cage = (cagestruct *) NULL;
140
141 #define PlankWidth      3.0
142 #define PlankHeight     0.35
143 #define PlankThickness  0.15
144
145 static Bool 
146 draw_woodplank(ModeInfo *mi, cagestruct * cp, int wire)
147 {
148         glBegin(wire ? GL_LINES : GL_QUADS);
149         glNormal3f(0, 0, 1);
150         glTexCoord2f(0, 0);
151         glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
152         glTexCoord2f(1, 0);
153         glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
154         glTexCoord2f(1, 1);
155         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
156         glTexCoord2f(0, 1);
157         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
158     mi->polygon_count++;
159         glNormal3f(0, 0, -1);
160         glTexCoord2f(0, 0);
161         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
162         glTexCoord2f(1, 0);
163         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
164         glTexCoord2f(1, 1);
165         glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
166         glTexCoord2f(0, 1);
167         glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
168     mi->polygon_count++;
169         glNormal3f(0, 1, 0);
170         glTexCoord2f(0, 0);
171         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
172         glTexCoord2f(1, 0);
173         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
174         glTexCoord2f(1, 1);
175         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
176         glTexCoord2f(0, 1);
177         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
178     mi->polygon_count++;
179         glNormal3f(0, -1, 0);
180         glTexCoord2f(0, 0);
181         glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
182         glTexCoord2f(1, 0);
183         glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
184         glTexCoord2f(1, 1);
185         glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
186         glTexCoord2f(0, 1);
187         glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
188     mi->polygon_count++;
189         glNormal3f(1, 0, 0);
190         glTexCoord2f(0, 0);
191         glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
192         glTexCoord2f(1, 0);
193         glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
194         glTexCoord2f(1, 1);
195         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
196         glTexCoord2f(0, 1);
197         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
198     mi->polygon_count++;
199         glNormal3f(-1, 0, 0);
200         glTexCoord2f(0, 0);
201         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
202         glTexCoord2f(1, 0);
203         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
204         glTexCoord2f(1, 1);
205         glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
206         glTexCoord2f(0, 1);
207         glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
208     mi->polygon_count++;
209         glEnd();
210
211         return True;
212 }
213
214 static Bool
215 draw_impossiblecage(ModeInfo *mi, cagestruct * cp, int wire)
216 {
217         glPushMatrix();
218         glRotatef(90, 0, 1, 0);
219         glTranslatef(0.0, PlankHeight - PlankWidth, -PlankThickness - PlankWidth);
220         if (!draw_woodplank(mi, cp, wire))
221                 return False;
222         glPopMatrix();
223         glPushMatrix();
224         glRotatef(90, 0, 0, 1);
225         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - PlankThickness);
226         if (!draw_woodplank(mi, cp, wire))
227                 return False;
228         glPopMatrix();
229         glPushMatrix();
230         glRotatef(90, 0, 1, 0);
231         glTranslatef(0.0, PlankWidth - PlankHeight, -PlankThickness - PlankWidth);
232         if (!draw_woodplank(mi, cp, wire))
233                 return False;
234         glPopMatrix();
235         glPushMatrix();
236         glTranslatef(0.0, PlankWidth - PlankHeight, 3 * PlankThickness - PlankWidth);
237         if (!draw_woodplank(mi, cp, wire))
238                 return False;
239         glPopMatrix();
240         glPushMatrix();
241         glRotatef(90, 0, 0, 1);
242         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - PlankThickness);
243         if (!draw_woodplank(mi, cp, wire))
244                 return False;
245         glPopMatrix();
246         glPushMatrix();
247         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - 3 * PlankThickness);
248         if (!draw_woodplank(mi, cp, wire))
249                 return False;
250         glPopMatrix();
251         glPushMatrix();
252         glTranslatef(0.0, PlankHeight - PlankWidth, 3 * PlankThickness - PlankWidth);
253         if (!draw_woodplank(mi, cp, wire))
254                 return False;
255         glPopMatrix();
256         glPushMatrix();
257         glRotatef(90, 0, 0, 1);
258         glTranslatef(0.0, PlankHeight - PlankWidth, PlankThickness - PlankWidth);
259         if (!draw_woodplank(mi, cp, wire))
260                 return False;
261         glPopMatrix();
262         glPushMatrix();
263         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - 3 * PlankThickness);
264         if (!draw_woodplank(mi, cp, wire))
265                 return False;
266         glPopMatrix();
267         glPushMatrix();
268         glRotatef(90, 0, 1, 0);
269         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth + PlankThickness);
270         if (!draw_woodplank(mi, cp, wire))
271                 return False;
272         glPopMatrix();
273         glPushMatrix();
274         glRotatef(90, 0, 0, 1);
275         glTranslatef(0.0, PlankWidth - PlankHeight, PlankThickness - PlankWidth);
276         if (!draw_woodplank(mi, cp, wire))
277                 return False;
278         glPopMatrix();
279         glPushMatrix();
280         glRotatef(90, 0, 1, 0);
281         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth + PlankThickness);
282         if (!draw_woodplank(mi, cp, wire))
283                 return False;
284         glPopMatrix();
285         return True;
286 }
287
288 static void
289 reshape(ModeInfo * mi, int width, int height)
290 {
291         cagestruct *cp = &cage[MI_SCREEN(mi)];
292         int i;
293
294         glViewport(0, 0, cp->WindW = (GLint) width, cp->WindH = (GLint) height);
295         glMatrixMode(GL_PROJECTION);
296         glLoadIdentity();
297         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
298         glMatrixMode(GL_MODELVIEW);
299         i = width / 512 + 1;
300         glLineWidth(i);
301         glPointSize(i);
302 }
303
304 static void
305 pinit(ModeInfo *mi)
306 {
307         int status;
308
309         glClearDepth(1.0);
310
311     if (MI_IS_WIREFRAME(mi))
312       return;
313
314         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
315         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
316         glLightfv(GL_LIGHT0, GL_POSITION, position0);
317         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
318         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
319         glLightfv(GL_LIGHT1, GL_POSITION, position1);
320         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
321         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
322         glEnable(GL_LIGHTING);
323         glEnable(GL_LIGHT0);
324         glEnable(GL_LIGHT1);
325         glEnable(GL_NORMALIZE);
326         glFrontFace(GL_CCW);
327         glCullFace(GL_BACK);
328
329         /* cage */
330         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
331         glShadeModel(GL_FLAT);
332         glDisable(GL_DEPTH_TEST);
333         glEnable(GL_TEXTURE_2D);
334         glEnable(GL_CULL_FACE);
335
336         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
337
338         clear_gl_error();
339         if (MI_IS_MONO(mi))
340       status = 0;
341     else
342       status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
343                                  WoodTextureWidth, WoodTextureHeight,
344                                  GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
345         if (status)
346           {
347                 const char *s = (char *) gluErrorString (status);
348                 fprintf (stderr, "%s: error mipmapping texture: %s\n",
349                                  progname, (s ? s : "(unknown)"));
350                 exit (1);
351           }
352         check_gl_error("mipmapping");
353
354         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
355         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
356         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
357         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
358         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
359
360         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
361         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
362 }
363
364 ENTRYPOINT void
365 release_cage (ModeInfo * mi)
366 {
367         if (cage != NULL) {
368                 int screen;
369
370                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
371                         cagestruct *cp = &cage[screen];
372
373                         if (cp->glx_context) {
374                                 cp->glx_context = (GLXContext *) NULL;
375                         }
376                 }
377                 (void) free((void *) cage);
378                 cage = (cagestruct *) NULL;
379         }
380         FreeAllGL(mi);
381 }
382
383 ENTRYPOINT void
384 init_cage (ModeInfo * mi)
385 {
386         cagestruct *cp;
387
388         if (cage == NULL) {
389                 if ((cage = (cagestruct *) calloc(MI_NUM_SCREENS(mi),
390                                                sizeof (cagestruct))) == NULL)
391                         return;
392         }
393         cp = &cage[MI_SCREEN(mi)];
394
395         cp->step = NRAND(90);
396         if ((cp->glx_context = init_GL(mi)) != NULL) {
397
398                 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
399                 glDrawBuffer(GL_BACK);
400                 pinit(mi);
401         } else {
402                 MI_CLEARWINDOW(mi);
403         }
404 }
405
406 ENTRYPOINT void
407 draw_cage (ModeInfo * mi)
408 {
409         Display    *display = MI_DISPLAY(mi);
410         Window      window = MI_WINDOW(mi);
411         cagestruct *cp;
412
413         if (cage == NULL)
414                 return;
415         cp = &cage[MI_SCREEN(mi)];
416
417         MI_IS_DRAWN(mi) = True;
418         if (!cp->glx_context)
419                 return;
420
421     mi->polygon_count = 0;
422         glXMakeCurrent(display, window, *(cp->glx_context));
423
424         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
425
426         glPushMatrix();
427
428         glTranslatef(0.0, 0.0, -10.0);
429
430         if (!MI_IS_ICONIC(mi)) {
431                 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
432         } else {
433                 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
434         }
435
436         /* cage */
437         glRotatef(cp->step * 100, 0, 0, 1);
438         glRotatef(25 + cos(cp->step * 5) * 6, 1, 0, 0);
439         glRotatef(204.5 - sin(cp->step * 5) * 8, 0, 1, 0);
440         if (!draw_impossiblecage(mi, cp, MI_IS_WIREFRAME(mi))) {
441                 release_cage(mi);
442                 return;
443         }
444
445         glPopMatrix();
446         if (MI_IS_FPS(mi)) do_fps (mi);
447         glFlush();
448
449         glXSwapBuffers(display, window);
450
451         cp->step += 0.025;
452 }
453
454 #ifndef STANDALONE
455 ENTRYPOINT void
456 change_cage (ModeInfo * mi)
457 {
458         cagestruct *cp = &cage[MI_SCREEN(mi)];
459
460         if (!cp->glx_context)
461                 return;
462
463         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
464         pinit(mi);
465 }
466 #endif /* !STANDALONE */
467
468 XSCREENSAVER_MODULE ("Cage", cage)
469
470 #endif