From http://www.jwz.org/xscreensaver/xscreensaver-5.39.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 free_cage 0
86 # define release_cage 0
87 # define cage_handle_event xlockmore_no_events
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 "ximage-loader.h"
99 #include "images/gen/wood_png.h"
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     int y = 0;
300
301     if (width > height * 5) {   /* tiny window: show middle */
302       height = width * 9/16;
303       y = -height/2;
304     }
305
306         glViewport(0, y, cp->WindW = (GLint) width, cp->WindH = (GLint) height);
307         glMatrixMode(GL_PROJECTION);
308         glLoadIdentity();
309         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
310         glMatrixMode(GL_MODELVIEW);
311         i = width / 512 + 1;
312         glLineWidth(i);
313         glPointSize(i);
314 }
315
316 static void
317 pinit(ModeInfo *mi)
318 {
319   /* int status; */
320
321     if (MI_IS_WIREFRAME(mi))
322       return;
323
324         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
325         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
326         glLightfv(GL_LIGHT0, GL_POSITION, position0);
327         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
328         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
329         glLightfv(GL_LIGHT1, GL_POSITION, position1);
330         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
331         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
332         glEnable(GL_LIGHTING);
333         glEnable(GL_LIGHT0);
334         glEnable(GL_LIGHT1);
335         glEnable(GL_NORMALIZE);
336         glFrontFace(GL_CCW);
337         glCullFace(GL_BACK);
338
339         /* cage */
340         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
341         glShadeModel(GL_FLAT);
342         glDisable(GL_DEPTH_TEST);
343         glEnable(GL_TEXTURE_2D);
344         glEnable(GL_CULL_FACE);
345
346         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
347
348 #if 0
349         clear_gl_error();
350         if (MI_IS_MONO(mi))
351       status = 0;
352     else
353       status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
354                                  WoodTextureWidth, WoodTextureHeight,
355                                  GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
356         if (status)
357           {
358                 const char *s = (char *) gluErrorString (status);
359                 fprintf (stderr, "%s: error mipmapping texture: %s\n",
360                                  progname, (s ? s : "(unknown)"));
361                 exit (1);
362           }
363         check_gl_error("mipmapping");
364 #else
365     {
366       XImage *img = image_data_to_ximage (mi->dpy, mi->xgwa.visual,
367                                           wood_png, sizeof(wood_png));
368           glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
369                     img->width, img->height, 0,
370                     GL_RGBA, GL_UNSIGNED_BYTE, img->data);
371       check_gl_error("texture");
372       XDestroyImage (img);
373     }
374 #endif
375
376         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
377         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
378         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
379         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
380         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
381
382         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
383         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
384 }
385
386 ENTRYPOINT void
387 init_cage (ModeInfo * mi)
388 {
389         cagestruct *cp;
390
391         MI_INIT (mi, cage);
392         cp = &cage[MI_SCREEN(mi)];
393
394         cp->step = NRAND(90);
395         if ((cp->glx_context = init_GL(mi)) != NULL) {
396
397                 reshape_cage(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
398                 glDrawBuffer(GL_BACK);
399                 pinit(mi);
400         } else {
401                 MI_CLEARWINDOW(mi);
402         }
403 }
404
405 ENTRYPOINT void
406 draw_cage (ModeInfo * mi)
407 {
408         Display    *display = MI_DISPLAY(mi);
409         Window      window = MI_WINDOW(mi);
410         cagestruct *cp;
411
412         if (cage == NULL)
413                 return;
414         cp = &cage[MI_SCREEN(mi)];
415
416         MI_IS_DRAWN(mi) = True;
417         if (!cp->glx_context)
418                 return;
419
420     mi->polygon_count = 0;
421         glXMakeCurrent(display, window, *(cp->glx_context));
422
423         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
424
425         glPushMatrix();
426         glTranslatef(0.0, 0.0, -10.0);
427
428         if (!MI_IS_ICONIC(mi)) {
429                 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
430         } else {
431                 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
432         }
433
434 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
435   {
436     int o = (int) current_device_rotation();
437     GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
438     if (o != 0 && o != 180 && o != -180) {
439       glScalef (1/h, h, 1/h);  /* #### not quite right */
440       h = 1.7;
441       glScalef (h, h, h);
442     }
443   }
444 # endif
445
446         /* cage */
447         glRotatef(cp->step * 100, 0, 0, 1);
448         glRotatef(25 + cos(cp->step * 5) * 6, 1, 0, 0);
449         glRotatef(204.5 - sin(cp->step * 5) * 8, 0, 1, 0);
450         if (!draw_impossiblecage(mi, cp, MI_IS_WIREFRAME(mi))) {
451                 MI_ABORT(mi);
452                 return;
453         }
454
455         glPopMatrix();
456         if (MI_IS_FPS(mi)) do_fps (mi);
457         glFlush();
458
459         glXSwapBuffers(display, window);
460
461         cp->step += 0.025;
462 }
463
464 #ifndef STANDALONE
465 ENTRYPOINT void
466 change_cage (ModeInfo * mi)
467 {
468         cagestruct *cp = &cage[MI_SCREEN(mi)];
469
470         if (!cp->glx_context)
471                 return;
472
473         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
474         pinit(mi);
475 }
476 #endif /* !STANDALONE */
477
478 XSCREENSAVER_MODULE ("Cage", cage)
479
480 #endif