http://www.jwz.org/xscreensaver/xscreensaver-5.12.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_CW);
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         glClearColor(0.0, 0.0, 0.0, 1.0);
395
396         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
397         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
398         glLightfv(GL_LIGHT0, GL_POSITION, position0);
399         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
400         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
401         glLightfv(GL_LIGHT1, GL_POSITION, position1);
402         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
403         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
404         glEnable(GL_LIGHTING);
405         glEnable(GL_LIGHT0);
406         glEnable(GL_LIGHT1);
407         glEnable(GL_NORMALIZE);
408         glCullFace(GL_BACK);
409
410         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
411         glShadeModel(GL_FLAT);
412         glEnable(GL_DEPTH_TEST);
413         glEnable(GL_TEXTURE_2D);
414         glEnable(GL_CULL_FACE);
415
416         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
417
418     clear_gl_error();
419     status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
420                                WoodTextureWidth, WoodTextureHeight,
421                                GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
422     if (status)
423       {
424         const char *s = (char *) gluErrorString (status);
425         fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
426                  progname, WoodTextureWidth, WoodTextureHeight,
427                  (s ? s : "(unknown)"));
428         exit (1);
429       }
430     check_gl_error("mipmapping");
431
432         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
433         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
434         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
435         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
436         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
437
438         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
439         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
440 }
441
442 ENTRYPOINT void
443 init_stairs (ModeInfo * mi)
444 {
445         int         screen = MI_SCREEN(mi);
446         stairsstruct *sp;
447
448         if (stairs == NULL) {
449                 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
450                                              sizeof (stairsstruct))) == NULL)
451                         return;
452         }
453         sp = &stairs[screen];
454
455         sp->step = 0.0;
456         sp->rotating = 0;
457         sp->sphere_position = NRAND(NPOSITIONS);
458         sp->sphere_tick = 0;
459
460         if ((sp->glx_context = init_GL(mi)) != NULL) {
461
462                 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
463                 glDrawBuffer(GL_BACK);
464                 if (!glIsList(sp->objects))
465                         sp->objects = glGenLists(1);
466                 pinit();
467         } else {
468                 MI_CLEARWINDOW(mi);
469         }
470
471     sp->trackball = gltrackball_init ();
472 }
473
474 ENTRYPOINT void
475 draw_stairs (ModeInfo * mi)
476 {
477         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
478
479         Display    *display = MI_DISPLAY(mi);
480         Window      window = MI_WINDOW(mi);
481
482         if (!sp->glx_context)
483                 return;
484
485         glXMakeCurrent(display, window, *(sp->glx_context));
486
487         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
488
489         glPushMatrix();
490
491         glTranslatef(0.0, 0.0, -10.0);
492
493         if (!MI_IS_ICONIC(mi)) {
494                 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
495         } else {
496                 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
497         }
498
499     gltrackball_rotate (sp->trackball);
500
501     glTranslatef(0, 0.5, 0);
502         glRotatef(44.5, 1, 0, 0);
503     glRotatef(50, 0, 1, 0);
504
505     if (!sp->rotating) {
506       if ((LRAND() % 500) == 0)
507         sp->rotating = (LRAND() & 1) ? 1 : -1;
508     }
509
510     if (sp->rotating) {
511       glRotatef(sp->rotating * sp->step, 0, 1, 0);
512       if (sp->step >= 360) {
513         sp->rotating = 0;
514                 sp->step = 0;
515       }
516
517 # ifndef DEBUG
518       if (!sp->button_down_p)
519         sp->step += 2;
520 # endif /* DEBUG */
521     }
522
523         draw_stairs_internal(mi);
524
525
526 # ifdef DEBUG
527     {
528       int i, j;
529 #  ifdef DEBUG_PATH
530       glDisable(GL_LIGHTING);
531       glDisable(GL_TEXTURE_2D);
532       glBegin (GL_LINE_LOOP);
533 #  endif /* DEBUG_PATH */
534       for (i = 0; i < NPOSITIONS; i ++)
535         for (j = 0; j < SPHERE_TICKS; j++)
536           mi->polygon_count += draw_sphere(i, j);
537 #  ifdef DEBUG_PATH
538       glEnd();
539       glEnable(GL_LIGHTING);
540       glEnable(GL_TEXTURE_2D);
541 #  endif /* DEBUG_PATH */
542     }
543 #else  /* !DEBUG */
544     mi->polygon_count += draw_sphere(sp->sphere_position, sp->sphere_tick);
545 #endif /* !DEBUG */
546
547     if (sp->button_down_p)
548       ;
549     else if (++sp->sphere_tick >= SPHERE_TICKS)
550       {
551         sp->sphere_tick = 0;
552         if (++sp->sphere_position >= NPOSITIONS)
553           sp->sphere_position = 0;
554       }
555
556         glPopMatrix();
557
558     if (mi->fps_p) do_fps (mi);
559         glFlush();
560
561         glXSwapBuffers(display, window);
562 }
563
564 #ifndef STANDALONE
565 ENTRYPOINT void
566 change_stairs (ModeInfo * mi)
567 {
568         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
569
570         if (!sp->glx_context)
571                 return;
572
573         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
574         pinit();
575 }
576 #endif /* !STANDALONE */
577
578 ENTRYPOINT void
579 release_stairs (ModeInfo * mi)
580 {
581         if (stairs != NULL) {
582       int i;
583       for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
584         stairsstruct *sp = &stairs[i];
585         if (glIsList(sp->objects)) {
586           glDeleteLists(sp->objects, 1);
587         }
588       }
589       free(stairs);
590       stairs = NULL;
591         }
592         FreeAllGL(mi);
593 }
594
595 XSCREENSAVER_MODULE ("Stairs", stairs)
596
597 #endif