1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* stairs --- Infinite Stairs, and Escher-like scene. */
5 static const char sccsid[] = "@(#)stairs.c 4.07 97/11/24 xlockmore";
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
23 * This mode shows some interesting scenes that are impossible OR very
24 * weird to build in the real universe. Much of the scenes are inspirated
25 * on Mauritz Cornelis stairs's works which derivated the mode's name.
26 * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
27 * say he was a mathematician.
29 * Thanks goes to Brian Paul for making it possible and inexpensive to use
32 * Since I'm not a native English speaker, my apologies for any grammatical
35 * My e-mail address is
38 * Marcelo F. Vianna (Jun-01-1997)
41 * 07-Jan-98: This would be a scene for the escher mode, but now escher mode
42 * was splitted in different modes for each scene. This is the
43 * initial release and is not working yet.
49 * Texture mapping is only available on RGBA contexts, Mono and color index
50 * visuals DO NOT support texture mapping in OpenGL.
52 * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
53 * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
54 * is not officially supported for both OpenGL and Mesa, but seems to not crash
57 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
60 #include <X11/Intrinsic.h>
63 # define PROGCLASS "Stairs"
64 # define HACK_INIT init_stairs
65 # define HACK_DRAW draw_stairs
66 # define HACK_RESHAPE reshape_stairs
67 # define stairs_opts xlockmore_opts
68 # define DEFAULTS "*delay: 20000 \n" \
70 # include "xlockmore.h" /* from the xscreensaver distribution */
71 #else /* !STANDALONE */
72 # include "xlock.h" /* from the xlockmore distribution */
74 #endif /* !STANDALONE */
79 #include "e_textures.h"
81 ModeSpecOpt stairs_opts =
82 {0, NULL, 0, NULL, NULL};
85 ModStruct stairs_description =
86 {"stairs", "init_stairs", "draw_stairs", "release_stairs",
87 "draw_stairs", "change_stairs", NULL, &stairs_opts,
88 1000, 1, 1, 1, 4, 1.0, "",
89 "Shows Infinite Stairs, an Escher-like scene", 0, NULL};
93 #define Scale4Window 0.3
94 #define Scale4Iconic 0.4
96 #define sqr(A) ((A)*(A))
102 /*************************************************************************/
108 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 #define SPHERE_TICKS 8
200 static stairsstruct *stairs = NULL;
201 static GLuint objects;
205 #define PlankWidth 3.0
206 #define PlankHeight 0.35
207 #define PlankThickness 0.15
210 mySphere(float radius)
212 GLUquadricObj *quadObj;
214 quadObj = gluNewQuadric();
215 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
216 gluSphere(quadObj, radius, 16, 16);
217 gluDeleteQuadric(quadObj);
221 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
226 glVertex3f(-width, -height, thickness);
228 glVertex3f(width, -height, thickness);
230 glVertex3f(width, height, thickness);
232 glVertex3f(-width, height, thickness);
233 glNormal3f(0, 0, -1);
235 glVertex3f(-width, height, -thickness);
237 glVertex3f(width, height, -thickness);
239 glVertex3f(width, -height, -thickness);
241 glVertex3f(-width, -height, -thickness);
244 glVertex3f(-width, height, thickness);
246 glVertex3f(width, height, thickness);
248 glVertex3f(width, height, -thickness);
250 glVertex3f(-width, height, -thickness);
251 glNormal3f(0, -1, 0);
253 glVertex3f(-width, -height, -thickness);
255 glVertex3f(width, -height, -thickness);
257 glVertex3f(width, -height, thickness);
259 glVertex3f(-width, -height, thickness);
262 glVertex3f(width, -height, thickness);
264 glVertex3f(width, -height, -thickness);
266 glVertex3f(width, height, -thickness);
268 glVertex3f(width, height, thickness);
269 glNormal3f(-1, 0, 0);
271 glVertex3f(-width, height, thickness);
273 glVertex3f(-width, height, -thickness);
275 glVertex3f(-width, -height, -thickness);
277 glVertex3f(-width, -height, thickness);
282 draw_stairs_internal(ModeInfo * mi)
284 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
289 glTranslatef(-3.0, 0.1, 2.0);
290 for (X = 0; X < 2; X++) {
291 draw_block(0.5, 2.7 + 0.1 * X, 0.5);
292 glTranslatef(0.0, 0.1, -1.0);
295 glTranslatef(-3.0, 0.0, 3.0);
298 for (X = 0; X < 6; X++) {
299 draw_block(0.5, 2.6 - 0.1 * X, 0.5);
300 glTranslatef(1.0, -0.1, 0.0);
302 glTranslatef(-1.0, -0.9, -1.0);
303 for (X = 0; X < 5; X++) {
304 draw_block(0.5, 3.0 - 0.1 * X, 0.5);
305 glTranslatef(0.0, 0.0, -1.0);
307 glTranslatef(-1.0, -1.1, 1.0);
308 for (X = 0; X < 3; X++) {
309 draw_block(0.5, 3.5 - 0.1 * X, 0.5);
310 glTranslatef(-1.0, -0.1, 0.0);
316 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
319 int pos = sp->sphere_position;
320 int ppos = sp->sphere_position - 3;
321 int npos = sp->sphere_position + 3;
322 GLfloat spx, spy, spz;
326 if (ppos < 0) ppos += NPOSITIONS;
327 if (npos >= NPOSITIONS) npos -= NPOSITIONS;
329 if (sp->sphere_tick < 0)
331 dx = positions[ppos] - positions[pos];
332 dy = positions[ppos+1] - positions[pos+1];
333 dz = positions[ppos+2] - positions[pos+2];
334 div = SPHERE_TICKS + sp->sphere_tick;
338 dx = positions[npos] - positions[pos];
339 dy = positions[npos+1] - positions[pos+1];
340 dz = positions[npos+2] - positions[ppos+2];
341 div = SPHERE_TICKS - sp->sphere_tick;
344 spx = positions[pos];
345 spy = positions[pos+1];
346 spz = positions[pos+2];
355 spy -= 0.5; /* move the bottom of the ball closer to the stairs */
359 fprintf(stderr, "%3d %3d %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f\n",
360 sp->sphere_position, sp->sphere_tick,
364 glBegin(GL_LINE_LOOP); /* path 1 */
365 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
366 glVertex3f(positions[npos], positions[npos+1], positions[npos+2]);
369 glBegin(GL_LINE_LOOP); /* path 2 */
370 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
371 glVertex3f(positions[ppos], positions[ppos+1], positions[ppos+2]);
374 glBegin(GL_LINE_LOOP); /* base origin */
375 glVertex3f(positions[pos], positions[pos+1]-10, positions[pos+2]);
376 glVertex3f(positions[pos], positions[pos+1]+10, positions[pos+2]);
379 glBegin(GL_LINE_LOOP); /* base origin */
380 glVertex3f(positions[pos]-10, positions[pos+1], positions[pos+2]);
381 glVertex3f(positions[pos]+10, positions[pos+1], positions[pos+2]);
384 glBegin(GL_LINE_LOOP); /* base origin */
385 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]-10);
386 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]+10);
390 glTranslatef(spx, spy, spz);
392 #ifdef DEBUG /* ball origin */
393 glBegin(GL_LINE_LOOP); glVertex3f(0,-2,0); glVertex3f(0,2,0); glEnd();
394 glBegin(GL_LINE_LOOP); glVertex3f(-2,0,0); glVertex3f(2,0,0); glEnd();
395 glBegin(GL_LINE_LOOP); glVertex3f(0,0,-2); glVertex3f(0,0,2); glEnd();
399 if (sp->sphere_position == 0) /* FUDGE soo its not so obvious */
405 if (++sp->sphere_tick >= SPHERE_TICKS-1)
407 sp->sphere_tick = -(SPHERE_TICKS-2);
408 sp->sphere_position += 3;
409 sp->sphere_position += 3;
412 if (sp->sphere_position >= NPOSITIONS)
413 sp->sphere_position = 0;
417 reshape_stairs(ModeInfo * mi, int width, int height)
419 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
421 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
422 glMatrixMode(GL_PROJECTION);
424 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
425 glMatrixMode(GL_MODELVIEW);
429 } 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);
469 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
470 WoodTextureWidth, WoodTextureHeight,
471 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
474 const char *s = (char *) gluErrorString (status);
475 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
476 progname, WoodTextureWidth, WoodTextureHeight,
477 (s ? s : "(unknown)"));
480 check_gl_error("mipmapping");
482 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
483 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
484 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
485 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
486 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
488 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
489 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
493 init_stairs(ModeInfo * mi)
495 int screen = MI_SCREEN(mi);
498 if (stairs == NULL) {
499 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
500 sizeof (stairsstruct))) == NULL)
503 sp = &stairs[screen];
505 sp->direction = LRAND() & 1;
506 sp->sphere_position = NRAND(NPOSITIONS / 3) * 3;
509 if ((sp->glx_context = init_GL(mi)) != NULL) {
511 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
512 glDrawBuffer(GL_BACK);
513 if (!glIsList(objects))
514 objects = glGenLists(1);
522 draw_stairs(ModeInfo * mi)
524 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
526 Display *display = MI_DISPLAY(mi);
527 Window window = MI_WINDOW(mi);
529 if (!sp->glx_context)
532 glXMakeCurrent(display, window, *(sp->glx_context));
534 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
538 glTranslatef(0.0, 0.0, -10.0);
540 if (!MI_IS_ICONIC(mi)) {
541 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
543 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
546 glRotatef(44.5, 1, 0, 0);
547 glRotatef(50 + ((sp->direction) ? 1 : -1) *
548 ((sp->step * 100 > 120) ? sp->step * 100 - 120 : 0), 0, 1, 0);
549 if (sp->step * 100 >= 360 + 120) { /* stop showing secrets */
551 sp->direction = LRAND() & 1;
553 draw_stairs_internal(mi);
557 if (mi->fps_p) do_fps (mi);
560 glXSwapBuffers(display, window);
566 change_stairs(ModeInfo * mi)
568 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
570 if (!sp->glx_context)
573 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
578 release_stairs(ModeInfo * mi)
580 if (stairs != NULL) {
581 (void) free((void *) stairs);
584 if (glIsList(objects)) {
585 glDeleteLists(objects, 1);