summaryrefslogtreecommitdiffstats
path: root/hacks/glx/stairs.c
blob: faf9a37b192e4a37efe4f332956d03466896b293 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* -*- Mode: C; tab-width: 4 -*- */
/* stairs --- Infinite Stairs, and Escher-like scene. */

#if 0
static const char sccsid[] = "@(#)stairs.c	4.07 97/11/24 xlockmore";
#endif

/*-
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.
 *
 * This mode shows some interesting scenes that are impossible OR very
 * weird to build in the real universe. Much of the scenes are inspirated
 * on Mauritz Cornelis stairs's works which derivated the mode's name.
 * M.C. Escher (1898-1972) was a dutch artist and many people prefer to
 * say he was a mathematician.
 *
 * Thanks goes to Brian Paul for making it possible and inexpensive to use 
 * OpenGL at home.
 *
 * Since I'm not a native English speaker, my apologies for any grammatical
 * mistake.
 *
 * My e-mail address is
 * m-vianna@usa.net
 *
 * Marcelo F. Vianna (Jun-01-1997)
 *
 * Revision History:
 * 07-Jan-98: This would be a scene for the escher mode, but now escher mode
 *            was splitted in different modes for each scene. This is the
 *            initial release and is not working yet.
 *            Marcelo F. Vianna.
 *
 */

/*-
 * Texture mapping is only available on RGBA contexts, Mono and color index
 * visuals DO NOT support texture mapping in OpenGL.
 *
 * BUT Mesa do implements RGBA contexts in pseudo color visuals, so texture
 * mapping shuld work on PseudoColor, DirectColor, TrueColor using Mesa. Mono
 * is not officially supported for both OpenGL and Mesa, but seems to not crash
 * Mesa.
 *
 * In real OpenGL, PseudoColor DO NOT support texture map (as far as I know).
 */

#ifdef STANDALONE
# define DEFAULTS			"*delay:		20000   \n" \
							"*showFPS:      False   \n"

# define release_stairs 0
# include "xlockmore.h"		/* from the xscreensaver distribution */
#else /* !STANDALONE */
# include "xlock.h"			/* from the xlockmore distribution */

#endif /* !STANDALONE */

#ifdef USE_GL

#if 0
#include "e_textures.h"
#else
#include "ximage-loader.h"
#include "images/gen/wood_png.h"
#endif

#include "sphere.h"
#include "gltrackball.h"

ENTRYPOINT ModeSpecOpt stairs_opts =
{0, NULL, 0, NULL, NULL};

#ifdef USE_MODULES
ModStruct   stairs_description =
{"stairs", "init_stairs", "draw_stairs", NULL,
 "draw_stairs", "change_stairs", "free_stairs", &stairs_opts,
 1000, 1, 1, 1, 4, 1.0, "",
 "Shows Infinite Stairs, an Escher-like scene", 0, NULL};

#endif

#define Scale4Window               0.3
#define Scale4Iconic               0.4

#define sqr(A)                     ((A)*(A))

#ifndef Pi
#define Pi                         M_PI
#endif

/*************************************************************************/

typedef struct {
	GLint       WindH, WindW;
	GLfloat     step;
	int         rotating;
	int         AreObjectsDefined[1];
	int         sphere_position;
	int         sphere_tick;
	GLXContext *glx_context;
    trackball_state *trackball;
    Bool button_down_p;
    GLuint objects;
} stairsstruct;

static const float front_shininess[] = {60.0};
static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
static const float position0[] = {1.0, 1.0, 1.0, 0.0};
static const float position1[] = {-1.0, -1.0, 1.0, 0.0};
static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
static const float lmodel_twoside[] = {GL_TRUE};

static const float MaterialYellow[] = {0.7, 0.7, 0.0, 1.0};
static const float MaterialWhite[] = {0.7, 0.7, 0.7, 1.0};

static const float ball_positions[] = {
  -3.0, 3.0, 1.0,
  -3.0, 2.8, 2.0,
  -3.0, 2.6, 3.0,

  -2.0, 2.4, 3.0,
  -1.0, 2.2, 3.0,
   0.0, 2.0, 3.0,
   1.0, 1.8, 3.0,
   2.0, 1.6, 3.0,

   2.0, 1.5, 2.0,
   2.0, 1.4, 1.0,
   2.0, 1.3, 0.0,
   2.0, 1.2, -1.0,
   2.0, 1.1, -2.0,

   1.0, 0.9, -2.0,
   0.0, 0.7, -2.0,
  -1.0, 0.5, -2.0,
};

