http://www.jwz.org/xscreensaver/xscreensaver-5.13.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 # include "xlockmore.h"         /* from the xscreensaver distribution */
64 #else /* !STANDALONE */
65 # include "xlock.h"                     /* from the xlockmore distribution */
66
67 #endif /* !STANDALONE */
68
69 #ifdef USE_GL
70
71 #include "e_textures.h"
72 #include "sphere.h"
73 #include "gltrackball.h"
74
75 ENTRYPOINT ModeSpecOpt stairs_opts =
76 {0, NULL, 0, NULL, NULL};
77
78 #ifdef USE_MODULES
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};
84
85 #endif
86
87 #define Scale4Window               0.3
88 #define Scale4Iconic               0.4
89
90 #define sqr(A)                     ((A)*(A))
91
92 #ifndef Pi
93 #define Pi                         M_PI
94 #endif
95
96 /*************************************************************************/
97
98 typedef struct {
99         GLint       WindH, WindW;
100         GLfloat     step;
101         int         rotating;
102         int         AreObjectsDefined[1];
103         int         sphere_position;
104         int         sphere_tick;
105         GLXContext *glx_context;
106     trackball_state *trackball;
107     Bool button_down_p;
108     GLuint objects;
109 } stairsstruct;
110
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};
119
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};
122
123 static const float ball_positions[] = {
124   -3.0, 3.0, 1.0,
125   -3.0, 2.8, 2.0,
126   -3.0, 2.6, 3.0,
127
128   -2.0, 2.4, 3.0,
129   -1.0, 2.2, 3.0,
130    0.0, 2.0, 3.0,
131    1.0, 1.8, 3.0,
132    2.0, 1.6, 3.0,
133
134    2.0, 1.5, 2.0,
135    2.0, 1.4, 1.0,
136    2.0, 1.3, 0.0,
137    2.0, 1.2, -1.0,
138    2.0, 1.1, -2.0,
139
140    1.0, 0.9, -2.0,
141    0.0, 0.7, -2.0,
142   -1.0, 0.5, -2.0,
143 };
144
145 #define NPOSITIONS ((sizeof ball_positions) / (sizeof ball_positions[0]) / 3)
146 #define SPHERE_TICKS 32
147
148 static stairsstruct *stairs = NULL;
149
150 static int
151 draw_block(GLfloat width, GLfloat height, GLfloat thickness)
152 {
153     int polys = 0;
154         glFrontFace(GL_CCW);
155         glBegin(GL_QUADS);
156         glNormal3f(0, 0, 1);
157         glTexCoord2f(0, 0);
158         glVertex3f(-width, -height, thickness);
159         glTexCoord2f(1, 0);
160         glVertex3f(width, -height, thickness);
161         glTexCoord2f(1, 1);
162         glVertex3f(width, height, thickness);
163         glTexCoord2f(0, 1);
164         glVertex3f(-width, height, thickness);
165     polys++;
166         glNormal3f(0, 0, -1);
167         glTexCoord2f(0, 0);
168         glVertex3f(-width, height, -thickness);
169         glTexCoord2f(1, 0);
170         glVertex3f(width, height, -thickness);
171         glTexCoord2f(1, 1);
172         glVertex3f(width, -height, -thickness);
173         glTexCoord2f(0, 1);
174         glVertex3f(-width, -height, -thickness);
175     polys++;
176         glNormal3f(0, 1, 0);
177         glTexCoord2f(0, 0);
178         glVertex3f(-width, height, thickness);
179         glTexCoord2f(1, 0);
180         glVertex3f(width, height, thickness);
181         glTexCoord2f(1, 1);
182         glVertex3f(width, height, -thickness);
183         glTexCoord2f(0, 1);
184         glVertex3f(-width, height, -thickness);
185     polys++;
186         glNormal3f(0, -1, 0);
187         glTexCoord2f(0, 0);
188         glVertex3f(-width, -height, -thickness);
189         glTexCoord2f(1, 0);
190         glVertex3f(width, -height, -thickness);
191         glTexCoord2f(1, 1);
192         glVertex3f(width, -height, thickness);
193         glTexCoord2f(0, 1);
194         glVertex3f(-width, -height, thickness);
195     polys++;
196         glNormal3f(1, 0, 0);
197         glTexCoord2f(0, 0);
198         glVertex3f(width, -height, thickness);
199         glTexCoord2f(1, 0);
200         glVertex3f(width, -height, -thickness);
201         glTexCoord2f(1, 1);
202         glVertex3f(width, height, -thickness);
203         glTexCoord2f(0, 1);
204         glVertex3f(width, height, thickness);
205     polys++;
206         glNormal3f(-1, 0, 0);
207         glTexCoord2f(0, 0);
208         glVertex3f(-width, height, thickness);
209         glTexCoord2f(1, 0);
210         glVertex3f(-width, height, -thickness);
211         glTexCoord2f(1, 1);
212         glVertex3f(-width, -height, -thickness);
213         glTexCoord2f(0, 1);
214         glVertex3f(-width, -height, thickness);
215     polys++;
216         glEnd();
217     return polys;
218 }
219
220 static void
221 draw_stairs_internal(ModeInfo * mi)
222 {
223         GLfloat     X;
224
225     mi->polygon_count = 0;
226
227         glPushMatrix();
228         glPushMatrix();
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);
233         }
234         glPopMatrix();
235         glTranslatef(-3.0, 0.0, 3.0);
236         glPushMatrix();
237
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);
241         }
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);
246         }
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);
251         }
252         glPopMatrix();
253         glPopMatrix();
254 }
255
256 /*#define DEBUG*/
257 /*#define DEBUG_PATH*/
258
259 static int
260 draw_sphere(int pos, int tick)
261 {
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;
273     int polys = 0;
274
275         glPushMatrix();
276
277 # ifdef DEBUG_PATH
278     glVertex3f(x, y, z);
279     if (tick == 0) {
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);
285       glVertex3f(x, y, z);
286     }
287
288 # else /* !DEBUG_PATH */
289     y += 0.5;
290     glTranslatef(x, y, z);
291
292     glScalef (0.5, 0.5, 0.5);
293
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);
297
298         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
299     glDisable (GL_TEXTURE_2D);
300         glShadeModel(GL_SMOOTH);
301         glFrontFace(GL_CCW);
302     polys += unit_sphere (32, 32, False);
303         glShadeModel(GL_FLAT);
304     glEnable (GL_TEXTURE_2D);
305 #endif /* !DEBUG_PATH */
306
307         glPopMatrix();
308     return polys;
309 }
310
311
312
313 ENTRYPOINT void
314 reshape_stairs (ModeInfo * mi, int width, int height)
315 {
316         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
317
318         glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
319         glMatrixMode(GL_PROJECTION);
320         glLoadIdentity();
321         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
322         glMatrixMode(GL_MODELVIEW);
323         if (width >= 1024) {
324                 glLineWidth(3);
325                 glPointSize(3);
326         } else if (width >= 512) {
327                 glLineWidth(2);
328                 glPointSize(2);
329         } else {
330                 glLineWidth(1);
331                 glPointSize(1);
332         }
333 }
334
335 ENTRYPOINT Bool
336 stairs_handle_event (ModeInfo *mi, XEvent *event)
337 {
338   stairsstruct *sp = &stairs[MI_SCREEN(mi)];
339
340   if (event->xany.type == ButtonPress &&
341       event->xbutton.button == Button1)
342     {
343       sp->button_down_p = True;
344       gltrackball_start (sp->trackball,
345                          event->xbutton.x, event->xbutton.y,
346                          MI_WIDTH (mi), MI_HEIGHT (mi));
347       return True;
348     }
349   else if (event->xany.type == ButtonRelease &&
350            event->xbutton.button == Button1)
351     {
352       sp->button_down_p = False;
353       return True;
354     }
355   else if (event->xany.type == ButtonPress &&
356            (event->xbutton.button == Button4 ||
357             event->xbutton.button == Button5 ||
358             event->xbutton.button == Button6 ||
359             event->xbutton.button == Button7))
360     {
361       gltrackball_mousewheel (sp->trackball, event->xbutton.button, 10,
362                               !!event->xbutton.state);
363       return True;
364     }
365   else if (event->xany.type == MotionNotify &&
366            sp->button_down_p)
367     {
368       gltrackball_track (sp->trackball,
369                          event->xmotion.x, event->xmotion.y,
370                          MI_WIDTH (mi), MI_HEIGHT (mi));
371       return True;
372     }
373   else if (event->xany.type == KeyPress)
374     {
375       KeySym keysym;
376       char c = 0;
377       XLookupString (&event->xkey, &c, 1, &keysym, 0);
378       if (c == ' ')
379         {
380           gltrackball_reset (sp->trackball);
381           return True;
382         }
383     }
384
385   return False;
386 }
387
388
389 static void
390 pinit(void)
391 {
392     int status;
393         glClearDepth(1.0);
394         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
395         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
396         glLightfv(GL_LIGHT0, GL_POSITION, position0);
397         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
398         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
399         glLightfv(GL_LIGHT1, GL_POSITION, position1);
400         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
401         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
402         glEnable(GL_LIGHTING);
403         glEnable(GL_LIGHT0);
404         glEnable(GL_LIGHT1);
405         glEnable(GL_NORMALIZE);
406         glCullFace(GL_BACK);
407
408         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
409         glShadeModel(GL_FLAT);
410         glEnable(GL_DEPTH_TEST);
411         glEnable(GL_TEXTURE_2D);
412         glEnable(GL_CULL_FACE);
413
414         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
415
416     clear_gl_error();
417     status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
418                                WoodTextureWidth, WoodTextureHeight,
419                                GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
420     if (status)
421       {
422         const char *s = (char *) gluErrorString (status);
423         fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
424                  progname, WoodTextureWidth, WoodTextureHeight,
425                  (s ? s : "(unknown)"));
426         exit (1);
427       }
428     check_gl_error("mipmapping");
429
430         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
431         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
432         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
433         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
434         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
435
436         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
437         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
438 }
439
440 ENTRYPOINT void
441 init_stairs (ModeInfo * mi)
442 {
443         int         screen = MI_SCREEN(mi);
444         stairsstruct *sp;
445
446         if (stairs == NULL) {
447                 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
448                                              sizeof (stairsstruct))) == NULL)
449                         return;
450         }
451         sp = &stairs[screen];
452
453         sp->step = 0.0;
454         sp->rotating = 0;
455         sp->sphere_position = NRAND(NPOSITIONS);
456         sp->sphere_tick = 0;
457
458         if ((sp->glx_context = init_GL(mi)) != NULL) {
459
460                 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
461                 glDrawBuffer(GL_BACK);
462                 if (!glIsList(sp->objects))
463                         sp->objects = glGenLists(1);
464                 pinit();
465         } else {
466                 MI_CLEARWINDOW(mi);
467         }
468
469     sp->trackball = gltrackball_init ();
470 }
471
472 ENTRYPOINT void
473 draw_stairs (ModeInfo * mi)
474 {
475         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
476
477         Display    *display = MI_DISPLAY(mi);
478         Window      window = MI_WINDOW(mi);
479
480         if (!sp->glx_context)
481                 return;
482
483         glXMakeCurrent(display, window, *(sp->glx_context));
484
485         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
486
487         glPushMatrix();
488
489         glTranslatef(0.0, 0.0, -10.0);
490
491         if (!MI_IS_ICONIC(mi)) {
492                 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
493         } else {
494                 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
495         }
496
497     gltrackball_rotate (sp->trackball);
498
499     glTranslatef(0, 0.5, 0);
500         glRotatef(44.5, 1, 0, 0);
501     glRotatef(50, 0, 1, 0);
502
503     if (!sp->rotating) {
504       if ((LRAND() % 500) == 0)
505         sp->rotating = (LRAND() & 1) ? 1 : -1;
506     }
507
508     if (sp->rotating) {
509       glRotatef(sp->rotating * sp->step, 0, 1, 0);
510       if (sp->step >= 360) {
511         sp->rotating = 0;
512                 sp->step = 0;
513       }
514
515 # ifndef DEBUG
516       if (!sp->button_down_p)
517         sp->step += 2;
518 # endif /* DEBUG */
519     }
520
521         draw_stairs_internal(mi);
522
523
524 # ifdef DEBUG
525     {
526       int i, j;
527 #  ifdef DEBUG_PATH
528       glDisable(GL_LIGHTING);
529       glDisable(GL_TEXTURE_2D);
530       glBegin (GL_LINE_LOOP);
531 #  endif /* DEBUG_PATH */
532       for (i = 0; i < NPOSITIONS; i ++)
533         for (j = 0; j < SPHERE_TICKS; j++)
534           mi->polygon_count += draw_sphere(i, j);
535 #  ifdef DEBUG_PATH
536       glEnd();
537       glEnable(GL_LIGHTING);
538       glEnable(GL_TEXTURE_2D);
539 #  endif /* DEBUG_PATH */
540     }
541 #else  /* !DEBUG */
542     mi->polygon_count += draw_sphere(sp->sphere_position, sp->sphere_tick);
543 #endif /* !DEBUG */
544
545     if (sp->button_down_p)
546       ;
547     else if (++sp->sphere_tick >= SPHERE_TICKS)
548       {
549         sp->sphere_tick = 0;
550         if (++sp->sphere_position >= NPOSITIONS)
551           sp->sphere_position = 0;
552       }
553
554         glPopMatrix();
555
556     if (mi->fps_p) do_fps (mi);
557         glFlush();
558
559         glXSwapBuffers(display, window);
560 }
561
562 #ifndef STANDALONE
563 ENTRYPOINT void
564 change_stairs (ModeInfo * mi)
565 {
566         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
567
568         if (!sp->glx_context)
569                 return;
570
571         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
572         pinit();
573 }
574 #endif /* !STANDALONE */
575
576 ENTRYPOINT void
577 release_stairs (ModeInfo * mi)
578 {
579         if (stairs != NULL) {
580       int i;
581       for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
582         stairsstruct *sp = &stairs[i];
583         if (glIsList(sp->objects)) {
584           glDeleteLists(sp->objects, 1);
585         }
586       }
587       free(stairs);
588       stairs = NULL;
589         }
590         FreeAllGL(mi);
591 }
592
593 XSCREENSAVER_MODULE ("Stairs", stairs)
594
595 #endif