1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* cage --- the Impossible Cage, an Escher like scene. */
5 static const char sccsid[] = "@(#)cage.c 5.01 2001/03/01 xlockmore";
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.
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.
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.
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.
32 * Thanks goes to Brian Paul for making it possible and inexpensive to use
35 * Since I'm not a native English speaker, my apologies for any grammatical
38 * My e-mail address is
39 * mfvianna@centroin.com.br
41 * Marcelo F. Vianna (Jun-01-1997)
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.
55 * 07-Jun-1997: Speed ups in Moebius Strip using GL_CULL_FACE.
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).
67 * Texture mapping is only available on RGBA contexts, Mono and color index
68 * visuals DO NOT support texture mapping in OpenGL.
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
75 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
80 # define DEFAULTS "*delay: 25000 \n" \
81 "*showFPS: False \n" \
82 "*wireframe: False \n"
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 */
94 #include "e_textures.h"
96 ENTRYPOINT ModeSpecOpt cage_opts =
97 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
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};
108 #define Scale4Window 0.3
109 #define Scale4Iconic 0.4
111 #define sqr(A) ((A)*(A))
117 #define ObjWoodPlank 0
120 /*************************************************************************/
125 GLXContext *glx_context;
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};
137 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
139 static cagestruct *cage = (cagestruct *) NULL;
141 #define PlankWidth 3.0
142 #define PlankHeight 0.35
143 #define PlankThickness 0.15
146 draw_woodplank(ModeInfo *mi, cagestruct * cp, int wire)
148 glBegin(wire ? GL_LINES : GL_QUADS);
151 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
153 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
155 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
157 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
159 glNormal3f(0, 0, -1);
161 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
163 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
165 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
167 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
171 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
173 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
175 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
177 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
179 glNormal3f(0, -1, 0);
181 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
183 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
185 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
187 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
191 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
193 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
195 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
197 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
199 glNormal3f(-1, 0, 0);
201 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
203 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
205 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
207 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
215 draw_impossiblecage(ModeInfo *mi, cagestruct * cp, int wire)
218 glRotatef(90, 0, 1, 0);
219 glTranslatef(0.0, PlankHeight - PlankWidth, -PlankThickness - PlankWidth);
220 if (!draw_woodplank(mi, cp, wire))
224 glRotatef(90, 0, 0, 1);
225 glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - PlankThickness);
226 if (!draw_woodplank(mi, cp, wire))
230 glRotatef(90, 0, 1, 0);
231 glTranslatef(0.0, PlankWidth - PlankHeight, -PlankThickness - PlankWidth);
232 if (!draw_woodplank(mi, cp, wire))
236 glTranslatef(0.0, PlankWidth - PlankHeight, 3 * PlankThickness - PlankWidth);
237 if (!draw_woodplank(mi, cp, wire))
241 glRotatef(90, 0, 0, 1);
242 glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - PlankThickness);
243 if (!draw_woodplank(mi, cp, wire))
247 glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - 3 * PlankThickness);
248 if (!draw_woodplank(mi, cp, wire))
252 glTranslatef(0.0, PlankHeight - PlankWidth, 3 * PlankThickness - PlankWidth);
253 if (!draw_woodplank(mi, cp, wire))
257 glRotatef(90, 0, 0, 1);
258 glTranslatef(0.0, PlankHeight - PlankWidth, PlankThickness - PlankWidth);
259 if (!draw_woodplank(mi, cp, wire))
263 glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - 3 * PlankThickness);
264 if (!draw_woodplank(mi, cp, wire))
268 glRotatef(90, 0, 1, 0);
269 glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth + PlankThickness);
270 if (!draw_woodplank(mi, cp, wire))
274 glRotatef(90, 0, 0, 1);
275 glTranslatef(0.0, PlankWidth - PlankHeight, PlankThickness - PlankWidth);
276 if (!draw_woodplank(mi, cp, wire))
280 glRotatef(90, 0, 1, 0);
281 glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth + PlankThickness);
282 if (!draw_woodplank(mi, cp, wire))
289 reshape(ModeInfo * mi, int width, int height)
291 cagestruct *cp = &cage[MI_SCREEN(mi)];
294 glViewport(0, 0, cp->WindW = (GLint) width, cp->WindH = (GLint) height);
295 glMatrixMode(GL_PROJECTION);
297 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
298 glMatrixMode(GL_MODELVIEW);
311 if (MI_IS_WIREFRAME(mi))
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);
325 glEnable(GL_NORMALIZE);
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);
336 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
342 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
343 WoodTextureWidth, WoodTextureHeight,
344 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
347 const char *s = (char *) gluErrorString (status);
348 fprintf (stderr, "%s: error mipmapping texture: %s\n",
349 progname, (s ? s : "(unknown)"));
352 check_gl_error("mipmapping");
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);
360 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
361 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
365 release_cage (ModeInfo * mi)
370 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
371 cagestruct *cp = &cage[screen];
373 if (cp->glx_context) {
374 cp->glx_context = (GLXContext *) NULL;
377 (void) free((void *) cage);
378 cage = (cagestruct *) NULL;
384 init_cage (ModeInfo * mi)
389 if ((cage = (cagestruct *) calloc(MI_NUM_SCREENS(mi),
390 sizeof (cagestruct))) == NULL)
393 cp = &cage[MI_SCREEN(mi)];
395 cp->step = NRAND(90);
396 if ((cp->glx_context = init_GL(mi)) != NULL) {
398 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
399 glDrawBuffer(GL_BACK);
407 draw_cage (ModeInfo * mi)
409 Display *display = MI_DISPLAY(mi);
410 Window window = MI_WINDOW(mi);
415 cp = &cage[MI_SCREEN(mi)];
417 MI_IS_DRAWN(mi) = True;
418 if (!cp->glx_context)
421 mi->polygon_count = 0;
422 glXMakeCurrent(display, window, *(cp->glx_context));
424 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
428 glTranslatef(0.0, 0.0, -10.0);
430 if (!MI_IS_ICONIC(mi)) {
431 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
433 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
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))) {
446 if (MI_IS_FPS(mi)) do_fps (mi);
449 glXSwapBuffers(display, window);
456 change_cage (ModeInfo * mi)
458 cagestruct *cp = &cage[MI_SCREEN(mi)];
460 if (!cp->glx_context)
463 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
466 #endif /* !STANDALONE */
468 XSCREENSAVER_MODULE ("Cage", cage)