From http://www.jwz.org/xscreensaver/xscreensaver-5.37.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                                                         "*suppressRotationAnimation: True\n" \
84
85 # define refresh_cage 0
86 # define release_cage 0
87 # define cage_handle_event 0
88 # include "xlockmore.h"         /* from the xscreensaver distribution */
89 #else /* !STANDALONE */
90 # include "xlock.h"             /* from the xlockmore distribution */
91 #endif /* !STANDALONE */
92
93 #ifdef MODE_cage
94
95 #if 0
96 #include "e_textures.h"
97 #else
98 #include "xpm-ximage.h"
99 #include "../images/wood.xpm"
100 #endif
101
102 ENTRYPOINT ModeSpecOpt cage_opts =
103 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
104
105 #ifdef USE_MODULES
106 ModStruct   cage_description =
107 {"cage", "init_cage", "draw_cage", NULL,
108  "draw_cage", "change_cage", (char *) NULL, &cage_opts,
109  25000, 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 #define ObjWoodPlank    0
124 #define MaxObj          1
125
126 /*************************************************************************/
127
128 typedef struct {
129         GLint       WindH, WindW;
130         GLfloat     step;
131         GLXContext *glx_context;
132 } cagestruct;
133
134 static const float front_shininess[] = {60.0};
135 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
136 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
137 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
138 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
139 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
140 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
141 static const float lmodel_twoside[] = {GL_TRUE};
142
143 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
144
145 static cagestruct *cage = (cagestruct *) NULL;
146
147 #define PlankWidth      3.0
148 #define PlankHeight     0.35
149 #define PlankThickness  0.15
150
151 static Bool 
152 draw_woodplank(ModeInfo *mi, cagestruct * cp, int wire)
153 {
154         glBegin(wire ? GL_LINES : GL_QUADS);
155         glNormal3f(0, 0, 1);
156         glTexCoord2f(0, 0);
157         glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
158         glTexCoord2f(1, 0);
159         glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
160         glTexCoord2f(1, 1);
161         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
162         glTexCoord2f(0, 1);
163         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
164     mi->polygon_count++;
165         glNormal3f(0, 0, -1);
166         glTexCoord2f(0, 0);
167         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
168         glTexCoord2f(1, 0);
169         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
170         glTexCoord2f(1, 1);
171         glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
172         glTexCoord2f(0, 1);
173         glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
174     mi->polygon_count++;
175         glNormal3f(0, 1, 0);
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     mi->polygon_count++;
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     mi->polygon_count++;
195         glNormal3f(1, 0, 0);
196         glTexCoord2f(0, 0);
197         glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
198         glTexCoord2f(1, 0);
199         glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
200         glTexCoord2f(1, 1);
201         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
202         glTexCoord2f(0, 1);
203         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
204     mi->polygon_count++;
205         glNormal3f(-1, 0, 0);
206         glTexCoord2f(0, 0);
207         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
208         glTexCoord2f(1, 0);
209         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
210         glTexCoord2f(1, 1);
211         glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
212         glTexCoord2f(0, 1);
213         glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
214     mi->polygon_count++;
215         glEnd();
216
217         return True;
218 }
219
220 static Bool
221 draw_impossiblecage(ModeInfo *mi, cagestruct * cp, int wire)
222 {
223         glPushMatrix();
224         glRotatef(90, 0, 1, 0);
225         glTranslatef(0.0, PlankHeight - PlankWidth, -PlankThickness - PlankWidth);
226         if (!draw_woodplank(mi, cp, wire))
227                 return False;
228         glPopMatrix();
229         glPushMatrix();
230         glRotatef(90, 0, 0, 1);
231         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - PlankThickness);
232         if (!draw_woodplank(mi, cp, wire))
233                 return False;
234         glPopMatrix();
235         glPushMatrix();
236         glRotatef(90, 0, 1, 0);
237         glTranslatef(0.0, PlankWidth - PlankHeight, -PlankThickness - PlankWidth);
238         if (!draw_woodplank(mi, cp, wire))
239                 return False;
240         glPopMatrix();
241         glPushMatrix();
242         glTranslatef(0.0, PlankWidth - PlankHeight, 3 * PlankThickness - PlankWidth);
243         if (!draw_woodplank(mi, cp, wire))
244                 return False;
245         glPopMatrix();
246         glPushMatrix();
247         glRotatef(90, 0, 0, 1);
248         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - PlankThickness);
249         if (!draw_woodplank(mi, cp, wire))
250                 return False;
251         glPopMatrix();
252         glPushMatrix();
253         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - 3 * PlankThickness);
254         if (!draw_woodplank(mi, cp, wire))
255                 return False;
256         glPopMatrix();
257         glPushMatrix();
258         glTranslatef(0.0, PlankHeight - PlankWidth, 3 * PlankThickness - PlankWidth);
259         if (!draw_woodplank(mi, cp, wire))
260                 return False;
261         glPopMatrix();
262         glPushMatrix();
263         glRotatef(90, 0, 0, 1);
264         glTranslatef(0.0, PlankHeight - PlankWidth, PlankThickness - PlankWidth);
265         if (!draw_woodplank(mi, cp, wire))
266                 return False;
267         glPopMatrix();
268         glPushMatrix();
269         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - 3 * PlankThickness);
270         if (!draw_woodplank(mi, cp, wire))
271                 return False;
272         glPopMatrix();
273         glPushMatrix();
274         glRotatef(90, 0, 1, 0);
275         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth + PlankThickness);
276         if (!draw_woodplank(mi, cp, wire))
277                 return False;
278         glPopMatrix();
279         glPushMatrix();
280         glRotatef(90, 0, 0, 1);
281         glTranslatef(0.0, PlankWidth - PlankHeight, PlankThickness - PlankWidth);
282         if (!draw_woodplank(mi, cp, wire))
283                 return False;
284         glPopMatrix();
285         glPushMatrix();
286         glRotatef(90, 0, 1, 0);
287         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth + PlankThickness);
288         if (!draw_woodplank(mi, cp, wire))
289                 return False;
290         glPopMatrix();
291         return True;
292 }
293
294 static void
295 reshape_cage(ModeInfo * mi, int width, int height)
296 {
297         cagestruct *cp = &cage[MI_SCREEN(mi)];
298         int i;
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         i = width / 512 + 1;
306         glLineWidth(i);
307         glPointSize(i);
308 }
309
310 static void
311 pinit(ModeInfo *mi)
312 {
313   /* int status; */
314
315     if (MI_IS_WIREFRAME(mi))
316       return;
317
318         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
319         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
320         glLightfv(GL_LIGHT0, GL_POSITION, position0);
321         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
322         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
323         glLightfv(GL_LIGHT1, GL_POSITION, position1);
324         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
325         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
326         glEnable(GL_LIGHTING);
327         glEnable(GL_LIGHT0);
328         glEnable(GL_LIGHT1);
329         glEnable(GL_NORMALIZE);
330         glFrontFace(GL_CCW);
331         glCullFace(GL_BACK);
332
333         /* cage */
334         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
335         glShadeModel(GL_FLAT);
336         glDisable(GL_DEPTH_TEST);
337         glEnable(GL_TEXTURE_2D);
338         glEnable(GL_CULL_FACE);
339
340         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
341
342 #if 0
343         clear_gl_error();
344         if (MI_IS_MONO(mi))
345       status = 0;
346     else
347       status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
348                                  WoodTextureWidth, WoodTextureHeight,
349                                  GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
350         if (status)
351           {
352                 const char *s = (char *) gluErrorString (status);
353                 fprintf (stderr, "%s: error mipmapping texture: %s\n",
354                                  progname, (s ? s : "(unknown)"));
355                 exit (1);
356           }
357         check_gl_error("mipmapping");
358 #else
359     {
360       XImage *img = xpm_to_ximage (mi->dpy,
361                                    mi->xgwa.visual,
362                                    mi->xgwa.colormap,
363                                    wood_texture);
364           glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
365                     img->width, img->height, 0,
366                     GL_RGBA,
367                     /* GL_UNSIGNED_BYTE, */
368                     GL_UNSIGNED_INT_8_8_8_8_REV,
369                     img->data);
370       check_gl_error("texture");
371       XDestroyImage (img);
372     }
373 #endif
374
375         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
376         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
377         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
378         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
379         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
380
381         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
382         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
383 }
384
385 ENTRYPOINT void
386 init_cage (ModeInfo * mi)
387 {
388         cagestruct *cp;
389
390         MI_INIT (mi, cage, NULL);
391         cp = &cage[MI_SCREEN(mi)];
392
393         cp->step = NRAND(90);
394         if ((cp->glx_context = init_GL(mi)) != NULL) {
395
396                 reshape_cage(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
397                 glDrawBuffer(GL_BACK);
398                 pinit(mi);
399         } else {
400                 MI_CLEARWINDOW(mi);
401         }
402 }
403
404 ENTRYPOINT void
405 draw_cage (ModeInfo * mi)
406 {
407         Display    *display = MI_DISPLAY(mi);
408         Window      window = MI_WINDOW(mi);
409         cagestruct *cp;
410
411         if (cage == NULL)
412                 return;
413         cp = &cage[MI_SCREEN(mi)];
414
415         MI_IS_DRAWN(mi) = True;
416         if (!cp->glx_context)
417                 return;
418
419     mi->polygon_count = 0;
420         glXMakeCurrent(display, window, *(cp->glx_context));
421
422         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
423
424         glPushMatrix();
425         glTranslatef(0.0, 0.0, -10.0);
426
427         if (!MI_IS_ICONIC(mi)) {
428                 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
429         } else {
430                 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
431         }
432
433 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
434   {
435     int o = (int) current_device_rotation();
436     GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
437     if (o != 0 && o != 180 && o != -180) {
438       glScalef (1/h, h, 1/h);  /* #### not quite right */
439       h = 1.7;
440       glScalef (h, h, h);
441     }
442   }
443 # endif
444
445         /* cage */
446         glRotatef(cp->step * 100, 0, 0, 1);
447         glRotatef(25 + cos(cp->step * 5) * 6, 1, 0, 0);
448         glRotatef(204.5 - sin(cp->step * 5) * 8, 0, 1, 0);
449         if (!draw_impossiblecage(mi, cp, MI_IS_WIREFRAME(mi))) {
450                 MI_ABORT(mi);
451                 return;
452         }
453
454         glPopMatrix();
455         if (MI_IS_FPS(mi)) do_fps (mi);
456         glFlush();
457
458         glXSwapBuffers(display, window);
459
460         cp->step += 0.025;
461 }
462
463 #ifndef STANDALONE
464 ENTRYPOINT void
465 change_cage (ModeInfo * mi)
466 {
467         cagestruct *cp = &cage[MI_SCREEN(mi)];
468
469         if (!cp->glx_context)
470                 return;
471
472         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
473         pinit(mi);
474 }
475 #endif /* !STANDALONE */
476
477 XSCREENSAVER_MODULE ("Cage", cage)
478
479 #endif