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