#define NPOSITIONS ((sizeof ball_positions) / (sizeof ball_positions[0]) / 3)
#define SPHERE_TICKS 32

static stairsstruct *stairs = NULL;

static int
draw_block(GLfloat width, GLfloat height, GLfloat thickness)
{
    int polys = 0;
	glFrontFace(GL_CCW);
	glBegin(GL_QUADS);
	glNormal3f(0, 0, 1);
	glTexCoord2f(0, 0);
	glVertex3f(-width, -height, thickness);
	glTexCoord2f(1, 0);
	glVertex3f(width, -height, thickness);
	glTexCoord2f(1, 1);
	glVertex3f(width, height, thickness);
	glTexCoord2f(0, 1);
	glVertex3f(-width, height, thickness);
    polys++;
	glNormal3f(0, 0, -1);
	glTexCoord2f(0, 0);
	glVertex3f(-width, height, -thickness);
	glTexCoord2f(1, 0);
	glVertex3f(width, height, -thickness);
	glTexCoord2f(1, 1);
	glVertex3f(width, -height, -thickness);
	glTexCoord2f(0, 1);
	glVertex3f(-width, -height, -thickness);
    polys++;
	glNormal3f(0, 1, 0);
	glTexCoord2f(0, 0);
	glVertex3f(-width, height, thickness);
	glTexCoord2f(1, 0);
	glVertex3f(width, height, thickness);
	glTexCoord2f(1, 1);
	glVertex3f(width, height, -thickness);
	glTexCoord2f(0, 1);
	glVertex3f(-width, height, -thickness);
    polys++;
	glNormal3f(0, -1, 0);
	glTexCoord2f(0, 0);
	glVertex3f(-width, -height, -thickness);
	glTexCoord2f(1, 0);
	glVertex3f(width, -height, -thickness);
	glTexCoord2f(1, 1);
	glVertex3f(width, -height, thickness);
	glTexCoord2f(0, 1);
	glVertex3f(-width, -height, thickness);
    polys++;
	glNormal3f(1, 0, 0);
	glTexCoord2f(0, 0);
	glVertex3f(width, -height, thickness);
	glTexCoord2f(1, 0);
	glVertex3f(width, -height, -thickness);
	glTexCoord2f(1, 1);
	glVertex3f(width, height, -thickness);
	glTexCoord2f(0, 1);
	glVertex3f(width, height, thickness);
    polys++;
	glNormal3f(-1, 0, 0);
	glTexCoord2f(0, 0);
	glVertex3f(-width, height, thickness);
	glTexCoord2f(1, 0);
	glVertex3f(-width, height, -thickness);
	glTexCoord2f(1, 1);
	glVertex3f(-width, -height, -thickness);
	glTexCoord2f(0, 1);
	glVertex3f(-width, -height, thickness);
    polys++;
	glEnd();
    return polys;
}

static void
draw_stairs_internal(ModeInfo * mi)
{
	GLfloat     X;

    mi->polygon_count = 0;

	glPushMatrix();
	glPushMatrix();
	glTranslatef(-3.0, 0.1, 2.0);
	for (X = 0; X < 2; X++) {
        mi->polygon_count += draw_block(0.5, 2.7 + 0.1 * X, 0.5);
		glTranslatef(0.0, 0.1, -1.0);
	}
	glPopMatrix();
	glTranslatef(-3.0, 0.0, 3.0);
	glPushMatrix();

	for (X = 0; X < 6; X++) {
		mi->polygon_count += draw_block(0.5, 2.6 - 0.1 * X, 0.5);
		glTranslatef(1.0, -0.1, 0.0);
	}
	glTranslatef(-1.0, -0.9, -1.0);
	for (X = 0; X < 5; X++) {
		mi->polygon_count += draw_block(0.5, 3.0 - 0.1 * X, 0.5);
		glTranslatef(0.0, 0.0, -1.0);
	}
	glTranslatef(-1.0, -1.1, 1.0);
	for (X = 0; X < 3; X++) {
		mi->polygon_count += draw_block(0.5, 3.5 - 0.1 * X, 0.5);
		glTranslatef(-1.0, -0.1, 0.0);
	}
	glPopMatrix();
	glPopMatrix();
}

/*#define DEBUG*/
/*#define DEBUG_PATH*/

