summaryrefslogtreecommitdiffstats
path: root/hacks/glx/beats.c
blob: 67a6eb891f10af9dcbcaea91458641a0ad44866f (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
/* beats, Copyright (c) 2020 David Eccles (gringer) <hacking@gringene.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.
 */

/* Beats changes the position of objects in time with a
 * synchronisation signal (or more correctly, based on the time
 * elapsed since the last synchronisation point). By default, the
 * system clock is used for this signal, with synchronisation
 * happening every minute. The location of objects is entirely
 * dependant on this synchronisation signal; there is no multi-object
 * state that needs to be stored, although there may be some styling
 * state required.
 */

#define DEFAULTS	"*count:	30          \n" \
			"*delay:	30000       \n" \
			"*showFPS:      False       \n" \
			"*wireframe:    False       \n" \

# define release_beats 0

#include "xlockmore.h"
#include "colors.h"
#include "sphere.h"
#include "hsv.h"
#include <ctype.h>
#include <sys/time.h>

#ifdef USE_GL /* whole file */

#define DEF_CYCLE       "-1"
#define DEF_TICK        "True"
#define DEF_BLUR        "True"

#define SPHERE_SLICES 16  /* how densely to render spheres */
#define SPHERE_STACKS 16


typedef struct {
  GLXContext *glx_context;
  Bool button_down_p;

  GLuint beats_list;

  GLfloat pos;

  int ball_count; /* Number of balls */
  int preset_cycle; /* Cycle to show (-1 for random) */
  Bool use_tick; /* Add tick for clockwise / galaxy */
  Bool use_blur; /* Motion blur */
  int ncolors;
  XColor *colors;
  int ccolor;
  int color_shift;

} beats_configuration;

static beats_configuration *bps = NULL;

static int cycle_arg;
static Bool tick_arg;
static Bool blur_arg;

static XrmOptionDescRec opts[] = {
  { "-cycle",   ".cycle",   XrmoptionSepArg, 0 },
  { "-count",   ".count",   XrmoptionSepArg, 0 },
  { "-tick",    ".tick",     XrmoptionNoArg, "on" },
  { "+tick",    ".tick",     XrmoptionNoArg, "off" },
  { "-blur",    ".blur",     XrmoptionNoArg, "on" },
  { "+blur",    ".blur",     XrmoptionNoArg, "off" }
};

static argtype vars[] = {
  {&cycle_arg,   "cycle",   "Cycle",   DEF_CYCLE,   t_Int},
  {&tick_arg,   "tick",     "Tick",   DEF_TICK,   t_Bool},
  {&blur_arg,   "blur",     "Blur",   DEF_BLUR,   t_Bool}
};

static OptionStruct desc[] = {
  {"-count num", "number of balls"},
  {"-cycle num", "cycle / pattern type"},
  {"-/+tick", "enable/disable tick for clockwise and galaxy"},
  {"-/+blur", "enable/disable motion blur"}
};

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

/* Window management, etc
 */
ENTRYPOINT void
reshape_beats (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 * 9/16;
    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);

  glClear(GL_COLOR_BUFFER_BIT);
}


ENTRYPOINT Bool
beats_handle_event (ModeInfo *mi, XEvent *event)
{
  return True;
}

static Bool getFracColour (GLfloat* retVal, float posFrac, float s){
  /* top: red, right: yellow, bottom: [dark] green, left: blue */
  /* note: fixed point; align to 0.1 degree increments */
  int theta, h, v;
  unsigned short r,g,b;
  theta = ((int)(posFrac * 3600) % 3600 + 3600) % 3600;
  v = 100;
  if        ((theta >=   0) && (theta <  900)) {
    h = (theta * 600) / 900;
  } else if ((theta >=  900) && (theta < 1800)) {
    h = ((theta - 900) * 600) / 900 + 600;
    v = 100 - ((theta - 900) / 18);
  } else if ((theta >= 1800) && (theta < 2700)) {
    h = ((theta - 1800) * 1200) / 900 + 1200;
    v = ((theta - 1800) / 18) + 50;
  } else /* if ((theta >= 2700) && (theta < 3600))*/ {
    h = ((theta - 2700) * 1200) / 900 + 2400;
  }
  hsv_to_rgb((int)h / 10.0, s, v / 100.0, &r, &g, &b);
  retVal[0] = r / 65535.0;
  retVal[1] = g / 65535.0;
  retVal[2] = b / 65535.0;
  return True;
}


