From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / energystream.c
1 /* energystream, Copyright (c) 2016 Eugene Sandulenko <sev@scummvm.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  * Based on Public Domain code by konrad "yoghurt" zagorowicz
12  * for Tesla demo by Sunflower (http://www.pouet.net/prod.php?which=33)
13  */
14
15 #define DEFAULTS  "*delay:  30000       \n" \
16       "*count:        30          \n" \
17       "*showFPS:      False       \n" \
18       "*wireframe:    False       \n" \
19
20 # define refresh_stream 0
21 # define release_stream 0
22 #undef countof
23 #define countof(x) (sizeof((x))/sizeof((*x)))
24
25 #include "xlockmore.h"
26 #include "gltrackball.h"
27 #include "rotator.h"
28 #include <ctype.h>
29
30 #ifdef USE_GL /* whole file */
31
32 static float change_time = 0;
33 static float change_time1 = 25;
34 static float change_time2 = 40;
35 static float change_time3 = 60;
36
37 #define DEF_STREAMS     "16"
38
39 #include <sys/time.h>
40 #include <time.h>
41
42 typedef struct timeval streamtime;
43
44 #define GETSECS(t) ((t).tv_sec)
45 #define GETMSECS(t) ((t).tv_usec/1000)
46
47 #define DEF_SPIN        "False"
48 #define DEF_WANDER      "False"
49 #define DEF_SPEED       "1.0"
50
51
52 typedef struct {
53   float x, y, z;
54 } Vector;
55
56 typedef struct {
57   Vector *flares;
58   int num_flares;
59   GLuint flare_tex;
60   float speed;
61 } flare_stream;
62
63 typedef struct {
64   GLXContext *glx_context;
65   rotator *rot;
66   trackball_state *trackball;
67   Bool button_down_p;
68
69   time_t start_time;
70
71   int num_streams;
72   flare_stream *streams;
73
74 } stream_configuration;
75
76 static stream_configuration *ess = NULL;
77
78 static int num_streams;
79 static Bool do_spin;
80 static GLfloat global_speed;
81 static Bool do_wander;
82
83 static XrmOptionDescRec opts[] = {
84   { "-streams",  ".streams",  XrmoptionSepArg, 0 },
85   { "-spin",   ".spin",   XrmoptionNoArg, "True" },
86   { "+spin",   ".spin",   XrmoptionNoArg, "False" },
87   { "-speed",  ".speed",  XrmoptionSepArg, 0 },
88   { "-wander", ".wander", XrmoptionNoArg, "True" },
89   { "+wander", ".wander", XrmoptionNoArg, "False" }
90 };
91
92 static argtype vars[] = {
93   {&num_streams, "streams",  "Streams",  DEF_STREAMS,  t_Int},
94   {&do_spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
95   {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
96   {&global_speed, "speed",  "Speed",  DEF_SPEED,  t_Float},
97 };
98
99 ENTRYPOINT ModeSpecOpt stream_opts = {countof(opts), opts, countof(vars), vars, NULL};
100
101 static void gettime(streamtime *t)
102 {
103 #ifdef GETTIMEOFDAY_TWO_ARGS
104         struct timezone tzp;
105         gettimeofday(t, &tzp);
106 #else /* !GETTIMEOFDAY_TWO_ARGS */
107         gettimeofday(t);
108 #endif /* !GETTIMEOFDAY_TWO_ARGS */
109 }
110
111
112 /* Window management, etc
113  */
114 ENTRYPOINT void
115 reshape_stream (ModeInfo *mi, int width, int height)
116 {
117   GLfloat h = (GLfloat) height / (GLfloat) width;
118
119   glViewport (0, 0, (GLint) width, (GLint) height);
120
121   glMatrixMode (GL_PROJECTION);
122   glLoadIdentity ();
123   gluPerspective (30.0, 1/h, 1.0, 100.0);
124
125   glMatrixMode (GL_MODELVIEW);
126   glLoadIdentity ();
127   gluLookAt (0.0, 0.0, 30.0,
128              0.0, 0.0, 0.0,
129              0.0, 1.0, 0.0);
130
131   glClear (GL_COLOR_BUFFER_BIT);
132 }
133
134
135 ENTRYPOINT Bool
136 stream_handle_event (ModeInfo *mi, XEvent *event)
137 {
138   stream_configuration *es = &ess[MI_SCREEN(mi)];
139
140   if (gltrackball_event_handler (event, es->trackball,
141                                  MI_WIDTH (mi), MI_HEIGHT (mi),
142                                  &es->button_down_p))
143     return True;
144
145   return False;
146 }
147
148 #define TEX_WIDTH 256
149 #define TEX_HEIGHT 256
150 #define COEFF 0.2
151
152 static GLuint gen_texture (void)
153 {
154     int x, y, i;
155     float color;
156     GLuint tex;
157
158     unsigned char *texture = (unsigned char *)calloc (TEX_WIDTH * TEX_HEIGHT, 4);
159     unsigned char *ptr = texture;
160
161     float tint[3];
162     for (i = 0; i < 3; i++)
163       tint[i] = 1.0 * random() / RAND_MAX;
164
165     for (y = 0; y < TEX_HEIGHT; y++) {
166         for (x = 0; x < TEX_WIDTH; x++) {
167             color = 255 - sqrt((x - TEX_WIDTH / 2) * (x - TEX_WIDTH / 2) / COEFF
168                         + (y - TEX_HEIGHT / 2) * (y - TEX_HEIGHT / 2) / COEFF);
169
170             if (color < 0)
171                 color = 0;
172
173             for (i = 0; i < 3; i++)
174                 ptr[i] = (unsigned char)(color * tint[i]);
175             ptr[3] = color ? 255 : 0;
176
177             ptr += 4;
178         }
179     }
180
181   glGenTextures (1, &tex);
182 #ifdef HAVE_GLBINDTEXTURE
183   glBindTexture (GL_TEXTURE_2D, tex);
184 #endif /* HAVE_GLBINDTEXTURE */
185   glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
186   clear_gl_error ();
187   glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT,
188       0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, texture);
189   check_gl_error ("texture");
190
191   /* Texture parameters, LINEAR scaling for better texture quality */
192   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
193   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
194   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
195   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
196
197   free (texture);
198
199   return tex;
200 }
201
202 static inline void vector_copy (Vector *a, Vector *b)
203 {
204   a->x = b->x;
205   a->y = b->y;
206   a->z = b->z;
207 }
208
209 /* a += b */
210 static inline void vector_add (Vector *a, Vector *b)
211 {
212   a->x += b->x;
213   a->y += b->y;
214   a->z += b->z;
215 }
216
217 /* a -= b */
218 static inline void vector_sub (Vector *a, Vector *b)
219 {
220   a->x -= b->x;
221   a->y -= b->y;
222   a->z -= b->z;
223 }
224
225 static void init_flare_stream (flare_stream *s, int num_flares, float bx, float by, float bz, float speed)
226 {
227   int i;
228
229   s->flares = (Vector *) calloc (num_flares, sizeof (Vector));
230   s->num_flares = num_flares;
231   s->flare_tex = gen_texture();
232   s->speed = speed;
233
234   for (i = 0; i != s->num_flares; i++)
235   {
236     s->flares[i].x = -800.0f * random() / RAND_MAX - 1150 + bx;
237     s->flares[i].y =   10.0f * random() / RAND_MAX -   20 + by;
238     s->flares[i].z =   10.0f * random() / RAND_MAX -   20 + bz;
239   }
240 }
241
242 static void render_flare_stream (flare_stream *s, float cur_time, Vector *vx, Vector *vy, float alpha)
243 {
244   float fMultipler = 1;
245   int i;
246
247   if (s->flare_tex == -1)
248     return;
249   if (!s->num_flares)
250     return;
251
252   if (cur_time < change_time)
253     return;
254
255   cur_time -= change_time;
256
257   glColor4f (1.0, 1, 1, alpha);
258 #ifdef HAVE_GLBINDTEXTURE
259   glBindTexture (GL_TEXTURE_2D, s->flare_tex);
260 #endif
261
262   glBegin (GL_QUADS);
263
264   if (cur_time + change_time > change_time1)
265   {
266     if (cur_time + change_time > change_time2)
267     {
268       fMultipler = 2.5;
269     }
270     else
271       fMultipler = 2;
272   }
273
274   for (i = 0; i != s->num_flares; i++)
275   {
276     Vector flare_pos;
277     Vector cc;
278
279     flare_pos.x = fmod (s->flares[i].x + cur_time * s->speed * fMultipler, 800) - 400;
280     flare_pos.y = s->flares[i].y + 2 * sin (cur_time * 7 + s->flares[i].x);
281     flare_pos.z = s->flares[i].z + 2 * cos (cur_time * 7 + i * 3.14);
282
283     glTexCoord2f (0, 0);
284     vector_copy  (&cc, &flare_pos);
285     vector_sub   (&cc, vx);
286     vector_add   (&cc, vy);
287     glVertex3fv  ((float *)&cc);
288
289     glTexCoord2f ( 1, 0 );
290     vector_copy  (&cc, &flare_pos);
291     vector_add   (&cc, vx);
292     vector_add   (&cc, vy);
293     glVertex3fv  ((float *)&cc);
294
295     glTexCoord2f ( 1, 1 );
296     vector_copy  (&cc, &flare_pos);
297     vector_add   (&cc, vx);
298     vector_sub   (&cc, vy);
299     glVertex3fv  ((float *)&cc);
300
301     glTexCoord2f ( 0, 1 );
302     vector_copy  (&cc, &flare_pos);
303     vector_sub   (&cc, vx);
304     vector_sub   (&cc, vy);
305     glVertex3fv  ((float *)&cc);
306   }
307
308   glEnd ();
309 }
310
311 static void free_stream (ModeInfo * mi);
312
313 ENTRYPOINT void
314 init_stream (ModeInfo *mi)
315 {
316   stream_configuration *es;
317   streamtime current_time;
318
319   MI_INIT (mi, ess, free_stream);
320
321   es = &ess[MI_SCREEN(mi)];
322
323   es->glx_context = init_GL (mi);
324
325   reshape_stream (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
326
327   gettime (&current_time);
328   es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time);
329
330   es->num_streams = num_streams;
331
332   es->streams = (flare_stream *) calloc (es->num_streams, sizeof(flare_stream));
333
334   init_flare_stream (&es->streams[0], 150, 0, 50, 0, 300);
335   init_flare_stream (&es->streams[1], 150, 0, 0, 0, 150);
336   init_flare_stream (&es->streams[2], 150, 0, 90, 60, 250);
337   init_flare_stream (&es->streams[3], 150, 0, -100, 30, 160);
338   init_flare_stream (&es->streams[4], 150, 0, 50, -100, 340);
339   init_flare_stream (&es->streams[5], 150, 0, -50, 50, 270 );
340   init_flare_stream (&es->streams[6], 150, 0, 100, 50, 180);
341   init_flare_stream (&es->streams[7], 150, 0, -30, 90, 130);
342
343   init_flare_stream (&es->streams[8], 150, 0, 150, 10, 200);
344   init_flare_stream (&es->streams[9], 150, 0, 100, -100, 210);
345   init_flare_stream (&es->streams[10], 150, 0, 190, 160, 220);
346   init_flare_stream (&es->streams[11], 150, 0, -200, 130, 230);
347   init_flare_stream (&es->streams[12], 150, 0, 150, -200, 240);
348   init_flare_stream (&es->streams[13], 150, 0, -150, 250, 160);
349   init_flare_stream (&es->streams[14], 150, 0, 200, 150, 230);
350   init_flare_stream (&es->streams[15], 150, 0, -130, 190, 250);
351
352   {
353     double spin_speed   = 0.5  * global_speed;
354     double wander_speed = 0.02 * global_speed;
355     double spin_accel   = 1.1;
356
357     es->rot = make_rotator (do_spin ? spin_speed : 0,
358                             do_spin ? spin_speed : 0,
359                             do_spin ? spin_speed : 0,
360                             spin_accel,
361                             do_wander ? wander_speed : 0,
362                             True);
363     es->trackball = gltrackball_init (True);
364   }
365 }
366
367 static void
368 free_stream (ModeInfo * mi)
369 {
370   stream_configuration *es = &ess[MI_SCREEN(mi)];
371   int i;
372
373   if (es->glx_context) {
374     glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
375
376     for (i = 0; i < es->num_streams; i++) {
377       free (es->streams[i].flares);
378       glDeleteTextures (1, &es->streams[i].flare_tex);
379     }
380
381     free (es->streams);
382   }
383 }
384
385
386 static void inverse_matrix (float m[16]) {
387   double a,b,c,d,e,f,g,h,i,j,k,l;
388   register double dW;
389
390   a = m[ 0]; b = m[ 1]; c = m[ 2];
391   d = m[ 4]; e = m[ 5]; f = m[ 6];
392   g = m[ 8]; h = m[ 9]; i = m[10];
393   j = m[12]; k = m[13]; l = m[14];
394
395   dW = 1.0 / (a * (e * i - f * h)
396            - (b * (d * i - f * g)
397            +  c * (e * g - d * h)));
398
399   m[ 0]= (float)((e * i - f * h) * dW);
400   m[ 1]= (float)((c * h - b * i) * dW);
401   m[ 2]= (float)((b * f - c * e) * dW);
402
403   m[ 4]= (float)((f * g - d * i) * dW);
404   m[ 5]= (float)((a * i - c * g) * dW);
405   m[ 6]= (float)((c * d - a * f) * dW);
406
407   m[ 8]= (float)((d * h - e * g) * dW);
408   m[ 9]= (float)((b * g - a * h) * dW);
409   m[10]= (float)((a * e - b * d) * dW);
410
411   m[12]= (float)((e * (g * l - i * j)
412                 + f * (h * j - g * k)
413                 - d * (h * l - i * k)) * dW);
414   m[13]= (float)((a * (h * l - i * k)
415                 + b * (i * j - g * l)
416                 + c * (g * k - h * j)) * dW);
417   m[14]= (float)((b * (d * l - f * j)
418                 + c * (e * j - d * k)
419                 - a * (e * l - f * k)) * dW);
420 }
421
422 ENTRYPOINT void
423 draw_stream (ModeInfo *mi)
424 {
425   stream_configuration *es = &ess[MI_SCREEN(mi)];
426   Display *dpy = MI_DISPLAY(mi);
427   Window window = MI_WINDOW(mi);
428   streamtime current_time;
429   float cur_time;
430   int i;
431   float alpha = 1.0;
432   Vector vx;
433   Vector vy;
434   GLfloat m[4*4];
435
436   if (!es->glx_context)
437     return;
438
439   gettime (&current_time);
440
441   cur_time = (float)(GETSECS(current_time) * 1000 + GETMSECS(current_time) - es->start_time) / 1000.0;
442   cur_time *= global_speed;
443
444   glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
445
446   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
447
448   glMatrixMode (GL_PROJECTION);
449   glLoadIdentity ();
450   glFrustum (-.6f, .6f, -.45f, .45f, 1, 1000);
451
452   glMatrixMode (GL_MODELVIEW);
453   glLoadIdentity ();
454
455   glEnable (GL_LIGHTING);
456   glEnable (GL_TEXTURE_2D);
457   glEnable (GL_BLEND);
458   glBlendFunc (GL_SRC_ALPHA, GL_ONE);
459   glDisable (GL_CULL_FACE);
460   glDisable (GL_DEPTH_TEST);
461   glDepthMask (0);
462
463   glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
464
465   glTranslatef (0, 0, -300);
466   glRotatef (cur_time * 30, 1, 0, 0);
467   glRotatef (30 * sin(cur_time / 3) + 10, 0, 0, 1);
468
469   {
470     double x, y, z;
471     get_position (es->rot, &x, &y, &z, !es->button_down_p);
472     glTranslatef((x - 0.5) * 8,
473                  (y - 0.5) * 8,
474                  (z - 0.5) * 15);
475
476     gltrackball_rotate (es->trackball);
477
478     get_rotation (es->rot, &x, &y, &z, !es->button_down_p);
479     glRotatef (x * 360, 1.0, 0.0, 0.0);
480     glRotatef (y * 360, 0.0, 1.0, 0.0);
481     glRotatef (z * 360, 0.0, 0.0, 1.0);
482   }
483
484   if (cur_time > change_time1)
485   {
486     if (cur_time > change_time2)
487     {
488       glRotatef (90, 0, 1, 0);
489
490       if (cur_time > change_time3)
491         es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time) - 5000;
492     }
493     else
494     {
495       glRotatef (180, 0, 1, 0);
496     }
497   }
498
499   glEnable ( GL_FOG);
500   glFogf (GL_FOG_START, 200);
501   glFogf (GL_FOG_END, 500);
502   glFogf (GL_FOG_MODE, GL_LINEAR);
503
504   glGetFloatv (GL_MODELVIEW_MATRIX, m);
505
506   inverse_matrix (m);
507
508   vx.x = m[0] * 10;
509   vx.y = m[1] * 10;
510   vx.z = m[2] * 10;
511
512   vy.x = m[4] * 10;
513   vy.y = m[5] * 10;
514   vy.z = m[6] * 10;
515
516   mi->polygon_count = 0;
517
518   for (i = 0; i != es->num_streams; i++)
519   {
520     mi->polygon_count += es->streams[i].num_flares;
521     render_flare_stream (&es->streams[i], cur_time, &vx, &vy, alpha);
522   }
523
524   glDisable (GL_TEXTURE_2D);
525   glDisable (GL_LIGHTING);
526   glDisable (GL_FOG);
527   glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
528
529   if (mi->fps_p) do_fps (mi);
530   glFinish();
531
532   glXSwapBuffers (dpy, window);
533 }
534
535 XSCREENSAVER_MODULE_2("EnergyStream", energystream, stream)
536
537 #endif /* USE_GL */