1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* stairs --- Infinite Stairs, and Escher-like scene. */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)stairs.c 4.07 97/11/24 xlockmore";
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and that
15 * both that copyright notice and this permission notice appear in
16 * supporting documentation.
18 * This file is provided AS IS with no warranties of any kind. The author
19 * shall have no liability with respect to the infringement of copyrights,
20 * trade secrets or any patents by this file or any part thereof. In no
21 * event will the author be liable for any lost revenue or profits or
22 * other special, indirect and consequential damages.
24 * This mode shows some interesting scenes that are impossible OR very
25 * weird to build in the real universe. Much of the scenes are inspirated
26 * on Mauritz Cornelis stairs's works which derivated the mode's name.
27 * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
28 * say he was a mathematician.
30 * Thanks goes to Brian Paul for making it possible and inexpensive to use
33 * Since I'm not a native English speaker, my apologies for any grammatical
36 * My e-mail address is
39 * Marcelo F. Vianna (Jun-01-1997)
42 * 07-Jan-98: This would be a scene for the escher mode, but now escher mode
43 * was splitted in different modes for each scene. This is the
44 * initial release and is not working yet.
50 * Texture mapping is only available on RGBA contexts, Mono and color index
51 * visuals DO NOT support texture mapping in OpenGL.
53 * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
54 * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
55 * is not officially supported for both OpenGL and Mesa, but seems to not crash
58 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
61 #include <X11/Intrinsic.h>
64 # define PROGCLASS "Stairs"
65 # define HACK_INIT init_stairs
66 # define HACK_DRAW draw_stairs
67 # define stairs_opts xlockmore_opts
68 # define DEFAULTS "*cycles: 1 \n" \
70 "*wireframe: False \n"
71 # include "xlockmore.h" /* from the xscreensaver distribution */
72 #else /* !STANDALONE */
73 # include "xlock.h" /* from the xlockmore distribution */
75 #endif /* !STANDALONE */
80 #include "e_textures.h"
82 ModeSpecOpt stairs_opts =
83 {0, NULL, 0, NULL, NULL};
86 ModStruct stairs_description =
87 {"stairs", "init_stairs", "draw_stairs", "release_stairs",
88 "draw_stairs", "change_stairs", NULL, &stairs_opts,
89 1000, 1, 1, 1, 4, 1.0, "",
90 "Shows Infinite Stairs, an Escher-like scene", 0, NULL};
94 #define Scale4Window 0.3
95 #define Scale4Iconic 0.4
97 #define sqr(A) ((A)*(A))
103 /*************************************************************************/
109 int AreObjectsDefined[1];
111 GLXContext *glx_context;
114 static float front_shininess[] =
116 static float front_specular[] =
117 {0.7, 0.7, 0.7, 1.0};
118 static float ambient[] =
119 {0.0, 0.0, 0.0, 1.0};
120 static float diffuse[] =
121 {1.0, 1.0, 1.0, 1.0};
122 static float position0[] =
123 {1.0, 1.0, 1.0, 0.0};
124 static float position1[] =
125 {-1.0, -1.0, 1.0, 0.0};
126 static float lmodel_ambient[] =
127 {0.5, 0.5, 0.5, 1.0};
128 static float lmodel_twoside[] =
132 static float MaterialRed[] =
133 {0.7, 0.0, 0.0, 1.0};
134 static float MaterialGreen[] =
135 {0.1, 0.5, 0.2, 1.0};
136 static float MaterialBlue[] =
137 {0.0, 0.0, 0.7, 1.0};
138 static float MaterialCyan[] =
139 {0.2, 0.5, 0.7, 1.0};
140 static float MaterialMagenta[] =
141 {0.6, 0.2, 0.5, 1.0};
142 static float MaterialGray[] =
143 {0.2, 0.2, 0.2, 1.0};
144 static float MaterialGray5[] =
145 {0.5, 0.5, 0.5, 1.0};
146 static float MaterialGray6[] =
147 {0.6, 0.6, 0.6, 1.0};
148 static float MaterialGray8[] =
149 {0.8, 0.8, 0.8, 1.0};
152 static float MaterialYellow[] =
153 {0.7, 0.7, 0.0, 1.0};
154 static float MaterialWhite[] =
155 {0.7, 0.7, 0.7, 1.0};
157 static float positions[] =
159 -2.5, 4.0, 0.0, /* First one is FUDGED :) */
196 #define NPOSITIONS ((sizeof positions) / (sizeof positions[0]))
198 static stairsstruct *stairs = NULL;
199 static GLuint objects;
203 #define PlankWidth 3.0
204 #define PlankHeight 0.35
205 #define PlankThickness 0.15
208 mySphere(float radius)
210 GLUquadricObj *quadObj;
212 quadObj = gluNewQuadric();
213 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
214 gluSphere(quadObj, radius, 16, 16);
215 gluDeleteQuadric(quadObj);
219 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
224 glVertex3f(-width, -height, thickness);
226 glVertex3f(width, -height, thickness);
228 glVertex3f(width, height, thickness);
230 glVertex3f(-width, height, thickness);
231 glNormal3f(0, 0, -1);
233 glVertex3f(-width, height, -thickness);
235 glVertex3f(width, height, -thickness);
237 glVertex3f(width, -height, -thickness);
239 glVertex3f(-width, -height, -thickness);
242 glVertex3f(-width, height, thickness);
244 glVertex3f(width, height, thickness);
246 glVertex3f(width, height, -thickness);
248 glVertex3f(-width, height, -thickness);
249 glNormal3f(0, -1, 0);
251 glVertex3f(-width, -height, -thickness);
253 glVertex3f(width, -height, -thickness);
255 glVertex3f(width, -height, thickness);
257 glVertex3f(-width, -height, thickness);
260 glVertex3f(width, -height, thickness);
262 glVertex3f(width, -height, -thickness);
264 glVertex3f(width, height, -thickness);
266 glVertex3f(width, height, thickness);
267 glNormal3f(-1, 0, 0);
269 glVertex3f(-width, height, thickness);
271 glVertex3f(-width, height, -thickness);
273 glVertex3f(-width, -height, -thickness);
275 glVertex3f(-width, -height, thickness);
280 draw_stairs_internal(ModeInfo * mi)
282 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
287 glTranslatef(-3.0, 0.1, 2.0);
288 for (X = 0; X < 2; X++) {
289 draw_block(0.5, 2.7 + 0.1 * X, 0.5);
290 glTranslatef(0.0, 0.1, -1.0);
293 glTranslatef(-3.0, 0.0, 3.0);
296 for (X = 0; X < 6; X++) {
297 draw_block(0.5, 2.6 - 0.1 * X, 0.5);
298 glTranslatef(1.0, -0.1, 0.0);
300 glTranslatef(-1.0, -0.9, -1.0);
301 for (X = 0; X < 5; X++) {
302 draw_block(0.5, 3.0 - 0.1 * X, 0.5);
303 glTranslatef(0.0, 0.0, -1.0);
305 glTranslatef(-1.0, -1.1, 1.0);
306 for (X = 0; X < 3; X++) {
307 draw_block(0.5, 3.5 - 0.1 * X, 0.5);
308 glTranslatef(-1.0, -0.1, 0.0);
314 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
316 glTranslatef((GLfloat) positions[sp->sphere_position],
317 (GLfloat) positions[sp->sphere_position + 1],
318 (GLfloat) positions[sp->sphere_position + 2]);
319 if (sp->sphere_position == 0) /* FUDGE soo its not so obvious */
324 sp->sphere_position += 3;
325 if (sp->sphere_position >= NPOSITIONS)
326 sp->sphere_position = 0;
330 reshape(ModeInfo * mi, int width, int height)
332 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
334 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
335 glMatrixMode(GL_PROJECTION);
337 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
338 glMatrixMode(GL_MODELVIEW);
342 } else if (width >= 512) {
355 glClearColor(0.0, 0.0, 0.0, 1.0);
357 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
358 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
359 glLightfv(GL_LIGHT0, GL_POSITION, position0);
360 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
361 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
362 glLightfv(GL_LIGHT1, GL_POSITION, position1);
363 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
364 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
365 glEnable(GL_LIGHTING);
368 glEnable(GL_NORMALIZE);
372 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
373 glShadeModel(GL_FLAT);
374 glEnable(GL_DEPTH_TEST);
375 glEnable(GL_TEXTURE_2D);
376 glEnable(GL_CULL_FACE);
378 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
379 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, WoodTextureWidth, WoodTextureHeight,
380 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
381 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
382 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
383 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
384 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
385 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
387 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
388 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
392 init_stairs(ModeInfo * mi)
394 int screen = MI_SCREEN(mi);
397 if (stairs == NULL) {
398 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
399 sizeof (stairsstruct))) == NULL)
402 sp = &stairs[screen];
404 sp->direction = LRAND() & 1;
405 sp->sphere_position = NRAND(NPOSITIONS / 3) * 3;
407 if ((sp->glx_context = init_GL(mi)) != NULL) {
409 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
410 glDrawBuffer(GL_BACK);
411 if (!glIsList(objects))
412 objects = glGenLists(1);
420 draw_stairs(ModeInfo * mi)
422 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
424 Display *display = MI_DISPLAY(mi);
425 Window window = MI_WINDOW(mi);
427 if (!sp->glx_context)
430 glXMakeCurrent(display, window, *(sp->glx_context));
432 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
436 glTranslatef(0.0, 0.0, -10.0);
438 if (!MI_IS_ICONIC(mi)) {
439 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
441 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
444 glRotatef(44.5, 1, 0, 0);
445 glRotatef(50 + ((sp->direction) ? 1 : -1) *
446 ((sp->step * 100 > 120) ? sp->step * 100 - 120 : 0), 0, 1, 0);
447 if (sp->step * 100 >= 360 + 120) { /* stop showing secrets */
449 sp->direction = LRAND() & 1;
451 draw_stairs_internal(mi);
457 glXSwapBuffers(display, window);
463 change_stairs(ModeInfo * mi)
465 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
467 if (!sp->glx_context)
470 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
475 release_stairs(ModeInfo * mi)
477 if (stairs != NULL) {
478 (void) free((void *) stairs);
481 if (glIsList(objects)) {
482 glDeleteLists(objects, 1);