From http://www.jwz.org/xscreensaver/xscreensaver-5.38.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 release_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   MI_INIT (mi, ess);
317
318   es = &ess[MI_SCREEN(mi)];
319
320   es->glx_context = init_GL (mi);
321
322   reshape_stream (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
323
324   gettime (&current_time);
325   es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time);
326
327   es->num_streams = num_streams;
328
329   es->streams = (flare_stream *) calloc (es->num_streams, sizeof(flare_stream));
330
331   init_flare_stream (&es->streams[0], 150, 0, 50, 0, 300);
332   init_flare_stream (&es->streams[1], 150, 0, 0, 0, 150);
333   init_flare_stream (&es->streams[2], 150, 0, 90, 60, 250);
334   init_flare_stream (&es->streams[3], 150, 0, -100, 30, 160);
335   init_flare_stream (&es->streams[4], 150, 0, 50, -100, 340);
336   init_flare_stream (&es->streams[5], 150, 0, -50, 50, 270 );
337   init_flare_stream (&es->streams[6], 150, 0, 100, 50, 180);
338   init_flare_stream (&es->streams[7], 150, 0, -30, 90, 130);
339
340   init_flare_stream (&es->streams[8], 150, 0, 150, 10, 200);
341   init_flare_stream (&es->streams[9], 150, 0, 100, -100, 210);
342   init_flare_stream (&es->streams[10], 150, 0, 190, 160, 220);
343   init_flare_stream (&es->streams[11], 150, 0, -200, 130, 230);
344   init_flare_stream (&es->streams[12], 150, 0, 150, -200, 240);
345   init_flare_stream (&es->streams[13], 150, 0, -150, 250, 160);
346   init_flare_stream (&es->streams[14], 150, 0, 200, 150, 230);
347   init_flare_stream (&es->streams[15], 150, 0, -130, 190, 250);
348
349   {
350     double spin_speed   = 0.5  * global_speed;
351     double wander_speed = 0.02 * global_speed;
352     double spin_accel   = 1.1;
353
354     es->rot = make_rotator (do_spin ? spin_speed : 0,
355                             do_spin ? spin_speed : 0,
356                             do_spin ? spin_speed : 0,
357                             spin_accel,
358                             do_wander ? wander_speed : 0,
359                             True);
360     es->trackball = gltrackball_init (True);
361   }
362 }
363
364 ENTRYPOINT void
365 free_stream (ModeInfo * mi)
366 {
367   stream_configuration *es = &ess[MI_SCREEN(mi)];
368   int i;
369
370   if (es->glx_context) {
371     glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
372
373     for (i = 0; i < es->num_streams; i++) {
374       free (es->streams[i].flares);
375       glDeleteTextures (1, &es->streams[i].flare_tex);
376     }
377
378     free (es->streams);
379   }
380 }
381
382
383 static void inverse_matrix (float m[16]) {
384   double a,b,c,d,e,f,g,h,i,j,k,l;
385   register double dW;
386
387   a = m[ 0]; b = m[ 1]; c = m[ 2];
388   d = m[ 4]; e = m[ 5]; f = m[ 6];
389   g = m[ 8]; h = m[ 9]; i = m[10];
390   j = m[12]; k = m[13]; l = m[14];
391
392   dW = 1.0 / (a * (e * i - f * h)
393            - (b * (d * i - f * g)
394            +  c * (e * g - d * h)));
395
396   m[ 0]= (float)((e * i - f * h) * dW);
397   m[ 1]= (float)((c * h - b * i) * dW);
398   m[ 2]= (float)((b * f - c * e) * dW);
399
400   m[ 4]= (float)((f * g - d * i) * dW);
401   m[ 5]= (float)((a * i - c * g) * dW);
402   m[ 6]= (float)((c * d - a * f) * dW);
403
404   m[ 8]= (float)((d * h - e * g) * dW);
405   m[ 9]= (float)((b * g - a * h) * dW);
406   m[10]= (float)((a * e - b * d) * dW);
407
408   m[12]= (float)((e * (g * l - i * j)
409                 + f * (h * j - g * k)
410                 - d * (h * l - i * k)) * dW);
411   m[13]= (float)((a * (h * l - i * k)
412                 + b * (i * j - g * l)
413                 + c * (g * k - h * j)) * dW);
414   m[14]= (float)((b * (d * l - f * j)
415                 + c * (e * j - d * k)
416                 - a * (e * l - f * k)) * dW);
417 }
418
419 ENTRYPOINT void
420 draw_stream (ModeInfo *mi)
421 {
422   stream_configuration *es = &ess[MI_SCREEN(mi)];
423   Display *dpy = MI_DISPLAY(mi);
424   Window window = MI_WINDOW(mi);
425   streamtime current_time;
426   float cur_time;
427   int i;
428   float alpha = 1.0;
429   Vector vx;
430   Vector vy;
431   GLfloat m[4*4];
432
433   if (!es->glx_context)
434     return;
435
436   gettime (&current_time);
437
438   cur_time = (float)(GETSECS(current_time) * 1000 + GETMSECS(current_time) - es->start_time) / 1000.0;
439   cur_time *= global_speed;
440
441   glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
442
443   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
444
445   glMatrixMode (GL_PROJECTION);
446   glLoadIdentity ();
447   glFrustum (-.6f, .6f, -.45f, .45f, 1, 1000);
448
449   glMatrixMode (GL_MODELVIEW);
450   glLoadIdentity ();
451
452   glEnable (GL_LIGHTING);
453   glEnable (GL_TEXTURE_2D);
454   glEnable (GL_BLEND);
455   glBlendFunc (GL_SRC_ALPHA, GL_ONE);
456   glDisable (GL_CULL_FACE);
457   glDisable (GL_DEPTH_TEST);
458   glDepthMask (0);
459
460   glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
461
462   glTranslatef (0, 0, -300);
463   glRotatef (cur_time * 30, 1, 0, 0);
464   glRotatef (30 * sin(cur_time / 3) + 10, 0, 0, 1);
465
466   {
467     double x, y, z;
468     get_position (es->rot, &x, &y, &z, !es->button_down_p);
469     glTranslatef((x - 0.5) * 8,
470                  (y - 0.5) * 8,
471                  (z - 0.5) * 15);
472
473     gltrackball_rotate (es->trackball);
474
475     get_rotation (es->rot, &x, &y, &z, !es->button_down_p);
476     glRotatef (x * 360, 1.0, 0.0, 0.0);
477     glRotatef (y * 360, 0.0, 1.0, 0.0);
478     glRotatef (z * 360, 0.0, 0.0, 1.0);
479   }
480
481   if (cur_time > change_time1)
482   {
483     if (cur_time > change_time2)
484     {
485       glRotatef (90, 0, 1, 0);
486
487       if (cur_time > change_time3)
488         es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time) - 5000;
489     }
490     else
491     {
492       glRotatef (180, 0, 1, 0);
493     }
494   }
495
496   glEnable ( GL_FOG);
497   glFogf (GL_FOG_START, 200);
498   glFogf (GL_FOG_END, 500);
499   glFogf (GL_FOG_MODE, GL_LINEAR);
500
501   glGetFloatv (GL_MODELVIEW_MATRIX, m);
502
503   inverse_matrix (m);
504
505   vx.x = m[0] * 10;
506   vx.y = m[1] * 10;
507   vx.z = m[2] * 10;
508
509   vy.x = m[4] * 10;
510   vy.y = m[5] * 10;
511   vy.z = m[6] * 10;
512
513   mi->polygon_count = 0;
514
515   for (i = 0; i != es->num_streams; i++)
516   {
517     mi->polygon_count += es->streams[i].num_flares;
518     render_flare_stream (&es->streams[i], cur_time, &vx, &vy, alpha);
519   }
520
521   glDisable (GL_TEXTURE_2D);
522   glDisable (GL_LIGHTING);
523   glDisable (GL_FOG);
524   glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
525
526   if (mi->fps_p) do_fps (mi);
527   glFinish();
528
529   glXSwapBuffers (dpy, window);
530 }
531
532 XSCREENSAVER_MODULE_2("EnergyStream", energystream, stream)
533
534 #endif /* USE_GL */