summaryrefslogtreecommitdiffstats
path: root/hacks/glx/cubetwist.c
blob: 61af2879219bc564d2379a29d0bd54d4ac83d3c2 (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
591
592
593
594
595
/* cubetwist, Copyright (c) 2016-2017 Jamie Zawinski <jwz@jwz.org>
 *
 * 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 DEFAULTS	"*delay:	30000       \n" \
			"*showFPS:      False       \n" \
			"*wireframe:    False       \n" \
			"*suppressRotationAnimation: True\n" \

# define release_cube 0
#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))

#include "xlockmore.h"
#include "normals.h"
#include "rotator.h"
#include "gltrackball.h"
#include <ctype.h>

#ifdef USE_GL /* whole file */


#define DEF_SPIN         "True"
#define DEF_WANDER       "True"
#define DEF_SPEED        "1.0"
#define DEF_FLAT         "True"
#define DEF_THICKNESS    "0.0"
#define DEF_DISPLACEMENT "0.0"

typedef struct cube cube;
struct cube {
  GLfloat size, thickness;
  XYZ pos, rot;
  GLfloat color[4];
  cube *next;
};

typedef struct oscillator oscillator;
struct oscillator {
  double ratio, from, to, speed, *var;
  int remaining;
  oscillator *next;
};

typedef struct {
  GLXContext *glx_context;
  rotator *rot;
  trackball_state *trackball;
  Bool button_down_p;
  cube *cubes;
  oscillator *oscillators;
} cube_configuration;

static cube_configuration *bps = NULL;

static Bool do_flat;
static Bool do_spin;
static GLfloat speed;
static GLfloat thickness;
static GLfloat displacement;
static Bool do_wander;

static XrmOptionDescRec opts[] = {
  { "-spin",   ".spin",   XrmoptionNoArg, "True" },
  { "+spin",   ".spin",   XrmoptionNoArg, "False" },
  { "-wander", ".wander", XrmoptionNoArg, "True" },
  { "+wander", ".wander", XrmoptionNoArg, "False" },
  { "-flat",   ".flat",   XrmoptionNoArg, "True" },
  { "+flat",   ".flat",   XrmoptionNoArg, "False" },
  { "-speed",  ".speed",  XrmoptionSepArg, 0 },
  { "-thickness",  ".thickness",  XrmoptionSepArg, 0 },
  { "-displacement",  ".displacement",  XrmoptionSepArg, 0 },
};

static argtype vars[] = {
  {&do_flat,   "flat",   "flat",   DEF_FLAT,   t_Bool},
  {&do_spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
  {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
  {&speed,     "speed",  "Speed",  DEF_SPEED,  t_Float},
  {&thickness, "thickness", "Thickness", DEF_THICKNESS, t_Float},
  {&displacement, "displacement", "Displacement", DEF_DISPLACEMENT, t_Float},
};

ENTRYPOINT ModeSpecOpt cube_opts = {countof(opts), opts, countof(vars), vars, NULL};


static int
draw_strut (ModeInfo *mi, cube *c)
{
  int wire = MI_IS_WIREFRAME(mi);
  int polys = 0;

  glPushMatrix();
  glFrontFace (GL_CW);
  glNormal3f (0, 0, -1);
  glTranslatef (-c->size/2, -c->size/2, -c->size/2);

  glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
  glVertex3f (0, 0, 0);
  glVertex3f (c->size, 0, 0);
  glVertex3f (c->size - c->thickness, c->thickness, 0);
  glVertex3f (c->thickness, c->thickness, 0);
  glEnd();
  polys += 2;

  glNormal3f (0, 1, 0);
  glBegin (wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
  glVertex3f (c->thickness, c->thickness, 0);
  glVertex3f (c->size - c->thickness, c->thickness, 0);
  glVertex3f (c->size - c->thickness, c->thickness, c->thickness);
  glVertex3f (c->thickness, c->thickness, c->thickness);
  glEnd();
  polys += 2;
  glPopMatrix();

  return polys;
}


static int
draw_cubes (ModeInfo *mi, cube *c)
{
  int polys = 0;
  int i, j;

  glColor4fv (c->color);
  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c->color);

  glPushMatrix();
  for (j = 0; j < 6; j++)
    {
      for (i = 0; i < 4; i++)
        {
          polys += draw_strut (mi, c);
          glRotatef (90, 0, 0, 1);
        }
      if (j == 3)
        glRotatef (90, 0, 0, 1);
      if (j < 4)
        glRotatef (90, 0, 1, 0);
      else
        glRotatef (180, 1, 0, 0);
    }
  glPopMatrix();

  if (c->next)
    {
      /* This leaves rotations on the prevailing matrix stack, but since
         this is a tail-call, that's fine.  Don't blow the matrix stack. */
      glRotatef (c->rot.x, 1, 0, 0);
      glRotatef (c->rot.y, 0, 1, 0);
      glRotatef (c->rot.z, 0, 0, 1);
      glTranslatef (c->pos.x, c->pos.y, c->pos.z);
      c->next->pos = c->pos;
      c->next->rot = c->rot;
      polys += draw_cubes (mi, c->next);
    }

  check_gl_error("cubetwist");
  return polys;
}


static void
make_cubes (ModeInfo *mi)
{
  cube_configuration *bp = &bps[MI_SCREEN(mi)];
  GLfloat step = 2 * (thickness + displacement);
  GLfloat size = 1.0;
  cube *tail = 0;
  GLfloat cc[4], cstep;
  int depth = 0;
  cube *c;

  cc[0] = 0.3 + frand(0.7);
  cc[1] = 0.3 + frand(0.7);
  cc[2] = 0.3 + frand(0.7);
  cc[3] = 1;

  if (bp->cubes) abort();
  while (1)
    {
      cube *c = (cube *) calloc (1, sizeof (*c));
      c->size = size;
      c->thickness = thickness;
      if (tail)
        tail->next = c;
      else
        bp->cubes = c;
      tail = c;

      depth++;
      size -= step;
      if (size <= step)
        break;
    }

  cstep = 0.8 / depth;
  for (c = bp->cubes; c; c = c->next)
    {
      memcpy (c->color, cc, sizeof(cc));
      cc[0] -= cstep;
      cc[1] -= cstep;
      cc[2] -= cstep;
    }
}


static GLfloat
ease_fn (GLfloat r)
{
  return cos ((r/2 + 1) * M_PI) + 1; /* Smooth curve up, end at slope 1. */
}


static GLfloat
ease_ratio (GLfloat r)
{
  GLfloat ease = 0.5;
  if      (r <= 0)     return 0;
  else if (r >= 1)     return 1;
  else if (r <= ease)  return     ease * ease_fn (r / ease);
  else if (r > 1-ease) return 1 - ease * ease_fn ((1 - r) / ease);
  else                 return r;
}


static void
tick_oscillators (ModeInfo *mi)
{
  cube_configuration *bp = &bps[MI_SCREEN(mi)];
  oscillator *prev = 0;
  oscillator *a = bp->oscillators;
  GLfloat tick = 0.1 / speed;

  while (a)
    {
      oscillator *next = a->next;
      a->ratio += tick * a->speed;
      if (a->ratio > 1)
        a->ratio = 1;

      *a->var = a->from + (a->to - a->from) * ease_ratio (a->ratio);

      if (a->ratio < 1)			/* mid cycle */
        prev = a;
      else if (--a->remaining <= 0)	/* ended, and expired */
        {
          if (prev)
            prev->next = next;
          else
            bp->oscillators = next;
          free (a);
        }
      else				/* keep going the other way */
        {
          GLfloat swap = a->from;
          a->from = a->to;
          a->to = swap;
          a->ratio = 0;
          prev = a;
        }

      a = next;
    }
}


static void
add_oscillator (ModeInfo *mi, double *var, GLfloat speed, GLfloat to,
                int repeat)
{
  cube_configuration *bp = &bps[MI_SCREEN(mi)];
  oscillator *a;

  /* If an oscillator is already running on this variable, don't
     add another. */
  for (a = bp->oscillators; a && a->next; a = a->next)
    if (a->var == var)
      return;

  a = (oscillator *) calloc (1, sizeof (*a));
  if (repeat <= 0) abort();
  a->ratio = 0;
  a->from = *var;
  a->to = to;
  a->speed = speed;
  a->var = var;
  a->remaining = repeat;
  a->next = bp->oscillators;
  bp->oscillators = a;
# if 0
  fprintf (stderr, "%s: %3d %6.2f -> %6.2f %s\n",
           progname, repeat, *var, to,
           (var == &bp->midpoint.z ? "z" :
            var == &bp->tilt ? "tilt" :
            var == &bp->axial_radius ? "r" :
            var == &bp->speed ? "speed" : "?"));
# endif
}


#undef RANDSIGN
#define RANDSIGN() ((random() & 1) ? 1 : -1)

static void
add_random_oscillator (ModeInfo *mi)
{
  cube_configuration *bp = &bps[MI_SCREEN(mi)];
  cube *c = bp->cubes;
  double s1 = speed * 0.07;
  double s2 = speed * 0.3;
  double disp = (thickness + displacement);
  int c1 = 1 + ((random() % 4) ? 0 : (random() % 3));
  int c2 = 2;
  int n = random() % 6;

  switch (n) {
  case 0: add_oscillator (mi, &c->rot.x, s1, 90 * RANDSIGN(), c1); break;
  case 1: add_oscillator (mi, &c->rot.y, s1, 90 * RANDSIGN(), c1); break;
  case 2: add_oscillator (mi, &c->rot.z, s1, 90 * RANDSIGN(), c1); break;
  case 3: add_oscillator (mi, &c->pos.x, s2, disp * RANDSIGN(), c2); break;
  case 4: add_oscillator (mi, &c->pos.y, s2, disp * RANDSIGN(), c2); break;
  case 5: add_oscillator (mi, &c->pos.z, s2, disp * RANDSIGN(), c2); break;
  default: abort(); break;
  }
}


/* Window management, etc
 */
ENTRYPOINT void
reshape_cube (ModeInfo *mi, int width, int height)
{
  GLfloat h = (GLfloat) height / (GLfloat) width;
  int y = 0;

  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 (30.0, 1/h, 1.0, 100.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt( 0.0, 0.0, 30.0,
             0.0, 0.0, 0.0,
             0.0, 1.0, 0.0);

# 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

  glClear(GL_COLOR_BUFFER_BIT);
}


ENTRYPOINT Bool
cube_handle_event (ModeInfo *mi, XEvent *event)
{
  cube_configuration *bp = &bps[MI_SCREEN(mi)];

  if (gltrackball_event_handler (event, bp->trackball,
                                 MI_WIDTH (mi), MI_HEIGHT (mi),
                                 &bp->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')
        {
          while (bp->cubes)
            {
              cube *c = bp->cubes->next;
              free (bp->cubes);
              bp->cubes = c;
            }

          while (bp->oscillators)
            {
              oscillator *o = bp->oscillators->next;
              free (bp->oscillators);
              bp->oscillators = o;
            }

          if (random() & 1)
            {
              thickness = 0.03 + frand(0.02);
              displacement = (random() & 1) ? 0 : (thickness / 3);
            }
          else
            {
              thickness = 0.001 + frand(0.02);
              displacement = 0;
            }

          make_cubes (mi);

          return True;
        }
    }

  return False;
}


ENTRYPOINT void 
init_cube (ModeInfo *mi)
{
  cube_configuration *bp;
  int wire = MI_IS_WIREFRAME(mi);

  MI_INIT (mi, bps);

  bp = &bps[MI_SCREEN(mi)];

  bp->glx_context = init_GL(mi);

  reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));

  if (!wire && !do_flat)
    {
      GLfloat color[4] = {1, 1, 1, 1};
      GLfloat cspec[4] = {1, 1, 0, 1};
      static const GLfloat shiny = 30;

      static GLfloat pos0[4] = { 0.5, -1, -0.5, 0};
      static GLfloat pos1[4] = {-0.75, -1, 0, 0};
      static GLfloat amb[4] = {0, 0, 0, 1};
      static GLfloat dif[4] = {1, 1, 1, 1};
      static GLfloat spc[4] = {1, 1, 1, 1};

      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glEnable(GL_LIGHT1);
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_CULL_FACE);

      glLightfv(GL_LIGHT0, GL_POSITION, pos0);
      glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
      glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
      glLightfv(GL_LIGHT0, GL_SPECULAR, spc);

      glLightfv(GL_LIGHT1, GL_POSITION, pos1);
      glLightfv(GL_LIGHT1, GL_AMBIENT,  amb);
      glLightfv(GL_LIGHT1, GL_DIFFUSE,  dif);
      glLightfv(GL_LIGHT1, GL_SPECULAR, spc);

      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
      glMaterialfv (GL_FRONT, GL_SPECULAR,  cspec);
      glMateriali  (GL_FRONT, GL_SHININESS, shiny);
    }

  {
    double spin_speed   = 0.05;
    double wander_speed = 0.005;
    double spin_accel   = 1.0;

    bp->rot = make_rotator (do_spin ? spin_speed : 0,
                            do_spin ? spin_speed : 0,
                            do_spin ? spin_speed : 0,
                            spin_accel,
                            do_wander ? wander_speed : 0,
                            True);
    bp->trackball = gltrackball_init (True);
  }

  if (thickness > 0.5)
    thickness = 0.5;
  if (displacement > 0.5)
    displacement = 0.5;

  if (thickness <= 0.0001)
    {
      if (random() & 1)
        {
          thickness = 0.03 + frand(0.02);
          displacement = (random() & 1) ? 0 : (thickness / 3);
        }
      else
        {
          thickness = 0.001 + frand(0.02);
          displacement = 0;
        }
    }

  make_cubes (mi);
}


ENTRYPOINT void
draw_cube (ModeInfo *mi)
{
  cube_configuration *bp = &bps[MI_SCREEN(mi)];
  Display *dpy = MI_DISPLAY(mi);
  Window window = MI_WINDOW(mi);

  if (!bp->glx_context)
    return;

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

  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_CULL_FACE);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glPushMatrix ();

  glScalef(1.1, 1.1, 1.1);

  {
    double x, y, z;
    get_position (bp->rot, &x, &y, &z, !bp->button_down_p);
    glTranslatef((x - 0.5) * 4,
                 (y - 0.5) * 4,
                 (z - 0.5) * 2);

    gltrackball_rotate (bp->trackball);

    get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
    glRotatef (x * 360, 1.0, 0.0, 0.0);
    glRotatef (y * 360, 0.0, 1.0, 0.0);
    glRotatef (z * 360, 0.0, 0.0, 1.0);
  }

  mi->polygon_count = 0;

  glScalef (6, 6, 6);

  mi->polygon_count = draw_cubes (mi, bp->cubes);
  glPopMatrix ();

  if (!bp->button_down_p)
    tick_oscillators (mi);

  if (! bp->oscillators &&
      !bp->button_down_p &&
      !(random() % 60))
    {
      bp->cubes->pos.x = bp->cubes->pos.y = bp->cubes->pos.z = 0;
      bp->cubes->rot.x = bp->cubes->rot.y = bp->cubes->rot.z = 0;
      add_random_oscillator (mi);
    }

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

  glXSwapBuffers(dpy, window);
}

ENTRYPOINT void
free_cube (ModeInfo *mi)
{
  cube_configuration *bp = &bps[MI_SCREEN(mi)];
  while (bp->cubes)
    {
      cube *c = bp->cubes->next;
      free (bp->cubes);
      bp->cubes = c;
    }

  while (bp->oscillators)
    {
      oscillator *o = bp->oscillators->next;
      free (bp->oscillators);
      bp->oscillators = o;
    }
}


XSCREENSAVER_MODULE_2 ("CubeTwist", cubetwist, cube)

#endif /* USE_GL */