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];
112 GLXContext *glx_context;
115 static float front_shininess[] =
117 static float front_specular[] =
118 {0.7, 0.7, 0.7, 1.0};
119 static float ambient[] =
120 {0.0, 0.0, 0.0, 1.0};
121 static float diffuse[] =
122 {1.0, 1.0, 1.0, 1.0};
123 static float position0[] =
124 {1.0, 1.0, 1.0, 0.0};
125 static float position1[] =
126 {-1.0, -1.0, 1.0, 0.0};
127 static float lmodel_ambient[] =
128 {0.5, 0.5, 0.5, 1.0};
129 static float lmodel_twoside[] =
133 static float MaterialRed[] =
134 {0.7, 0.0, 0.0, 1.0};
135 static float MaterialGreen[] =
136 {0.1, 0.5, 0.2, 1.0};
137 static float MaterialBlue[] =
138 {0.0, 0.0, 0.7, 1.0};
139 static float MaterialCyan[] =
140 {0.2, 0.5, 0.7, 1.0};
141 static float MaterialMagenta[] =
142 {0.6, 0.2, 0.5, 1.0};
143 static float MaterialGray[] =
144 {0.2, 0.2, 0.2, 1.0};
145 static float MaterialGray5[] =
146 {0.5, 0.5, 0.5, 1.0};
147 static float MaterialGray6[] =
148 {0.6, 0.6, 0.6, 1.0};
149 static float MaterialGray8[] =
150 {0.8, 0.8, 0.8, 1.0};
153 static float MaterialYellow[] =
154 {0.7, 0.7, 0.0, 1.0};
155 static float MaterialWhite[] =
156 {0.7, 0.7, 0.7, 1.0};
158 static float positions[] =
160 -2.5, 4.0, 0.0, /* First one is FUDGED :) */
197 #define NPOSITIONS ((sizeof positions) / (sizeof positions[0]))
199 #define SPHERE_TICKS 8
201 static stairsstruct *stairs = NULL;
202 static GLuint objects;
206 #define PlankWidth 3.0
207 #define PlankHeight 0.35
208 #define PlankThickness 0.15
211 mySphere(float radius)
213 GLUquadricObj *quadObj;
215 quadObj = gluNewQuadric();
216 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
217 gluSphere(quadObj, radius, 16, 16);
218 gluDeleteQuadric(quadObj);
222 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
227 glVertex3f(-width, -height, thickness);
229 glVertex3f(width, -height, thickness);
231 glVertex3f(width, height, thickness);
233 glVertex3f(-width, height, thickness);
234 glNormal3f(0, 0, -1);
236 glVertex3f(-width, height, -thickness);
238 glVertex3f(width, height, -thickness);
240 glVertex3f(width, -height, -thickness);
242 glVertex3f(-width, -height, -thickness);
245 glVertex3f(-width, height, thickness);
247 glVertex3f(width, height, thickness);
249 glVertex3f(width, height, -thickness);
251 glVertex3f(-width, height, -thickness);
252 glNormal3f(0, -1, 0);
254 glVertex3f(-width, -height, -thickness);
256 glVertex3f(width, -height, -thickness);
258 glVertex3f(width, -height, thickness);
260 glVertex3f(-width, -height, thickness);
263 glVertex3f(width, -height, thickness);
265 glVertex3f(width, -height, -thickness);
267 glVertex3f(width, height, -thickness);
269 glVertex3f(width, height, thickness);
270 glNormal3f(-1, 0, 0);
272 glVertex3f(-width, height, thickness);
274 glVertex3f(-width, height, -thickness);
276 glVertex3f(-width, -height, -thickness);
278 glVertex3f(-width, -height, thickness);
283 draw_stairs_internal(ModeInfo * mi)
285 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
290 glTranslatef(-3.0, 0.1, 2.0);
291 for (X = 0; X < 2; X++) {
292 draw_block(0.5, 2.7 + 0.1 * X, 0.5);
293 glTranslatef(0.0, 0.1, -1.0);
296 glTranslatef(-3.0, 0.0, 3.0);
299 for (X = 0; X < 6; X++) {
300 draw_block(0.5, 2.6 - 0.1 * X, 0.5);
301 glTranslatef(1.0, -0.1, 0.0);
303 glTranslatef(-1.0, -0.9, -1.0);
304 for (X = 0; X < 5; X++) {
305 draw_block(0.5, 3.0 - 0.1 * X, 0.5);
306 glTranslatef(0.0, 0.0, -1.0);
308 glTranslatef(-1.0, -1.1, 1.0);
309 for (X = 0; X < 3; X++) {
310 draw_block(0.5, 3.5 - 0.1 * X, 0.5);
311 glTranslatef(-1.0, -0.1, 0.0);
317 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
320 int pos = sp->sphere_position;
321 int ppos = sp->sphere_position - 3;
322 int npos = sp->sphere_position + 3;
323 GLfloat spx, spy, spz;
327 if (ppos < 0) ppos += NPOSITIONS;
328 if (npos >= NPOSITIONS) npos -= NPOSITIONS;
330 if (sp->sphere_tick < 0)
332 dx = positions[ppos] - positions[pos];
333 dy = positions[ppos+1] - positions[pos+1];
334 dz = positions[ppos+2] - positions[pos+2];
335 div = SPHERE_TICKS + sp->sphere_tick;
339 dx = positions[npos] - positions[pos];
340 dy = positions[npos+1] - positions[pos+1];
341 dz = positions[npos+2] - positions[ppos+2];
342 div = SPHERE_TICKS - sp->sphere_tick;
345 spx = positions[pos];
346 spy = positions[pos+1];
347 spz = positions[pos+2];
356 spy -= 0.5; /* move the bottom of the ball closer to the stairs */
360 fprintf(stderr, "%3d %3d %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f\n",
361 sp->sphere_position, sp->sphere_tick,
365 glBegin(GL_LINE_LOOP); /* path 1 */
366 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
367 glVertex3f(positions[npos], positions[npos+1], positions[npos+2]);
370 glBegin(GL_LINE_LOOP); /* path 2 */
371 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
372 glVertex3f(positions[ppos], positions[ppos+1], positions[ppos+2]);
375 glBegin(GL_LINE_LOOP); /* base origin */
376 glVertex3f(positions[pos], positions[pos+1]-10, positions[pos+2]);
377 glVertex3f(positions[pos], positions[pos+1]+10, positions[pos+2]);
380 glBegin(GL_LINE_LOOP); /* base origin */
381 glVertex3f(positions[pos]-10, positions[pos+1], positions[pos+2]);
382 glVertex3f(positions[pos]+10, positions[pos+1], positions[pos+2]);
385 glBegin(GL_LINE_LOOP); /* base origin */
386 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]-10);
387 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]+10);
391 glTranslatef(spx, spy, spz);
393 #ifdef DEBUG /* ball origin */
394 glBegin(GL_LINE_LOOP); glVertex3f(0,-2,0); glVertex3f(0,2,0); glEnd();
395 glBegin(GL_LINE_LOOP); glVertex3f(-2,0,0); glVertex3f(2,0,0); glEnd();
396 glBegin(GL_LINE_LOOP); glVertex3f(0,0,-2); glVertex3f(0,0,2); glEnd();
400 if (sp->sphere_position == 0) /* FUDGE soo its not so obvious */
406 if (++sp->sphere_tick >= SPHERE_TICKS-1)
408 sp->sphere_tick = -(SPHERE_TICKS-2);
409 sp->sphere_position += 3;
410 sp->sphere_position += 3;
413 if (sp->sphere_position >= NPOSITIONS)
414 sp->sphere_position = 0;
418 reshape(ModeInfo * mi, int width, int height)
420 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
422 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
423 glMatrixMode(GL_PROJECTION);
425 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
426 glMatrixMode(GL_MODELVIEW);
430 } else if (width >= 512) {
443 glClearColor(0.0, 0.0, 0.0, 1.0);
445 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
446 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
447 glLightfv(GL_LIGHT0, GL_POSITION, position0);
448 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
449 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
450 glLightfv(GL_LIGHT1, GL_POSITION, position1);
451 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
452 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
453 glEnable(GL_LIGHTING);
456 glEnable(GL_NORMALIZE);
460 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
461 glShadeModel(GL_FLAT);
462 glEnable(GL_DEPTH_TEST);
463 glEnable(GL_TEXTURE_2D);
464 glEnable(GL_CULL_FACE);
466 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
467 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, WoodTextureWidth, WoodTextureHeight,
468 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
469 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
470 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
471 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
472 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
473 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
475 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
476 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
480 init_stairs(ModeInfo * mi)
482 int screen = MI_SCREEN(mi);
485 if (stairs == NULL) {
486 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
487 sizeof (stairsstruct))) == NULL)
490 sp = &stairs[screen];
492 sp->direction = LRAND() & 1;
493 sp->sphere_position = NRAND(NPOSITIONS / 3) * 3;
496 if ((sp->glx_context = init_GL(mi)) != NULL) {
498 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
499 glDrawBuffer(GL_BACK);
500 if (!glIsList(objects))
501 objects = glGenLists(1);
509 draw_stairs(ModeInfo * mi)
511 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
513 Display *display = MI_DISPLAY(mi);
514 Window window = MI_WINDOW(mi);
516 if (!sp->glx_context)
519 glXMakeCurrent(display, window, *(sp->glx_context));
521 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
525 glTranslatef(0.0, 0.0, -10.0);
527 if (!MI_IS_ICONIC(mi)) {
528 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
530 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
533 glRotatef(44.5, 1, 0, 0);
534 glRotatef(50 + ((sp->direction) ? 1 : -1) *
535 ((sp->step * 100 > 120) ? sp->step * 100 - 120 : 0), 0, 1, 0);
536 if (sp->step * 100 >= 360 + 120) { /* stop showing secrets */
538 sp->direction = LRAND() & 1;
540 draw_stairs_internal(mi);
546 glXSwapBuffers(display, window);
552 change_stairs(ModeInfo * mi)
554 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
556 if (!sp->glx_context)
559 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
564 release_stairs(ModeInfo * mi)
566 if (stairs != NULL) {
567 (void) free((void *) stairs);
570 if (glIsList(objects)) {
571 glDeleteLists(objects, 1);