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";
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
21 * This mode shows some interesting scenes that are impossible OR very
22 * weird to build in the real universe. Much of the scenes are inspirated
23 * on Mauritz Cornelis stairs's works which derivated the mode's name.
24 * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
25 * say he was a mathematician.
27 * Thanks goes to Brian Paul for making it possible and inexpensive to use
30 * Since I'm not a native English speaker, my apologies for any grammatical
33 * My e-mail address is
36 * Marcelo F. Vianna (Jun-01-1997)
39 * 07-Jan-98: This would be a scene for the escher mode, but now escher mode
40 * was splitted in different modes for each scene. This is the
41 * initial release and is not working yet.
47 * Texture mapping is only available on RGBA contexts, Mono and color index
48 * visuals DO NOT support texture mapping in OpenGL.
50 * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
51 * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
52 * is not officially supported for both OpenGL and Mesa, but seems to not crash
55 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
59 # define DEFAULTS "*delay: 20000 \n" \
62 # define refresh_stairs 0
63 # include "xlockmore.h" /* from the xscreensaver distribution */
64 #else /* !STANDALONE */
65 # include "xlock.h" /* from the xlockmore distribution */
67 #endif /* !STANDALONE */
71 #include "e_textures.h"
73 #include "gltrackball.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;
106 trackball_state *trackball;
111 static const float front_shininess[] = {60.0};
112 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
113 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
114 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
115 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
116 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
117 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
118 static const float lmodel_twoside[] = {GL_TRUE};
120 static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
121 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
123 static const float ball_positions[] = {
145 #define NPOSITIONS ((sizeof ball_positions) / (sizeof ball_positions[0]) / 3)
146 #define SPHERE_TICKS 32
148 static stairsstruct *stairs = NULL;
151 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
158 glVertex3f(-width, -height, thickness);
160 glVertex3f(width, -height, thickness);
162 glVertex3f(width, height, thickness);
164 glVertex3f(-width, height, thickness);
166 glNormal3f(0, 0, -1);
168 glVertex3f(-width, height, -thickness);
170 glVertex3f(width, height, -thickness);
172 glVertex3f(width, -height, -thickness);
174 glVertex3f(-width, -height, -thickness);
178 glVertex3f(-width, height, thickness);
180 glVertex3f(width, height, thickness);
182 glVertex3f(width, height, -thickness);
184 glVertex3f(-width, height, -thickness);
186 glNormal3f(0, -1, 0);
188 glVertex3f(-width, -height, -thickness);
190 glVertex3f(width, -height, -thickness);
192 glVertex3f(width, -height, thickness);
194 glVertex3f(-width, -height, thickness);
198 glVertex3f(width, -height, thickness);
200 glVertex3f(width, -height, -thickness);
202 glVertex3f(width, height, -thickness);
204 glVertex3f(width, height, thickness);
206 glNormal3f(-1, 0, 0);
208 glVertex3f(-width, height, thickness);
210 glVertex3f(-width, height, -thickness);
212 glVertex3f(-width, -height, -thickness);
214 glVertex3f(-width, -height, thickness);
221 draw_stairs_internal(ModeInfo * mi)
225 mi->polygon_count = 0;
229 glTranslatef(-3.0, 0.1, 2.0);
230 for (X = 0; X < 2; X++) {
231 mi->polygon_count += draw_block(0.5, 2.7 + 0.1 * X, 0.5);
232 glTranslatef(0.0, 0.1, -1.0);
235 glTranslatef(-3.0, 0.0, 3.0);
238 for (X = 0; X < 6; X++) {
239 mi->polygon_count += draw_block(0.5, 2.6 - 0.1 * X, 0.5);
240 glTranslatef(1.0, -0.1, 0.0);
242 glTranslatef(-1.0, -0.9, -1.0);
243 for (X = 0; X < 5; X++) {
244 mi->polygon_count += draw_block(0.5, 3.0 - 0.1 * X, 0.5);
245 glTranslatef(0.0, 0.0, -1.0);
247 glTranslatef(-1.0, -1.1, 1.0);
248 for (X = 0; X < 3; X++) {
249 mi->polygon_count += draw_block(0.5, 3.5 - 0.1 * X, 0.5);
250 glTranslatef(-1.0, -0.1, 0.0);
257 /*#define DEBUG_PATH*/
260 draw_sphere(int pos, int tick)
262 int pos2 = (pos+1) % NPOSITIONS;
263 GLfloat x1 = ball_positions[pos*3];
264 GLfloat y1 = ball_positions[pos*3+1];
265 GLfloat z1 = ball_positions[pos*3+2];
266 GLfloat x2 = ball_positions[pos2*3];
267 GLfloat y2 = ball_positions[pos2*3+1];
268 GLfloat z2 = ball_positions[pos2*3+2];
269 GLfloat frac = tick / (GLfloat) SPHERE_TICKS;
270 GLfloat x = x1 + (x2 - x1) * frac;
271 GLfloat y = y1 + (y2 - y1) * frac + (2 * sin (M_PI * frac));
272 GLfloat z = z1 + (z2 - z1) * frac;
280 glVertex3f(x, y-7.5, z);
281 glVertex3f(x, y, z); glVertex3f(x, y, z-0.6);
282 glVertex3f(x, y, z); glVertex3f(x, y, z+0.6);
283 glVertex3f(x, y, z); glVertex3f(x+0.6, y, z);
284 glVertex3f(x, y, z); glVertex3f(x-0.6, y, z);
288 # else /* !DEBUG_PATH */
290 glTranslatef(x, y, z);
292 glScalef (0.5, 0.5, 0.5);
294 /* make ball a little smaller on the gap to obscure distance */
295 if (pos == NPOSITIONS-1)
296 glScalef (0.95, 0.95, 0.95);
298 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
299 glDisable (GL_TEXTURE_2D);
300 glShadeModel(GL_SMOOTH);
301 polys += unit_sphere (32, 32, False);
302 glShadeModel(GL_FLAT);
303 glEnable (GL_TEXTURE_2D);
304 #endif /* !DEBUG_PATH */
313 reshape_stairs (ModeInfo * mi, int width, int height)
315 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
317 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
318 glMatrixMode(GL_PROJECTION);
320 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
321 glMatrixMode(GL_MODELVIEW);
325 } else if (width >= 512) {
335 stairs_handle_event (ModeInfo *mi, XEvent *event)
337 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
339 if (event->xany.type == ButtonPress &&
340 event->xbutton.button == Button1)
342 sp->button_down_p = True;
343 gltrackball_start (sp->trackball,
344 event->xbutton.x, event->xbutton.y,
345 MI_WIDTH (mi), MI_HEIGHT (mi));
348 else if (event->xany.type == ButtonRelease &&
349 event->xbutton.button == Button1)
351 sp->button_down_p = False;
354 else if (event->xany.type == ButtonPress &&
355 (event->xbutton.button == Button4 ||
356 event->xbutton.button == Button5 ||
357 event->xbutton.button == Button6 ||
358 event->xbutton.button == Button7))
360 gltrackball_mousewheel (sp->trackball, event->xbutton.button, 10,
361 !!event->xbutton.state);
364 else if (event->xany.type == MotionNotify &&
367 gltrackball_track (sp->trackball,
368 event->xmotion.x, event->xmotion.y,
369 MI_WIDTH (mi), MI_HEIGHT (mi));
372 else if (event->xany.type == KeyPress)
376 XLookupString (&event->xkey, &c, 1, &keysym, 0);
379 gltrackball_reset (sp->trackball);
393 glClearColor(0.0, 0.0, 0.0, 1.0);
395 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
396 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
397 glLightfv(GL_LIGHT0, GL_POSITION, position0);
398 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
399 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
400 glLightfv(GL_LIGHT1, GL_POSITION, position1);
401 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
402 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
403 glEnable(GL_LIGHTING);
406 glEnable(GL_NORMALIZE);
409 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
410 glShadeModel(GL_FLAT);
411 glEnable(GL_DEPTH_TEST);
412 glEnable(GL_TEXTURE_2D);
413 glEnable(GL_CULL_FACE);
415 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
418 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
419 WoodTextureWidth, WoodTextureHeight,
420 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
423 const char *s = (char *) gluErrorString (status);
424 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
425 progname, WoodTextureWidth, WoodTextureHeight,
426 (s ? s : "(unknown)"));
429 check_gl_error("mipmapping");
431 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
432 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
433 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
434 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
435 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
437 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
438 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
442 init_stairs (ModeInfo * mi)
444 int screen = MI_SCREEN(mi);
447 if (stairs == NULL) {
448 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
449 sizeof (stairsstruct))) == NULL)
452 sp = &stairs[screen];
456 sp->sphere_position = NRAND(NPOSITIONS);
459 if ((sp->glx_context = init_GL(mi)) != NULL) {
461 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
462 glDrawBuffer(GL_BACK);
463 if (!glIsList(sp->objects))
464 sp->objects = glGenLists(1);
470 sp->trackball = gltrackball_init ();
474 draw_stairs (ModeInfo * mi)
476 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
478 Display *display = MI_DISPLAY(mi);
479 Window window = MI_WINDOW(mi);
481 if (!sp->glx_context)
484 glXMakeCurrent(display, window, *(sp->glx_context));
486 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
490 glTranslatef(0.0, 0.0, -10.0);
492 if (!MI_IS_ICONIC(mi)) {
493 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
495 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
498 gltrackball_rotate (sp->trackball);
500 glTranslatef(0, 0.5, 0);
501 glRotatef(44.5, 1, 0, 0);
502 glRotatef(50, 0, 1, 0);
505 if ((LRAND() % 500) == 0)
506 sp->rotating = (LRAND() & 1) ? 1 : -1;
510 glRotatef(sp->rotating * sp->step, 0, 1, 0);
511 if (sp->step >= 360) {
517 if (!sp->button_down_p)
522 draw_stairs_internal(mi);
529 glDisable(GL_LIGHTING);
530 glDisable(GL_TEXTURE_2D);
531 glBegin (GL_LINE_LOOP);
532 # endif /* DEBUG_PATH */
533 for (i = 0; i < NPOSITIONS; i ++)
534 for (j = 0; j < SPHERE_TICKS; j++)
535 mi->polygon_count += draw_sphere(i, j);
538 glEnable(GL_LIGHTING);
539 glEnable(GL_TEXTURE_2D);
540 # endif /* DEBUG_PATH */
543 mi->polygon_count += draw_sphere(sp->sphere_position, sp->sphere_tick);
546 if (sp->button_down_p)
548 else if (++sp->sphere_tick >= SPHERE_TICKS)
551 if (++sp->sphere_position >= NPOSITIONS)
552 sp->sphere_position = 0;
557 if (mi->fps_p) do_fps (mi);
560 glXSwapBuffers(display, window);
565 change_stairs (ModeInfo * mi)
567 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
569 if (!sp->glx_context)
572 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
575 #endif /* !STANDALONE */
578 release_stairs (ModeInfo * mi)
580 if (stairs != NULL) {
582 for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
583 stairsstruct *sp = &stairs[i];
584 if (glIsList(sp->objects)) {
585 glDeleteLists(sp->objects, 1);
594 XSCREENSAVER_MODULE ("Stairs", stairs)