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).
61 # define DEFAULTS "*delay: 20000 \n" \
63 # define refresh_stairs 0
64 # define stairs_handle_event 0
65 # include "xlockmore.h" /* from the xscreensaver distribution */
66 #else /* !STANDALONE */
67 # include "xlock.h" /* from the xlockmore distribution */
69 #endif /* !STANDALONE */
73 #include "e_textures.h"
75 ENTRYPOINT ModeSpecOpt stairs_opts =
76 {0, NULL, 0, NULL, NULL};
79 ModStruct stairs_description =
80 {"stairs", "init_stairs", "draw_stairs", "release_stairs",
81 "draw_stairs", "change_stairs", NULL, &stairs_opts,
82 1000, 1, 1, 1, 4, 1.0, "",
83 "Shows Infinite Stairs, an Escher-like scene", 0, NULL};
87 #define Scale4Window 0.3
88 #define Scale4Iconic 0.4
90 #define sqr(A) ((A)*(A))
96 /*************************************************************************/
102 int AreObjectsDefined[1];
105 GLXContext *glx_context;
109 static const float front_shininess[] = {60.0};
110 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
111 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
112 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
113 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
114 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
115 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
116 static const float lmodel_twoside[] = {GL_TRUE};
119 static const float MaterialRed[] = {0.7, 0.0, 0.0, 1.0};
120 static const float MaterialGreen[] = {0.1, 0.5, 0.2, 1.0};
121 static const float MaterialBlue[] = {0.0, 0.0, 0.7, 1.0};
122 static const float MaterialCyan[] = {0.2, 0.5, 0.7, 1.0};
123 static const float MaterialMagenta[] = {0.6, 0.2, 0.5, 1.0};
124 static const float MaterialGray[] = {0.2, 0.2, 0.2, 1.0};
125 static const float MaterialGray5[] = {0.5, 0.5, 0.5, 1.0};
126 static const float MaterialGray6[] = {0.6, 0.6, 0.6, 1.0};
127 static const float MaterialGray8[] = {0.8, 0.8, 0.8, 1.0};
130 static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
131 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
133 static const float positions[] =
135 -2.5, 4.0, 0.0, /* First one is FUDGED :) */
172 #define NPOSITIONS ((sizeof positions) / (sizeof positions[0]))
174 #define SPHERE_TICKS 8
176 static stairsstruct *stairs = NULL;
180 #define PlankWidth 3.0
181 #define PlankHeight 0.35
182 #define PlankThickness 0.15
185 mySphere(float radius)
187 GLUquadricObj *quadObj;
189 quadObj = gluNewQuadric();
190 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
191 gluSphere(quadObj, radius, 16, 16);
192 gluDeleteQuadric(quadObj);
196 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
201 glVertex3f(-width, -height, thickness);
203 glVertex3f(width, -height, thickness);
205 glVertex3f(width, height, thickness);
207 glVertex3f(-width, height, thickness);
208 glNormal3f(0, 0, -1);
210 glVertex3f(-width, height, -thickness);
212 glVertex3f(width, height, -thickness);
214 glVertex3f(width, -height, -thickness);
216 glVertex3f(-width, -height, -thickness);
219 glVertex3f(-width, height, thickness);
221 glVertex3f(width, height, thickness);
223 glVertex3f(width, height, -thickness);
225 glVertex3f(-width, height, -thickness);
226 glNormal3f(0, -1, 0);
228 glVertex3f(-width, -height, -thickness);
230 glVertex3f(width, -height, -thickness);
232 glVertex3f(width, -height, thickness);
234 glVertex3f(-width, -height, thickness);
237 glVertex3f(width, -height, thickness);
239 glVertex3f(width, -height, -thickness);
241 glVertex3f(width, height, -thickness);
243 glVertex3f(width, height, thickness);
244 glNormal3f(-1, 0, 0);
246 glVertex3f(-width, height, thickness);
248 glVertex3f(-width, height, -thickness);
250 glVertex3f(-width, -height, -thickness);
252 glVertex3f(-width, -height, thickness);
257 draw_stairs_internal(ModeInfo * mi)
259 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
264 glTranslatef(-3.0, 0.1, 2.0);
265 for (X = 0; X < 2; X++) {
266 draw_block(0.5, 2.7 + 0.1 * X, 0.5);
267 glTranslatef(0.0, 0.1, -1.0);
270 glTranslatef(-3.0, 0.0, 3.0);
273 for (X = 0; X < 6; X++) {
274 draw_block(0.5, 2.6 - 0.1 * X, 0.5);
275 glTranslatef(1.0, -0.1, 0.0);
277 glTranslatef(-1.0, -0.9, -1.0);
278 for (X = 0; X < 5; X++) {
279 draw_block(0.5, 3.0 - 0.1 * X, 0.5);
280 glTranslatef(0.0, 0.0, -1.0);
282 glTranslatef(-1.0, -1.1, 1.0);
283 for (X = 0; X < 3; X++) {
284 draw_block(0.5, 3.5 - 0.1 * X, 0.5);
285 glTranslatef(-1.0, -0.1, 0.0);
291 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
294 int pos = sp->sphere_position;
295 int ppos = sp->sphere_position - 3;
296 int npos = sp->sphere_position + 3;
297 GLfloat spx, spy, spz;
301 if (ppos < 0) ppos += NPOSITIONS;
302 if (npos >= NPOSITIONS) npos -= NPOSITIONS;
304 if (sp->sphere_tick < 0)
306 dx = positions[ppos] - positions[pos];
307 dy = positions[ppos+1] - positions[pos+1];
308 dz = positions[ppos+2] - positions[pos+2];
309 div = SPHERE_TICKS + sp->sphere_tick;
313 dx = positions[npos] - positions[pos];
314 dy = positions[npos+1] - positions[pos+1];
315 dz = positions[npos+2] - positions[ppos+2];
316 div = SPHERE_TICKS - sp->sphere_tick;
319 spx = positions[pos];
320 spy = positions[pos+1];
321 spz = positions[pos+2];
330 spy -= 0.5; /* move the bottom of the ball closer to the stairs */
334 fprintf(stderr, "%3d %3d %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f\n",
335 sp->sphere_position, sp->sphere_tick,
339 glBegin(GL_LINE_LOOP); /* path 1 */
340 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
341 glVertex3f(positions[npos], positions[npos+1], positions[npos+2]);
344 glBegin(GL_LINE_LOOP); /* path 2 */
345 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]);
346 glVertex3f(positions[ppos], positions[ppos+1], positions[ppos+2]);
349 glBegin(GL_LINE_LOOP); /* base origin */
350 glVertex3f(positions[pos], positions[pos+1]-10, positions[pos+2]);
351 glVertex3f(positions[pos], positions[pos+1]+10, positions[pos+2]);
354 glBegin(GL_LINE_LOOP); /* base origin */
355 glVertex3f(positions[pos]-10, positions[pos+1], positions[pos+2]);
356 glVertex3f(positions[pos]+10, positions[pos+1], positions[pos+2]);
359 glBegin(GL_LINE_LOOP); /* base origin */
360 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]-10);
361 glVertex3f(positions[pos], positions[pos+1], positions[pos+2]+10);
365 glTranslatef(spx, spy, spz);
367 #ifdef DEBUG /* ball origin */
368 glBegin(GL_LINE_LOOP); glVertex3f(0,-2,0); glVertex3f(0,2,0); glEnd();
369 glBegin(GL_LINE_LOOP); glVertex3f(-2,0,0); glVertex3f(2,0,0); glEnd();
370 glBegin(GL_LINE_LOOP); glVertex3f(0,0,-2); glVertex3f(0,0,2); glEnd();
374 if (sp->sphere_position == 0) /* FUDGE soo its not so obvious */
380 if (++sp->sphere_tick >= SPHERE_TICKS-1)
382 sp->sphere_tick = -(SPHERE_TICKS-2);
383 sp->sphere_position += 3;
384 sp->sphere_position += 3;
387 if (sp->sphere_position >= NPOSITIONS)
388 sp->sphere_position = 0;
392 reshape_stairs (ModeInfo * mi, int width, int height)
394 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
396 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
397 glMatrixMode(GL_PROJECTION);
399 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
400 glMatrixMode(GL_MODELVIEW);
404 } else if (width >= 512) {
418 glClearColor(0.0, 0.0, 0.0, 1.0);
420 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
421 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
422 glLightfv(GL_LIGHT0, GL_POSITION, position0);
423 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
424 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
425 glLightfv(GL_LIGHT1, GL_POSITION, position1);
426 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
427 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
428 glEnable(GL_LIGHTING);
431 glEnable(GL_NORMALIZE);
435 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
436 glShadeModel(GL_FLAT);
437 glEnable(GL_DEPTH_TEST);
438 glEnable(GL_TEXTURE_2D);
439 glEnable(GL_CULL_FACE);
441 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
444 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
445 WoodTextureWidth, WoodTextureHeight,
446 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
449 const char *s = (char *) gluErrorString (status);
450 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
451 progname, WoodTextureWidth, WoodTextureHeight,
452 (s ? s : "(unknown)"));
455 check_gl_error("mipmapping");
457 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
458 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
459 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
460 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
461 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
463 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
464 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
468 init_stairs (ModeInfo * mi)
470 int screen = MI_SCREEN(mi);
473 if (stairs == NULL) {
474 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
475 sizeof (stairsstruct))) == NULL)
478 sp = &stairs[screen];
481 /* make multiple screens rotate at slightly different rates. */
482 sp->step -= frand(5.0);
484 sp->direction = LRAND() & 1;
485 sp->sphere_position = NRAND(NPOSITIONS / 3) * 3;
488 if ((sp->glx_context = init_GL(mi)) != NULL) {
490 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
491 glDrawBuffer(GL_BACK);
492 if (!glIsList(sp->objects))
493 sp->objects = glGenLists(1);
501 draw_stairs (ModeInfo * mi)
503 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
505 Display *display = MI_DISPLAY(mi);
506 Window window = MI_WINDOW(mi);
508 if (!sp->glx_context)
511 glXMakeCurrent(display, window, *(sp->glx_context));
513 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
517 glTranslatef(0.0, 0.0, -10.0);
519 if (!MI_IS_ICONIC(mi)) {
520 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
522 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
525 glRotatef(44.5, 1, 0, 0);
526 glRotatef(50 + ((sp->direction) ? 1 : -1) *
527 ((sp->step * 100 > 120) ? sp->step * 100 - 120 : 0), 0, 1, 0);
528 if (sp->step * 100 >= 360 + 120) { /* stop showing secrets */
530 sp->direction = LRAND() & 1;
532 draw_stairs_internal(mi);
536 if (mi->fps_p) do_fps (mi);
539 glXSwapBuffers(display, window);
546 change_stairs (ModeInfo * mi)
548 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
550 if (!sp->glx_context)
553 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
556 #endif /* !STANDALONE */
559 release_stairs (ModeInfo * mi)
561 if (stairs != NULL) {
563 for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
564 stairsstruct *sp = &stairs[i];
565 if (glIsList(sp->objects)) {
566 glDeleteLists(sp->objects, 1);
575 XSCREENSAVER_MODULE ("Stairs", stairs)