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 addresses are
41 * Marcelo F. Vianna (Jun-01-1997)
44 * 07-Jan-98: This would be a scene for the escher mode, but now escher mode
45 * was splitted in different modes for each scene. This is the
46 * initial release and is not working yet.
52 * Texture mapping is only available on RGBA contexts, Mono and color index
53 * visuals DO NOT support texture mapping in OpenGL.
55 * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
56 * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
57 * is not officially supported for both OpenGL and Mesa, but seems to not crash
60 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
63 #include <X11/Intrinsic.h>
66 # define PROGCLASS "Stairs"
67 # define HACK_INIT init_stairs
68 # define HACK_DRAW draw_stairs
69 # define stairs_opts xlockmore_opts
70 # define DEFAULTS "*cycles: 1 \n" \
72 "*wireframe: False \n"
73 # include "xlockmore.h" /* from the xscreensaver distribution */
74 #else /* !STANDALONE */
75 # include "xlock.h" /* from the xlockmore distribution */
77 #endif /* !STANDALONE */
82 #include "e_textures.h"
84 ModeSpecOpt stairs_opts =
85 {0, NULL, 0, NULL, NULL};
88 ModStruct stairs_description =
89 {"stairs", "init_stairs", "draw_stairs", "release_stairs",
90 "draw_stairs", "change_stairs", NULL, &stairs_opts,
91 1000, 1, 1, 1, 4, 1.0, "",
92 "Shows Infinite Stairs, an Escher-like scene", 0, NULL};
96 #define Scale4Window 0.3
97 #define Scale4Iconic 0.4
99 #define sqr(A) ((A)*(A))
105 /*************************************************************************/
111 int AreObjectsDefined[1];
113 GLXContext *glx_context;
116 static float front_shininess[] =
118 static float front_specular[] =
119 {0.7, 0.7, 0.7, 1.0};
120 static float ambient[] =
121 {0.0, 0.0, 0.0, 1.0};
122 static float diffuse[] =
123 {1.0, 1.0, 1.0, 1.0};
124 static float position0[] =
125 {1.0, 1.0, 1.0, 0.0};
126 static float position1[] =
127 {-1.0, -1.0, 1.0, 0.0};
128 static float lmodel_ambient[] =
129 {0.5, 0.5, 0.5, 1.0};
130 static float lmodel_twoside[] =
134 static float MaterialRed[] =
135 {0.7, 0.0, 0.0, 1.0};
136 static float MaterialGreen[] =
137 {0.1, 0.5, 0.2, 1.0};
138 static float MaterialBlue[] =
139 {0.0, 0.0, 0.7, 1.0};
140 static float MaterialCyan[] =
141 {0.2, 0.5, 0.7, 1.0};
142 static float MaterialMagenta[] =
143 {0.6, 0.2, 0.5, 1.0};
144 static float MaterialGray[] =
145 {0.2, 0.2, 0.2, 1.0};
146 static float MaterialGray5[] =
147 {0.5, 0.5, 0.5, 1.0};
148 static float MaterialGray6[] =
149 {0.6, 0.6, 0.6, 1.0};
150 static float MaterialGray8[] =
151 {0.8, 0.8, 0.8, 1.0};
154 static float MaterialYellow[] =
155 {0.7, 0.7, 0.0, 1.0};
156 static float MaterialWhite[] =
157 {0.7, 0.7, 0.7, 1.0};
159 static float positions[] =
161 -2.5, 4.0, 0.0, /* First one is FUDGED :) */
198 #define NPOSITIONS ((sizeof positions) / (sizeof positions[0]))
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);
318 glTranslatef((GLfloat) positions[sp->sphere_position],
319 (GLfloat) positions[sp->sphere_position + 1],
320 (GLfloat) positions[sp->sphere_position + 2]);
321 if (sp->sphere_position == 0) /* FUDGE soo its not so obvious */
326 sp->sphere_position += 3;
327 if (sp->sphere_position >= NPOSITIONS)
328 sp->sphere_position = 0;
332 reshape(ModeInfo * mi, int width, int height)
334 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
336 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
337 glMatrixMode(GL_PROJECTION);
339 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
340 glMatrixMode(GL_MODELVIEW);
344 } else if (width >= 512) {
357 glClearColor(0.0, 0.0, 0.0, 1.0);
359 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
360 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
361 glLightfv(GL_LIGHT0, GL_POSITION, position0);
362 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
363 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
364 glLightfv(GL_LIGHT1, GL_POSITION, position1);
365 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
366 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
367 glEnable(GL_LIGHTING);
370 glEnable(GL_NORMALIZE);
374 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
375 glShadeModel(GL_FLAT);
376 glEnable(GL_DEPTH_TEST);
377 glEnable(GL_TEXTURE_2D);
378 glEnable(GL_CULL_FACE);
380 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
381 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, WoodTextureWidth, WoodTextureHeight,
382 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
383 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
384 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
385 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
386 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
387 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
389 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
390 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
394 init_stairs(ModeInfo * mi)
396 int screen = MI_SCREEN(mi);
399 if (stairs == NULL) {
400 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
401 sizeof (stairsstruct))) == NULL)
404 sp = &stairs[screen];
406 sp->direction = LRAND() & 1;
407 sp->sphere_position = NRAND(NPOSITIONS / 3) * 3;
409 if ((sp->glx_context = init_GL(mi)) != NULL) {
411 reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
412 glDrawBuffer(GL_BACK);
413 if (!glIsList(objects))
414 objects = glGenLists(1);
422 draw_stairs(ModeInfo * mi)
424 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
426 Display *display = MI_DISPLAY(mi);
427 Window window = MI_WINDOW(mi);
429 if (!sp->glx_context)
432 glXMakeCurrent(display, window, *(sp->glx_context));
434 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
438 glTranslatef(0.0, 0.0, -10.0);
440 if (!MI_IS_ICONIC(mi)) {
441 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
443 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
446 glRotatef(44.5, 1, 0, 0);
447 glRotatef(50 + ((sp->direction) ? 1 : -1) *
448 ((sp->step * 100 > 120) ? sp->step * 100 - 120 : 0), 0, 1, 0);
449 if (sp->step * 100 >= 360 + 120) { /* stop showing secrets */
451 sp->direction = LRAND() & 1;
453 draw_stairs_internal(mi);
459 glXSwapBuffers(display, window);
465 change_stairs(ModeInfo * mi)
467 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
469 if (!sp->glx_context)
472 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
477 release_stairs(ModeInfo * mi)
479 if (stairs != NULL) {
480 (void) free((void *) stairs);
483 if (glIsList(objects)) {
484 glDeleteLists(objects, 1);