3216dd37b27c4b14443e2203e14a7c25ca07b1ee
[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     polys += unit_sphere (32, 32, False);
302         glShadeModel(GL_FLAT);
303     glEnable (GL_TEXTURE_2D);
304 #endif /* !DEBUG_PATH */
305
306         glPopMatrix();
307     return polys;
308 }
309
310
311
312 ENTRYPOINT void
313 reshape_stairs (ModeInfo * mi, int width, int height)
314 {
315         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
316
317         glViewport(0, 0, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
318         glMatrixMode(GL_PROJECTION);
319         glLoadIdentity();
320         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
321         glMatrixMode(GL_MODELVIEW);
322         if (width >= 1024) {
323                 glLineWidth(3);
324                 glPointSize(3);
325         } else if (width >= 512) {
326                 glLineWidth(2);
327                 glPointSize(2);
328         } else {
329                 glLineWidth(1);
330                 glPointSize(1);
331         }
332 }
333
334 ENTRYPOINT Bool
335 stairs_handle_event (ModeInfo *mi, XEvent *event)
336 {
337   stairsstruct *sp = &stairs[MI_SCREEN(mi)];
338
339   if (event->xany.type == ButtonPress &&
340       event->xbutton.button == Button1)
341     {
342       sp->button_down_p = True;
343       gltrackball_start (sp->trackball,
344                          event->xbutton.x, event->xbutton.y,
345                          MI_WIDTH (mi), MI_HEIGHT (mi));
346       return True;
347     }
348   else if (event->xany.type == ButtonRelease &&
349            event->xbutton.button == Button1)
350     {
351       sp->button_down_p = False;
352       return True;
353     }
354   else if (event->xany.type == ButtonPress &&
355            (event->xbutton.button == Button4 ||
356             event->xbutton.button == Button5 ||
357             event->xbutton.button == Button6 ||
358             event->xbutton.button == Button7))
359     {
360       gltrackball_mousewheel (sp->trackball, event->xbutton.button, 10,
361                               !!event->xbutton.state);
362       return True;
363     }
364   else if (event->xany.type == MotionNotify &&
365            sp->button_down_p)
366     {
367       gltrackball_track (sp->trackball,
368                          event->xmotion.x, event->xmotion.y,
369                          MI_WIDTH (mi), MI_HEIGHT (mi));
370       return True;
371     }
372   else if (event->xany.type == KeyPress)
373     {
374       KeySym keysym;
375       char c = 0;
376       XLookupString (&event->xkey, &c, 1, &keysym, 0);
377       if (c == ' ')
378         {
379           gltrackball_reset (sp->trackball);
380           return True;
381         }
382     }
383
384   return False;
385 }
386
387
388 static void
389 pinit(void)
390 {
391     int status;
392         glClearDepth(1.0);
393         glClearColor(0.0, 0.0, 0.0, 1.0);
394
395         glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
396         glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
397         glLightfv(GL_LIGHT0, GL_POSITION, position0);
398         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
399         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
400         glLightfv(GL_LIGHT1, GL_POSITION, position1);
401         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
402         glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
403         glEnable(GL_LIGHTING);
404         glEnable(GL_LIGHT0);
405         glEnable(GL_LIGHT1);
406         glEnable(GL_NORMALIZE);
407         glCullFace(GL_BACK);
408
409         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
410         glShadeModel(GL_FLAT);
411         glEnable(GL_DEPTH_TEST);
412         glEnable(GL_TEXTURE_2D);
413         glEnable(GL_CULL_FACE);
414
415         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
416
417     clear_gl_error();
418     status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
419                                WoodTextureWidth, WoodTextureHeight,
420                                GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
421     if (status)
422       {
423         const char *s = (char *) gluErrorString (status);
424         fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
425                  progname, WoodTextureWidth, WoodTextureHeight,
426                  (s ? s : "(unknown)"));
427         exit (1);
428       }
429     check_gl_error("mipmapping");
430
431         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
432         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
433         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
434         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
435         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
436
437         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
438         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
439 }
440
441 ENTRYPOINT void
442 init_stairs (ModeInfo * mi)
443 {
444         int         screen = MI_SCREEN(mi);
445         stairsstruct *sp;
446
447         if (stairs == NULL) {
448                 if ((stairs = (stairsstruct *) calloc(MI_NUM_SCREENS(mi),
449                                              sizeof (stairsstruct))) == NULL)
450                         return;
451         }
452         sp = &stairs[screen];
453
454         sp->step = 0.0;
455         sp->rotating = 0;
456         sp->sphere_position = NRAND(NPOSITIONS);
457         sp->sphere_tick = 0;
458
459         if ((sp->glx_context = init_GL(mi)) != NULL) {
460
461                 reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
462                 glDrawBuffer(GL_BACK);
463                 if (!glIsList(sp->objects))
464                         sp->objects = glGenLists(1);
465                 pinit();
466         } else {
467                 MI_CLEARWINDOW(mi);
468         }
469
470     sp->trackball = gltrackball_init ();
471 }
472
473 ENTRYPOINT void
474 draw_stairs (ModeInfo * mi)
475 {
476         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
477
478         Display    *display = MI_DISPLAY(mi);
479         Window      window = MI_WINDOW(mi);
480
481         if (!sp->glx_context)
482                 return;
483
484         glXMakeCurrent(display, window, *(sp->glx_context));
485
486         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
487
488         glPushMatrix();
489
490         glTranslatef(0.0, 0.0, -10.0);
491
492         if (!MI_IS_ICONIC(mi)) {
493                 glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
494         } else {
495                 glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
496         }
497
498     gltrackball_rotate (sp->trackball);
499
500     glTranslatef(0, 0.5, 0);
501         glRotatef(44.5, 1, 0, 0);
502     glRotatef(50, 0, 1, 0);
503
504     if (!sp->rotating) {
505       if ((LRAND() % 500) == 0)
506         sp->rotating = (LRAND() & 1) ? 1 : -1;
507     }
508
509     if (sp->rotating) {
510       glRotatef(sp->rotating * sp->step, 0, 1, 0);
511       if (sp->step >= 360) {
512         sp->rotating = 0;
513                 sp->step = 0;
514       }
515
516 # ifndef DEBUG
517       if (!sp->button_down_p)
518         sp->step += 2;
519 # endif /* DEBUG */
520     }
521
522         draw_stairs_internal(mi);
523
524
525 # ifdef DEBUG
526     {
527       int i, j;
528 #  ifdef DEBUG_PATH
529       glDisable(GL_LIGHTING);
530       glDisable(GL_TEXTURE_2D);
531       glBegin (GL_LINE_LOOP);
532 #  endif /* DEBUG_PATH */
533       for (i = 0; i < NPOSITIONS; i ++)
534         for (j = 0; j < SPHERE_TICKS; j++)
535           mi->polygon_count += draw_sphere(i, j);
536 #  ifdef DEBUG_PATH
537       glEnd();
538       glEnable(GL_LIGHTING);
539       glEnable(GL_TEXTURE_2D);
540 #  endif /* DEBUG_PATH */
541     }
542 #else  /* !DEBUG */
543     mi->polygon_count += draw_sphere(sp->sphere_position, sp->sphere_tick);
544 #endif /* !DEBUG */
545
546     if (sp->button_down_p)
547       ;
548     else if (++sp->sphere_tick >= SPHERE_TICKS)
549       {
550         sp->sphere_tick = 0;
551         if (++sp->sphere_position >= NPOSITIONS)
552           sp->sphere_position = 0;
553       }
554
555         glPopMatrix();
556
557     if (mi->fps_p) do_fps (mi);
558         glFlush();
559
560         glXSwapBuffers(display, window);
561 }
562
563 #ifndef STANDALONE
564 ENTRYPOINT void
565 change_stairs (ModeInfo * mi)
566 {
567         stairsstruct *sp = &stairs[MI_SCREEN(mi)];
568
569         if (!sp->glx_context)
570                 return;
571
572         glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sp->glx_context));
573         pinit();
574 }
575 #endif /* !STANDALONE */
576
577 ENTRYPOINT void
578 release_stairs (ModeInfo * mi)
579 {
580         if (stairs != NULL) {
581       int i;
582       for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
583         stairsstruct *sp = &stairs[i];
584         if (glIsList(sp->objects)) {
585           glDeleteLists(sp->objects, 1);
586         }
587       }
588       free(stairs);
589       stairs = NULL;
590         }
591         FreeAllGL(mi);
592 }
593
594 XSCREENSAVER_MODULE ("Stairs", stairs)
595
596 #endif