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 release_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 es = &ess[MI_SCREEN(mi)];
320 es->glx_context = init_GL (mi);
322 reshape_stream (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
324 gettime (¤t_time);
325 es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time);
327 es->num_streams = num_streams;
329 es->streams = (flare_stream *) calloc (es->num_streams, sizeof(flare_stream));
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);
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);
350 double spin_speed = 0.5 * global_speed;
351 double wander_speed = 0.02 * global_speed;
352 double spin_accel = 1.1;
354 es->rot = make_rotator (do_spin ? spin_speed : 0,
355 do_spin ? spin_speed : 0,
356 do_spin ? spin_speed : 0,
358 do_wander ? wander_speed : 0,
360 es->trackball = gltrackball_init (True);
365 free_stream (ModeInfo * mi)
367 stream_configuration *es = &ess[MI_SCREEN(mi)];
370 if (es->glx_context) {
371 glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
373 for (i = 0; i < es->num_streams; i++) {
374 free (es->streams[i].flares);
375 glDeleteTextures (1, &es->streams[i].flare_tex);
383 static void inverse_matrix (float m[16]) {
384 double a,b,c,d,e,f,g,h,i,j,k,l;
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];
392 dW = 1.0 / (a * (e * i - f * h)
393 - (b * (d * i - f * g)
394 + c * (e * g - d * h)));
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);
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);
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);
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);
420 draw_stream (ModeInfo *mi)
422 stream_configuration *es = &ess[MI_SCREEN(mi)];
423 Display *dpy = MI_DISPLAY(mi);
424 Window window = MI_WINDOW(mi);
425 streamtime current_time;
433 if (!es->glx_context)
436 gettime (¤t_time);
438 cur_time = (float)(GETSECS(current_time) * 1000 + GETMSECS(current_time) - es->start_time) / 1000.0;
439 cur_time *= global_speed;
441 glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
443 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
445 glMatrixMode (GL_PROJECTION);
447 glFrustum (-.6f, .6f, -.45f, .45f, 1, 1000);
449 glMatrixMode (GL_MODELVIEW);
452 glEnable (GL_LIGHTING);
453 glEnable (GL_TEXTURE_2D);
455 glBlendFunc (GL_SRC_ALPHA, GL_ONE);
456 glDisable (GL_CULL_FACE);
457 glDisable (GL_DEPTH_TEST);
460 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
462 glTranslatef (0, 0, -300);
463 glRotatef (cur_time * 30, 1, 0, 0);
464 glRotatef (30 * sin(cur_time / 3) + 10, 0, 0, 1);
468 get_position (es->rot, &x, &y, &z, !es->button_down_p);
469 glTranslatef((x - 0.5) * 8,
473 gltrackball_rotate (es->trackball);
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);
481 if (cur_time > change_time1)
483 if (cur_time > change_time2)
485 glRotatef (90, 0, 1, 0);
487 if (cur_time > change_time3)
488 es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time) - 5000;
492 glRotatef (180, 0, 1, 0);
497 glFogf (GL_FOG_START, 200);
498 glFogf (GL_FOG_END, 500);
499 glFogf (GL_FOG_MODE, GL_LINEAR);
501 glGetFloatv (GL_MODELVIEW_MATRIX, m);
513 mi->polygon_count = 0;
515 for (i = 0; i != es->num_streams; i++)
517 mi->polygon_count += es->streams[i].num_flares;
518 render_flare_stream (&es->streams[i], cur_time, &vx, &vy, alpha);
521 glDisable (GL_TEXTURE_2D);
522 glDisable (GL_LIGHTING);
524 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
526 if (mi->fps_p) do_fps (mi);
529 glXSwapBuffers (dpy, window);
532 XSCREENSAVER_MODULE_2("EnergyStream", energystream, stream)