ENTRYPOINT void 
init_beats (ModeInfo *mi)
{
  beats_configuration *bp;
  int wire = MI_IS_WIREFRAME(mi);

  MI_INIT (mi, bps);
  bp = &bps[MI_SCREEN(mi)];

  bp->glx_context = init_GL(mi);

  reshape_beats (mi, MI_WIDTH(mi), MI_HEIGHT(mi));

  if (!wire)
    {
      GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
      GLfloat amb[4] = {0.02, 0.02, 0.02, 1.0};
      GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
      GLfloat spc[4] = {0.2, 0.2, 0.2, 0.2};

      glEnable(GL_LIGHTING);
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_CULL_FACE);

      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_LIGHT0);
    }

  if (cycle_arg > 3) cycle_arg = -1;

  bp->ball_count = MI_COUNT(mi);
  if (bp->ball_count < 2) bp->ball_count = 2;

  bp->preset_cycle = cycle_arg;
  bp->use_tick = tick_arg;
  bp->use_blur = blur_arg;
  
# ifdef HAVE_ANDROID
  bp->use_blur = False; /* Works on iOS but not Android */
# endif

  bp->ncolors = 128;
  bp->colors = (XColor *) calloc(bp->ncolors, sizeof(XColor));
  make_smooth_colormap (0, 0, 0,
                        bp->colors, &bp->ncolors,
                        False, 0, False);

  bp->beats_list = glGenLists(1);

  glNewList (bp->beats_list, GL_COMPILE);
  glScalef(0.71, 0.71, 0.71);
  unit_sphere (SPHERE_STACKS, SPHERE_SLICES, wire);
  glEndList ();

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}


