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