summaryrefslogtreecommitdiffstats
path: root/hacks/glx/blocktube.c
blob: 811b1859761143f056539d371f16169bdb194ba4 (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
/* blocktube, Copyright (c) 2003 Lars Damerow <lars@oddment.org>
 *
 * Based on Jamie Zawinski's original dangerball code.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#define DEBUG 1

#define DEFAULTS        "*delay:	40000           \n" \
                        "*wireframe:    False           \n" \
			"*showFPS:      False           \n" \
			"*suppressRotationAnimation: True\n" \

# define release_blocktube 0
# define blocktube_handle_event xlockmore_no_events
#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))

#include "xlockmore.h"
#include "colors.h"
#include <math.h>
#include <ctype.h>

#ifdef USE_GL /* whole file */

#define DEF_HOLDTIME    "1000"
#define DEF_CHANGETIME  "200"
#define MAX_ENTITIES     1000
#define DEF_TEXTURE     "True"
#define DEF_FOG         "True"

#if defined(USE_XPM) || defined(USE_XPMINC) || defined(STANDALONE)
/* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
#include "ximage-loader.h"
#define I_HAVE_XPM

#include "images/gen/blocktube_png.h"
#endif /* HAVE_XPM */

typedef struct {
    int id, r, g, b;
    GLfloat tVal;
    int age;
    int lifetime;
    GLfloat position[3];
    GLfloat angle;
    GLfloat angularVelocity;
} entity;

typedef struct {
  GLXContext *glx_context;
  GLuint  block_dlist;
  int nextID;

  entity entities[MAX_ENTITIES];
  float targetR, targetG, targetB,
    currentR, currentG, currentB,
    deltaR, deltaG, deltaB;
  int counter;
  int changing;
  GLfloat zoom;
  GLfloat tilt;
  GLuint envTexture;
  XImage *texti;

  GLfloat tunnelLength;
  GLfloat tunnelWidth;
  int polys;

} blocktube_configuration;

static blocktube_configuration *lps = NULL;

static GLint holdtime;
static GLint changetime;
static int do_texture;
static int do_fog;

static XrmOptionDescRec opts[] = {
    { "-holdtime",  ".holdtime",  XrmoptionSepArg, 0 },
    { "-changetime",  ".changetime",  XrmoptionSepArg, 0 },
    {"-texture",     ".texture",   XrmoptionNoArg, "True" },
    {"+texture",     ".texture",   XrmoptionNoArg, "False" },
    {"-fog",         ".fog",       XrmoptionNoArg, "True" },
    {"+fog",         ".fog",       XrmoptionNoArg, "False" },
};

static argtype vars[] = {
    {&holdtime, "holdtime",  "Hold Time",  DEF_HOLDTIME,  t_Int},
    {&changetime, "changetime",  "Change Time",  DEF_CHANGETIME, \
     t_Int},
    {&do_texture, "texture",    "Texture", DEF_TEXTURE,   t_Bool},
    {&do_fog,     "fog",        "Fog",     DEF_FOG,       t_Bool},
};

static OptionStruct desc[] = {
    {"-holdtime", "how long to stay on the same color"},
    {"-changetime", "how long it takes to fade to a new color"},
};

ENTRYPOINT ModeSpecOpt blocktube_opts = {countof(opts), opts, countof(vars), vars, desc};

#ifdef USE_MODULES
ModStruct blocktube_description =
    {"blocktube", "init_blocktube", "draw_blocktube", (char *)NULL,
     "draw_blocktube", "init_blocktube", "free_blocktube", &blocktube_opts,
     40000, 30, 1, 1, 64, 1.0, "",
     "A shifting tunnel of reflective blocks", 0, NULL};
#endif /* USE_MODULES */

#if defined( I_HAVE_XPM )
static Bool LoadGLTextures(ModeInfo *mi)
{
    blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
    Bool status;

    status = True;
    glGenTextures(1, &lp->envTexture);
    glBindTexture(GL_TEXTURE_2D, lp->envTexture);
    lp->texti = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
                                     blocktube_png, sizeof(blocktube_png));
    if (!lp->texti) {
        status = False;
    } else {
        glPixelStorei(GL_UNPACK_ALIGNMENT,1);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lp->texti->width, lp->texti->height, 0,
                     GL_RGBA, GL_UNSIGNED_BYTE, lp->texti->data);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
# ifndef HAVE_JWZGLES /* #### Sphere maps unimplemented */
        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
# endif
    }
    return status;
}
#endif

