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