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