From http://www.jwz.org/xscreensaver/xscreensaver-5.38.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 free_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   int y = 0;
191
192   if (width > height * 5) {   /* tiny window: show middle */
193     height = width;
194     y = -height/2;
195     h = height / (GLfloat) width;
196   }
197
198   glViewport (0, y, (GLint) width, (GLint) height);
199
200   glMatrixMode(GL_PROJECTION);
201   glLoadIdentity();
202
203   gluPerspective (30.0, 1/h, 1.0, 100.0);
204   glMatrixMode(GL_MODELVIEW);
205   glLoadIdentity();
206   gluLookAt( 0.0, 0.0, 30.0,
207              0.0, 0.0, 0.0,
208              0.0, 1.0, 0.0);
209
210 # ifdef HAVE_MOBILE     /* Keep it the same relative size when rotated. */
211   {
212     int o = (int) current_device_rotation();
213     if (o != 0 && o != 180 && o != -180)
214       glScalef (1/h, 1/h, 1/h);
215   }
216 # endif
217
218   glClear(GL_COLOR_BUFFER_BIT);
219 }
220
221
222 \f
223 /* Waves.
224    Adapted from ../hacks/interference.c by Hannu Mallat.
225  */
226
227 static void
228 init_wave (ModeInfo *mi)
229 {
230   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
231   waves *ww;
232   int i;
233   cc->waves = ww = (waves *) calloc (sizeof(*cc->waves), 1);
234   ww->nwaves = wave_count;
235   ww->radius = wave_radius;
236   ww->speed  = wave_speed;
237   ww->heights = (int *) calloc (sizeof(*ww->heights), ww->radius);
238   ww->srcs = (wave_src *) calloc (sizeof(*ww->srcs), ww->nwaves);
239
240   for (i = 0; i < ww->radius; i++)
241     {
242       float max = (cc->ncolors * (ww->radius - i) / (float) ww->radius);
243       ww->heights[i] = ((max + max * cos(i / 50.0)) / 2.0);
244     }
245
246   for (i = 0; i < ww->nwaves; i++)
247     {
248       ww->srcs[i].xth = frand(2.0) * M_PI;
249       ww->srcs[i].yth = frand(2.0) * M_PI;
250     }
251 }
252
253 static void
254 interference (ModeInfo *mi)
255 {
256   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
257   waves *ww = cc->waves;
258   int x, y, i;
259
260   /* Move the wave origins around
261    */
262   for (i = 0; i < ww->nwaves; i++)
263     {
264       ww->srcs[i].xth += (ww->speed / 1000.0);
265       if (ww->srcs[i].xth > 2*M_PI)
266         ww->srcs[i].xth -= 2*M_PI;
267       ww->srcs[i].yth += (ww->speed / 1000.0);
268       if (ww->srcs[i].yth > 2*M_PI)
269         ww->srcs[i].yth -= 2*M_PI;
270
271       ww->srcs[i].x = (cc->texture_width/2 +
272                        (cos (ww->srcs[i].xth) *
273                         cc->texture_width / 2));
274       ww->srcs[i].y = (cc->texture_height/2 +
275                        (cos (ww->srcs[i].yth) *
276                         cc->texture_height / 2));
277     }
278
279   /* Compute the effect of the waves on each pixel,
280      and generate the output map.
281    */
282   for (y = 0; y < cc->texture_height; y++)
283     for (x = 0; x < cc->texture_width; x++)
284       {
285         int result = 0;
286         unsigned char *o;
287         for (i = 0; i < ww->nwaves; i++)
288           {
289             int dx = x - ww->srcs[i].x;
290             int dy = y - ww->srcs[i].y;
291             int dist = sqrt (dx*dx + dy*dy);
292             result += (dist > ww->radius ? 0 : ww->heights[dist]);
293           }
294         result %= cc->ncolors;
295
296         o = cc->texture + (((y * cc->texture_width) + x) << 2);
297         o[0] = (cc->texture_colors[result].red   >> 8);
298         o[1] = (cc->texture_colors[result].green >> 8);
299         o[2] = (cc->texture_colors[result].blue  >> 8);
300      /* o[3] = 0xFF; */
301       }
302 }
303
304 \f
305 /* Textures
306  */
307
308 static void
309 init_texture (ModeInfo *mi)
310 {
311   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
312   int i;
313
314   glEnable(GL_TEXTURE_2D);
315
316   clear_gl_error();
317   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
318   glGenTextures (1, &cc->texture_id);
319   glBindTexture (GL_TEXTURE_2D, cc->texture_id);
320   check_gl_error("texture binding");
321
322   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
323   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
324   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
325   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
326   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
327   check_gl_error("texture initialization");
328
329   cc->texture_width  = texture_size;
330   cc->texture_height = texture_size;
331
332   i = texture_size * texture_size * 4;
333   cc->texture = (unsigned char *) malloc (i);
334   memset (cc->texture, 0xFF, i);
335 }
336
337
338 static void
339 shuffle_texture (ModeInfo *mi)
340 {
341   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
342   interference (mi);
343   clear_gl_error();
344   glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
345                 cc->texture_width, cc->texture_height, 0,
346                 GL_RGBA, GL_UNSIGNED_BYTE,
347                 cc->texture);
348   check_gl_error("texture");
349 }
350
351
352 static void
353 reset_colors (ModeInfo *mi)
354 {
355   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
356   double H[3], S[3], V[3];
357   int shift = 60;
358   H[0] = frand(360.0); 
359   H[1] = ((H[0] + shift) < 360) ? (H[0]+shift) : (H[0] + shift - 360);
360   H[2] = ((H[1] + shift) < 360) ? (H[1]+shift) : (H[1] + shift - 360);
361   S[0] = S[1] = S[2] = 1.0;
362   V[0] = V[1] = V[2] = 1.0;
363   make_color_loop(0, 0, 0,
364                   H[0], S[0], V[0], 
365                   H[1], S[1], V[1], 
366                   H[2], S[2], V[2], 
367                   cc->texture_colors, &cc->ncolors,
368                   False, False);
369
370   make_smooth_colormap (0, 0, 0,
371                         cc->cube_colors, &cc->ncolors, 
372                         False, 0, False);
373 }
374
375
376 ENTRYPOINT Bool
377 cube_handle_event (ModeInfo *mi, XEvent *event)
378 {
379   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
380
381   if (gltrackball_event_handler (event, cc->trackball,
382                                  MI_WIDTH (mi), MI_HEIGHT (mi),
383                                  &cc->button_down_p))
384     return True;
385   else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
386     {
387       reset_colors (mi);
388       return True;
389     }
390
391   return False;
392 }
393
394
395 ENTRYPOINT void 
396 init_cube (ModeInfo *mi)
397 {
398   int i;
399   cube_configuration *cc;
400   int wire = MI_IS_WIREFRAME(mi);
401
402   MI_INIT (mi, ccs);
403
404   cc = &ccs[MI_SCREEN(mi)];
405
406   if ((cc->glx_context = init_GL(mi)) != NULL) {
407     reshape_cube (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
408   }
409
410   if (!wire)
411     {
412       static const GLfloat pos[4] = {1.0, 0.5, 1.0, 0.0};
413       static const GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
414       static const GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
415
416       glLightfv(GL_LIGHT0, GL_POSITION, pos);
417       glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
418       glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
419
420       glEnable(GL_LIGHTING);
421       glEnable(GL_LIGHT0);
422       glEnable(GL_DEPTH_TEST);
423       glEnable(GL_CULL_FACE);
424     }
425
426
427   {
428     Bool spinx=False, spiny=False, spinz=False;
429     double spin_speed   = 1.0;
430     double wander_speed = 0.05;
431
432     char *s = do_spin;
433     while (*s)
434       {
435         if      (*s == 'x' || *s == 'X') spinx = True;
436         else if (*s == 'y' || *s == 'Y') spiny = True;
437         else if (*s == 'z' || *s == 'Z') spinz = True;
438         else if (*s == '0') ;
439         else
440           {
441             fprintf (stderr,
442          "%s: spin must contain only the characters X, Y, or Z (not \"%s\")\n",
443                      progname, do_spin);
444             exit (1);
445           }
446         s++;
447       }
448
449     cc->rot = make_rotator (spinx ? spin_speed : 0,
450                             spiny ? spin_speed : 0,
451                             spinz ? spin_speed : 0,
452                             1.0,
453                             do_wander ? wander_speed : 0,
454                             (spinx && spiny && spinz));
455     cc->trackball = gltrackball_init (True);
456   }
457
458   cc->ncolors = 256;
459   cc->texture_colors = (XColor *) calloc(cc->ncolors, sizeof(XColor));
460   cc->cube_colors    = (XColor *) calloc(cc->ncolors, sizeof(XColor));
461
462   reset_colors (mi);
463
464   cc->ncubes = MI_COUNT (mi);
465   cc->cubes = (cube *) calloc (sizeof(cube), cc->ncubes);
466   for (i = 0; i < cc->ncubes; i++)
467     {
468       cube *cube = &cc->cubes[i];
469       cube->color = random() % cc->ncolors;
470       cube->w = 1.0;
471       cube->h = 1.0;
472       cube->d = 1.0;
473       cube->dx = frand(0.1);
474       cube->dy = frand(0.1);
475       cube->dz = frand(0.1);
476       cube->dw = frand(0.1);
477       cube->dh = frand(0.1);
478       cube->dd = frand(0.1);
479     }
480
481   if (wire)
482     do_texture = False;
483     
484   if (do_texture)
485     {
486       init_texture (mi);
487       init_wave (mi);
488       shuffle_texture (mi);
489     }
490
491   cc->cube_list = glGenLists (1);
492   glNewList (cc->cube_list, GL_COMPILE);
493   cc->cube_polys = unit_cube (wire);
494   glEndList ();
495 }
496
497
498 static void
499 shuffle_cubes (ModeInfo *mi)
500 {
501   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
502   int i;
503   for (i = 0; i < cc->ncubes; i++)
504     {
505 #     define SINOID(SCALE,FRAME,SIZE) \
506         ((((1 + sin((FRAME * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
507
508       cube *cube = &cc->cubes[i];
509       cube->x = SINOID(cube->dx, cube->frame, 0.5);
510       cube->y = SINOID(cube->dy, cube->frame, 0.5);
511       cube->z = SINOID(cube->dz, cube->frame, 0.5);
512       cube->w = SINOID(cube->dw, cube->frame, 0.9) + 1.0;
513       cube->h = SINOID(cube->dh, cube->frame, 0.9) + 1.0;
514       cube->d = SINOID(cube->dd, cube->frame, 0.9) + 1.0;
515       cube->frame++;
516 #     undef SINOID
517     }
518 }
519
520
521 ENTRYPOINT void
522 draw_cube (ModeInfo *mi)
523 {
524   cube_configuration *cc = &ccs[MI_SCREEN(mi)];
525   Display *dpy = MI_DISPLAY(mi);
526   Window window = MI_WINDOW(mi);
527   int i;
528
529   if (!cc->glx_context)
530     return;
531
532   mi->polygon_count = 0;
533   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cc->glx_context));
534
535   glShadeModel(GL_FLAT);
536
537   glEnable(GL_DEPTH_TEST);
538   glEnable(GL_NORMALIZE);
539   glEnable(GL_CULL_FACE);
540
541   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
542
543   glPushMatrix ();
544
545   glScalef(1.1, 1.1, 1.1);
546
547   {
548     double x, y, z;
549     get_position (cc->rot, &x, &y, &z, !cc->button_down_p);
550     glTranslatef((x - 0.5) * 8,
551                  (y - 0.5) * 6,
552                  (z - 0.5) * 15);
553
554     gltrackball_rotate (cc->trackball);
555
556     get_rotation (cc->rot, &x, &y, &z, !cc->button_down_p);
557     glRotatef (x * 360, 1.0, 0.0, 0.0);
558     glRotatef (y * 360, 0.0, 1.0, 0.0);
559     glRotatef (z * 360, 0.0, 0.0, 1.0);
560   }
561
562   glScalef (2.5, 2.5, 2.5);
563
564   for (i = 0; i < cc->ncubes; i++)
565     {
566       cube *cube = &cc->cubes[i];
567       GLfloat color[4];
568       color[0] = cc->cube_colors[cube->color].red   / 65536.0;
569       color[1] = cc->cube_colors[cube->color].green / 65536.0;
570       color[2] = cc->cube_colors[cube->color].blue  / 65536.0;
571       color[3] = 1.0;
572       cube->color++;
573       if (cube->color >= cc->ncolors) cube->color = 0;
574
575       glPushMatrix ();
576       glTranslatef (cube->x, cube->y, cube->z);
577       glScalef (cube->w, cube->h, cube->d);
578       glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
579       glCallList (cc->cube_list);
580       mi->polygon_count += cc->cube_polys;
581       glPopMatrix ();
582     }
583
584   shuffle_cubes (mi);
585   if (do_texture)
586     shuffle_texture (mi);
587
588   glPopMatrix();
589
590   if (mi->fps_p) do_fps (mi);
591   glFinish();
592
593   glXSwapBuffers(dpy, window);
594 }
595
596 XSCREENSAVER_MODULE_2 ("Cubenetic", cubenetic, cube)
597
598 #endif /* USE_GL */