Debian Codegraph @

plugins/actor/madspin/madspin.c

raw
1 /* lIBvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2004, 2005, 2006 Dennis Smit <[email protected]>
4 *
5 * Authors: Dennis Smit <[email protected]>
6 * Andrew Birck <[email protected]>
7 *
8 * $Id: madspin.c,v 1.32 2006/02/25 18:45:15 synap Exp $
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <math.h>
33 #include <gettext.h>
34
35 #include <time.h>
36
37 #ifdef HAVE_GL_GL_H
38 # include <GL/gl.h>
39 #elif defined(HAVE_OPENGL_GL_H)
40 # include <OpenGL/gl.h>
41 #else
42 # error neither GL/gl.h nor OpenGL/gl.h available
43 #endif
44
45 #include <libvisual/libvisual.h>
46
47 typedef struct {
48 int initialized;
49
50 int draw_mode;
51
52 GLuint texture[2];
53
54 int maxlines;
55 float texsize;
56 float xrot;
57 float yrot;
58 float zrot;
59 float total;
60 float frame;
61
62 float gdata[256];
63
64 struct timeval tv_past;
65
66 /* Config */
67 int num_stars;
68 int speed;
69
70 VisRandomContext *rcontext;
71 } MadspinPrivate;
72
73 int lv_madspin_init (VisPluginData *plugin);
74 int lv_madspin_cleanup (VisPluginData *plugin);
75 int lv_madspin_requisition (VisPluginData *plugin, int *width, int *height);
76 int lv_madspin_dimension (VisPluginData *plugin, VisVideo *video, int width, int height);
77 int lv_madspin_events (VisPluginData *plugin, VisEventQueue *events);
78 VisPalette *lv_madspin_palette (VisPluginData *plugin);
79 int lv_madspin_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio);
80
81 static int madspin_load_textures (MadspinPrivate *priv);
82 static int madspin_sound (MadspinPrivate *priv, VisAudio *audio);
83 static int madspin_draw (MadspinPrivate *priv, VisVideo *video);
84
85 VISUAL_PLUGIN_API_VERSION_VALIDATOR
86
87 /* Main plugin stuff */
88 const VisPluginInfo *get_plugin_info (int *count)
89 {
90 static VisActorPlugin actor[] = {{
91 .requisition = lv_madspin_requisition,
92 .palette = lv_madspin_palette,
93 .render = lv_madspin_render,
94 .vidoptions.depth = VISUAL_VIDEO_DEPTH_GL
95 }};
96
97 static VisPluginInfo info[] = {{
98 .type = VISUAL_PLUGIN_TYPE_ACTOR,
99
100 .plugname = "madspin",
101 .name = "libvisual madspin port",
102 .author = N_("Original by: Andrew Birck <[email protected]>, Port by: Dennis Smit <[email protected]>"),
103 .version = "0.1",
104 .about = N_("Libvisual madspin plugin"),
105 .help = N_("This plugin shows a nifty visual effect using openGL"),
106 .license = VISUAL_PLUGIN_LICENSE_GPL,
107
108 .init = lv_madspin_init,
109 .cleanup = lv_madspin_cleanup,
110 .events = lv_madspin_events,
111
112 .plugin = VISUAL_OBJECT (&actor[0])
113 }};
114
115 *count = sizeof (info) / sizeof (*info);
116
117 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RED_SIZE, 5);
118 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_GREEN_SIZE, 5);
119 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_BLUE_SIZE, 5);
120 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DEPTH_SIZE, 16);
121 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DOUBLEBUFFER, 1);
122 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RGBA, 1);
123
124 return info;
125 }
126
127 int lv_madspin_init (VisPluginData *plugin)
128 {
129 MadspinPrivate *priv;
130 VisParamContainer *paramcontainer = visual_plugin_get_params (plugin);
131
132 static VisParamEntry params[] = {
133 VISUAL_PARAM_LIST_ENTRY_INTEGER ("num stars", 512),
134 VISUAL_PARAM_LIST_ENTRY_INTEGER ("speed", 715),
135 VISUAL_PARAM_LIST_END
136 };
137
138 /* UI vars */
139 VisUIWidget *table;
140 VisUIWidget *label1;
141 VisUIWidget *label2;
142 VisUIWidget *slider1;
143 VisUIWidget *slider2;
144 VisUIWidget *numeric1;
145 VisUIWidget *numeric2;
146
147 #if ENABLE_NLS
148 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
149 #endif
150
151 priv = visual_mem_new0 (MadspinPrivate, 1);
152 visual_object_set_private (VISUAL_OBJECT (plugin), priv);
153
154 priv->rcontext = visual_plugin_get_random_context (plugin);
155
156 priv->maxlines = 1;
157 priv->texsize = 0.25f;
158 priv->xrot = 0.0f;
159 priv->yrot = 0.0f;
160 priv->zrot = 0.0f;
161 priv->total = 0;
162 priv->frame = 0;
163
164 visual_param_container_add_many (paramcontainer, params);
165
166 /* The VisUI description that serves as config dialog */
167 table = visual_ui_table_new (2, 3);
168
169 label1 = visual_ui_label_new (_("Number of stars:"), FALSE);
170 label2 = visual_ui_label_new (_("Speed:"), FALSE);
171
172 slider1 = visual_ui_slider_new (FALSE);
173 visual_ui_widget_set_size_request (VISUAL_UI_WIDGET (slider1), 200, -1);
174 visual_ui_mutator_set_param (VISUAL_UI_MUTATOR (slider1), visual_param_container_get (paramcontainer, "num stars"));
175 visual_ui_range_set_properties (VISUAL_UI_RANGE (slider1), 50, 2500, 10, 0);
176
177 slider2 = visual_ui_slider_new (FALSE);
178 visual_ui_widget_set_size_request (VISUAL_UI_WIDGET (slider2), 200, -1);
179 visual_ui_mutator_set_param (VISUAL_UI_MUTATOR (slider2), visual_param_container_get (paramcontainer, "speed"));
180 visual_ui_range_set_properties (VISUAL_UI_RANGE (slider2), 200, 2000, 10, 0);
181
182 numeric1 = visual_ui_numeric_new ();
183 visual_ui_mutator_set_param (VISUAL_UI_MUTATOR (numeric1), visual_param_container_get (paramcontainer, "num stars"));
184 visual_ui_range_set_properties (VISUAL_UI_RANGE (numeric1), 50, 2500, 10, 0);
185
186 numeric2 = visual_ui_numeric_new ();
187 visual_ui_mutator_set_param (VISUAL_UI_MUTATOR (numeric2), visual_param_container_get (paramcontainer, "speed"));
188 visual_ui_range_set_properties (VISUAL_UI_RANGE (numeric2), 200, 2000, 10, 0);
189
190 visual_ui_table_attach (VISUAL_UI_TABLE (table), label1, 0, 0);
191 visual_ui_table_attach (VISUAL_UI_TABLE (table), slider1, 0, 1);
192 visual_ui_table_attach (VISUAL_UI_TABLE (table), numeric1, 0, 2);
193
194 visual_ui_table_attach (VISUAL_UI_TABLE (table), label2, 1, 0);
195 visual_ui_table_attach (VISUAL_UI_TABLE (table), slider2, 1, 1);
196 visual_ui_table_attach (VISUAL_UI_TABLE (table), numeric2, 1, 2);
197
198 visual_plugin_set_userinterface (plugin, table);
199
200 /* GL and the such */
201 glMatrixMode (GL_PROJECTION);
202 glLoadIdentity ();
203 glOrtho (-4.0, 4.0, -4.0, 4.0, -18.0, 18.0);
204 glMatrixMode (GL_MODELVIEW);
205 glLoadIdentity ();
206
207 glDisable (GL_DEPTH_TEST);
208
209 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
210 glShadeModel (GL_SMOOTH);
211 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
212 glClearDepth (1.0);
213 glBlendFunc (GL_SRC_ALPHA,GL_ONE);
214 glEnable (GL_BLEND);
215 glEnable (GL_TEXTURE_2D);
216 glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
217
218 madspin_load_textures (priv);
219
220 priv->initialized = TRUE;
221
222 return 0;
223 }
224
225 int lv_madspin_cleanup (VisPluginData *plugin)
226 {
227 MadspinPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
228 VisUIWidget *ui;
229
230 if (priv->initialized == TRUE)
231 glDeleteTextures (2, priv->texture);
232
233 /* Destroy the VisUI tree */
234 ui = visual_plugin_get_userinterface (plugin);
235 visual_object_unref (VISUAL_OBJECT (ui));
236
237 visual_mem_free (priv);
238
239 return 0;
240 }
241
242 int lv_madspin_requisition (VisPluginData *plugin, int *width, int *height)
243 {
244 int reqw, reqh;
245
246 reqw = *width;
247 reqh = *height;
248
249 if (reqw < 1)
250 reqw = 1;
251
252 if (reqh < 1)
253 reqh = 1;
254
255 *width = reqw;
256 *height = reqh;
257
258 return 0;
259 }
260
261 int lv_madspin_dimension (VisPluginData *plugin, VisVideo *video, int width, int height)
262 {
263 visual_video_set_dimension (video, width, height);
264
265 glViewport (0, 0, width, height);
266
267 return 0;
268 }
269
270 int lv_madspin_events (VisPluginData *plugin, VisEventQueue *events)
271 {
272 MadspinPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
273 VisEvent ev;
274 VisParamEntry *param;
275
276 while (visual_event_queue_poll (events, &ev)) {
277 switch (ev.type) {
278 case VISUAL_EVENT_RESIZE:
279 lv_madspin_dimension (plugin, ev.event.resize.video,
280 ev.event.resize.width, ev.event.resize.height);
281 break;
282
283 case VISUAL_EVENT_PARAM:
284 param = ev.event.param.param;
285
286 if (visual_param_entry_is (param, "num stars"))
287 priv->num_stars = visual_param_entry_get_integer (param);
288 else if (visual_param_entry_is (param, "speed"))
289 priv->speed = visual_param_entry_get_integer (param);
290
291 default: /* to avoid warnings */
292 break;
293 }
294 }
295
296 return 0;
297 }
298
299 VisPalette *lv_madspin_palette (VisPluginData *plugin)
300 {
301 return NULL;
302 }
303
304 int lv_madspin_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio)
305 {
306 MadspinPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
307
308 madspin_sound (priv, audio);
309 madspin_draw (priv, video);
310
311 return 0;
312 }
313
314 static int madspin_load_textures (MadspinPrivate *priv)
315 {
316 VisVideo *textureimage;
317
318 glGenTextures (2, &priv->texture[0]);
319
320 textureimage = visual_bitmap_load_new_video (STAR_DIR "star1.bmp");
321 glBindTexture (GL_TEXTURE_2D, priv->texture[0]);
322 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
323 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
324 glTexImage2D (GL_TEXTURE_2D, 0, 3, textureimage->width, textureimage->height, 0,
325 GL_RGB, GL_UNSIGNED_BYTE, visual_video_get_pixels (textureimage));
326
327 visual_object_unref (VISUAL_OBJECT (textureimage));
328
329 textureimage = visual_bitmap_load_new_video (STAR_DIR "star2.bmp");
330 glBindTexture (GL_TEXTURE_2D, priv->texture[1]);
331 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
332 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
333 glTexImage2D (GL_TEXTURE_2D, 0, 3, textureimage->width, textureimage->height, 0,
334 GL_RGB, GL_UNSIGNED_BYTE, visual_video_get_pixels (textureimage));
335
336 visual_object_unref (VISUAL_OBJECT (textureimage));
337
338 return 0;
339 }
340
341 static int madspin_sound (MadspinPrivate *priv, VisAudio *audio)
342 {
343 int i;
344 VisBuffer buffer;
345 VisBuffer pcmb;
346 float freq[256];
347 float pcm[256];
348
349 visual_buffer_set_data_pair (&buffer, freq, sizeof (freq));
350 visual_buffer_set_data_pair (&pcmb, pcm, sizeof (pcm));
351
352 visual_audio_get_sample_mixed_simple (audio, &pcmb, 2, VISUAL_AUDIO_CHANNEL_LEFT,
353 VISUAL_AUDIO_CHANNEL_RIGHT);
354
355 visual_audio_get_spectrum_for_sample (&buffer, &pcmb, TRUE);
356
357 /* Make our data from the freq data */
358 for (i = 0; i < 256; i++) {
359 priv->gdata[i] = (float) freq[i] * 2000.0;
360
361 if (priv->gdata[i] > 1.0)
362 priv->gdata[i] = 1.0;
363 if (i > 80)
364 priv->gdata[i] = priv->gdata[i] * (2 + (i /256));
365 }
366
367 return 0;
368 }
369
370 static int madspin_draw (MadspinPrivate *priv, VisVideo *video)
371 {
372 double csab, csapb;
373 double snab, snapb;
374 double sa, ca, s5;
375 double s1r, s1g, s1b, s1a;
376 double s2r, s2g, s2b, s2a;
377 float point;
378 int line;
379 int trail = 40;
380 float b;
381 float aa, a;
382 double x, y, z;
383 int i;
384 int ampl = 200;
385 float elapsed_time;
386
387 struct timeval tv_now;
388
389 gettimeofday (&priv->tv_past, NULL);
390
391 for (i = 1; i < 50; i++)
392 priv->total += priv->gdata[i];
393
394 if (priv->total > 2.5)
395 priv->total = 2.5;
396
397 priv->total /= 2.5f;
398
399 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
400 glBlendFunc (GL_SRC_ALPHA,GL_ONE);
401 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
402
403 for (line = priv->maxlines; line > 0; line--) {
404 for (point = 0; point <= priv->num_stars; point++) {
405 b = 1.5f + point / 33.33333f;
406 aa = trail - line * 1.0f;
407 a = (aa + priv->frame) / 33.33333f;
408
409 csab = cos (a / b);
410 csapb = cos (a + b);
411 snab = sin (a / b);
412 snapb = sin (a + b);
413
414 sa = sin (a * .1f);
415 ca = cos (a * .1f);
416 s5 = sin (a * .05f);
417
418 x = sa * snapb * ampl * csab + ca * (csapb * ampl * b * csab + s5 * (ampl * (csab + 2 *snapb)));
419 y = sa * csapb * ampl * csab + ca * (csapb * ampl * b * snab + s5 * (ampl * (csab + 2 *csapb)));
420 z = ca * csapb * ampl * csab + ca * (snapb * ampl * b * snab + s5 * (ampl * (snab + 2 *snapb)));
421
422 x /= 255.0f;
423 y /= 255.0f;
424 z /= 255.0f;
425
426 glPushMatrix ();
427
428 glTranslatef ((float) x, (float) y, (float) z);
429 glBindTexture (GL_TEXTURE_2D, priv->texture[0]);
430
431 s1r = ((point * 1.0f) / priv->num_stars);
432 s1g = (priv->num_stars - point) / (priv->num_stars * 1.0f);
433 s1b = ((point * 1.0f) / priv->num_stars) * 0.5f;
434 s1a = ((priv->gdata[(int) (point / priv->num_stars * 220)] + (priv->total / 200.0f)) / 4.0f);
435
436 s2r = sin (priv->frame / 400.0f);
437 s2g = cos (priv->frame / 200.0f);
438 s2b = cos (priv->frame / 300.0f);
439 s2a = (priv->gdata[(int) (point / priv->num_stars * 220)] / 2.0f );
440
441 if (s1a > 0.008f) {
442 glBegin (GL_TRIANGLE_STRIP);
443 glColor4f ((float) s1r, (float) s1g, (float) s1b, (float) s1a);
444 priv->texsize = (((priv->gdata[(int) (point / priv->num_stars * 220)]))
445 / (2048.01f - (point * 4.0f))) *
446 (((point - priv->num_stars) / (-priv->num_stars)) * 18.0f) + 0.15f;
447
448 /* Top Right */
449 glTexCoord2d (1, 1);
450 glVertex3f (priv->texsize, priv->texsize, (float) z);
451 /* Top Left */
452 glTexCoord2d (0, 1);
453 glVertex3f (-priv->texsize, priv->texsize, (float) z);
454 /* Bottom Right */
455 glTexCoord2d (1, 0);
456 glVertex3f (priv->texsize, -priv->texsize, (float) z);
457 /* Bottom Left */
458 glTexCoord2d (0, 0);
459 glVertex3f (-priv->texsize, -priv->texsize, (float) z);
460
461 glEnd ();
462 }
463
464 glBindTexture (GL_TEXTURE_2D, priv->texture[1]);
465 glRotatef (priv->frame + point, 0.0f, 0.0f, 1.0f);
466
467 if (s2a > 0.005f) {
468 glBegin (GL_TRIANGLE_STRIP);
469 glColor4f ((float) s2r, (float) s2g, (float) s2b, (float) s2a);
470 priv->texsize = (((priv->gdata[(int) (point / priv->num_stars * 220)]))
471 / (2048.01f - (point * 4.0f))) *
472 (((point - priv->num_stars) / (-priv->num_stars)) * 18.0f) + 0.35f;
473
474 priv->texsize *= ((visual_random_context_int(priv->rcontext) % 100) / 100.0f) * 2.0f;
475
476 /* Top Right */
477 glTexCoord2d (1, 1);
478 glVertex3f (priv->texsize, priv->texsize, (float) z);
479 /* Top Left */
480 glTexCoord2d (0, 1);
481 glVertex3f (-priv->texsize, priv->texsize, (float) z);
482 /* Bottom Right */
483 glTexCoord2d (1, 0);
484 glVertex3f (priv->texsize, -priv->texsize, (float) z);
485 /* Bottom Left */
486 glTexCoord2d (0, 0);
487 glVertex3f (-priv->texsize, -priv->texsize, (float) z);
488
489 glEnd ();
490 }
491
492 /* Move back to main position */
493 glPopMatrix ();
494 }
495 }
496
497 glLoadIdentity ();
498
499 gettimeofday (&tv_now, NULL);
500
501 elapsed_time = ((float) tv_now.tv_usec - (float) priv->tv_past.tv_usec) / 1000000;
502 if (elapsed_time < 0)
503 elapsed_time = 0;
504
505 priv->frame += elapsed_time * priv->speed;
506
507 return 0;
508 }
509