ftp://ftp.linux.ncsu.edu/mirror/ftp.redhat.com/pub/redhat/linux/enterprise/4/en/os...
[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 PROGCLASS                      "Cage"
81 # define HACK_INIT                      init_cage
82 # define HACK_DRAW                      draw_cage
83 # define HACK_RESHAPE           reshape
84 # define cage_opts                      xlockmore_opts
85 # define DEFAULTS                       "*delay:                25000   \n"                     \
86                                                         "*showFPS:      False   \n"                     \
87                                                         "*wireframe:    False   \n"
88 # include "xlockmore.h"         /* from the xscreensaver distribution */
89 #else /* !STANDALONE */
90 # include "xlock.h"             /* from the xlockmore distribution */
91
92 #endif /* !STANDALONE */
93
94 #ifdef MODE_cage
95
96
97 #include <GL/glu.h>
98 #include "e_textures.h"
99
100 ModeSpecOpt cage_opts =
101 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
102
103 #ifdef USE_MODULES
104 ModStruct   cage_description =
105 {"cage", "init_cage", "draw_cage", "release_cage",
106  "draw_cage", "change_cage", (char *) NULL, &cage_opts,
107  25000, 1, 1, 1, 1.0, 4, "",
108  "Shows the Impossible Cage, an Escher-like GL scene", 0, NULL};
109
110 #endif
111
112 #define Scale4Window               0.3
113 #define Scale4Iconic               0.4
114
115 #define sqr(A)                     ((A)*(A))
116
117 #ifndef Pi
118 #define Pi                         M_PI
119 #endif
120
121 #define ObjWoodPlank    0
122 #define MaxObj          1
123
124 /*************************************************************************/
125
126 typedef struct {
127         GLint       WindH, WindW;
128         GLfloat     step;
129         GLXContext *glx_context;
130 } cagestruct;
131
132 static float front_shininess[] =
133 {60.0};
134 static float front_specular[] =
135 {0.7, 0.7, 0.7, 1.0};
136 static float ambient[] =
137 {0.0, 0.0, 0.0, 1.0};
138 static float diffuse[] =
139 {1.0, 1.0, 1.0, 1.0};
140 static float position0[] =
141 {1.0, 1.0, 1.0, 0.0};
142 static float position1[] =
143 {-1.0, -1.0, 1.0, 0.0};
144 static float lmodel_ambient[] =
145 {0.5, 0.5, 0.5, 1.0};
146 static float lmodel_twoside[] =
147 {GL_TRUE};
148
149 static float MaterialWhite[] =
150 {0.7, 0.7, 0.7, 1.0};
151
152 static cagestruct *cage = (cagestruct *) NULL;
153
154 #define PlankWidth      3.0
155 #define PlankHeight     0.35
156 #define PlankThickness  0.15
157
158 static Bool 
159 draw_woodplank(cagestruct * cp)
160 {
161         glBegin(GL_QUADS);
162         glNormal3f(0, 0, 1);
163         glTexCoord2f(0, 0);
164         glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
165         glTexCoord2f(1, 0);
166         glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
167         glTexCoord2f(1, 1);
168         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
169         glTexCoord2f(0, 1);
170         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
171         glNormal3f(0, 0, -1);
172         glTexCoord2f(0, 0);
173         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
174         glTexCoord2f(1, 0);
175         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
176         glTexCoord2f(1, 1);
177         glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
178         glTexCoord2f(0, 1);
179         glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
180         glNormal3f(0, 1, 0);
181         glTexCoord2f(0, 0);
182         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
183         glTexCoord2f(1, 0);
184         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
185         glTexCoord2f(1, 1);
186         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
187         glTexCoord2f(0, 1);
188         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
189         glNormal3f(0, -1, 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         glNormal3f(1, 0, 0);
199         glTexCoord2f(0, 0);
200         glVertex3f(PlankWidth, -PlankHeight, PlankThickness);
201         glTexCoord2f(1, 0);
202         glVertex3f(PlankWidth, -PlankHeight, -PlankThickness);
203         glTexCoord2f(1, 1);
204         glVertex3f(PlankWidth, PlankHeight, -PlankThickness);
205         glTexCoord2f(0, 1);
206         glVertex3f(PlankWidth, PlankHeight, PlankThickness);
207         glNormal3f(-1, 0, 0);
208         glTexCoord2f(0, 0);
209         glVertex3f(-PlankWidth, PlankHeight, PlankThickness);
210         glTexCoord2f(1, 0);
211         glVertex3f(-PlankWidth, PlankHeight, -PlankThickness);
212         glTexCoord2f(1, 1);
213         glVertex3f(-PlankWidth, -PlankHeight, -PlankThickness);
214         glTexCoord2f(0, 1);
215         glVertex3f(-PlankWidth, -PlankHeight, PlankThickness);
216         glEnd();
217
218         return True;
219 }
220
221 static Bool
222 draw_impossiblecage(cagestruct * cp)
223 {
224         glPushMatrix();
225         glRotatef(90, 0, 1, 0);
226         glTranslatef(0.0, PlankHeight - PlankWidth, -PlankThickness - PlankWidth);
227         if (!draw_woodplank(cp))
228                 return False;
229         glPopMatrix();
230         glPushMatrix();
231         glRotatef(90, 0, 0, 1);
232         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - PlankThickness);
233         if (!draw_woodplank(cp))
234                 return False;
235         glPopMatrix();
236         glPushMatrix();
237         glRotatef(90, 0, 1, 0);
238         glTranslatef(0.0, PlankWidth - PlankHeight, -PlankThickness - PlankWidth);
239         if (!draw_woodplank(cp))
240                 return False;
241         glPopMatrix();
242         glPushMatrix();
243         glTranslatef(0.0, PlankWidth - PlankHeight, 3 * PlankThickness - PlankWidth);
244         if (!draw_woodplank(cp))
245                 return False;
246         glPopMatrix();
247         glPushMatrix();
248         glRotatef(90, 0, 0, 1);
249         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - PlankThickness);
250         if (!draw_woodplank(cp))
251                 return False;
252         glPopMatrix();
253         glPushMatrix();
254         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth - 3 * PlankThickness);
255         if (!draw_woodplank(cp))
256                 return False;
257         glPopMatrix();
258         glPushMatrix();
259         glTranslatef(0.0, PlankHeight - PlankWidth, 3 * PlankThickness - PlankWidth);
260         if (!draw_woodplank(cp))
261                 return False;
262         glPopMatrix();
263         glPushMatrix();
264         glRotatef(90, 0, 0, 1);
265         glTranslatef(0.0, PlankHeight - PlankWidth, PlankThickness - PlankWidth);
266         if (!draw_woodplank(cp))
267                 return False;
268         glPopMatrix();
269         glPushMatrix();
270         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth - 3 * PlankThickness);
271         if (!draw_woodplank(cp))
272                 return False;
273         glPopMatrix();
274         glPushMatrix();
275         glRotatef(90, 0, 1, 0);
276         glTranslatef(0.0, PlankHeight - PlankWidth, PlankWidth + PlankThickness);
277         if (!draw_woodplank(cp))
278                 return False;
279         glPopMatrix();
280         glPushMatrix();
281         glRotatef(90, 0, 0, 1);
282         glTranslatef(0.0, PlankWidth - PlankHeight, PlankThickness - PlankWidth);
283         if (!draw_woodplank(cp))
284                 return False;
285         glPopMatrix();
286         glPushMatrix();
287         glRotatef(90, 0, 1, 0);
288         glTranslatef(0.0, PlankWidth - PlankHeight, PlankWidth + PlankThickness);
289         if (!draw_woodplank(cp))
290                 return False;
291         glPopMatrix();
292         return True;
293 }
294
295 void
296 reshape(ModeInfo * mi, int width, int height)
297 {
298         cagestruct *cp = &cage[MI_SCREEN(mi)];
299         int i;
300
301         glViewport(0, 0, cp->WindW = (GLint) width, cp->WindH = (GLint) height);
302         glMatrixMode(GL_PROJECTION);
303         glLoadIdentity();
304         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
305         glMatrixMode(GL_MODELVIEW);
306         i = width / 512 + 1;
307         glLineWidth(i);
308         glPointSize(i);
309 }
310
311 static void
312 pinit(ModeInfo *mi)
313 {
314         int status;
315         glClearDepth(1.0);
316         glClearColor(0.0, 0.0, 0.0, 1.0);
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         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
358         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
359         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
360         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
361         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
362         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
363
364         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
365         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
366 }
367
368 void
369 release_cage(ModeInfo * mi)
370 {
371         if (cage != NULL) {
372                 int screen;
373
374                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
375                         cagestruct *cp = &cage[screen];
376
377                         if (cp->glx_context) {
378                                 cp->glx_context = (GLXContext *) NULL;
379                         }
380                 }
381                 (void) free((void *) cage);
382                 cage = (cagestruct *) NULL;
383         }
384         FreeAllGL(mi);
385 }
386
387 void
388 init_cage(ModeInfo * mi)
389 {
390         cagestruct *cp;
391
392         if (cage == NULL) {
393                 if ((cage = (cagestruct *) calloc(MI_NUM_SCREENS(mi),
394                                                sizeof (cagestruct))) == NULL)
395                         return;
396         }
397         cp = &cage[MI_SCREEN(mi)];
398
399         cp->step = NRAND(90);
400         if ((cp->glx_context = init_GL(mi)) != NULL) {
401
402                 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
403                 glDrawBuffer(GL_BACK);
404                 pinit(mi);
405         } else {
406                 MI_CLEARWINDOW(mi);
407         }
408 }
409
410 void
411 draw_cage(ModeInfo * mi)
412 {
413         Display    *display = MI_DISPLAY(mi);
414         Window      window = MI_WINDOW(mi);
415         cagestruct *cp;
416
417         if (cage == NULL)
418                 return;
419         cp = &cage[MI_SCREEN(mi)];
420
421         MI_IS_DRAWN(mi) = True;
422         if (!cp->glx_context)
423                 return;
424
425         glXMakeCurrent(display, window, *(cp->glx_context));
426
427         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
428
429         glPushMatrix();
430
431         glTranslatef(0.0, 0.0, -10.0);
432
433         if (!MI_IS_ICONIC(mi)) {
434                 glScalef(Scale4Window * cp->WindH / cp->WindW, Scale4Window, Scale4Window);
435         } else {
436                 glScalef(Scale4Iconic * cp->WindH / cp->WindW, Scale4Iconic, Scale4Iconic);
437         }
438
439         /* cage */
440         glRotatef(cp->step * 100, 0, 0, 1);
441         glRotatef(25 + cos(cp->step * 5) * 6, 1, 0, 0);
442         glRotatef(204.5 - sin(cp->step * 5) * 8, 0, 1, 0);
443         if (!draw_impossiblecage(cp)) {
444                 release_cage(mi);
445                 return;
446         }
447
448         glPopMatrix();
449         if (MI_IS_FPS(mi)) do_fps (mi);
450         glFlush();
451
452         glXSwapBuffers(display, window);
453
454         cp->step += 0.025;
455 }
456
457 void
458 change_cage(ModeInfo * mi)
459 {
460         cagestruct *cp = &cage[MI_SCREEN(mi)];
461
462         if (!cp->glx_context)
463                 return;
464
465         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cp->glx_context));
466         pinit(mi);
467 }
468
469 #endif