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 */
72 #include "e_textures.h"
74 #include "xpm-ximage.h"
75 #include "../images/wood.xpm"
79 #include "gltrackball.h"
81 ENTRYPOINT 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;
112 trackball_state *trackball;
117 static const float front_shininess[] = {60.0};
118 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
119 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
120 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
121 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
122 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
123 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
124 static const float lmodel_twoside[] = {GL_TRUE};
126 static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
127 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
129 static const float ball_positions[] = {
151 #define NPOSITIONS ((sizeof ball_positions) / (sizeof ball_positions[0]) / 3)
152 #define SPHERE_TICKS 32
154 static stairsstruct *stairs = NULL;
157 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
164 glVertex3f(-width, -height, thickness);
166 glVertex3f(width, -height, thickness);
168 glVertex3f(width, height, thickness);
170 glVertex3f(-width, height, thickness);
172 glNormal3f(0, 0, -1);
174 glVertex3f(-width, height, -thickness);
176 glVertex3f(width, height, -thickness);
178 glVertex3f(width, -height, -thickness);
180 glVertex3f(-width, -height, -thickness);
184 glVertex3f(-width, height, thickness);
186 glVertex3f(width, height, thickness);
188 glVertex3f(width, height, -thickness);
190 glVertex3f(-width, height, -thickness);
192 glNormal3f(0, -1, 0);
194 glVertex3f(-width, -height, -thickness);
196 glVertex3f(width, -height, -thickness);
198 glVertex3f(width, -height, thickness);
200 glVertex3f(-width, -height, thickness);
204 glVertex3f(width, -height, thickness);
206 glVertex3f(width, -height, -thickness);
208 glVertex3f(width, height, -thickness);
210 glVertex3f(width, height, thickness);
212 glNormal3f(-1, 0, 0);
214 glVertex3f(-width, height, thickness);
216 glVertex3f(-width, height, -thickness);
218 glVertex3f(-width, -height, -thickness);
220 glVertex3f(-width, -height, thickness);
227 draw_stairs_internal(ModeInfo * mi)
231 mi->polygon_count = 0;
235 glTranslatef(-3.0, 0.1, 2.0);
236 for (X = 0; X < 2; X++) {
237 mi->polygon_count += draw_block(0.5, 2.7 + 0.1 * X, 0.5);
238 glTranslatef(0.0, 0.1, -1.0);
241 glTranslatef(-3.0, 0.0, 3.0);
244 for (X = 0; X < 6; X++) {
245 mi->polygon_count += draw_block(0.5, 2.6 - 0.1 * X, 0.5);
246 glTranslatef(1.0, -0.1, 0.0);
248 glTranslatef(-1.0, -0.9, -1.0);
249 for (X = 0; X < 5; X++) {
250 mi->polygon_count += draw_block(0.5, 3.0 - 0.1 * X, 0.5);
251 glTranslatef(0.0, 0.0, -1.0);
253 glTranslatef(-1.0, -1.1, 1.0);
254 for (X = 0; X < 3; X++) {
255 mi->polygon_count += draw_block(0.5, 3.5 - 0.1 * X, 0.5);
256 glTranslatef(-1.0, -0.1, 0.0);
263 /*#define DEBUG_PATH*/
266 draw_sphere(int pos, int tick)
268 int pos2 = (pos+1) % NPOSITIONS;
269 GLfloat x1 = ball_positions[pos*3];
270 GLfloat y1 = ball_positions[pos*3+1];
271 GLfloat z1 = ball_positions[pos*3+2];
272 GLfloat x2 = ball_positions[pos2*3];
273 GLfloat y2 = ball_positions[pos2*3+1];
274 GLfloat z2 = ball_positions[pos2*3+2];
275 GLfloat frac = tick / (GLfloat) SPHERE_TICKS;
276 GLfloat x = x1 + (x2 - x1) * frac;
277 GLfloat y = y1 + (y2 - y1) * frac + (2 * sin (M_PI * frac));
278 GLfloat z = z1 + (z2 - z1) * frac;
286 glVertex3f(x, y-7.5, z);
287 glVertex3f(x, y, z); glVertex3f(x, y, z-0.6);
288 glVertex3f(x, y, z); glVertex3f(x, y, z+0.6);
289 glVertex3f(x, y, z); glVertex3f(x+0.6, y, z);
290 glVertex3f(x, y, z); glVertex3f(x-0.6, y, z);
294 # else /* !DEBUG_PATH */
296 glTranslatef(x, y, z);
298 glScalef (0.5, 0.5, 0.5);
300 /* make ball a little smaller on the gap to obscure distance */
301 if (pos == NPOSITIONS-1)
302 glScalef (0.95, 0.95, 0.95);
304 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
305 glDisable (GL_TEXTURE_2D);
306 glShadeModel(GL_SMOOTH);
308 polys += unit_sphere (32, 32, False);
309 glShadeModel(GL_FLAT);
310 glEnable (GL_TEXTURE_2D);
311 #endif /* !DEBUG_PATH */
320 reshape_stairs (ModeInfo * mi, int width, int height)
322 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
324 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
325 glMatrixMode(GL_PROJECTION);
327 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
328 glMatrixMode(GL_MODELVIEW);
332 } else if (width >= 512) {
342 stairs_handle_event (ModeInfo *mi, XEvent *event)
344 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
346 if (gltrackball_event_handler (event, sp->trackball,
347 MI_WIDTH (mi), MI_HEIGHT (mi),
350 else if (event->xany.type == KeyPress)
354 XLookupString (&event->xkey, &c, 1, &keysym, 0);
355 if (c == ' ' || c == '\t')
357 gltrackball_reset (sp->trackball);
371 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
372 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
373 glLightfv(GL_LIGHT0, GL_POSITION, position0);
374 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
375 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
376 glLightfv(GL_LIGHT1, GL_POSITION, position1);
377 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
378 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
379 glEnable(GL_LIGHTING);
382 glEnable(GL_NORMALIZE);
385 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
386 glShadeModel(GL_FLAT);
387 glEnable(GL_DEPTH_TEST);
388 glEnable(GL_TEXTURE_2D);
389 glEnable(GL_CULL_FACE);
391 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
395 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
396 WoodTextureWidth, WoodTextureHeight,
397 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
400 const char *s = (char *) gluErrorString (status);
401 fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
402 progname, WoodTextureWidth, WoodTextureHeight,
403 (s ? s : "(unknown)"));
406 check_gl_error("mipmapping");
409 XImage *img = xpm_to_ximage (mi->dpy,
413 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
414 img->width, img->height, 0,
416 /* GL_UNSIGNED_BYTE, */
417 GL_UNSIGNED_INT_8_8_8_8_REV,
419 check_gl_error("texture");
424 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
425 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
426 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
427 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
428 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
430 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
431 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
435 init_stairs (ModeInfo * mi)
437 int screen = MI_SCREEN(mi);
440 if (stairs == NULL) {
441 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
442 sizeof (stairsstruct))) == NULL)
445 sp = &stairs[screen];
449 sp->sphere_position = NRAND(NPOSITIONS);
452 if ((sp->glx_context = init_GL(mi)) != NULL) {
454 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
455 glDrawBuffer(GL_BACK);
456 if (!glIsList(sp->objects))
457 sp->objects = glGenLists(1);
463 sp->trackball = gltrackball_init (False);
467 draw_stairs (ModeInfo * mi)
469 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
470 GLfloat rot = current_device_rotation();
472 Display *display = MI_DISPLAY(mi);
473 Window window = MI_WINDOW(mi);
475 if (!sp->glx_context)
478 glXMakeCurrent(display, window, *(sp->glx_context));
480 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
484 glRotatef(rot, 0, 0, 1);
485 if ((rot > 45 && rot < 135) ||
486 (rot < -45 && rot > -135))
488 GLfloat s = MI_WIDTH(mi) / (GLfloat) MI_HEIGHT(mi);
489 glScalef (s, 1/s, 1);
492 glTranslatef(0.0, 0.0, -10.0);
494 if (!MI_IS_ICONIC(mi)) {
495 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
497 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
500 gltrackball_rotate (sp->trackball);
502 glTranslatef(0, 0.5, 0);
503 glRotatef(44.5, 1, 0, 0);
504 glRotatef(50, 0, 1, 0);
507 if ((LRAND() % 500) == 0)
508 sp->rotating = (LRAND() & 1) ? 1 : -1;
512 glRotatef(sp->rotating * sp->step, 0, 1, 0);
513 if (sp->step >= 360) {
519 if (!sp->button_down_p)
524 draw_stairs_internal(mi);
531 glDisable(GL_LIGHTING);
532 glDisable(GL_TEXTURE_2D);
533 glBegin (GL_LINE_LOOP);
534 # endif /* DEBUG_PATH */
535 for (i = 0; i < NPOSITIONS; i ++)
536 for (j = 0; j < SPHERE_TICKS; j++)
537 mi->polygon_count += draw_sphere(i, j);
540 glEnable(GL_LIGHTING);
541 glEnable(GL_TEXTURE_2D);
542 # endif /* DEBUG_PATH */
545 mi->polygon_count += draw_sphere(sp->sphere_position, sp->sphere_tick);
548 if (sp->button_down_p)
550 else if (++sp->sphere_tick >= SPHERE_TICKS)
553 if (++sp->sphere_position >= NPOSITIONS)
554 sp->sphere_position = 0;
559 if (mi->fps_p) do_fps (mi);
562 glXSwapBuffers(display, window);
567 change_stairs (ModeInfo * mi)
569 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
571 if (!sp->glx_context)
574 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
577 #endif /* !STANDALONE */
580 release_stairs (ModeInfo * mi)
582 if (stairs != NULL) {
584 for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
585 stairsstruct *sp = &stairs[i];
586 if (glIsList(sp->objects)) {
587 glDeleteLists(sp->objects, 1);
596 XSCREENSAVER_MODULE ("Stairs", stairs)