1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* cage --- the Impossible Cage, an Escher like scene. */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)cage.c 5.01 2001/03/01 xlockmore";
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
22 * The RotateAroundU() routine was adapted from the book
23 * "Computer Graphics Principles and Practice
24 * Foley - vanDam - Feiner - Hughes
25 * Second Edition" Pag. 227, exercise 5.15.
27 * This mode shows some interesting scenes that are impossible OR very
28 * wierd to build in the real universe. Much of the scenes are inspirated
29 * on Mauritz Cornelis Escher's works which derivated the mode's name.
30 * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
31 * say he was a mathematician.
33 * Thanks goes to Brian Paul for making it possible and inexpensive to use
36 * Since I'm not a native English speaker, my apologies for any grammatical
39 * My e-mail address is
40 * mfvianna@centroin.com.br
42 * Marcelo F. Vianna (Jun-01-1997)
45 * 05-Apr-2002: Removed all gllist uses (fix some bug with nvidia driver)
46 * 01-Mar-2001: Added FPS stuff E.Lassauge <lassauge@mail.dotcom.fr>
47 * 01-Nov-2000: Allocation checks
48 * 01-Jan-1998: Mode separated from escher and renamed
49 * 08-Jun-1997: New scene implemented: "Impossible Cage" based in a M.C.
50 * Escher's painting with the same name (quite similar). The
51 * first GL mode to use texture mapping.
52 * The "Impossible Cage" scene doesn't use DEPTH BUFFER, the
53 * wood planks are drawn consistently using GL_CULL_FACE, and
54 * the painter's algorithm is used to sort the planks.
56 * 07-Jun-1997: Speed ups in Moebius Strip using GL_CULL_FACE.
58 * 03-Jun-1997: Initial Release (Only one scene: "Moebius Strip")
59 * The Moebius Strip scene was inspirated in a M.C. Escher's
60 * painting named Moebius Strip II in wich ants walk across a
61 * Moebius Strip path, sometimes meeting each other and sometimes
62 * being in "opposite faces" (note that the moebius strip has
63 * only one face and one edge).
68 * Texture mapping is only available on RGBA contexts, Mono and color index
69 * visuals DO NOT support texture mapping in OpenGL.
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
76 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
81 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
82 * otherwise caddr_t is not defined correctly
84 #include <X11/Intrinsic.h>
89 # define PROGCLASS "Cage"
90 # define HACK_INIT init_cage
91 # define HACK_DRAW draw_cage
92 # define HACK_RESHAPE reshape
93 # define cage_opts xlockmore_opts
94 # define DEFAULTS "*delay: 25000 \n" \
95 "*showFPS: False \n" \
96 "*wireframe: False \n"
97 # include "xlockmore.h" /* from the xscreensaver distribution */
98 #else /* !STANDALONE */
99 # include "xlock.h" /* from the xlockmore distribution */
101 #endif /* !STANDALONE */
107 #include "e_textures.h"
109 ModeSpecOpt cage_opts =
110 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
113 ModStruct cage_description =
114 {"cage", "init_cage", "draw_cage", "release_cage",
115 "draw_cage", "change_cage", (char *) NULL, &cage_opts,
116 25000, 1, 1, 1, 1.0, 4, "",
117 "Shows the Impossible Cage, an Escher-like GL scene", 0, NULL};
121 #define Scale4Window 0.3
122 #define Scale4Iconic 0.4
124 #define sqr(A) ((A)*(A))
130 #define ObjWoodPlank 0
133 /*************************************************************************/
138 GLXContext *glx_context;
141 static float front_shininess[] =
143 static float front_specular[] =
144 {0.7, 0.7, 0.7, 1.0};
145 static float ambient[] =
146 {0.0, 0.0, 0.0, 1.0};
147 static float diffuse[] =
148 {1.0, 1.0, 1.0, 1.0};
149 static float position0[] =
150 {1.0, 1.0, 1.0, 0.0};
151 static float position1[] =
152 {-1.0, -1.0, 1.0, 0.0};
153 static float lmodel_ambient[] =
154 {0.5, 0.5, 0.5, 1.0};
155 static float lmodel_twoside[] =
158 static float MaterialWhite[] =
159 {0.7, 0.7, 0.7, 1.0};
161 static cagestruct *cage = (cagestruct *) NULL;
163 #define PlankWidth 3.0
164 #define PlankHeight 0.35
165 #define PlankThickness 0.15
168 draw_woodplank(cagestruct * cp)
173 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
175 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
177 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
179 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
180 glNormal3f(0, 0, -1);
182 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
184 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
186 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
188 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
191 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
193 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
195 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
197 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
198 glNormal3f(0, -1, 0);
200 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
202 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
204 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
206 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
209 glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
211 glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
213 glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
215 glVertex3f(PlankWidth, PlankHeight, PlankThickness);
216 glNormal3f(-1, 0, 0);
218 glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
220 glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
222 glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
224 glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
231 draw_impossiblecage(cagestruct * cp)
234 glRotatef(90, 0, 1, 0);
235 glTranslatef(0.0, PlankHeight - PlankWidth, -PlankThickness - PlankWidth);
236 if (!draw_woodplank(cp))
240 glRotatef(90, 0, 0, 1);
241 glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - PlankThickness);
242 if (!draw_woodplank(cp))
246 glRotatef(90, 0, 1, 0);
247 glTranslatef(0.0, PlankWidth - PlankHeight, -PlankThickness - PlankWidth);
248 if (!draw_woodplank(cp))
252 glTranslatef(0.0, PlankWidth - PlankHeight, 3 * PlankThickness - PlankWidth);
253 if (!draw_woodplank(cp))
257 glRotatef(90, 0, 0, 1);
258 glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - PlankThickness);
259 if (!draw_woodplank(cp))
263 glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - 3 * PlankThickness);
264 if (!draw_woodplank(cp))
268 glTranslatef(0.0, PlankHeight - PlankWidth, 3 * PlankThickness - PlankWidth);
269 if (!draw_woodplank(cp))
273 glRotatef(90, 0, 0, 1);
274 glTranslatef(0.0, PlankHeight - PlankWidth, PlankThickness - PlankWidth);
275 if (!draw_woodplank(cp))
279 glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - 3 * PlankThickness);
280 if (!draw_woodplank(cp))
284 glRotatef(90, 0, 1, 0);
285 glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth + PlankThickness);
286 if (!draw_woodplank(cp))
290 glRotatef(90, 0, 0, 1);
291 glTranslatef(0.0, PlankWidth - PlankHeight, PlankThickness - PlankWidth);
292 if (!draw_woodplank(cp))
296 glRotatef(90, 0, 1, 0);
297 glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth + PlankThickness);
298 if (!draw_woodplank(cp))
305 reshape(ModeInfo * mi, int width, int height)
307 cagestruct *cp = &cage[MI_SCREEN(mi)];
310 glViewport(0, 0, cp->WindW = (GLint) width, cp->WindH = (GLint) height);
311 glMatrixMode(GL_PROJECTION);
313 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
314 glMatrixMode(GL_MODELVIEW);
325 glClearColor(0.0, 0.0, 0.0, 1.0);
327 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
328 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
329 glLightfv(GL_LIGHT0, GL_POSITION, position0);
330 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
331 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
332 glLightfv(GL_LIGHT1, GL_POSITION, position1);
333 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
334 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
335 glEnable(GL_LIGHTING);
338 glEnable(GL_NORMALIZE);
343 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
344 glShadeModel(GL_FLAT);
345 glDisable(GL_DEPTH_TEST);
346 glEnable(GL_TEXTURE_2D);
347 glEnable(GL_CULL_FACE);
349 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
355 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
356 WoodTextureWidth, WoodTextureHeight,
357 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
360 const char *s = (char *) gluErrorString (status);
361 fprintf (stderr, "%s: error mipmapping texture: %s\n",
362 progname, (s ? s : "(unknown)"));
365 check_gl_error("mipmapping");
367 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
368 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
369 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
370 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
371 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
373 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
374 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
378 release_cage(ModeInfo * mi)
383 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
384 cagestruct *cp = &cage[screen];
386 if (cp->glx_context) {
387 cp->glx_context = (GLXContext *) NULL;
390 (void) free((void *) cage);
391 cage = (cagestruct *) NULL;
397 init_cage(ModeInfo * mi)
402 if ((cage = (cagestruct *) calloc(MI_NUM_SCREENS(mi),
403 sizeof (cagestruct))) == NULL)
406 cp = &cage[MI_SCREEN(mi)];
408 cp->step = NRAND(90);
409 if ((cp->glx_context = init_GL(mi)) != NULL) {
411 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
412 glDrawBuffer(GL_BACK);
420 draw_cage(ModeInfo * mi)
422 Display *display = MI_DISPLAY(mi);
423 Window window = MI_WINDOW(mi);
428 cp = &cage[MI_SCREEN(mi)];
430 MI_IS_DRAWN(mi) = True;
431 if (!cp->glx_context)
434 glXMakeCurrent(display, window, *(cp->glx_context));
436 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
440 glTranslatef(0.0, 0.0, -10.0);
442 if (!MI_IS_ICONIC(mi)) {
443 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
445 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
449 glRotatef(cp->step * 100, 0, 0, 1);
450 glRotatef(25 + cos(cp->step * 5) * 6, 1, 0, 0);
451 glRotatef(204.5 - sin(cp->step * 5) * 8, 0, 1, 0);
452 if (!draw_impossiblecage(cp)) {
458 if (MI_IS_FPS(mi)) do_fps (mi);
461 glXSwapBuffers(display, window);
467 change_cage(ModeInfo * mi)
469 cagestruct *cp = &cage[MI_SCREEN(mi)];
471 if (!cp->glx_context)
474 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));