From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / stairs.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* stairs --- Infinite Stairs, and Escher-like scene. */
3
4 #if 0
5 static const char sccsid[] = "@(#)stairs.c      4.07 97/11/24 xlockmore";
6 #endif
7
8 /*-
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.
14  *
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.
20  *
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.
26  *
27  * Thanks goes to Brian Paul for making it possible and inexpensive to use 
28  * OpenGL at home.
29  *
30  * Since I'm not a native English speaker, my apologies for any grammatical
31  * mistake.
32  *
33  * My e-mail address is
34  * m-vianna@usa.net
35  *
36  * Marcelo F. Vianna (Jun-01-1997)
37  *
38  * Revision History:
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.
42  *            Marcelo F. Vianna.
43  *
44  */
45
46 /*-
47  * Texture mapping is only available on RGBA contexts, Mono and color index
48  * visuals DO NOT support texture mapping in OpenGL.
49  *
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
53  * Mesa.
54  *
55  * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
56  */
57
58 #ifdef STANDALONE
59 # define DEFAULTS                       "*delay:                20000   \n" \
60                                                         "*showFPS:      False   \n"
61
62 # define refresh_stairs 0
63 # define release_stairs 0
64 # include "xlockmore.h"         /* from the xscreensaver distribution */
65 #else /* !STANDALONE */
66 # include "xlock.h"                     /* from the xlockmore distribution */
67
68 #endif /* !STANDALONE */
69
70 #ifdef USE_GL
71
72 #if 0
73 #include "e_textures.h"
74 #else
75 #include "xpm-ximage.h"
76 #include "../images/wood.xpm"
77 #endif
78
79 #include "sphere.h"
80 #include "gltrackball.h"
81
82 ENTRYPOINT ModeSpecOpt stairs_opts =
83 {0, NULL, 0, NULL, NULL};
84
85 #ifdef USE_MODULES
86 ModStruct   stairs_description =
87 {"stairs", "init_stairs", "draw_stairs", NULL,
88  "draw_stairs", "change_stairs", NULL, &stairs_opts,
89  1000, 1, 1, 1, 4, 1.0, "",
90  "Shows Infinite Stairs, an Escher-like scene", 0, NULL};
91
92 #endif
93
94 #define Scale4Window               0.3
95 #define Scale4Iconic               0.4
96
97 #define sqr(A)                     ((A)*(A))
98
99 #ifndef Pi
100 #define Pi                         M_PI
101 #endif
102
103 /*************************************************************************/
104
105 typedef struct {
106         GLint       WindH, WindW;
107         GLfloat     step;
108         int         rotating;
109         int         AreObjectsDefined[1];
110         int         sphere_position;
111         int         sphere_tick;
112         GLXContext *glx_context;
113     trackball_state *trackball;
114     Bool button_down_p;
115     GLuint objects;
116 } stairsstruct;
117
118 static const float front_shininess[] = {60.0};
119 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
120 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
121 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
122 static const float position0[] = {1.0, 1.0, 1.0, 0.0};
123 static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
124 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
125 static const float lmodel_twoside[] = {GL_TRUE};
126
127 static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
128 static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};
129
130 static const float ball_positions[] = {
131   -3.0, 3.0, 1.0,
132   -3.0, 2.8, 2.0,
133   -3.0, 2.6, 3.0,
134
135   -2.0, 2.4, 3.0,
136   -1.0, 2.2, 3.0,
137    0.0, 2.0, 3.0,
138    1.0, 1.8, 3.0,
139    2.0, 1.6, 3.0,
140
141    2.0, 1.5, 2.0,
142    2.0, 1.4, 1.0,
143    2.0, 1.3, 0.0,
144    2.0, 1.2, -1.0,
145    2.0, 1.1, -2.0,
146
147    1.0, 0.9, -2.0,
148    0.0, 0.7, -2.0,
149   -1.0, 0.5, -2.0,
150 };
151
152 #define NPOSITIONS ((sizeof ball_positions) / (sizeof ball_positions[0]) / 3)
153 #define SPHERE_TICKS 32
154
155 static stairsstruct *stairs = NULL;
156
157 static int
158 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
159 {
160     int polys = 0;
161         glFrontFace(GL_CCW);
162         glBegin(GL_QUADS);
163         glNormal3f(0, 0, 1);
164         glTexCoord2f(0, 0);
165         glVertex3f(-width, -height, thickness);
166         glTexCoord2f(1, 0);
167         glVertex3f(width, -height, thickness);
168         glTexCoord2f(1, 1);
169         glVertex3f(width, height, thickness);
170         glTexCoord2f(0, 1);
171         glVertex3f(-width, height, thickness);
172     polys++;
173         glNormal3f(0, 0, -1);
174         glTexCoord2f(0, 0);
175         glVertex3f(-width, height, -thickness);
176         glTexCoord2f(1, 0);
177         glVertex3f(width, height, -thickness);
178         glTexCoord2f(1, 1);
179         glVertex3f(width, -height, -thickness);
180         glTexCoord2f(0, 1);
181         glVertex3f(-width, -height, -thickness);
182     polys++;
183         glNormal3f(0, 1, 0);
184         glTexCoord2f(0, 0);
185         glVertex3f(-width, height, thickness);
186         glTexCoord2f(1, 0);
187         glVertex3f(width, height, thickness);
188         glTexCoord2f(1, 1);
189         glVertex3f(width, height, -thickness);
190         glTexCoord2f(0, 1);
191         glVertex3f(-width, height, -thickness);
192     polys++;
193         glNormal3f(0, -1, 0);
194         glTexCoord2f(0, 0);
195         glVertex3f(-width, -height, -thickness);
196         glTexCoord2f(1, 0);
197         glVertex3f(width, -height, -thickness);
198         glTexCoord2f(1, 1);
199         glVertex3f(width, -height, thickness);
200         glTexCoord2f(0, 1);
201         glVertex3f(-width, -height, thickness);
202     polys++;
203         glNormal3f(1, 0, 0);
204         glTexCoord2f(0, 0);
205         glVertex3f(width, -height, thickness);
206         glTexCoord2f(1, 0);
207         glVertex3f(width, -height, -thickness);
208         glTexCoord2f(1, 1);
209         glVertex3f(width, height, -thickness);
210         glTexCoord2f(0, 1);
211         glVertex3f(width, height, thickness);
212     polys++;
213         glNormal3f(-1, 0, 0);
214         glTexCoord2f(0, 0);
215         glVertex3f(-width, height, thickness);
216         glTexCoord2f(1, 0);
217         glVertex3f(-width, height, -thickness);
218         glTexCoord2f(1, 1);
219         glVertex3f(-width, -height, -thickness);
220         glTexCoord2f(0, 1);
221         glVertex3f(-width, -height, thickness);
222     polys++;
223         glEnd();
224     return polys;
225 }
226
227 static void
228 draw_stairs_internal(ModeInfo * mi)
229 {
230         GLfloat     X;
231
232     mi->polygon_count = 0;
233
234         glPushMatrix();
235         glPushMatrix();
236         glTranslatef(-3.0, 0.1, 2.0);
237         for (X = 0; X < 2; X++) {
238         mi->polygon_count += draw_block(0.5, 2.7 + 0.1 * X, 0.5);
239                 glTranslatef(0.0, 0.1, -1.0);
240         }
241         glPopMatrix();
242         glTranslatef(-3.0, 0.0, 3.0);
243         glPushMatrix();
244
245         for (X = 0; X < 6; X++) {
246                 mi->polygon_count += draw_block(0.5, 2.6 - 0.1 * X, 0.5);
247                 glTranslatef(1.0, -0.1, 0.0);
248         }
249         glTranslatef(-1.0, -0.9, -1.0);
250         for (X = 0; X < 5; X++) {
251                 mi->polygon_count += draw_block(0.5, 3.0 - 0.1 * X, 0.5);
252                 glTranslatef(0.0, 0.0, -1.0);
253         }
254         glTranslatef(-1.0, -1.1, 1.0);
255         for (X = 0; X < 3; X++) {
256                 mi->polygon_count += draw_block(0.5, 3.5 - 0.1 * X, 0.5);
257                 glTranslatef(-1.0, -0.1, 0.0);
258         }
259         glPopMatrix();
260         glPopMatrix();
261 }
262
263 /*#define DEBUG*/
264 /*#define DEBUG_PATH*/
265
266 static int
267 draw_sphere(int pos, int tick)
268 {
269     int pos2 = (pos+1) % NPOSITIONS;
270     GLfloat x1 = ball_positions[pos*3];
271     GLfloat y1 = ball_positions[pos*3+1];
272     GLfloat z1 = ball_positions[pos*3+2];
273     GLfloat x2 = ball_positions[pos2*3];
274     GLfloat y2 = ball_positions[pos2*3+1];
275     GLfloat z2 = ball_positions[pos2*3+2];
276     GLfloat frac = tick / (GLfloat) SPHERE_TICKS;
277     GLfloat x = x1 + (x2 - x1) * frac;
278     GLfloat y = y1 + (y2 - y1) * frac + (2 * sin (M_PI * frac));
279     GLfloat z = z1 + (z2 - z1) * frac;
280     int polys = 0;
281
282         glPushMatrix();
283
284 # ifdef DEBUG_PATH
285     glVertex3f(x, y, z);
286     if (tick == 0) {
287       glVertex3f(x, y-7.5, z);
288       glVertex3f(x, y, z); glVertex3f(x, y, z-0.6);
289       glVertex3f(x, y, z); glVertex3f(x, y, z+0.6);
290       glVertex3f(x, y, z); glVertex3f(x+0.6, y, z);
291       glVertex3f(x, y, z); glVertex3f(x-0.6, y, z);
292       glVertex3f(x, y, z);
293     }
294
295 # else /* !DEBUG_PATH */
296     y += 0.5;
297     glTranslatef(x, y, z);
298
299     glScalef (0.5, 0.5, 0.5);
300
301     /* make ball a little smaller on the gap to obscure distance */
302         if (pos == NPOSITIONS-1)
303       glScalef (0.95, 0.95, 0.95);
304
305         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
306     glDisable (GL_TEXTURE_2D);
307         glShadeModel(GL_SMOOTH);
308         glFrontFace(GL_CCW);
309     polys += unit_sphere (32, 32, False);
310         glShadeModel(GL_FLAT);
311     glEnable (GL_TEXTURE_2D);
312 #endif /* !DEBUG_PATH */
313
314         glPopMatrix();
315     return polys;
316 }
317
318
319
320 ENTRYPOINT void
321 reshape_stairs (ModeInfo * mi, int width, int height)
322 {
323         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
324
325         glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
326         glMatrixMode(GL_PROJECTION);
327         glLoadIdentity();
328         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
329         glMatrixMode(GL_MODELVIEW);
330         if (width >= 1024) {
331                 glLineWidth(3);
332                 glPointSize(3);
333         } else if (width >= 512) {
334                 glLineWidth(2);
335                 glPointSize(2);
336         } else {
337                 glLineWidth(1);
338                 glPointSize(1);
339         }
340 }
341
342 ENTRYPOINT Bool
343 stairs_handle_event (ModeInfo *mi, XEvent *event)
344 {
345   stairsstruct *sp = &stairs[MI_SCREEN(mi)];
346
347   if (gltrackball_event_handler (event, sp->trackball,
348                                  MI_WIDTH (mi), MI_HEIGHT (mi),
349                                  &sp->button_down_p))
350     return True;
351   else if (event->xany.type == KeyPress)
352     {
353       KeySym keysym;
354       char c = 0;
355       XLookupString (&event->xkey, &c, 1, &keysym, 0);
356       if (c == ' ' || c == '\t')
357         {
358           gltrackball_reset (sp->trackball, 0, 0);
359           return True;
360         }
361     }
362
363   return False;
364 }
365
366
367 static void
368 pinit(ModeInfo *mi)
369 {
370   /* int status; */
371         glClearDepth(1.0);
372         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
373         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
374         glLightfv(GL_LIGHT0, GL_POSITION, position0);
375         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
376         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
377         glLightfv(GL_LIGHT1, GL_POSITION, position1);
378         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
379         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
380         glEnable(GL_LIGHTING);
381         glEnable(GL_LIGHT0);
382         glEnable(GL_LIGHT1);
383         glEnable(GL_NORMALIZE);
384         glCullFace(GL_BACK);
385
386         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
387         glShadeModel(GL_FLAT);
388         glEnable(GL_DEPTH_TEST);
389         glEnable(GL_TEXTURE_2D);
390         glEnable(GL_CULL_FACE);
391
392         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
393
394 #if 0
395     clear_gl_error();
396     status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
397                                WoodTextureWidth, WoodTextureHeight,
398                                GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
399     if (status)
400       {
401         const char *s = (char *) gluErrorString (status);
402         fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
403                  progname, WoodTextureWidth, WoodTextureHeight,
404                  (s ? s : "(unknown)"));
405         exit (1);
406       }
407     check_gl_error("mipmapping");
408 #else
409     {
410       XImage *img = xpm_to_ximage (mi->dpy,
411                                    mi->xgwa.visual,
412                                    mi->xgwa.colormap,
413                                    wood_texture);
414           glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
415                     img->width, img->height, 0,
416                     GL_RGBA,
417                     /* GL_UNSIGNED_BYTE, */
418                     GL_UNSIGNED_INT_8_8_8_8_REV,
419                     img->data);
420       check_gl_error("texture");
421       XDestroyImage (img);
422     }
423 #endif
424
425         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
426         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
427         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
428         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
429         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
430
431         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
432         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
433 }
434
435 static void free_stairs (ModeInfo * mi);
436
437 ENTRYPOINT void
438 init_stairs (ModeInfo * mi)
439 {
440         int         screen = MI_SCREEN(mi);
441         stairsstruct *sp;
442
443         MI_INIT (mi, stairs, free_stairs);
444         sp = &stairs[screen];
445
446         sp->step = 0.0;
447         sp->rotating = 0;
448         sp->sphere_position = NRAND(NPOSITIONS);
449         sp->sphere_tick = 0;
450
451         if ((sp->glx_context = init_GL(mi)) != NULL) {
452
453                 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
454                 glDrawBuffer(GL_BACK);
455                 if (!glIsList(sp->objects))
456                         sp->objects = glGenLists(1);
457                 pinit(mi);
458         } else {
459                 MI_CLEARWINDOW(mi);
460         }
461
462     sp->trackball = gltrackball_init (False);
463 }
464
465 ENTRYPOINT void
466 draw_stairs (ModeInfo * mi)
467 {
468         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
469     GLfloat rot = current_device_rotation();
470
471         Display    *display = MI_DISPLAY(mi);
472         Window      window = MI_WINDOW(mi);
473
474         if (!sp->glx_context)
475                 return;
476
477         glXMakeCurrent(display, window, *(sp->glx_context));
478
479         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
480
481         glPushMatrix();
482
483     glRotatef(rot, 0, 0, 1);
484
485         glTranslatef(0.0, 0.0, -10.0);
486
487         if (!MI_IS_ICONIC(mi)) {
488                 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
489         } else {
490                 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
491         }
492
493 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
494   {
495     GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
496     if (rot != 0 && rot != 180 && rot != -180)
497       glScalef (1/h, 1/h, 1/h);
498   }
499 # endif
500
501     gltrackball_rotate (sp->trackball);
502
503     glTranslatef(0, 0.5, 0);
504         glRotatef(44.5, 1, 0, 0);
505     glRotatef(50, 0, 1, 0);
506
507     if (!sp->rotating) {
508       if ((LRAND() % 500) == 0)
509         sp->rotating = (LRAND() & 1) ? 1 : -1;
510     }
511
512     if (sp->rotating) {
513       glRotatef(sp->rotating * sp->step, 0, 1, 0);
514       if (sp->step >= 360) {
515         sp->rotating = 0;
516                 sp->step = 0;
517       }
518
519 # ifndef DEBUG
520       if (!sp->button_down_p)
521         sp->step += 2;
522 # endif /* DEBUG */
523     }
524
525         draw_stairs_internal(mi);
526
527
528 # ifdef DEBUG
529     {
530       int i, j;
531 #  ifdef DEBUG_PATH
532       glDisable(GL_LIGHTING);
533       glDisable(GL_TEXTURE_2D);
534       glBegin (GL_LINE_LOOP);
535 #  endif /* DEBUG_PATH */
536       for (i = 0; i < NPOSITIONS; i ++)
537         for (j = 0; j < SPHERE_TICKS; j++)
538           mi->polygon_count += draw_sphere(i, j);
539 #  ifdef DEBUG_PATH
540       glEnd();
541       glEnable(GL_LIGHTING);
542       glEnable(GL_TEXTURE_2D);
543 #  endif /* DEBUG_PATH */
544     }
545 #else  /* !DEBUG */
546     mi->polygon_count += draw_sphere(sp->sphere_position, sp->sphere_tick);
547 #endif /* !DEBUG */
548
549     if (sp->button_down_p)
550       ;
551     else if (++sp->sphere_tick >= SPHERE_TICKS)
552       {
553         sp->sphere_tick = 0;
554         if (++sp->sphere_position >= NPOSITIONS)
555           sp->sphere_position = 0;
556       }
557
558         glPopMatrix();
559
560     if (mi->fps_p) do_fps (mi);
561         glFlush();
562
563         glXSwapBuffers(display, window);
564 }
565
566 #ifndef STANDALONE
567 ENTRYPOINT void
568 change_stairs (ModeInfo * mi)
569 {
570         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
571
572         if (!sp->glx_context)
573                 return;
574
575         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
576         pinit();
577 }
578 #endif /* !STANDALONE */
579
580 static void
581 free_stairs (ModeInfo * mi)
582 {
583         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
584         if (glIsList(sp->objects)) {
585                 glDeleteLists(sp->objects, 1);
586         }
587 }
588
589 XSCREENSAVER_MODULE ("Stairs", stairs)
590
591 #endif