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, 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};
153 static float MaterialYellow[] =
154 {0.7, 0.7, 0.0, 1.0};
155 static float MaterialWhite[] =
156 {0.7, 0.7, 0.7, 1.0};
158 static float positions[] =
160 -2.5, 4.0, 0.0, /* First one is FUDGED :) */
196 #define NPOSITIONS ((sizeof positions) / (sizeof positions[0]))
198 static stairsstruct *stairs = NULL;
199 static GLuint objects;
203 #define PlankWidth 3.0
204 #define PlankHeight 0.35
205 #define PlankThickness 0.15
208 mySphere(float radius)
210 GLUquadricObj *quadObj;
212 quadObj = gluNewQuadric();
213 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
214 gluSphere(quadObj, radius, 16, 16);
215 gluDeleteQuadric(quadObj);
219 draw_block(stairsstruct * sp, GLfloat width, GLfloat height, GLfloat thickness)
224 glVertex3f(-width, -height, thickness);
226 glVertex3f(width, -height, thickness);
228 glVertex3f(width, height, thickness);
230 glVertex3f(-width, height, thickness);
231 glNormal3f(0, 0, -1);
233 glVertex3f(-width, height, -thickness);
235 glVertex3f(width, height, -thickness);
237 glVertex3f(width, -height, -thickness);
239 glVertex3f(-width, -height, -thickness);
242 glVertex3f(-width, height, thickness);
244 glVertex3f(width, height, thickness);
246 glVertex3f(width, height, -thickness);
248 glVertex3f(-width, height, -thickness);
249 glNormal3f(0, -1, 0);
251 glVertex3f(-width, -height, -thickness);
253 glVertex3f(width, -height, -thickness);
255 glVertex3f(width, -height, thickness);
257 glVertex3f(-width, -height, thickness);
260 glVertex3f(width, -height, thickness);
262 glVertex3f(width, -height, -thickness);
264 glVertex3f(width, height, -thickness);
266 glVertex3f(width, height, thickness);
267 glNormal3f(-1, 0, 0);
269 glVertex3f(-width, height, thickness);
271 glVertex3f(-width, height, -thickness);
273 glVertex3f(-width, -height, -thickness);
275 glVertex3f(-width, -height, thickness);
280 draw_degree(stairsstruct * sp, GLfloat w, GLfloat h , GLfloat t)
282 draw_block(sp, w, h, t);
286 draw_stairs_internal(ModeInfo *mi)
288 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
293 glTranslatef(-3.0, 0.1, 2.0);
294 for (X=0; X< 2; X++) {
295 draw_degree(sp, 0.5, 2.7+0.1*X, 0.5);
296 glTranslatef( 0.0, 0.1,-1.0);
299 glTranslatef(-3.0, 0.0, 3.0);
302 for (X=0; X< 6; X++) {
303 draw_degree(sp, 0.5, 2.6-0.1*X, 0.5);
304 glTranslatef( 1.0,-0.1, 0.0);
306 glTranslatef(-1.0,-0.9,-1.0);
307 for (X=0; X< 5; X++) {
308 draw_degree(sp, 0.5, 3.0-0.1*X, 0.5);
309 glTranslatef( 0.0, 0.0,-1.0);
311 glTranslatef(-1.0,-1.1, 1.0);
312 for (X=0; X< 3; X++) {
313 draw_degree(sp, 0.5, 3.5-0.1*X, 0.5);
314 glTranslatef(-1.0,-0.1, 0.0);
320 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
322 glTranslatef((GLfloat) positions[sp->sphere_position],
323 (GLfloat) positions[sp->sphere_position + 1],
324 (GLfloat) positions[sp->sphere_position + 2]);
325 if (sp->sphere_position == 0) /* FUDGE soo its not so obvious */
330 sp->sphere_position += 3;
331 if (sp->sphere_position >= NPOSITIONS)
332 sp->sphere_position = 0;
336 reshape(ModeInfo * mi, int width, int height)
338 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
340 glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
341 glMatrixMode(GL_PROJECTION);
343 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
344 glMatrixMode(GL_MODELVIEW);
348 } else if (width >= 512) {
360 /* stairsstruct *sp = &stairs[MI_SCREEN(mi)];*/
363 glClearColor(0.0, 0.0, 0.0, 1.0);
365 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
366 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
367 glLightfv(GL_LIGHT0, GL_POSITION, position0);
368 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
369 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
370 glLightfv(GL_LIGHT1, GL_POSITION, position1);
371 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
372 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
373 glEnable(GL_LIGHTING);
376 glEnable(GL_NORMALIZE);
380 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
381 glShadeModel(GL_FLAT);
382 glEnable(GL_DEPTH_TEST);
383 glEnable(GL_TEXTURE_2D);
384 glEnable(GL_CULL_FACE);
386 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
387 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, WoodTextureWidth, WoodTextureHeight,
388 GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
389 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
390 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
391 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
392 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
393 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
395 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
396 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
400 init_stairs(ModeInfo * mi)
402 int screen = MI_SCREEN(mi);
405 if (stairs == NULL) {
406 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
407 sizeof (stairsstruct))) == NULL)
410 sp = &stairs[screen];
412 sp->direction = LRAND() & 1;
413 sp->sphere_position = NRAND(NPOSITIONS / 3) * 3;
415 if ((sp->glx_context = init_GL(mi)) != NULL) {
417 reshape(mi, MI_WIN_WIDTH(mi), MI_WIN_HEIGHT(mi));
418 glDrawBuffer(GL_BACK);
419 if (!glIsList(objects))
420 objects = glGenLists(1);
428 draw_stairs(ModeInfo * mi)
430 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
432 Display *display = MI_DISPLAY(mi);
433 Window window = MI_WINDOW(mi);
435 if (!sp->glx_context)
438 glXMakeCurrent(display, window, *(sp->glx_context));
440 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
444 glTranslatef(0.0, 0.0, -10.0);
446 if (!MI_WIN_IS_ICONIC(mi)) {
447 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
449 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
452 glRotatef(44.5, 1, 0, 0);
453 glRotatef(50 + ((sp->direction) ? 1 : -1 ) *
454 ((sp->step * 100 > 120) ? sp->step * 100 - 120 : 0), 0, 1, 0);
455 if (sp->step * 100 >= 360 + 120) { /* stop showing secrets */
457 sp->direction = LRAND() & 1;
459 draw_stairs_internal(mi);
465 glXSwapBuffers(display, window);
471 change_stairs(ModeInfo * mi)
473 stairsstruct *sp = &stairs[MI_SCREEN(mi)];
475 if (!sp->glx_context)
478 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
483 release_stairs(ModeInfo * mi)
485 if (stairs != NULL) {
486 (void) free((void *) stairs);
489 if (glIsList(objects)) {
490 glDeleteLists(objects, 1);