ENTRYPOINT void
draw_beats (ModeInfo *mi)
{
  beats_configuration *bp = &bps[MI_SCREEN(mi)];
  Display *dpy = MI_DISPLAY(mi);
  Window window = MI_WINDOW(mi);
  unsigned num_objects = bp->ball_count, oi;
  struct timeval tv, tvOrig;
  struct tm *now;
  Bool sineWaveTick = bp->use_tick;
  Bool motionBlur = bp->use_blur;
  size_t cycle, dist;
  unsigned int tmS, tmM, tmH, tmD;
  unsigned int timeSeed;
  int timeDelta = 0;
  size_t blurOffset = 10; /* offset per blur frame, in milliseconds */
  size_t framesPerBlur = 20;  /* number of sub-frames to blur */
  size_t deltaLimit = (motionBlur) ? (blurOffset * framesPerBlur) : 1;
  float ballAlpha;
  float secFrac, minFrac, minProp, hourProp, halfDayProp,
    z, op, mp, m2m,
    theta, delta, blurFrac, oFP, pathLength;

  static const GLfloat bspec[4]  = {1.0, 1.0, 1.0, 1.0};
  static const GLfloat bshiny    = 92.0;

  GLfloat bcolor[4] = {0.85, 0.75, 0.75, 1.0};

  if (!bp->glx_context)
    return;
  gettimeofday (&tvOrig, NULL);
  
  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *bp->glx_context);
  
  glShadeModel(GL_SMOOTH);
  
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_ALPHA_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_CULL_FACE);
  glEnable(GL_BLEND);
  glEnable(GL_DEPTH_TEST);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glPushMatrix ();
  glRotatef(current_device_rotation(), 0, 0, 1);

  {
    GLfloat s = (MI_WIDTH(mi) < MI_HEIGHT(mi)
                 ? MI_WIDTH(mi) / (GLfloat) MI_HEIGHT(mi)
                 : 1);
    glScalef (s, s, s);
  }

  /* timeDelta is in milliseconds */
  for(timeDelta = 0; timeDelta <= deltaLimit; timeDelta += blurOffset){
    if(timeDelta < blurOffset){
      /* glEnable(GL_DEPTH_TEST); */
      ballAlpha = 1.0;
    } else {
      /* glDisable(GL_DEPTH_TEST); */
      ballAlpha = 1.0 / framesPerBlur;
    }
    blurFrac = sin((1 - (float) timeDelta / deltaLimit) * M_PI_2) * ballAlpha;
    tv = tvOrig;
    now = localtime (&tv.tv_sec); /* This seems to be needed for seconds */
    tmS = now->tm_sec;
    tmM = now->tm_min;
    tmH = now->tm_hour;
    tmD = now->tm_yday;
    secFrac = ((tv.tv_usec % 1000000) - (timeDelta * 1000)) / (1e6);
    if(secFrac < 0){
      secFrac += 1;
      tmS--;
      if(tmS < 0){
	tmS += 60;
	tmM--;
      }
      if(tmM < 0){
	tmM += 60;
	tmH--;
      }
      if(tmH < 0){
	tmH += 24;
	tmD--;
      }
      if(tmD < 0){
	/* note: this won't be accurate for leap years, but the rare
	   event logic is complex enough */
	tmD += 365;
      }
    }
    /* pseudo-random generator based on current minute */
    timeSeed = (((tmM+1) * (tmM+1) * ((tmH+1) * 37) *
		 ((tmD+1) * 1151) * 1233599) % 653);
    cycle = timeSeed % 4;
    if(bp->preset_cycle != -1){
      cycle = bp->preset_cycle;
    }
    if(sineWaveTick && (cycle == 0 || cycle == 3)){
      Bool doTick = (timeSeed % 2 == 0);
      if(doTick){ /* choose to tick randomly */
	/* sine-wave 'tick' motion, converts linear 0..1 to 
	   pause/fast/pause 0..1 */
	secFrac = (1.0 - sin((0.5-secFrac) * M_PI))/2.0;
      }
    }
    minFrac = tmS / 60.0;
    /* now we have enough information to calculate our goal statistic,
       minProp: the position in the synchronisation cycle of one
       minute */
    minProp = (minFrac - trunc(minFrac)) + (secFrac / 60);
    m2m = minProp * 2 * M_PI;

    /* change colour based on the minute and hour */
    hourProp = tmM / 60.0 + minProp / 60.0;
    hourProp = hourProp - trunc(hourProp);

    halfDayProp = tmH / 12.0 + hourProp / 12.0;
    halfDayProp = halfDayProp - trunc(halfDayProp);

    mi->polygon_count = 0;

    for(oi = 0; oi < num_objects; oi++){
      glPushMatrix ();
      glScalef(1.1, 1.1, 1.1);

      /* Object Fraction Position - 0..1 depending on native Z order */
      oFP = oi * 1.0 / (num_objects - 1);

      /* set Z distance between [-3.5 .. 0.5] (common to all cycles) */
      z = (oFP) * 4.0 - 3.5;

      /* set colour (common to all cycles) */
      if(oFP < (1 / 3.0)){ /* "second" objects */
	getFracColour(bcolor, minProp, 1.0);
      } else if(oFP < (2 / 3.0)) { /* "minute" objects */
	getFracColour(bcolor, hourProp, 1.0);
      } else { /* "hour" objects */
	getFracColour(bcolor, halfDayProp, 1.0);
      }
    
      /* set x/y location */
      if(cycle == 0){
	/* clockwise */
	glRotatef(-minProp * 360 * (oi + 1), 0, 0, 1);
	glTranslatef(0, 5, 0);
      } else if(cycle == 1){
	/* rain dance */
	float y = 10 * cos(m2m * (oi + 1.0))/2;
	/* rotate around Y axis */
	glTranslatef(0, 0, -20);
	glRotatef(minProp * 360, 0, 1, 0);
	glTranslatef(0, y, 20);
      } else if(cycle == 2){
	/* metronome */
	theta = sin(-m2m * (oi + 1.0)) * 90;
	/* rotate around z axis at (-5, 0, 0) */
	glTranslatef(0, -5, 0);
	glRotatef(theta, 0, 0, 1);
	glTranslatef(0, 10, 0);
      } else if (cycle == 3){
	/* galaxy */
	mp = (num_objects - 1.0) / 2;
	op = mp - oi;
	dist = (int)(fabs(op)+0.5); /* dist from centre */
	/* make sure each object travels an integer number of loops in
	   a path through one cycle */
	pathLength = (int)((60.0 / dist) + 0.5) * 720.0;
	delta = pathLength / 2;
	theta = -minProp * delta - 180;
	/* rotate around X axis after translating (0,-5,0) */
	glTranslatef(0, 0, -20);
	glRotatef(minProp * 360 - 180, 1, 0, 0);
	glTranslatef(0, 0, 20);
	glTranslatef(0, -5, 0);
	/* rotate around Y axis */
	glTranslatef(0, 0, -20);
	glRotatef(theta, 0, 1, 0);
	glTranslatef(0, 0, 20);
      }

      /* spread out based on Z position */
      glTranslatef(0, 0, (z - 0.5) * 10);

      /* set up colours */
      glMaterialfv (GL_FRONT, GL_SPECULAR,            bspec);
      glMateriali  (GL_FRONT, GL_SHININESS,           bshiny);
      if(motionBlur){
	bcolor[3] = (timeDelta == 0) ? 1.0 : blurFrac; /* was ballAlpha */
      }
      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, bcolor);
      glCallList (bp->beats_list); /* draw sphere */
      mi->polygon_count += (SPHERE_SLICES * SPHERE_STACKS);

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


ENTRYPOINT void
free_beats (ModeInfo *mi)
{
  beats_configuration *bp = &bps[MI_SCREEN(mi)];
  if (!bp->glx_context) return;
  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *bp->glx_context);
  if (bp->colors) free (bp->colors);
  if (glIsList(bp->beats_list)) glDeleteLists(bp->beats_list, 1);
}

XSCREENSAVER_MODULE ("Beats", beats)

#endif /* USE_GL */