static int
draw_sphere(int pos, int tick)
{
    int pos2 = (pos+1) % NPOSITIONS;
    GLfloat x1 = ball_positions[pos*3];
    GLfloat y1 = ball_positions[pos*3+1];
    GLfloat z1 = ball_positions[pos*3+2];
    GLfloat x2 = ball_positions[pos2*3];
    GLfloat y2 = ball_positions[pos2*3+1];
    GLfloat z2 = ball_positions[pos2*3+2];
    GLfloat frac = tick / (GLfloat) SPHERE_TICKS;
    GLfloat x = x1 + (x2 - x1) * frac;
    GLfloat y = y1 + (y2 - y1) * frac + (2 * sin (M_PI * frac));
    GLfloat z = z1 + (z2 - z1) * frac;
    int polys = 0;

	glPushMatrix();

# ifdef DEBUG_PATH
    glVertex3f(x, y, z);
    if (tick == 0) {
      glVertex3f(x, y-7.5, z);
      glVertex3f(x, y, z); glVertex3f(x, y, z-0.6);
      glVertex3f(x, y, z); glVertex3f(x, y, z+0.6);
      glVertex3f(x, y, z); glVertex3f(x+0.6, y, z);
      glVertex3f(x, y, z); glVertex3f(x-0.6, y, z);
      glVertex3f(x, y, z);
    }

# else /* !DEBUG_PATH */
    y += 0.5;
    glTranslatef(x, y, z);

    glScalef (0.5, 0.5, 0.5);

    /* make ball a little smaller on the gap to obscure distance */
	if (pos == NPOSITIONS-1)
      glScalef (0.95, 0.95, 0.95);

	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialYellow);
    glDisable (GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glFrontFace(GL_CCW);
    polys += unit_sphere (32, 32, False);
	glShadeModel(GL_FLAT);
    glEnable (GL_TEXTURE_2D);
#endif /* !DEBUG_PATH */

	glPopMatrix();
    return polys;
}



ENTRYPOINT void
reshape_stairs (ModeInfo * mi, int width, int height)
{
	stairsstruct *sp = &stairs[MI_SCREEN(mi)];
    int y = 0;

    if (width > height * 5) {   /* tiny window: show middle */
      height = width;
      y = -height/2;
    }

	glViewport(0, y, sp->WindW = (GLint) width, sp->WindH = (GLint) height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
	glMatrixMode(GL_MODELVIEW);
	if (width >= 1024) {
		glLineWidth(3);
		glPointSize(3);
	} else if (width >= 512) {
		glLineWidth(2);
		glPointSize(2);
	} else {
		glLineWidth(1);
		glPointSize(1);
	}
}

ENTRYPOINT Bool
stairs_handle_event (ModeInfo *mi, XEvent *event)
{
  stairsstruct *sp = &stairs[MI_SCREEN(mi)];

  if (gltrackball_event_handler (event, sp->trackball,
                                 MI_WIDTH (mi), MI_HEIGHT (mi),
                                 &sp->button_down_p))
    return True;
  else if (event->xany.type == KeyPress)
    {
      KeySym keysym;
      char c = 0;
      XLookupString (&event->xkey, &c, 1, &keysym, 0);
      if (c == ' ' || c == '\t')
        {
          gltrackball_reset (sp->trackball, 0, 0);
          return True;
        }
    }

  return False;
}


static void
pinit(ModeInfo *mi)
{
  /* int status; */
	glClearDepth(1.0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, position0);
	glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
	glLightfv(GL_LIGHT1, GL_POSITION, position1);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
	glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHT1);
	glEnable(GL_NORMALIZE);
	glCullFace(GL_BACK);

	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialWhite);
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_CULL_FACE);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

#if 0
    clear_gl_error();
    status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
                               WoodTextureWidth, WoodTextureHeight,
                               GL_RGB, GL_UNSIGNED_BYTE, WoodTextureData);
    if (status)
      {
        const char *s = (char *) gluErrorString (status);
        fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
                 progname, WoodTextureWidth, WoodTextureHeight,
                 (s ? s : "(unknown)"));
        exit (1);
      }
    check_gl_error("mipmapping");
#else
    {
      XImage *img = image_data_to_ximage (mi->dpy, mi->xgwa.visual,
                                          wood_png, sizeof(wood_png));
	  glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
                    img->width, img->height, 0,
                    GL_RGBA, GL_UNSIGNED_BYTE, img->data);
      check_gl_error("texture");
      XDestroyImage (img);
    }
#endif

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
}