static void newTargetColor(blocktube_configuration *lp)
{
    int luminance = 0;

    while (luminance <= 150) {
        lp->targetR = random() % 256;
        lp->targetG = random() % 256;
        lp->targetB = random() % 256;
        lp->deltaR = (lp->targetR - lp->currentR) / changetime;
        lp->deltaG = (lp->targetG - lp->currentG) / changetime;
        lp->deltaB = (lp->targetB - lp->currentB) / changetime;
        luminance = 0.3 * lp->targetR + 0.59 * lp->targetG + 0.11 * lp->targetB;
    }
}

static void randomize_entity (blocktube_configuration *lp, entity *ent)
{
    ent->id = lp->nextID++;
    ent->tVal = 1 - ((float)random() / RAND_MAX / 1.5);
    ent->age = 0;
    ent->lifetime = 100;
    ent->angle = random() % 360;
    ent->angularVelocity = 0.5-((float)(random()) / RAND_MAX);
    ent->position[0] = (float)(random()) / RAND_MAX + lp->tunnelWidth;
    ent->position[1] = (float)(random()) / RAND_MAX * 2;
    ent->position[2] = -(float)(random()) / RAND_MAX * lp->tunnelLength;
}

static void entityTick(blocktube_configuration *lp, entity *ent)
{
    ent->angle += ent->angularVelocity;
    ent->position[2] += 0.1;
    if (ent->position[2] > lp->zoom) {
        ent->position[2] = -lp->tunnelLength + ((float)(random()) / RAND_MAX) * 20;
    }
    ent->age += 0.1;
}

static void tick(blocktube_configuration *lp)
{
    lp->counter--;
    if (!lp->counter) {
        if (!lp->changing) {
            newTargetColor(lp);
            lp->counter = changetime;
        } else {
            lp->counter = holdtime;
        }
        lp->changing = (!lp->changing);
    } else {
        if (lp->changing) {
            lp->currentR += lp->deltaR;
            lp->currentG += lp->deltaG;
            lp->currentB += lp->deltaB;
        }
    }
}

static int cube_vertices(float x, float y, float z, int wire);

ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height);

ENTRYPOINT void init_blocktube (ModeInfo *mi)
{
    int loop;
    GLfloat fogColor[4] = {0,0,0,1};
    blocktube_configuration *lp;
    int wire = MI_IS_WIREFRAME(mi);

    MI_INIT(mi, lps);

    lp = &lps[MI_SCREEN(mi)];
    lp->glx_context = init_GL(mi);

    lp->zoom = 30;
    lp->tilt = 4.5;
    lp->tunnelLength = 200;
    lp->tunnelWidth = 5;

    if (wire) {
      do_fog = False;
      do_texture = False;
      glLineWidth(2);
    }

    lp->block_dlist = glGenLists (1);
    glNewList (lp->block_dlist, GL_COMPILE);
    lp->polys = cube_vertices(0.15, 1.2, 5.25, wire);
    glEndList ();

#if defined( I_HAVE_XPM )
    if (do_texture) {
      if (!LoadGLTextures(mi)) {
        fprintf(stderr, "%s: can't load textures!\n", progname);
        exit(1);
      }
      glEnable(GL_TEXTURE_2D);
    }
#endif

    /* kick on the fog machine */
    if (do_fog) {
      glEnable(GL_FOG);
      glFogi(GL_FOG_MODE, GL_LINEAR);
      glHint(GL_FOG_HINT, GL_NICEST);
      glFogf(GL_FOG_START, 0);
      glFogf(GL_FOG_END, lp->tunnelLength/1.8);
      glFogfv(GL_FOG_COLOR, fogColor);
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    }
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glClearDepth(1.0f);

    if (!do_texture && !wire) {
      /* If there is no texture, the boxes don't show up without a light.
         Though I don't understand why all the blocks come out gray.
       */
      GLfloat pos[4] = {0.0, 1.0, 1.0, 0.0};
      GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
      GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
      GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
      glLightfv(GL_LIGHT0, GL_POSITION, pos);
      glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
      glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
      glLightfv(GL_LIGHT0, GL_SPECULAR, spc);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
    }

    lp->counter = holdtime;
    lp->currentR = random() % 256;
    lp->currentG = random() % 256;
    lp->currentB = random() % 256;
    newTargetColor(lp);
    for (loop = 0; loop < MAX_ENTITIES; loop++)
    {
        randomize_entity(lp, &lp->entities[loop]);
    }
    reshape_blocktube(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
    glFlush();
}

ENTRYPOINT void free_blocktube (ModeInfo *mi)
{
  blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
# if defined ( I_HAVE_XPM )
  if (!lp->glx_context) return;
  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *lp->glx_context);
  if (lp->envTexture) glDeleteTextures(1, &lp->envTexture);
  if (lp->texti) XDestroyImage(lp->texti);
  if (glIsList(lp->block_dlist)) glDeleteLists(lp->block_dlist, 1);
  if (lp->envTexture) glDeleteTextures (1, &lp->envTexture);

# endif
}

