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 HACK_RESHAPE reshape_stairs
68 # define stairs_opts xlockmore_opts
69 # define DEFAULTS "*cycles: 1 \n" \
71 "*showFPS: False \n" \
72 "*wireframe: False \n"
73 # include "xlockmore.h" /* from the xscreensaver distribution */
74 #else /* !STANDALONE */
75 # include "xlock.h" /* from the xlockmore distribution */
77 #endif /* !STANDALONE */
82 #include "e_textures.h"
84 ModeSpecOpt stairs_opts =
85 {0, NULL, 0, NULL, NULL};
88 ModStruct stairs_description =
89 {"stairs", "init_stairs", "draw_stairs", "release_stairs",
90 "draw_stairs", "change_stairs", NULL, &stairs_opts,
91 1000, 1, 1, 1, 4, 1.0, "",
92 "Shows Infinite Stairs, an Escher-like scene", 0, NULL};
96 #define Scale4Window 0.3
97 #define Scale4Iconic 0.4
99 #define sqr(A) ((A)*(A))
105 /*************************************************************************/
111 int AreObjectsDefined[1];
114 GLXContext *glx_context;
117 static float front_shininess[] =
119 static float front_specular[] =
120 {0.7, 0.7, 0.7, 1.0};
121 static float ambient[] =
122 {0.0, 0.0, 0.0, 1.0};
123 static float diffuse[] =
124 {1.0, 1.0, 1.0, 1.0};
125 static float position0[] =
126 {1.0, 1.0, 1.0, 0.0};
127 static float position1[] =
128 {-1.0, -1.0, 1.0, 0.0};
129 static float lmodel_ambient[] =
130 {0.5, 0.5, 0.5, 1.0};
131 static float lmodel_twoside[] =
135 static float MaterialRed[] =
136 {0.7, 0.0, 0.0, 1.0};
137 static float MaterialGreen[] =
138 {0.1, 0.5, 0.2, 1.0};
139 static float MaterialBlue[] =
140 {0.0, 0.0, 0.7, 1.0};
141 static float MaterialCyan[] =
142 {0.2, 0.5, 0.7, 1.0};
143 static float MaterialMagenta[] =
144 {0.6, 0.2, 0.5, 1.0};
145 static float MaterialGray[] =
146 {0.2, 0.2, 0.2, 1.0};
147 static float MaterialGray5[] =
148 {0.5, 0.5, 0.5, 1.0};
149 static float MaterialGray6[] =
150 {0.6, 0.6, 0.6, 1.0};
151 static float MaterialGray8[] =
152 {0.8, 0.8, 0.8, 1.0};
155 static float MaterialYellow[] =
156 {0.7, 0.7, 0.0, 1.0};
157 static float MaterialWhite[] =
158 {0.7, 0.7, 0.7, 1.0};
160 static float positions[] =
162 -2.5, 4.0, 0.0, /* First one is FUDGED :) */
199 #define NPOSITIONS ((sizeof positions) / (sizeof positions[0]))
201 #define SPHERE_TICKS 8
203 static stairsstruct *stairs = NULL;
204 static GLuint objects;
208 #define PlankWidth 3.0
209 #define PlankHeight 0.35
210 #define PlankThickness 0.15
213 mySphere(float radius)
215 GLUquadricObj *quadObj;
217 quadObj = gluNewQuadric();
218 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
219 gluSphere(quadObj, radius, 16, 16);
220 gluDeleteQuadric(quadObj);
224 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
229 glVertex3f(-width, -height, thickness);
231 glVertex3f(width, -height, thickness);
233 glVertex3f(width, height, thickness);
235 glVertex3f(-width, height, thickness);
236 glNormal3f(0, 0, -1);
238 glVertex3f(-width, height, -thickness);
240 glVertex3f(width, height, -thickness);
242 glVertex3f(width, -height, -thickness);
244 glVertex3f(-width, -height, -thickness);
247 glVertex3f(-width, height, thickness);
249 glVertex3f(width, height, thickness);
251 glVertex3f(width, height, -thickness);
253 glVertex3f(-width, height, -thickness);
254 glNormal3f(0, -1, 0);
256 glVertex3f(-width, -height, -thickness);
258 glVertex3f(width, -height, -thickness);
260 glVertex3f(width, -height, thickness);
262 glVertex3f(-width, -height, thickness);
265 glVertex3f(width, -height, thickness);
267 glVertex3f(width, -height, -thickness);
269 glVertex3f(width, height, -thickness);
271 glVertex3f(width, height, thickness);
272 glNormal3f(-1, 0, 0);
274 glVertex3f(-width, height, thickness);
276 glVertex3f(-width, height, -thickness);
278 glVertex3f(-width, -height, -thickness);
280 glVertex3f(-width, -height, thickness);
285 draw_stairs_internal(ModeInfo * mi)
287 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
292 glTranslatef(-3.0, 0.1, 2.0);
293 for (X = 0; X < 2; X++) {
294 draw_block(0.5, 2.7 + 0.1 * X, 0.5);
295 glTranslatef(0.0, 0.1, -1.0);
298 glTranslatef(-3.0, 0.0, 3.0);
301 for (X = 0; X < 6; X++) {
302 draw_block(0.5, 2.6 - 0.1 * X, 0.5);
303 glTranslatef(1.0, -0.1, 0.0);
305 glTranslatef(-1.0, -0.9, -1.0);
306 for (X = 0; X < 5; X++) {
307 draw_block(0.5, 3.0 - 0.1 * X, 0.5);
308 glTranslatef(0.0, 0.0, -1.0);
310 glTranslatef(-1.0, -1.1, 1.0);
311 for (X = 0; X < 3; X++) {
312 draw_block(0.5, 3.5 - 0.1 * X, 0.5);
313 glTranslatef(-1.0, -0.1, 0.0);
319 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
322 int pos = sp->sphere_position;
323 int ppos = sp->sphere_position - 3;
324 int npos = sp->sphere_position + 3;
325 GLfloat spx, spy, spz;
329 if (ppos < 0) ppos += NPOSITIONS;
330 if (npos >= NPOSITIONS) npos -= NPOSITIONS;
332 if (sp->sphere_tick < 0)
334 dx = positions[ppos] - positions[pos];
335 dy = positions[ppos+1] - positions[pos+1];
336 dz = positions[ppos+2] - positions[pos+2];
337 div = SPHERE_TICKS + sp->sphere_tick;
341 dx = positions[npos] - positions[pos];
342 dy = positions[npos+1] - positions[pos+1];
343 dz = positions[npos+2] - positions[ppos+2];
344 div = SPHERE_TICKS - sp->sphere_tick;
347 spx = positions[pos];
348 spy = positions[pos+1];
349 spz = positions[pos+2];
358 spy -= 0.5; /* move the bottom of the ball closer to the stairs */
362 fprintf(stderr, "%3d %3d %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f\n",
363 sp->sphere_position, sp->sphere_tick,
367 glBegin(GL_LINE_LOOP); /* path 1 */
368 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
369 glVertex3f(positions[npos], positions[npos+1], positions[npos+2]);
372 glBegin(GL_LINE_LOOP); /* path 2 */
373 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
374 glVertex3f(positions[ppos], positions[ppos+1], positions[ppos+2]);
377 glBegin(GL_LINE_LOOP); /* base origin */
378 glVertex3f(positions[pos], positions[pos+1]-10, positions[pos+2]);
379 glVertex3f(positions[pos], positions[pos+1]+10, positions[pos+2]);
382 glBegin(GL_LINE_LOOP); /* base origin */
383 glVertex3f(positions[pos]-10, positions[pos+1], positions[pos+2]);
384 glVertex3f(positions[pos]+10, positions[pos+1], positions[pos+2]);
387 glBegin(GL_LINE_LOOP); /* base origin */
388 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]-10);
389 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]+10);
393 glTranslatef(spx, spy, spz);
395 #ifdef DEBUG /* ball origin */
396 glBegin(GL_LINE_LOOP); glVertex3f(0,-2,0); glVertex3f(0,2,0); glEnd();
397 glBegin(GL_LINE_LOOP); glVertex3f(-2,0,0); glVertex3f(2,0,0); glEnd();
398 glBegin(GL_LINE_LOOP); glVertex3f(0,0,-2); glVertex3f(0,0,2); glEnd();
402 if (sp->sphere_position == 0) /* FUDGE soo its not so obvious */
408 if (++sp->sphere_tick >= SPHERE_TICKS-1)
410 sp->sphere_tick = -(SPHERE_TICKS-2);
411 sp->sphere_position += 3;
412 sp->sphere_position += 3;
415 if (sp->sphere_position >= NPOSITIONS)
416 sp->sphere_position = 0;
420 reshape_stairs(ModeInfo * mi, int width, int height)
422 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
424 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
425 glMatrixMode(GL_PROJECTION);
427 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
428 glMatrixMode(GL_MODELVIEW);
432 } else if (width >= 512) {
445 glClearColor(0.0, 0.0, 0.0, 1.0);
447 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
448 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
449 glLightfv(GL_LIGHT0, GL_POSITION, position0);
450 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
451 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
452 glLightfv(GL_LIGHT1, GL_POSITION, position1);
453 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
454 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
455 glEnable(GL_LIGHTING);
458 glEnable(GL_NORMALIZE);
462 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
463 glShadeModel(GL_FLAT);
464 glEnable(GL_DEPTH_TEST);
465 glEnable(GL_TEXTURE_2D);
466 glEnable(GL_CULL_FACE);
468 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
469 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, WoodTextureWidth, WoodTextureHeight,
470 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
471 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
472 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
473 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
474 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
475 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
477 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
478 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
482 init_stairs(ModeInfo * mi)
484 int screen = MI_SCREEN(mi);
487 if (stairs == NULL) {
488 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
489 sizeof (stairsstruct))) == NULL)
492 sp = &stairs[screen];
494 sp->direction = LRAND() & 1;
495 sp->sphere_position = NRAND(NPOSITIONS / 3) * 3;
498 if ((sp->glx_context = init_GL(mi)) != NULL) {
500 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
501 glDrawBuffer(GL_BACK);
502 if (!glIsList(objects))
503 objects = glGenLists(1);
511 draw_stairs(ModeInfo * mi)
513 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
515 Display *display = MI_DISPLAY(mi);
516 Window window = MI_WINDOW(mi);
518 if (!sp->glx_context)
521 glXMakeCurrent(display, window, *(sp->glx_context));
523 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
527 glTranslatef(0.0, 0.0, -10.0);
529 if (!MI_IS_ICONIC(mi)) {
530 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
532 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
535 glRotatef(44.5, 1, 0, 0);
536 glRotatef(50 + ((sp->direction) ? 1 : -1) *
537 ((sp->step * 100 > 120) ? sp->step * 100 - 120 : 0), 0, 1, 0);
538 if (sp->step * 100 >= 360 + 120) { /* stop showing secrets */
540 sp->direction = LRAND() & 1;
542 draw_stairs_internal(mi);
546 if (mi->fps_p) do_fps (mi);
549 glXSwapBuffers(display, window);
555 change_stairs(ModeInfo * mi)
557 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
559 if (!sp->glx_context)
562 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
567 release_stairs(ModeInfo * mi)
569 if (stairs != NULL) {
570 (void) free((void *) stairs);
573 if (glIsList(objects)) {
574 glDeleteLists(objects, 1);