ENTRYPOINT void
init_stairs (ModeInfo * mi)
{
	int         screen = MI_SCREEN(mi);
	stairsstruct *sp;

	MI_INIT (mi, stairs);
	sp = &stairs[screen];

	sp->step = 0.0;
	sp->rotating = 0;
	sp->sphere_position = NRAND(NPOSITIONS);
	sp->sphere_tick = 0;

	if ((sp->glx_context = init_GL(mi)) != NULL) {

		reshape_stairs(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
		glDrawBuffer(GL_BACK);
		if (!glIsList(sp->objects))
			sp->objects = glGenLists(1);
		pinit(mi);
	} else {
		MI_CLEARWINDOW(mi);
	}

    sp->trackball = gltrackball_init (False);
}

ENTRYPOINT void
draw_stairs (ModeInfo * mi)
{
	stairsstruct *sp = &stairs[MI_SCREEN(mi)];
    GLfloat rot = current_device_rotation();

	Display    *display = MI_DISPLAY(mi);
	Window      window = MI_WINDOW(mi);

	if (!sp->glx_context)
		return;

	glXMakeCurrent(display, window, *sp->glx_context);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();

    glRotatef(rot, 0, 0, 1);

	glTranslatef(0.0, 0.0, -10.0);

	if (!MI_IS_ICONIC(mi)) {
		glScalef(Scale4Window * sp->WindH / sp->WindW, Scale4Window, Scale4Window);
	} else {
		glScalef(Scale4Iconic * sp->WindH / sp->WindW, Scale4Iconic, Scale4Iconic);
	}

# ifdef HAVE_MOBILE	/* Keep it the same relative size when rotated. */
  {
    GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
    if (rot != 0 && rot != 180 && rot != -180)
      glScalef (1/h, 1/h, 1/h);
  }
# endif

    gltrackball_rotate (sp->trackball);

    glTranslatef(0, 0.5, 0);
	glRotatef(44.5, 1, 0, 0);
    glRotatef(50, 0, 1, 0);

    if (!sp->rotating) {
      if ((LRAND() % 500) == 0)
        sp->rotating = (LRAND() & 1) ? 1 : -1;
    }

    if (sp->rotating) {
      glRotatef(sp->rotating * sp->step, 0, 1, 0);
      if (sp->step >= 360) {
        sp->rotating = 0;
		sp->step = 0;
      }

# ifndef DEBUG
      if (!sp->button_down_p)
        sp->step += 2;
# endif /* DEBUG */
    }

	draw_stairs_internal(mi);


# ifdef DEBUG
    {
      int i, j;
#  ifdef DEBUG_PATH
      glDisable(GL_LIGHTING);
      glDisable(GL_TEXTURE_2D);
      glBegin (GL_LINE_LOOP);
#  endif /* DEBUG_PATH */
      for (i = 0; i < NPOSITIONS; i ++)
        for (j = 0; j < SPHERE_TICKS; j++)
          mi->polygon_count += draw_sphere(i, j);
#  ifdef DEBUG_PATH
      glEnd();
      glEnable(GL_LIGHTING);
      glEnable(GL_TEXTURE_2D);
#  endif /* DEBUG_PATH */
    }
#else  /* !DEBUG */
    mi->polygon_count += draw_sphere(sp->sphere_position, sp->sphere_tick);
#endif /* !DEBUG */

    if (sp->button_down_p)
      ;
    else if (++sp->sphere_tick >= SPHERE_TICKS)
      {
        sp->sphere_tick = 0;
        if (++sp->sphere_position >= NPOSITIONS)
          sp->sphere_position = 0;
      }

	glPopMatrix();

    if (mi->fps_p) do_fps (mi);
	glFlush();

	glXSwapBuffers(display, window);
}

#ifndef STANDALONE
ENTRYPOINT void
change_stairs (ModeInfo * mi)
{
	stairsstruct *sp = &stairs[MI_SCREEN(mi)];

	if (!sp->glx_context)
		return;

	glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *sp->glx_context);
	pinit();
}
#endif /* !STANDALONE */

ENTRYPOINT void
free_stairs (ModeInfo * mi)
{
	stairsstruct *sp = &stairs[MI_SCREEN(mi)];
    if (!sp->glx_context) return;
	glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *sp->glx_context);
	if (glIsList(sp->objects)) glDeleteLists(sp->objects, 1);
    if (sp->trackball) gltrackball_free (sp->trackball);
}

XSCREENSAVER_MODULE ("Stairs", stairs)

#endif