From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / cubenetic.c
1 /* cubenetic, Copyright (c) 2002-2014 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #define DEFAULTS        "*delay:        20000       \n" \
13                         "*count:        5           \n" \
14                         "*showFPS:      False       \n" \
15                         "*wireframe:    False       \n" \
16                         "*suppressRotationAnimation: True\n" \
17
18 # define refresh_cube 0
19 # define release_cube 0
20 #undef countof
21 #define countof(x) (sizeof((x))/sizeof((*x)))
22
23 #include "xlockmore.h"
24 #include "colors.h"
25 #include "rotator.h"
26 #include "gltrackball.h"
27 #include <ctype.h>
28
29 #ifdef USE_GL /* whole file */
30
31
32 #define DEF_SPIN        "XYZ"
33 #define DEF_WANDER      "True"
34 #define DEF_TEXTURE     "True"
35
36 #define DEF_WAVES       "3"
37 #define DEF_WAVE_SPEED  "80"
38 #define DEF_WAVE_RADIUS "512"
39
40 typedef struct {
41   int color;
42   GLfloat x, y, z;
43   GLfloat w, h, d;
44   int frame;
45   GLfloat dx, dy, dz;
46   GLfloat dw, dh, dd;
47 } cube;
48
49 typedef struct {
50   int x, y;
51   double xth, yth;
52 } wave_src;
53
54 typedef struct {
55   int nwaves;
56   int radius;
57   int speed;
58   wave_src *srcs;
59   int *heights;
60 } waves;
61
62
63 typedef struct {
64   GLXContext *glx_context;
65   rotator *rot;
66   trackball_state *trackball;
67   Bool button_down_p;
68
69   GLuint cube_list;
70   GLuint texture_id;
71   int cube_polys;
72   int ncubes;
73   cube *cubes;
74   waves *waves;
75
76   int texture_width, texture_height;
77   unsigned char *texture;
78
79   int ncolors;
80   XColor *cube_colors;
81   XColor *texture_colors;
82
83 } cube_configuration;
84
85 static cube_configuration *ccs = NULL;
86
87 static char *do_spin;
88 static Bool do_wander;
89 static Bool do_texture;
90
91 static int wave_count;
92 static int wave_speed;
93 static int wave_radius;
94 static int texture_size = 256;
95
96 static XrmOptionDescRec opts[] = {
97   { "-spin",   ".spin",   XrmoptionSepArg, 0 },
98   { "+spin",   ".spin",   XrmoptionNoArg, "" },
99   { "-wander", ".wander", XrmoptionNoArg, "True" },
100   { "+wander", ".wander", XrmoptionNoArg, "False" },
101   {"-texture", ".texture", XrmoptionNoArg, "true" },
102   {"+texture", ".texture", XrmoptionNoArg, "false" },
103   {"-waves",       ".waves",      XrmoptionSepArg, 0 },
104   {"-wave-speed",  ".waveSpeed",  XrmoptionSepArg, 0 },
105   {"-wave-radius", ".waveRadius", XrmoptionSepArg, 0 },
106 };
107
108 static argtype vars[] = {
109   {&do_spin,   "spin",   "Spin",   DEF_SPIN,   t_String},
110   {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
111   {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
112   {&wave_count, "waves",     "Waves",      DEF_WAVES, t_Int},
113   {&wave_speed, "waveSpeed", "WaveSpeed",  DEF_WAVE_SPEED, t_Int},
114   {&wave_radius,"waveRadius","WaveRadius", DEF_WAVE_RADIUS,t_Int},
115 };
116
117 ENTRYPOINT ModeSpecOpt cube_opts = {countof(opts), opts, countof(vars), vars, NULL};
118
119
120 static int
121 unit_cube (Bool wire)
122 {
123   int polys = 0;
124   glBegin (wire ? GL_LINE_LOOP : GL_QUADS);     /* front */
125   glNormal3f (0, 0, 1);
126   glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5,  0.5);
127   glTexCoord2f(0, 0); glVertex3f ( 0.5,  0.5,  0.5);
128   glTexCoord2f(0, 1); glVertex3f (-0.5,  0.5,  0.5);
129   glTexCoord2f(1, 1); glVertex3f (-0.5, -0.5,  0.5);
130   polys++;
131   glEnd();
132
133   glBegin (wire ? GL_LINE_LOOP : GL_QUADS);     /* back */
134   glNormal3f (0, 0, -1);
135   glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
136   glTexCoord2f(0, 1); glVertex3f (-0.5,  0.5, -0.5);
137   glTexCoord2f(1, 1); glVertex3f ( 0.5,  0.5, -0.5);
138   glTexCoord2f(1, 0); glVertex3f ( 0.5, -0.5, -0.5);
139   polys++;
140   glEnd();
141
142   glBegin (wire ? GL_LINE_LOOP : GL_QUADS);     /* left */
143   glNormal3f (-1, 0, 0);
144   glTexCoord2f(1, 1); glVertex3f (-0.5,  0.5,  0.5);
145   glTexCoord2f(1, 0); glVertex3f (-0.5,  0.5, -0.5);
146   glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
147   glTexCoord2f(0, 1); glVertex3f (-0.5, -0.5,  0.5);
148   polys++;
149   glEnd();
150
151   glBegin (wire ? GL_LINE_LOOP : GL_QUADS);     /* right */
152   glNormal3f (1, 0, 0);
153   glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5, -0.5);
154   glTexCoord2f(1, 0); glVertex3f ( 0.5,  0.5, -0.5);
155   glTexCoord2f(0, 0); glVertex3f ( 0.5,  0.5,  0.5);
156   glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5,  0.5);
157   polys++;
158   glEnd();
159
160   if (wire) return polys;
161
162   glBegin (wire ? GL_LINE_LOOP : GL_QUADS);     /* top */
163   glNormal3f (0, 1, 0);
164   glTexCoord2f(0, 0); glVertex3f ( 0.5,  0.5,  0.5);
165   glTexCoord2f(0, 1); glVertex3f ( 0.5,  0.5, -0.5);
166   glTexCoord2f(1, 1); glVertex3f (-0.5,  0.5, -0.5);
167   glTexCoord2f(1, 0); glVertex3f (-0.5,  0.5,  0.5);
168   polys++;
169   glEnd();
170
171   glBegin (wire ? GL_LINE_LOOP : GL_QUADS);     /* bottom */
172   glNormal3f (0, -1, 0);
173   glTexCoord2f(1, 0); glVertex3f (-0.5, -0.5,  0.5);
174   glTexCoord2f(0, 0); glVertex3f (-0.5, -0.5, -0.5);
175   glTexCoord2f(0, 1); glVertex3f ( 0.5, -0.5, -0.5);
176   glTexCoord2f(1, 1); glVertex3f ( 0.5, -0.5,  0.5);
177   polys++;
178   glEnd();
179   return polys;
180 }
181
182
183
184 /* Window management, etc
185  */
186 ENTRYPOINT void
187 reshape_cube (ModeInfo *mi, int width, int height)
188 {
189   GLfloat h = (GLfloat) height / (GLfloat) width;
190
191   glViewport (0, 0, (GLint) width, (GLint) height);
192
193   glMatrixMode(GL_PROJECTION);
194   glLoadIdentity();
195
196   gluPerspective (30.0, 1/h, 1.0, 100.0);
197   glMatrixMode(GL_MODELVIEW);
198   glLoadIdentity();
199   gluLookAt( 0.0, 0.0, 30.0,
200              0.0, 0.0, 0.0,
201              0.0, 1.0, 0.0);
202
203 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
204   {
205     int o = (int) current_device_rotation();
206     if (o != 0 && o != 180 && o != -180)
207       glScalef (1/h, 1/h, 1/h);
208   }
209 # endif
210
211   glClear(GL_COLOR_BUFFER_BIT);
212 }
213
214
215 \f
216 /* Waves.
217    Adapted from ../hacks/interference.c by Hannu Mallat.
218  */
219
220 static void
221 init_wave (ModeInfo *mi)
222 {
223   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
224   waves *ww;
225   int i;
226   cc->waves = ww = (waves *) calloc (sizeof(*cc->waves), 1);
227   ww->nwaves = wave_count;
228   ww->radius = wave_radius;
229   ww->speed  = wave_speed;
230   ww->heights = (int *) calloc (sizeof(*ww->heights), ww->radius);
231   ww->srcs = (wave_src *) calloc (sizeof(*ww->srcs), ww->nwaves);
232
233   for (i = 0; i < ww->radius; i++)
234     {
235       float max = (cc->ncolors * (ww->radius - i) / (float) ww->radius);
236       ww->heights[i] = ((max + max * cos(i / 50.0)) / 2.0);
237     }
238
239   for (i = 0; i < ww->nwaves; i++)
240     {
241       ww->srcs[i].xth = frand(2.0) * M_PI;
242       ww->srcs[i].yth = frand(2.0) * M_PI;
243     }
244 }
245
246 static void
247 interference (ModeInfo *mi)
248 {
249   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
250   waves *ww = cc->waves;
251   int x, y, i;
252
253   /* Move the wave origins around
254    */
255   for (i = 0; i < ww->nwaves; i++)
256     {
257       ww->srcs[i].xth += (ww->speed / 1000.0);
258       if (ww->srcs[i].xth > 2*M_PI)
259         ww->srcs[i].xth -= 2*M_PI;
260       ww->srcs[i].yth += (ww->speed / 1000.0);
261       if (ww->srcs[i].yth > 2*M_PI)
262         ww->srcs[i].yth -= 2*M_PI;
263
264       ww->srcs[i].x = (cc->texture_width/2 +
265                        (cos (ww->srcs[i].xth) *
266                         cc->texture_width / 2));
267       ww->srcs[i].y = (cc->texture_height/2 +
268                        (cos (ww->srcs[i].yth) *
269                         cc->texture_height / 2));
270     }
271
272   /* Compute the effect of the waves on each pixel,
273      and generate the output map.
274    */
275   for (y = 0; y < cc->texture_height; y++)
276     for (x = 0; x < cc->texture_width; x++)
277       {
278         int result = 0;
279         unsigned char *o;
280         for (i = 0; i < ww->nwaves; i++)
281           {
282             int dx = x - ww->srcs[i].x;
283             int dy = y - ww->srcs[i].y;
284             int dist = sqrt (dx*dx + dy*dy);
285             result += (dist > ww->radius ? 0 : ww->heights[dist]);
286           }
287         result %= cc->ncolors;
288
289         o = cc->texture + (((y * cc->texture_width) + x) << 2);
290         o[0] = (cc->texture_colors[result].red   >> 8);
291         o[1] = (cc->texture_colors[result].green >> 8);
292         o[2] = (cc->texture_colors[result].blue  >> 8);
293      /* o[3] = 0xFF; */
294       }
295 }
296
297 \f
298 /* Textures
299  */
300
301 static void
302 init_texture (ModeInfo *mi)
303 {
304   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
305   int i;
306
307   glEnable(GL_TEXTURE_2D);
308
309   clear_gl_error();
310   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
311   glGenTextures (1, &cc->texture_id);
312   glBindTexture (GL_TEXTURE_2D, cc->texture_id);
313   check_gl_error("texture binding");
314
315   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
316   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
317   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
318   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
319   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
320   check_gl_error("texture initialization");
321
322   cc->texture_width  = texture_size;
323   cc->texture_height = texture_size;
324
325   i = texture_size * texture_size * 4;
326   cc->texture = (unsigned char *) malloc (i);
327   memset (cc->texture, 0xFF, i);
328 }
329
330
331 static void
332 shuffle_texture (ModeInfo *mi)
333 {
334   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
335   interference (mi);
336   clear_gl_error();
337   glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
338                 cc->texture_width, cc->texture_height, 0,
339                 GL_RGBA, GL_UNSIGNED_BYTE,
340                 cc->texture);
341   check_gl_error("texture");
342 }
343
344
345 static void
346 reset_colors (ModeInfo *mi)
347 {
348   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
349   double H[3], S[3], V[3];
350   int shift = 60;
351   H[0] = frand(360.0); 
352   H[1] = ((H[0] + shift) < 360) ? (H[0]+shift) : (H[0] + shift - 360);
353   H[2] = ((H[1] + shift) < 360) ? (H[1]+shift) : (H[1] + shift - 360);
354   S[0] = S[1] = S[2] = 1.0;
355   V[0] = V[1] = V[2] = 1.0;
356   make_color_loop(0, 0, 0,
357                   H[0], S[0], V[0], 
358                   H[1], S[1], V[1], 
359                   H[2], S[2], V[2], 
360                   cc->texture_colors, &cc->ncolors,
361                   False, False);
362
363   make_smooth_colormap (0, 0, 0,
364                         cc->cube_colors, &cc->ncolors, 
365                         False, 0, False);
366 }
367
368
369 ENTRYPOINT Bool
370 cube_handle_event (ModeInfo *mi, XEvent *event)
371 {
372   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
373
374   if (gltrackball_event_handler (event, cc->trackball,
375                                  MI_WIDTH (mi), MI_HEIGHT (mi),
376                                  &cc->button_down_p))
377     return True;
378   else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
379     {
380       reset_colors (mi);
381       return True;
382     }
383
384   return False;
385 }
386
387
388 ENTRYPOINT void 
389 init_cube (ModeInfo *mi)
390 {
391   int i;
392   cube_configuration *cc;
393   int wire = MI_IS_WIREFRAME(mi);
394
395   MI_INIT (mi, ccs, NULL);
396
397   cc = &ccs[MI_SCREEN(mi)];
398
399   if ((cc->glx_context = init_GL(mi)) != NULL) {
400     reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
401   }
402
403   if (!wire)
404     {
405       static const GLfloat pos[4] = {1.0, 0.5, 1.0, 0.0};
406       static const GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
407       static const GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
408
409       glLightfv(GL_LIGHT0, GL_POSITION, pos);
410       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
411       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
412
413       glEnable(GL_LIGHTING);
414       glEnable(GL_LIGHT0);
415       glEnable(GL_DEPTH_TEST);
416       glEnable(GL_CULL_FACE);
417     }
418
419
420   {
421     Bool spinx=False, spiny=False, spinz=False;
422     double spin_speed   = 1.0;
423     double wander_speed = 0.05;
424
425     char *s = do_spin;
426     while (*s)
427       {
428         if      (*s == 'x' || *s == 'X') spinx = True;
429         else if (*s == 'y' || *s == 'Y') spiny = True;
430         else if (*s == 'z' || *s == 'Z') spinz = True;
431         else if (*s == '0') ;
432         else
433           {
434             fprintf (stderr,
435          "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
436                      progname, do_spin);
437             exit (1);
438           }
439         s++;
440       }
441
442     cc->rot = make_rotator (spinx ? spin_speed : 0,
443                             spiny ? spin_speed : 0,
444                             spinz ? spin_speed : 0,
445                             1.0,
446                             do_wander ? wander_speed : 0,
447                             (spinx && spiny && spinz));
448     cc->trackball = gltrackball_init (True);
449   }
450
451   cc->ncolors = 256;
452   cc->texture_colors = (XColor *) calloc(cc->ncolors, sizeof(XColor));
453   cc->cube_colors    = (XColor *) calloc(cc->ncolors, sizeof(XColor));
454
455   reset_colors (mi);
456
457   cc->ncubes = MI_COUNT (mi);
458   cc->cubes = (cube *) calloc (sizeof(cube), cc->ncubes);
459   for (i = 0; i < cc->ncubes; i++)
460     {
461       cube *cube = &cc->cubes[i];
462       cube->color = random() % cc->ncolors;
463       cube->w = 1.0;
464       cube->h = 1.0;
465       cube->d = 1.0;
466       cube->dx = frand(0.1);
467       cube->dy = frand(0.1);
468       cube->dz = frand(0.1);
469       cube->dw = frand(0.1);
470       cube->dh = frand(0.1);
471       cube->dd = frand(0.1);
472     }
473
474   if (wire)
475     do_texture = False;
476     
477   if (do_texture)
478     {
479       init_texture (mi);
480       init_wave (mi);
481       shuffle_texture (mi);
482     }
483
484   cc->cube_list = glGenLists (1);
485   glNewList (cc->cube_list, GL_COMPILE);
486   cc->cube_polys = unit_cube (wire);
487   glEndList ();
488 }
489
490
491 static void
492 shuffle_cubes (ModeInfo *mi)
493 {
494   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
495   int i;
496   for (i = 0; i < cc->ncubes; i++)
497     {
498 #     define SINOID(SCALE,FRAME,SIZE) \
499         ((((1 + sin((FRAME * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
500
501       cube *cube = &cc->cubes[i];
502       cube->x = SINOID(cube->dx, cube->frame, 0.5);
503       cube->y = SINOID(cube->dy, cube->frame, 0.5);
504       cube->z = SINOID(cube->dz, cube->frame, 0.5);
505       cube->w = SINOID(cube->dw, cube->frame, 0.9) + 1.0;
506       cube->h = SINOID(cube->dh, cube->frame, 0.9) + 1.0;
507       cube->d = SINOID(cube->dd, cube->frame, 0.9) + 1.0;
508       cube->frame++;
509 #     undef SINOID
510     }
511 }
512
513
514 ENTRYPOINT void
515 draw_cube (ModeInfo *mi)
516 {
517   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
518   Display *dpy = MI_DISPLAY(mi);
519   Window window = MI_WINDOW(mi);
520   int i;
521
522   if (!cc->glx_context)
523     return;
524
525   mi->polygon_count = 0;
526   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cc->glx_context));
527
528   glShadeModel(GL_FLAT);
529
530   glEnable(GL_DEPTH_TEST);
531   glEnable(GL_NORMALIZE);
532   glEnable(GL_CULL_FACE);
533
534   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
535
536   glPushMatrix ();
537
538   glScalef(1.1, 1.1, 1.1);
539
540   {
541     double x, y, z;
542     get_position (cc->rot, &x, &y, &z, !cc->button_down_p);
543     glTranslatef((x - 0.5) * 8,
544                  (y - 0.5) * 6,
545                  (z - 0.5) * 15);
546
547     gltrackball_rotate (cc->trackball);
548
549     get_rotation (cc->rot, &x, &y, &z, !cc->button_down_p);
550     glRotatef (x * 360, 1.0, 0.0, 0.0);
551     glRotatef (y * 360, 0.0, 1.0, 0.0);
552     glRotatef (z * 360, 0.0, 0.0, 1.0);
553   }
554
555   glScalef (2.5, 2.5, 2.5);
556
557   for (i = 0; i < cc->ncubes; i++)
558     {
559       cube *cube = &cc->cubes[i];
560       GLfloat color[4];
561       color[0] = cc->cube_colors[cube->color].red   / 65536.0;
562       color[1] = cc->cube_colors[cube->color].green / 65536.0;
563       color[2] = cc->cube_colors[cube->color].blue  / 65536.0;
564       color[3] = 1.0;
565       cube->color++;
566       if (cube->color >= cc->ncolors) cube->color = 0;
567
568       glPushMatrix ();
569       glTranslatef (cube->x, cube->y, cube->z);
570       glScalef (cube->w, cube->h, cube->d);
571       glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
572       glCallList (cc->cube_list);
573       mi->polygon_count += cc->cube_polys;
574       glPopMatrix ();
575     }
576
577   shuffle_cubes (mi);
578   if (do_texture)
579     shuffle_texture (mi);
580
581   glPopMatrix();
582
583   if (mi->fps_p) do_fps (mi);
584   glFinish();
585
586   glXSwapBuffers(dpy, window);
587 }
588
589 XSCREENSAVER_MODULE_2 ("Cubenetic", cubenetic, cube)
590
591 #endif /* USE_GL */