Debian Codegraph @

plugins/actor/lv_gltest/actor_lv_gltest.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 * Peter Alm, Mikael Alm, Olle Hallnas, Thomas Nilsson and
7 * 4Front Technologies
8 *
9 * $Id: actor_lv_gltest.c,v 1.27 2006/03/02 23:50:06 synap Exp $
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as
13 * published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <math.h>
34 #include <gettext.h>
35
36 #ifdef HAVE_GL_GL_H
37 # include <GL/gl.h>
38 #elif defined(HAVE_OPENGL_GL_H)
39 # include <OpenGL/gl.h>
40 #else
41 # error neither GL/gl.h nor OpenGL/gl.h available
42 #endif
43
44 #if defined(HAVE_GL_GLU_H)
45 # include <GL/glu.h>
46 #elif defined(HAVE_OPENGL_GLU_H)
47 # include <OpenGL/glu.h>
48 #else
49 # error neither GL/glu.h nor OpenGL/glu.h available
50 #endif
51
52 #include <libvisual/libvisual.h>
53
54 #define BARS 16
55
56 static int xranges[] = {0, 1, 2, 3, 5, 7, 10, 14, 20, 28, 40, 54, 74, 101, 137, 187, 255};
57
58 typedef struct {
59 GLfloat y_angle;
60 GLfloat y_initial_angle;
61 GLfloat y_speed; // degrees per second
62 GLfloat x_angle;
63 GLfloat x_initial_angle;
64 GLfloat x_speed; // degrees per second
65 GLfloat z_angle;
66 GLfloat z_initial_angle;
67 GLfloat z_speed; // degrees per second
68 GLfloat heights[16][16];
69
70 int transparent;
71
72 VisTimer * started_at;
73 } GLtestPrivate;
74
75 int lv_gltest_init (VisPluginData *plugin);
76 int lv_gltest_cleanup (VisPluginData *plugin);
77 int lv_gltest_requisition (VisPluginData *plugin, int *width, int *height);
78 int lv_gltest_dimension (VisPluginData *plugin, VisVideo *video, int width, int height);
79 int lv_gltest_events (VisPluginData *plugin, VisEventQueue *events);
80 VisPalette *lv_gltest_palette (VisPluginData *plugin);
81 int lv_gltest_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio);
82
83 static void draw_bars (GLtestPrivate *priv);
84 static void draw_rectangle (GLtestPrivate *priv, GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2);
85 static void draw_bar (GLtestPrivate *priv, GLfloat x_offset, GLfloat z_offset, GLfloat height, GLfloat red, GLfloat green, GLfloat blue);
86 static void draw_bars (GLtestPrivate *priv);
87
88 VISUAL_PLUGIN_API_VERSION_VALIDATOR
89
90 /* Main plugin stuff */
91 const VisPluginInfo *get_plugin_info (int *count)
92 {
93 static VisActorPlugin actor[] = {{
94 .requisition = lv_gltest_requisition,
95 .palette = lv_gltest_palette,
96 .render = lv_gltest_render,
97 .vidoptions.depth = VISUAL_VIDEO_DEPTH_GL
98 }};
99
100 static VisPluginInfo info[] = {{
101 .type = VISUAL_PLUGIN_TYPE_ACTOR,
102
103 .plugname = "lv_gltest",
104 .name = "libvisual GL analyser",
105 .author = N_("Original by: Peter Alm, Mikael Alm, Olle Hallnas, Thomas Nilsson and 4Front Technologies, Port by: Dennis Smit <[email protected]>"),
106 .version = "0.1",
107 .about = N_("Libvisual GL analyzer plugin"),
108 .help = N_("This plugin shows an openGL bar analyzer like the xmms one"),
109 .license = VISUAL_PLUGIN_LICENSE_LGPL,
110
111 .init = lv_gltest_init,
112 .cleanup = lv_gltest_cleanup,
113 .events = lv_gltest_events,
114
115 .plugin = VISUAL_OBJECT (&actor[0])
116 }};
117
118 *count = sizeof (info) / sizeof (*info);
119
120 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RED_SIZE, 5);
121 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_GREEN_SIZE, 5);
122 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_BLUE_SIZE, 5);
123 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DEPTH_SIZE, 16);
124 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_DOUBLEBUFFER, 1);
125 VISUAL_VIDEO_ATTRIBUTE_OPTIONS_GL_ENTRY(actor[0].vidoptions, VISUAL_GL_ATTRIBUTE_RGBA, 1);
126
127 return info;
128 }
129
130 int lv_gltest_init (VisPluginData *plugin)
131 {
132 GLtestPrivate *priv;
133 VisParamContainer *paramcontainer = visual_plugin_get_params (plugin);
134
135 static VisParamEntry params[] = {
136 VISUAL_PARAM_LIST_ENTRY_INTEGER ("transparent bars", TRUE),
137 VISUAL_PARAM_LIST_END
138 };
139
140 int x, y;
141
142 /* UI Vars */
143 VisUIWidget *checkbox;
144
145 #if ENABLE_NLS
146 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
147 #endif
148
149 priv = visual_mem_new0 (GLtestPrivate, 1);
150 visual_object_set_private (VISUAL_OBJECT (plugin), priv);
151
152 visual_param_container_add_many (paramcontainer, params);
153
154 checkbox = visual_ui_checkbox_new (_("Transparant bars"), TRUE);
155 visual_ui_mutator_set_param (VISUAL_UI_MUTATOR (checkbox), visual_param_container_get (paramcontainer, "transparent bars"));
156
157 visual_plugin_set_userinterface (plugin, checkbox);
158
159 /* GL setting up the rest! */
160 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
161
162 glMatrixMode (GL_PROJECTION);
163
164 glLoadIdentity ();
165
166 glFrustum (-1, 1, -1, 1, 1.5, 10);
167
168 glMatrixMode (GL_MODELVIEW);
169 glLoadIdentity ();
170
171 glEnable (GL_DEPTH_TEST);
172 glDepthFunc (GL_LESS);
173
174 glBlendFunc (GL_SRC_ALPHA,GL_ONE);
175
176 for (x = 0; x < 16; x++) {
177 for (y = 0; y < 16; y++) {
178 priv->heights[y][x] = 0.0;
179 }
180 }
181
182 priv->x_speed = 0.0f;
183 priv->y_speed = 360.0f / 13.0f;
184 priv->z_speed = 0.0f;
185 priv->x_initial_angle = 20.0f;
186 priv->y_initial_angle = 45.0f;
187 priv->z_initial_angle = 0.0f;
188
189 priv->started_at = visual_timer_new();
190 visual_log_return_val_if_fail( priv->started_at != NULL, -1 );
191
192 visual_timer_start(priv->started_at);
193
194 return 0;
195 }
196
197 int lv_gltest_cleanup (VisPluginData *plugin)
198 {
199 GLtestPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
200 VisUIWidget *ui;
201
202 /* Destroy the VisUI tree */
203 ui = visual_plugin_get_userinterface (plugin);
204 visual_object_unref (VISUAL_OBJECT (ui));
205
206 if (priv) {
207 visual_object_unref (VISUAL_OBJECT (priv->started_at));
208 }
209
210 visual_mem_free (priv);
211
212 return 0;
213 }
214
215 int lv_gltest_requisition (VisPluginData *plugin, int *width, int *height)
216 {
217 int reqw, reqh;
218
219 reqw = *width;
220 reqh = *height;
221
222 if (reqw < 1)
223 reqw = 1;
224
225 if (reqh < 1)
226 reqh = 1;
227
228 *width = reqw;
229 *height = reqh;
230
231 return 0;
232 }
233
234 int lv_gltest_dimension (VisPluginData *plugin, VisVideo *video, int width, int height)
235 {
236 GLfloat ratio;
237
238 visual_video_set_dimension (video, width, height);
239
240 ratio = (GLfloat) width / (GLfloat) height;
241
242 glViewport (0, 0, (GLsizei) width, (GLsizei) height);
243 glMatrixMode (GL_PROJECTION);
244 glLoadIdentity ();
245
246 gluPerspective (45.0, ratio, 0.1, 100.0);
247
248 glMatrixMode (GL_MODELVIEW);
249 glLoadIdentity ();
250
251 return 0;
252 }
253
254 int lv_gltest_events (VisPluginData *plugin, VisEventQueue *events)
255 {
256 GLtestPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
257 VisEvent ev;
258 VisParamEntry *param;
259
260 while (visual_event_queue_poll (events, &ev)) {
261 switch (ev.type) {
262 case VISUAL_EVENT_RESIZE:
263 lv_gltest_dimension (plugin, ev.event.resize.video,
264 ev.event.resize.width, ev.event.resize.height);
265 break;
266
267 case VISUAL_EVENT_PARAM:
268 param = ev.event.param.param;
269
270 if (visual_param_entry_is (param, "transparent bars")) {
271 priv->transparent = visual_param_entry_get_integer (param);
272
273 if (priv->transparent == FALSE)
274 glDisable (GL_BLEND);
275 else
276 glEnable (GL_BLEND);
277 }
278
279 default: /* to avoid warnings */
280 break;
281 }
282 }
283
284 return 0;
285 }
286
287 VisPalette *lv_gltest_palette (VisPluginData *plugin)
288 {
289 return NULL;
290 }
291
292 int lv_gltest_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio)
293 {
294 GLtestPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
295 VisBuffer buffer;
296 VisBuffer pcmb;
297 float freq[256];
298 float pcm[256];
299 int i,c;
300 int y;
301 float ff;
302 GLfloat val;
303
304 visual_buffer_set_data_pair (&buffer, freq, sizeof (freq));
305 visual_buffer_set_data_pair (&pcmb, pcm, sizeof (pcm));
306
307 visual_audio_get_sample_mixed_simple (audio, &pcmb, 2,
308 VISUAL_AUDIO_CHANNEL_LEFT,
309 VISUAL_AUDIO_CHANNEL_RIGHT);
310
311 visual_audio_get_spectrum_for_sample (&buffer, &pcmb, TRUE);
312
313 for (y = BARS - 1; y > 0; y--)
314 {
315 for(i = 0; i < BARS; i++)
316 {
317 priv->heights[y][i] = priv->heights[y - 1][i];
318 }
319 }
320
321 for (i = 0; i < BARS; i++)
322 {
323 ff = 0;
324 for (c = xranges[i]; c < xranges[i + 1]; c++)
325 {
326 if (freq[c] > ff)
327 ff = freq[c];
328 }
329
330 priv->heights[0][i] = ff * 10;
331 }
332
333 const float seconds_elapsed = visual_timer_elapsed_msecs(priv->started_at) / 1000.0f;
334
335 priv->x_angle = fmodf(priv->x_initial_angle + priv->x_speed * seconds_elapsed, 360.0);
336 priv->y_angle = fmodf(priv->y_initial_angle + priv->y_speed * seconds_elapsed, 360.0);
337 priv->z_angle = fmodf(priv->z_initial_angle + priv->z_speed * seconds_elapsed, 360.0);
338
339 draw_bars (priv);
340
341 return 0;
342 }
343
344 /* Drawing stuff */
345 static void draw_rectangle (GLtestPrivate *priv, GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2)
346 {
347 if (y1 == y2) {
348
349 glVertex3f (x1, y1, z1);
350 glVertex3f (x2, y1, z1);
351 glVertex3f (x2, y2, z2);
352
353 glVertex3f (x2, y2, z2);
354 glVertex3f (x1, y2, z2);
355 glVertex3f (x1, y1, z1);
356 } else {
357 glVertex3f (x1, y1, z1);
358 glVertex3f (x2, y1, z2);
359 glVertex3f (x2, y2, z2);
360
361 glVertex3f (x2, y2, z2);
362 glVertex3f (x1, y2, z1);
363 glVertex3f (x1, y1, z1);
364 }
365 }
366
367 static void draw_bar (GLtestPrivate *priv, GLfloat x_offset, GLfloat z_offset, GLfloat height, GLfloat red, GLfloat green, GLfloat blue)
368 {
369 GLfloat width = 0.1;
370
371 glColor3f (red,green,blue);
372 draw_rectangle (priv, x_offset, height, z_offset, x_offset + width, height, z_offset + 0.1);
373 draw_rectangle (priv, x_offset, 0, z_offset, x_offset + width, 0, z_offset + 0.1);
374
375 glColor3f (0.5 * red, 0.5 * green, 0.5 * blue);
376 draw_rectangle (priv, x_offset, 0.0, z_offset + 0.1, x_offset + width, height, z_offset + 0.1);
377 draw_rectangle (priv, x_offset, 0.0, z_offset, x_offset + width, height, z_offset );
378
379 glColor3f (0.25 * red, 0.25 * green, 0.25 * blue);
380 draw_rectangle (priv, x_offset, 0.0, z_offset , x_offset, height, z_offset + 0.1);
381 draw_rectangle (priv, x_offset + width, 0.0, z_offset , x_offset + width, height, z_offset + 0.1);
382 }
383
384 static void draw_bars (GLtestPrivate *priv)
385 {
386 int x,y;
387 GLfloat x_offset, z_offset, r_base, b_base;
388
389 glClearColor (0,0,0,0);
390 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
391
392 glPushMatrix ();
393 glTranslatef (0.0,-0.5,-5.0);
394 glRotatef (priv->x_angle,1.0,0.0,0.0);
395 glRotatef (priv->y_angle,0.0,1.0,0.0);
396 glRotatef (priv->z_angle,0.0,0.0,1.0);
397
398 glBegin (GL_TRIANGLES);
399 for (y = 0; y < 16; y++)
400 {
401 z_offset = -1.6 + ((15 - y) * 0.2);
402
403 b_base = y * (1.0 / 15);
404 r_base = 1.0 - b_base;
405
406 for(x = 0; x < 16; x++)
407 {
408 x_offset = -1.6 + (x * 0.2);
409 draw_bar (priv, x_offset, z_offset, priv->heights[y][x] * 0.2, r_base - (x * (r_base / 15.0)), x * (1.0 / 15), b_base);
410 }
411 }
412 glEnd ();
413
414 glPopMatrix ();
415 }
416