ENTRYPOINT void reshape_blocktube (ModeInfo *mi, int width, int height)
{
    blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
    GLfloat h = (GLfloat) height / (GLfloat) width;
    int y = 0;

    glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *lp->glx_context);

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

    glViewport(0, y, (GLint) width, (GLint) height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, 1/h, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);

# ifdef HAVE_MOBILE	/* Keep it the same relative size when rotated. */
    {
      int o = (int) current_device_rotation();
      if (o != 0 && o != 180 && o != -180)
        glScalef (1/h, 1/h, 1/h);
    }
# endif
}

static int cube_vertices(float x, float y, float z, int wire)
{
    int polygon_count = 0;
    float x2, y2, z2, nv = 0.7;
    x2 = x/2;
    y2 = y/2;
    z2 = z/2;

    glFrontFace(GL_CW);

    glNormal3f(0, 0, nv);
    glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2,  z2);
    glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
    glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
    glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
    polygon_count++;
    glEnd();

    glNormal3f(0, 0, -nv);
    glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
    glTexCoord2f(1.0, 0.0); glVertex3f(-x2, -y2, -z2);
    glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2, -z2);
    glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2, -z2);
    glTexCoord2f(0.0, 0.0); glVertex3f(-x2,  y2, -z2);
    polygon_count++;
    glEnd();

    glNormal3f(0, nv, 0);
    glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
    glTexCoord2f(0.0, 1.0); glVertex3f(-x2,  y2, -z2);
    glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
    glTexCoord2f(1.0, 0.0); glVertex3f( x2,  y2,  z2);
    glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
    polygon_count++;
    glEnd();

    glNormal3f(0, -nv, 0);
    glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
    glTexCoord2f(1.0, 1.0); glVertex3f(-x2, -y2, -z2);
    glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
    glTexCoord2f(0.0, 0.0); glVertex3f( x2, -y2,  z2);
    glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
    polygon_count++;
    glEnd();

    if (wire) return polygon_count;

    glNormal3f(nv, 0, 0);
    glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
    glTexCoord2f(1.0, 0.0); glVertex3f( x2, -y2, -z2);
    glTexCoord2f(1.0, 1.0); glVertex3f( x2, -y2,  z2);
    glTexCoord2f(0.0, 1.0); glVertex3f( x2,  y2,  z2);
    glTexCoord2f(0.0, 0.0); glVertex3f( x2,  y2, -z2);
    polygon_count++;
    glEnd();

    glNormal3f(-nv, 0, 0);
    glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex3f(-x2, -y2, -z2);
    glTexCoord2f(1.0, 0.0); glVertex3f(-x2,  y2, -z2);
    glTexCoord2f(1.0, 1.0); glVertex3f(-x2,  y2,  z2);
    glTexCoord2f(0.0, 1.0); glVertex3f(-x2, -y2,  z2);
    polygon_count++;
    glEnd();

    return polygon_count;
}

static void draw_block(ModeInfo *mi, entity *ent)
{
    blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
    glCallList (lp->block_dlist);
    mi->polygon_count += lp->polys;
}

ENTRYPOINT void
draw_blocktube (ModeInfo *mi)
{
    blocktube_configuration *lp = &lps[MI_SCREEN(mi)];
    Display *dpy = MI_DISPLAY(mi);
    Window window = MI_WINDOW(mi);
    entity *cEnt = NULL;
    int loop = 0;

    if (!lp->glx_context)
      return;

    mi->polygon_count = 0;

    glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *lp->glx_context);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    if (do_texture) {
      glEnable(GL_TEXTURE_GEN_S);
      glEnable(GL_TEXTURE_GEN_T);
      glBindTexture(GL_TEXTURE_2D, lp->envTexture);
    }

    for (loop = 0; loop < MAX_ENTITIES; loop++) {
        cEnt = &lp->entities[loop];

        glLoadIdentity();
        glTranslatef(0.0f, 0.0f, lp->zoom);
        glRotatef(lp->tilt, 1.0f, 0.0f, 0.0f);
        glRotatef(cEnt->angle, 0.0f, 0.0f, 1.0f);
        glTranslatef(cEnt->position[0], cEnt->position[1], cEnt->position[2]);
        glColor4ub((int)(lp->currentR * cEnt->tVal),
                   (int)(lp->currentG * cEnt->tVal),
                   (int)(lp->currentB * cEnt->tVal), 255);
        draw_block(mi, cEnt);
        entityTick(lp, cEnt);
    }
    tick(lp);

    if (mi->fps_p) do_fps (mi);
    glFinish();
    glXSwapBuffers(dpy, window);
}

XSCREENSAVER_MODULE ("BlockTube", blocktube)

#endif /* USE_GL */