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
21 # define release_stream 0
23 #define countof(x) (sizeof((x))/sizeof((*x)))
25 #include "xlockmore.h"
26 #include "gltrackball.h"
30 #ifdef USE_GL /* whole file */
32 static float change_time = 0;
33 static float change_time1 = 25;
34 static float change_time2 = 40;
35 static float change_time3 = 60;
37 #define DEF_STREAMS "16"
42 typedef struct timeval streamtime;
44 #define GETSECS(t) ((t).tv_sec)
45 #define GETMSECS(t) ((t).tv_usec/1000)
47 #define DEF_SPIN "False"
48 #define DEF_WANDER "False"
49 #define DEF_SPEED "1.0"
64 GLXContext *glx_context;
66 trackball_state *trackball;
72 flare_stream *streams;
74 } stream_configuration;
76 static stream_configuration *ess = NULL;
78 static int num_streams;
80 static GLfloat global_speed;
81 static Bool do_wander;
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" }
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},
99 ENTRYPOINT ModeSpecOpt stream_opts = {countof(opts), opts, countof(vars), vars, NULL};
101 static void gettime(streamtime *t)
103 #ifdef GETTIMEOFDAY_TWO_ARGS
105 gettimeofday(t, &tzp);
106 #else /* !GETTIMEOFDAY_TWO_ARGS */
108 #endif /* !GETTIMEOFDAY_TWO_ARGS */
112 /* Window management, etc
115 reshape_stream (ModeInfo *mi, int width, int height)
117 GLfloat h = (GLfloat) height / (GLfloat) width;
119 glViewport (0, 0, (GLint) width, (GLint) height);
121 glMatrixMode (GL_PROJECTION);
123 gluPerspective (30.0, 1/h, 1.0, 100.0);
125 glMatrixMode (GL_MODELVIEW);
127 gluLookAt (0.0, 0.0, 30.0,
131 glClear (GL_COLOR_BUFFER_BIT);
136 stream_handle_event (ModeInfo *mi, XEvent *event)
138 stream_configuration *es = &ess[MI_SCREEN(mi)];
140 if (gltrackball_event_handler (event, es->trackball,
141 MI_WIDTH (mi), MI_HEIGHT (mi),
148 #define TEX_WIDTH 256
149 #define TEX_HEIGHT 256
152 static GLuint gen_texture (void)
158 unsigned char *texture = (unsigned char *)calloc (TEX_WIDTH * TEX_HEIGHT, 4);
159 unsigned char *ptr = texture;
162 for (i = 0; i < 3; i++)
163 tint[i] = 1.0 * random() / RAND_MAX;
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);
173 for (i = 0; i < 3; i++)
174 ptr[i] = (unsigned char)(color * tint[i]);
175 ptr[3] = color ? 255 : 0;
181 glGenTextures (1, &tex);
182 #ifdef HAVE_GLBINDTEXTURE
183 glBindTexture (GL_TEXTURE_2D, tex);
184 #endif /* HAVE_GLBINDTEXTURE */
185 glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
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");
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);
202 static inline void vector_copy (Vector *a, Vector *b)
210 static inline void vector_add (Vector *a, Vector *b)
218 static inline void vector_sub (Vector *a, Vector *b)
225 static void init_flare_stream (flare_stream *s, int num_flares, float bx, float by, float bz, float speed)
229 s->flares = (Vector *) calloc (num_flares, sizeof (Vector));
230 s->num_flares = num_flares;
231 s->flare_tex = gen_texture();
234 for (i = 0; i != s->num_flares; i++)
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;
242 static void render_flare_stream (flare_stream *s, float cur_time, Vector *vx, Vector *vy, float alpha)
244 float fMultipler = 1;
247 if (s->flare_tex == -1)
252 if (cur_time < change_time)
255 cur_time -= change_time;
257 glColor4f (1.0, 1, 1, alpha);
258 #ifdef HAVE_GLBINDTEXTURE
259 glBindTexture (GL_TEXTURE_2D, s->flare_tex);
264 if (cur_time + change_time > change_time1)
266 if (cur_time + change_time > change_time2)
274 for (i = 0; i != s->num_flares; i++)
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);
284 vector_copy (&cc, &flare_pos);
285 vector_sub (&cc, vx);
286 vector_add (&cc, vy);
287 glVertex3fv ((float *)&cc);
289 glTexCoord2f ( 1, 0 );
290 vector_copy (&cc, &flare_pos);
291 vector_add (&cc, vx);
292 vector_add (&cc, vy);
293 glVertex3fv ((float *)&cc);
295 glTexCoord2f ( 1, 1 );
296 vector_copy (&cc, &flare_pos);
297 vector_add (&cc, vx);
298 vector_sub (&cc, vy);
299 glVertex3fv ((float *)&cc);
301 glTexCoord2f ( 0, 1 );
302 vector_copy (&cc, &flare_pos);
303 vector_sub (&cc, vx);
304 vector_sub (&cc, vy);
305 glVertex3fv ((float *)&cc);
311 static void free_stream (ModeInfo * mi);
314 init_stream (ModeInfo *mi)
316 stream_configuration *es;
317 streamtime current_time;
319 MI_INIT (mi, ess, free_stream);
321 es = &ess[MI_SCREEN(mi)];
323 es->glx_context = init_GL (mi);
325 reshape_stream (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
327 gettime (¤t_time);
328 es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time);
330 es->num_streams = num_streams;
332 es->streams = (flare_stream *) calloc (es->num_streams, sizeof(flare_stream));
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);
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);
353 double spin_speed = 0.5 * global_speed;
354 double wander_speed = 0.02 * global_speed;
355 double spin_accel = 1.1;
357 es->rot = make_rotator (do_spin ? spin_speed : 0,
358 do_spin ? spin_speed : 0,
359 do_spin ? spin_speed : 0,
361 do_wander ? wander_speed : 0,
363 es->trackball = gltrackball_init (True);
368 free_stream (ModeInfo * mi)
370 stream_configuration *es = &ess[MI_SCREEN(mi)];
373 if (es->glx_context) {
374 glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
376 for (i = 0; i < es->num_streams; i++) {
377 free (es->streams[i].flares);
378 glDeleteTextures (1, &es->streams[i].flare_tex);
386 static void inverse_matrix (float m[16]) {
387 double a,b,c,d,e,f,g,h,i,j,k,l;
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];
395 dW = 1.0 / (a * (e * i - f * h)
396 - (b * (d * i - f * g)
397 + c * (e * g - d * h)));
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);
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);
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);
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);
423 draw_stream (ModeInfo *mi)
425 stream_configuration *es = &ess[MI_SCREEN(mi)];
426 Display *dpy = MI_DISPLAY(mi);
427 Window window = MI_WINDOW(mi);
428 streamtime current_time;
436 if (!es->glx_context)
439 gettime (¤t_time);
441 cur_time = (float)(GETSECS(current_time) * 1000 + GETMSECS(current_time) - es->start_time) / 1000.0;
442 cur_time *= global_speed;
444 glXMakeCurrent (MI_DISPLAY(mi), MI_WINDOW(mi), *(es->glx_context));
446 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
448 glMatrixMode (GL_PROJECTION);
450 glFrustum (-.6f, .6f, -.45f, .45f, 1, 1000);
452 glMatrixMode (GL_MODELVIEW);
455 glEnable (GL_LIGHTING);
456 glEnable (GL_TEXTURE_2D);
458 glBlendFunc (GL_SRC_ALPHA, GL_ONE);
459 glDisable (GL_CULL_FACE);
460 glDisable (GL_DEPTH_TEST);
463 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
465 glTranslatef (0, 0, -300);
466 glRotatef (cur_time * 30, 1, 0, 0);
467 glRotatef (30 * sin(cur_time / 3) + 10, 0, 0, 1);
471 get_position (es->rot, &x, &y, &z, !es->button_down_p);
472 glTranslatef((x - 0.5) * 8,
476 gltrackball_rotate (es->trackball);
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);
484 if (cur_time > change_time1)
486 if (cur_time > change_time2)
488 glRotatef (90, 0, 1, 0);
490 if (cur_time > change_time3)
491 es->start_time = GETSECS(current_time) * 1000 + GETMSECS(current_time) - 5000;
495 glRotatef (180, 0, 1, 0);
500 glFogf (GL_FOG_START, 200);
501 glFogf (GL_FOG_END, 500);
502 glFogf (GL_FOG_MODE, GL_LINEAR);
504 glGetFloatv (GL_MODELVIEW_MATRIX, m);
516 mi->polygon_count = 0;
518 for (i = 0; i != es->num_streams; i++)
520 mi->polygon_count += es->streams[i].num_flares;
521 render_flare_stream (&es->streams[i], cur_time, &vx, &vy, alpha);
524 glDisable (GL_TEXTURE_2D);
525 glDisable (GL_LIGHTING);
527 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
529 if (mi->fps_p) do_fps (mi);
532 glXSwapBuffers (dpy, window);
535 XSCREENSAVER_MODULE_2("EnergyStream", energystream, stream)