1 /* energystream, Copyright (c) 2016 Eugene Sandulenko <sev@scummvm.org>
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
11 * Based on Public Domain code by konrad "yoghurt" zagorowicz
12 * for Tesla demo by Sunflower (http://www.pouet.net/prod.php?which=33)
15 #define DEFAULTS "*delay: 30000 \n" \
17 "*showFPS: False \n" \
18 "*wireframe: False \n" \
20 # define refresh_stream 0
22 #define countof(x) (sizeof((x))/sizeof((*x)))
24 #include "xlockmore.h"
25 #include "gltrackball.h"
29 #ifdef USE_GL /* whole file */
31 static float change_time = 0;
32 static float change_time1 = 25;
33 static float change_time2 = 40;
34 static float change_time3 = 60;
36 #define DEF_STREAMS "16"
41 typedef struct timeval streamtime;
43 #define GETSECS(t) ((t).tv_sec)
44 #define GETMSECS(t) ((t).tv_usec/1000)
46 #define DEF_SPIN "False"
47 #define DEF_WANDER "False"
48 #define DEF_SPEED "1.0"
63 GLXContext *glx_context;
65 trackball_state *trackball;
71 flare_stream *streams;
73 } stream_configuration;
75 static stream_configuration *ess = NULL;
77 static int num_streams;
79 static GLfloat global_speed;
80 static Bool do_wander;
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" }
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},
98 ENTRYPOINT ModeSpecOpt stream_opts = {countof(opts), opts, countof(vars), vars, NULL};
100 static void gettime(streamtime *t)
102 #ifdef GETTIMEOFDAY_TWO_ARGS
104 gettimeofday(t, &tzp);
105 #else /* !GETTIMEOFDAY_TWO_ARGS */
107 #endif /* !GETTIMEOFDAY_TWO_ARGS */
111 /* Window management, etc
114 reshape_stream (ModeInfo *mi, int width, int height)
116 GLfloat h = (GLfloat) height / (GLfloat) width;
118 glViewport (0, 0, (GLint) width, (GLint) height);
120 glMatrixMode (GL_PROJECTION);
122 gluPerspective (30.0, 1/h, 1.0, 100.0);
124 glMatrixMode (GL_MODELVIEW);
126 gluLookAt (0.0, 0.0, 30.0,
130 glClear (GL_COLOR_BUFFER_BIT);
135 stream_handle_event (ModeInfo *mi, XEvent *event)
137 stream_configuration *es = &ess[MI_SCREEN(mi)];
139 if (gltrackball_event_handler (event, es->trackball,
140 MI_WIDTH (mi), MI_HEIGHT (mi),
147 #define TEX_WIDTH 256
148 #define TEX_HEIGHT 256
151 static GLuint gen_texture (void)
157 unsigned char *texture = (unsigned char *)calloc (TEX_WIDTH * TEX_HEIGHT, 4);
158 unsigned char *ptr = texture;
161 for (i = 0; i < 3; i++)
162 tint[i] = 1.0 * random() / RAND_MAX;
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);
172 for (i = 0; i < 3; i++)
173 ptr[i] = (unsigned char)(color * tint[i]);
174 ptr[3] = color ? 255 : 0;
180 glGenTextures (1, &tex);
181 #ifdef HAVE_GLBINDTEXTURE
182 glBindTexture (GL_TEXTURE_2D, tex);
183 #endif /* HAVE_GLBINDTEXTURE */
184 glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
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");
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);
201 static inline void vector_copy (Vector *a, Vector *b)
209 static inline void vector_add (Vector *a, Vector *b)
217 static inline void vector_sub (Vector *a, Vector *b)
224 static void init_flare_stream (flare_stream *s, int num_flares, float bx, float by, float bz, float speed)
228 s->flares = (Vector *) calloc (num_flares, sizeof (Vector));
229 s->num_flares = num_flares;
230 s->flare_tex = gen_texture();
233 for (i = 0; i != s->num_flares; i++)
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;
241 static void render_flare_stream (flare_stream *s, float cur_time, Vector *vx, Vector *vy, float alpha)
243 float fMultipler = 1;
246 if (s->flare_tex == -1)
251 if (cur_time < change_time)
254 cur_time -= change_time;
256 glColor4f (1.0, 1, 1, alpha);
257 #ifdef HAVE_GLBINDTEXTURE
258 glBindTexture (GL_TEXTURE_2D, s->flare_tex);
263 if (cur_time + change_time > change_time1)
265 if (cur_time + change_time > change_time2)
273 for (i = 0; i != s->num_flares; i++)
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);
283 vector_copy (&cc, &flare_pos);
284 vector_sub (&cc, vx);
285 vector_add (&cc, vy);
286 glVertex3fv ((float *)&cc);
288 glTexCoord2f ( 1, 0 );
289 vector_copy (&cc, &flare_pos);
290 vector_add (&cc, vx);
291 vector_add (&cc, vy);
292 glVertex3fv ((float *)&cc);
294 glTexCoord2f ( 1, 1 );
295 vector_copy (&cc, &flare_pos);
296 vector_add (&cc, vx);
297 vector_sub (&cc, vy);
298 glVertex3fv ((float *)&cc);
300 glTexCoord2f ( 0, 1 );
301 vector_copy (&cc, &flare_pos);
302 vector_sub (&cc, vx);
303 vector_sub (&cc, vy);
304 glVertex3fv ((float *)&cc);
311 init_stream (ModeInfo *mi)
313 stream_configuration *es;
314 streamtime current_time;
318 ess = (stream_configuration *)
319 calloc (MI_NUM_SCREENS(mi), sizeof (stream_configuration));
322 fprintf (stderr, "%s: out of memory\n", progname);
327 es = &ess[MI_SCREEN(mi)];
329 es->glx_context = init_GL (mi);
331 reshape_stream (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
333 gettime (¤t_time);
334 es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time);
336 es->num_streams = num_streams;
338 es->streams = (flare_stream *) calloc (es->num_streams, sizeof(flare_stream));
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);
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);
359 double spin_speed = 0.5 * global_speed;
360 double wander_speed = 0.02 * global_speed;
361 double spin_accel = 1.1;
363 es->rot = make_rotator (do_spin ? spin_speed : 0,
364 do_spin ? spin_speed : 0,
365 do_spin ? spin_speed : 0,
367 do_wander ? wander_speed : 0,
369 es->trackball = gltrackball_init (True);
374 release_stream (ModeInfo * mi)
378 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
380 stream_configuration *es = &ess[screen];
383 for (i = 0; i < es->num_streams; i++) {
384 free (es->streams[i].flares);
385 glDeleteTextures (1, &es->streams[i].flare_tex);
395 static void inverse_matrix (float m[16]) {
396 double a,b,c,d,e,f,g,h,i,j,k,l;
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];
404 dW = 1.0 / (a * (e * i - f * h)
405 - (b * (d * i - f * g)
406 + c * (e * g - d * h)));
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);
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);
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);
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);
432 draw_stream (ModeInfo *mi)
434 stream_configuration *es = &ess[MI_SCREEN(mi)];
435 Display *dpy = MI_DISPLAY(mi);
436 Window window = MI_WINDOW(mi);
437 streamtime current_time;
445 if (!es->glx_context)
448 gettime (¤t_time);
450 cur_time = (float)(GETSECS(current_time) * 1000 + GETMSECS(current_time) - es->start_time) / 1000.0;
451 cur_time *= global_speed;
453 glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
455 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
457 glMatrixMode (GL_PROJECTION);
459 glFrustum (-.6f, .6f, -.45f, .45f, 1, 1000);
461 glMatrixMode (GL_MODELVIEW);
464 glEnable (GL_LIGHTING);
465 glEnable (GL_TEXTURE_2D);
467 glBlendFunc (GL_SRC_ALPHA, GL_ONE);
468 glDisable (GL_CULL_FACE);
469 glDisable (GL_DEPTH_TEST);
472 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
474 glTranslatef (0, 0, -300);
475 glRotatef (cur_time * 30, 1, 0, 0);
476 glRotatef (30 * sin(cur_time / 3) + 10, 0, 0, 1);
480 get_position (es->rot, &x, &y, &z, !es->button_down_p);
481 glTranslatef((x - 0.5) * 8,
485 gltrackball_rotate (es->trackball);
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);
493 if (cur_time > change_time1)
495 if (cur_time > change_time2)
497 glRotatef (90, 0, 1, 0);
499 if (cur_time > change_time3)
500 es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time) - 5000;
504 glRotatef (180, 0, 1, 0);
509 glFogf (GL_FOG_START, 200);
510 glFogf (GL_FOG_END, 500);
511 glFogf (GL_FOG_MODE, GL_LINEAR);
513 glGetFloatv (GL_MODELVIEW_MATRIX, m);
525 mi->polygon_count = 0;
527 for (i = 0; i != es->num_streams; i++)
529 mi->polygon_count += es->streams[i].num_flares;
530 render_flare_stream (&es->streams[i], cur_time, &vx, &vy, alpha);
533 glDisable (GL_TEXTURE_2D);
534 glDisable (GL_LIGHTING);
536 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
538 if (mi->fps_p) do_fps (mi);
541 glXSwapBuffers (dpy, window);
544 XSCREENSAVER_MODULE_2("EnergyStream", energystream, stream)