1 /*****************************************************************************
2 * opengl.c: OpenGL video output
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@videolan.org>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Eric Petit <titer@m0k.org>
10 * Cedric Cocquebert <cedric.cocquebert@supelec.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
37 #include <vlc_plugin.h>
41 #include <OpenGL/gl.h>
42 #include <OpenGL/glext.h>
44 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
45 This allows sizes which are not powers of 2 */
46 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
48 /* OS X OpenGL supports YUV. Hehe. */
49 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
50 #define VLCGL_TYPE GL_UNSIGNED_SHORT_8_8_APPLE
54 #define VLCGL_TARGET GL_TEXTURE_2D
57 #ifndef GL_UNSIGNED_SHORT_5_6_5
58 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
60 //#define VLCGL_RGB_FORMAT GL_RGB
61 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
64 //#define VLCGL_RGB_FORMAT GL_RGB
65 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
68 #define VLCGL_RGB_FORMAT GL_RGBA
69 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
73 #define YCBCR_MESA 0x8757
75 #ifndef UNSIGNED_SHORT_8_8_MESA
76 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
78 #define VLCGL_YUV_FORMAT YCBCR_MESA
79 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
81 /* Use RGB on Win32/GLX */
82 #define VLCGL_FORMAT VLCGL_RGB_FORMAT
83 #define VLCGL_TYPE VLCGL_RGB_TYPE
84 //#define VLCGL_FORMAT VLCGL_YUV_FORMAT
85 //#define VLCGL_TYPE VLCGL_YUV_TYPE
88 #ifndef GL_CLAMP_TO_EDGE
89 # define GL_CLAMP_TO_EDGE 0x812F
93 #define OPENGL_EFFECT_NONE 1
94 #define OPENGL_EFFECT_CUBE 2
95 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
96 #if defined( HAVE_GL_GLU_H ) || defined( __APPLE__ )
97 #define OPENGL_MORE_EFFECT 8
100 #ifdef OPENGL_MORE_EFFECT
103 #include <OpenGL/glu.h>
111 /*GRID2D TRANSFORMATION */
118 #define INIFILE 4096 // not used, just for mark end ...
119 #define SIGN(x) (x < 0 ? (-1) : 1)
120 #define PID2 1.570796326794896619231322
122 static const char *ppsz_effects[] = {
123 "none", "cube", "transparent-cube", "cylinder", "torus", "sphere","SQUAREXY","SQUARER", "ASINXY", "ASINR", "SINEXY", "SINER" };
124 static const char *ppsz_effects_text[] = {
125 N_("None"), N_("Cube"), N_("Transparent Cube"),
126 N_("Cylinder"), N_("Torus"), N_("Sphere"), N_("SQUAREXY"),N_("SQUARER"), N_("ASINXY"), N_("ASINR"), N_("SINEXY"), N_("SINER") };
129 /*****************************************************************************
131 *****************************************************************************/
132 static int CreateVout ( vlc_object_t * );
133 static void DestroyVout ( vlc_object_t * );
134 static int Init ( vout_thread_t * );
135 static void End ( vout_thread_t * );
136 static int Manage ( vout_thread_t * );
137 static void Render ( vout_thread_t *, picture_t * );
138 static void DisplayVideo ( vout_thread_t *, picture_t * );
139 static int Control ( vout_thread_t *, int, va_list );
141 static inline int GetAlignedSize( int );
143 static int InitTextures ( vout_thread_t * );
144 static int SendEvents ( vlc_object_t *, char const *,
145 vlc_value_t, vlc_value_t, void * );
147 #ifdef OPENGL_MORE_EFFECT
148 static float Z_Compute ( float, int, float, float );
149 static void Transform ( int, float, float, int, int, int, int, double *, double * );
151 /*****************************************************************************
153 *****************************************************************************/
154 #define ACCURACY_TEXT N_( "OpenGL sampling accuracy " )
155 #define ACCURACY_LONGTEXT N_( "Select the accuracy of 3D object sampling(1 = min and 10 = max)" )
156 #define RADIUS_TEXT N_( "OpenGL Cylinder radius" )
157 #define RADIUS_LONGTEXT N_( "Radius of the OpenGL cylinder effect, if enabled" )
158 #define POV_X_TEXT N_("Point of view x-coordinate")
159 #define POV_X_LONGTEXT N_("Point of view (X coordinate) of the cube/cylinder "\
160 "effect, if enabled.")
161 #define POV_Y_TEXT N_("Point of view y-coordinate")
162 #define POV_Y_LONGTEXT N_("Point of view (Y coordinate) of the cube/cylinder "\
163 "effect, if enabled.")
164 #define POV_Z_TEXT N_("Point of view z-coordinate")
165 #define POV_Z_LONGTEXT N_("Point of view (Z coordinate) of the cube/cylinder "\
166 "effect, if enabled.")
168 #define PROVIDER_TEXT N_("OpenGL Provider")
169 #define PROVIDER_LONGTEXT N_("Allows you to modify what OpenGL provider should be used")
170 #define SPEED_TEXT N_( "OpenGL cube rotation speed" )
171 #define SPEED_LONGTEXT N_( "Rotation speed of the OpenGL cube effect, if " \
173 #define EFFECT_TEXT N_("Effect")
174 #define EFFECT_LONGTEXT N_( \
175 "Several visual OpenGL effects are available." )
177 #ifndef OPENGL_MORE_EFFECT
178 static const char *ppsz_effects[] = {
179 "none", "cube", "transparent-cube" };
180 static const char *ppsz_effects_text[] = {
181 N_("None"), N_("Cube"), N_("Transparent Cube") };
185 set_shortname( "OpenGL" );
186 set_category( CAT_VIDEO );
187 set_subcategory( SUBCAT_VIDEO_VOUT );
188 set_description( N_("OpenGL video output") );
190 set_capability( "video output", 200 );
192 set_capability( "video output", 20 );
194 add_shortcut( "opengl" );
195 add_float( "opengl-cube-speed", 2.0, NULL, SPEED_TEXT,
196 SPEED_LONGTEXT, true );
197 #ifdef OPENGL_MORE_EFFECT
198 add_integer_with_range( "opengl-accuracy", 4, 1, 10, NULL, ACCURACY_TEXT,
199 ACCURACY_LONGTEXT, true );
200 add_float_with_range( "opengl-pov-x", 0.0, -1.0, 1.0, NULL, POV_X_TEXT,
201 POV_X_LONGTEXT, true );
202 add_float_with_range( "opengl-pov-y", 0.0, -1.0, 1.0, NULL, POV_Y_TEXT,
203 POV_Y_LONGTEXT, true );
204 add_float_with_range( "opengl-pov-z", -1.0, -1.0, 1.0, NULL, POV_Z_TEXT,
205 POV_Z_LONGTEXT, true );
206 add_float( "opengl-cylinder-radius", -100.0, NULL, RADIUS_TEXT,
207 RADIUS_LONGTEXT, true );
210 /* Allow opengl provider plugin selection */
211 add_string( "opengl-provider", "default", NULL, PROVIDER_TEXT,
212 PROVIDER_LONGTEXT, true );
213 set_callbacks( CreateVout, DestroyVout );
214 add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
215 EFFECT_LONGTEXT, false );
216 change_string_list( ppsz_effects, ppsz_effects_text, 0 );
219 /*****************************************************************************
220 * vout_sys_t: video output method descriptor
221 *****************************************************************************
222 * This structure is part of the video output thread descriptor.
223 * It describes the OpenGL specific properties of the output thread.
224 *****************************************************************************/
227 vout_thread_t *p_vout;
229 uint8_t *pp_buffer[2];
233 GLuint p_textures[2];
241 /*****************************************************************************
242 * CreateVout: This function allocates and initializes the OpenGL vout method.
243 *****************************************************************************/
244 static int CreateVout( vlc_object_t *p_this )
246 vout_thread_t *p_vout = (vout_thread_t *)p_this;
250 /* Allocate structure */
251 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
254 msg_Err( p_vout, "out of memory" );
258 var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
262 p_sys->i_tex_width = p_vout->fmt_in.i_width;
263 p_sys->i_tex_height = p_vout->fmt_in.i_height;
265 /* A texture must have a size aligned on a power of 2 */
266 p_sys->i_tex_width = GetAlignedSize( p_vout->fmt_in.i_width );
267 p_sys->i_tex_height = GetAlignedSize( p_vout->fmt_in.i_height );
270 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
271 p_sys->i_tex_height );
275 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
276 if( p_sys->p_vout == NULL )
278 msg_Err( p_vout, "out of memory" );
281 vlc_object_attach( p_sys->p_vout, p_this );
283 p_sys->p_vout->i_window_width = p_vout->i_window_width;
284 p_sys->p_vout->i_window_height = p_vout->i_window_height;
285 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
286 p_sys->p_vout->render.i_width = p_vout->render.i_width;
287 p_sys->p_vout->render.i_height = p_vout->render.i_height;
288 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
289 p_sys->p_vout->fmt_render = p_vout->fmt_render;
290 p_sys->p_vout->fmt_in = p_vout->fmt_in;
291 p_sys->p_vout->b_scale = p_vout->b_scale;
292 p_sys->p_vout->i_alignment = p_vout->i_alignment;
294 psz = config_GetPsz( p_vout, "opengl-provider" );
296 msg_Dbg( p_vout, "requesting \"%s\" opengl provider",
297 psz ? psz : "default" );
299 p_sys->p_vout->p_module =
300 module_Need( p_sys->p_vout, "opengl provider", psz, 0 );
302 if( p_sys->p_vout->p_module == NULL )
304 msg_Warn( p_vout, "No OpenGL provider found" );
305 vlc_object_detach( p_sys->p_vout );
306 vlc_object_release( p_sys->p_vout );
310 p_sys->f_speed = var_CreateGetFloat( p_vout, "opengl-cube-speed" );
311 p_sys->f_radius = var_CreateGetFloat( p_vout, "opengl-cylinder-radius" );
313 p_vout->pf_init = Init;
314 p_vout->pf_end = End;
315 p_vout->pf_manage = Manage;
316 p_vout->pf_render = Render;
317 p_vout->pf_display = DisplayVideo;
318 p_vout->pf_control = Control;
320 /* Forward events from the opengl provider */
321 var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
322 var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
323 var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
324 var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_INTEGER );
325 var_Create( p_sys->p_vout, "mouse-button-down", VLC_VAR_INTEGER );
326 var_Create( p_sys->p_vout, "video-on-top",
327 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
329 var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
330 var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
331 var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
332 var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
333 var_AddCallback( p_sys->p_vout, "mouse-button-down", SendEvents, p_vout );
338 /*****************************************************************************
339 * Init: initialize the OpenGL video thread output method
340 *****************************************************************************/
341 static int Init( vout_thread_t *p_vout )
343 vout_sys_t *p_sys = p_vout->p_sys;
347 p_sys->p_vout->pf_init( p_sys->p_vout );
349 /* TODO: We use YCbCr on Mac which is Y422, but on OSX it seems to == YUY2. Verify */
350 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
351 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
354 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
355 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
358 #elif VLCGL_FORMAT == GL_RGB
359 # if VLCGL_TYPE == GL_UNSIGNED_BYTE
360 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
361 # if defined( WORDS_BIGENDIAN )
362 p_vout->output.i_rmask = 0x00ff0000;
363 p_vout->output.i_gmask = 0x0000ff00;
364 p_vout->output.i_bmask = 0x000000ff;
366 p_vout->output.i_rmask = 0x000000ff;
367 p_vout->output.i_gmask = 0x0000ff00;
368 p_vout->output.i_bmask = 0x00ff0000;
372 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
373 # if defined( WORDS_BIGENDIAN )
374 p_vout->output.i_rmask = 0x001f;
375 p_vout->output.i_gmask = 0x07e0;
376 p_vout->output.i_bmask = 0xf800;
378 p_vout->output.i_rmask = 0xf800;
379 p_vout->output.i_gmask = 0x07e0;
380 p_vout->output.i_bmask = 0x001f;
385 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
386 # if defined( WORDS_BIGENDIAN )
387 p_vout->output.i_rmask = 0xff000000;
388 p_vout->output.i_gmask = 0x00ff0000;
389 p_vout->output.i_bmask = 0x0000ff00;
391 p_vout->output.i_rmask = 0x000000ff;
392 p_vout->output.i_gmask = 0x0000ff00;
393 p_vout->output.i_bmask = 0x00ff0000;
398 /* Since OpenGL can do rescaling for us, stick to the default
399 * coordinates and aspect. */
400 p_vout->output.i_width = p_vout->render.i_width;
401 p_vout->output.i_height = p_vout->render.i_height;
402 p_vout->output.i_aspect = p_vout->render.i_aspect;
404 p_vout->fmt_out = p_vout->fmt_in;
405 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
407 /* We know the chroma, allocate one buffer which will be used
408 * directly by the decoder */
409 p_sys->pp_buffer[0] =
410 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
411 if( !p_sys->pp_buffer[0] )
413 msg_Err( p_vout, "out of memory" );
416 p_sys->pp_buffer[1] =
417 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
418 if( !p_sys->pp_buffer[1] )
420 msg_Err( p_vout, "out of memory" );
424 p_vout->p_picture[0].i_planes = 1;
425 p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
426 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
427 p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
428 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
429 p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
430 p_vout->p_picture[0].p->i_pixel_pitch;
431 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
432 p_vout->p_picture[0].p->i_pixel_pitch;
434 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
435 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
437 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
439 I_OUTPUTPICTURES = 1;
441 if( p_sys->p_vout->pf_lock &&
442 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
444 msg_Warn( p_vout, "could not lock OpenGL provider" );
448 InitTextures( p_vout );
451 glDisable(GL_DEPTH_TEST);
452 glDepthMask(GL_FALSE);
453 glDisable(GL_CULL_FACE);
454 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
455 glClear( GL_COLOR_BUFFER_BIT );
457 /* Check if the user asked for useless visual effects */
458 var_Get( p_vout, "opengl-effect", &val );
459 if( !val.psz_string || !strcmp( val.psz_string, "none" ))
461 p_sys->i_effect = OPENGL_EFFECT_NONE;
463 else if( !strcmp( val.psz_string, "cube" ) )
465 p_sys->i_effect = OPENGL_EFFECT_CUBE;
467 glEnable( GL_CULL_FACE);
469 else if( !strcmp( val.psz_string, "transparent-cube" ) )
471 p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
473 glDisable( GL_DEPTH_TEST );
474 glEnable( GL_BLEND );
475 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
479 #ifdef OPENGL_MORE_EFFECT
481 while (( strcmp( val.psz_string, ppsz_effects[p_sys->i_effect]) ) && (pow(2,p_sys->i_effect) < INIFILE))
485 if (pow(2,p_sys->i_effect) < INIFILE)
486 p_sys->i_effect = pow(2,p_sys->i_effect);
487 else if ( strcmp( val.psz_string, ppsz_effects[p_sys->i_effect]))
489 msg_Warn( p_vout, "no valid opengl effect provided, using "
491 p_sys->i_effect = OPENGL_EFFECT_NONE;
494 msg_Warn( p_vout, "no valid opengl effect provided, using "
496 p_sys->i_effect = OPENGL_EFFECT_NONE;
499 free( val.psz_string );
501 if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
502 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
504 /* Set the perpective */
505 glMatrixMode( GL_PROJECTION );
507 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
508 glMatrixMode( GL_MODELVIEW );
510 glTranslatef( 0.0, 0.0, - 5.0 );
512 #ifdef OPENGL_MORE_EFFECT
515 /* Set the perpective */
516 glMatrixMode( GL_PROJECTION );
518 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
519 glMatrixMode( GL_MODELVIEW );
521 glTranslatef( 0.0, 0.0, -3.0 );
523 float f_pov_x, f_pov_y, f_pov_z;
524 f_pov_x = var_CreateGetFloat( p_vout, "opengl-pov-x");
525 f_pov_y = var_CreateGetFloat( p_vout, "opengl-pov-y");
526 f_pov_z = var_CreateGetFloat( p_vout, "opengl-pov-z");
527 gluLookAt(0, 0, 0, f_pov_x, f_pov_y, f_pov_z, 0, 1, 0);
530 if( p_sys->p_vout->pf_unlock )
532 p_sys->p_vout->pf_unlock( p_sys->p_vout );
538 /*****************************************************************************
539 * End: terminate GLX video thread output method
540 *****************************************************************************/
541 static void End( vout_thread_t *p_vout )
543 vout_sys_t *p_sys = p_vout->p_sys;
545 if( p_sys->p_vout->pf_lock &&
546 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
548 msg_Warn( p_vout, "could not lock OpenGL provider" );
555 /* Free the texture buffer*/
556 glDeleteTextures( 2, p_sys->p_textures );
557 free( p_sys->pp_buffer[0] );
558 free( p_sys->pp_buffer[1] );
560 if( p_sys->p_vout->pf_unlock )
562 p_sys->p_vout->pf_unlock( p_sys->p_vout );
566 /*****************************************************************************
567 * Destroy: destroy GLX video thread output method
568 *****************************************************************************
569 * Terminate an output method created by CreateVout
570 *****************************************************************************/
571 static void DestroyVout( vlc_object_t *p_this )
573 vout_thread_t *p_vout = (vout_thread_t *)p_this;
574 vout_sys_t *p_sys = p_vout->p_sys;
576 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
577 vlc_object_detach( p_sys->p_vout );
578 vlc_object_release( p_sys->p_vout );
583 /*****************************************************************************
584 * Manage: handle Sys events
585 *****************************************************************************
586 * This function should be called regularly by video output thread. It returns
587 * a non null value if an error occurred.
588 *****************************************************************************/
589 static int Manage( vout_thread_t *p_vout )
591 vout_sys_t *p_sys = p_vout->p_sys;
592 int i_ret, i_fullscreen_change;
594 i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
596 p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
597 p_vout->fmt_in.i_x_offset;
598 p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
599 p_vout->fmt_in.i_y_offset;
600 p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
601 p_vout->fmt_in.i_visible_width;
602 p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
603 p_vout->fmt_in.i_visible_height;
604 p_vout->fmt_out.i_aspect = p_sys->p_vout->fmt_in.i_aspect =
605 p_vout->fmt_in.i_aspect;
606 p_vout->fmt_out.i_sar_num = p_sys->p_vout->fmt_in.i_sar_num =
607 p_vout->fmt_in.i_sar_num;
608 p_vout->fmt_out.i_sar_den = p_sys->p_vout->fmt_in.i_sar_den =
609 p_vout->fmt_in.i_sar_den;
610 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
612 p_sys->p_vout->i_changes = p_vout->i_changes;
613 i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
614 p_vout->i_changes = p_sys->p_vout->i_changes;
617 if( p_sys->p_vout->pf_lock &&
618 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
620 msg_Warn( p_vout, "could not lock OpenGL provider" );
624 /* On OS X, we create the window and the GL view when entering
625 fullscreen - the textures have to be inited again */
626 if( i_fullscreen_change )
628 InitTextures( p_vout );
630 switch( p_sys->i_effect )
632 case OPENGL_EFFECT_CUBE:
633 #ifdef OPENGL_MORE_EFFECT
644 glEnable( GL_CULL_FACE );
647 case OPENGL_EFFECT_TRANSPARENT_CUBE:
648 glDisable( GL_DEPTH_TEST );
649 glEnable( GL_BLEND );
650 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
654 if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
655 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
657 /* Set the perpective */
658 glMatrixMode( GL_PROJECTION );
660 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
661 glMatrixMode( GL_MODELVIEW );
663 glTranslatef( 0.0, 0.0, - 5.0 );
665 #ifdef OPENGL_MORE_EFFECT
668 glMatrixMode( GL_PROJECTION );
670 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
671 glMatrixMode( GL_MODELVIEW );
673 glTranslatef( 0.0, 0.0, -3.0 );
675 float f_pov_x, f_pov_y, f_pov_z;
676 f_pov_x = var_CreateGetFloat( p_vout, "opengl-pov-x");
677 f_pov_y = var_CreateGetFloat( p_vout, "opengl-pov-y");
678 f_pov_z = var_CreateGetFloat( p_vout, "opengl-pov-z");
679 gluLookAt(0, 0, 0, f_pov_x, f_pov_y, f_pov_z, 0, 1, 0);
684 if( p_sys->p_vout->pf_unlock )
686 p_sys->p_vout->pf_unlock( p_sys->p_vout );
689 // to align in real time in OPENGL
690 if (p_sys->p_vout->i_alignment != p_vout->i_alignment)
692 p_vout->i_changes = VOUT_CROP_CHANGE; //to force change
693 p_sys->p_vout->i_alignment = p_vout->i_alignment;
698 /*****************************************************************************
699 * Render: render previously calculated output
700 *****************************************************************************/
701 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
704 vout_sys_t *p_sys = p_vout->p_sys;
706 /* On Win32/GLX, we do this the usual way:
707 + Fill the buffer with new content,
708 + Reload the texture,
711 On OS X with VRAM or AGP texturing, the order has to be:
712 + Reload the texture,
713 + Fill the buffer with new content,
716 (Thanks to gcc from the Arstechnica forums for the tip)
718 Therefore, we have to use two buffers and textures. On Win32/GLX,
719 we reload the texture to be displayed and use it right away. On
720 OS X, we first render, then reload the texture to be used next
723 if( p_sys->p_vout->pf_lock &&
724 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
726 msg_Warn( p_vout, "could not lock OpenGL provider" );
732 i_new_index = ( p_sys->i_index + 1 ) & 1;
735 /* Update the texture */
736 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
737 glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
738 p_vout->fmt_out.i_width,
739 p_vout->fmt_out.i_height,
740 VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
742 /* Bind to the previous texture for drawing */
743 glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
746 p_sys->i_index = i_new_index;
747 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
750 /* Update the texture */
751 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
752 p_vout->fmt_out.i_width,
753 p_vout->fmt_out.i_height,
754 VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[0] );
757 if( p_sys->p_vout->pf_unlock )
759 p_sys->p_vout->pf_unlock( p_sys->p_vout );
764 #ifdef OPENGL_MORE_EFFECT
765 /*****************************************************************************
766 * Transform: Calculate the distorted grid coordinates
767 *****************************************************************************/
768 static void Transform( int distortion, float width, float height,int i, int j, int i_visible_width, int i_visible_height, double *ix, double *iy )
770 double x,y,xnew,ynew;
771 double r,theta,rnew,thetanew;
773 x = (double)i * (width / ((double)i_visible_width));
774 y = (double)j * (height / ((double)i_visible_height));
776 x = (2.0 * (double)x / width) - 1;
777 y = (2.0 * (double)y / height) - 1;
785 /* GRID2D TRANSFORMATION */
793 xnew = rnew * cos(thetanew);
794 ynew = rnew * sin(thetanew);
803 xnew = rnew * cos(thetanew);
804 ynew = rnew * sin(thetanew);
807 xnew = asin(x) / PID2;
808 ynew = asin(y) / PID2;
811 rnew = asin(r) / PID2;
813 xnew = rnew * cos(thetanew);
814 ynew = rnew * sin(thetanew);
816 /* OTHER WAY: 3D MODEL */
822 *ix = width * (xnew + 1) / (2.0);
823 *iy = height * (ynew + 1) / (2.0);
826 /*****************************************************************************
827 * Z_Compute: Calculate the Z-coordinate
828 *****************************************************************************/
829 static float Z_Compute(float p, int distortion, float x, float y)
832 double d_p = p / 100.0;
839 f_z = (1 - d_p * d_p) / (2 * d_p) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x));
841 f_z = (1 - d_p * d_p) / (2 * d_p) + d_p + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x));
845 f_z = (1 - d_p * d_p) / (d_p) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x)) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - y * y));
847 f_z = (1 - d_p * d_p) / (d_p) + 2 * d_p + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x)) + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - y * y));
851 f_z = (1 - d_p * d_p) / (2 * d_p) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x - y * y));
853 f_z = (1 - d_p * d_p) / (2 * d_p) + d_p + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x - y * y));
855 /* OTHER WAY: GRID2D TRANSFORMATION */
872 /*****************************************************************************
873 * DisplayVideo: displays previously rendered output
874 *****************************************************************************/
875 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
878 vout_sys_t *p_sys = p_vout->p_sys;
879 float f_width, f_height, f_x, f_y;
881 if( p_sys->p_vout->pf_lock &&
882 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
884 msg_Warn( p_vout, "could not lock OpenGL provider" );
888 /* glTexCoord works differently with GL_TEXTURE_2D and
889 GL_TEXTURE_RECTANGLE_EXT */
891 f_x = (float)p_vout->fmt_out.i_x_offset;
892 f_y = (float)p_vout->fmt_out.i_y_offset;
893 f_width = (float)p_vout->fmt_out.i_x_offset +
894 (float)p_vout->fmt_out.i_visible_width;
895 f_height = (float)p_vout->fmt_out.i_y_offset +
896 (float)p_vout->fmt_out.i_visible_height;
898 f_x = (float)p_vout->fmt_out.i_x_offset / p_sys->i_tex_width;
899 f_y = (float)p_vout->fmt_out.i_y_offset / p_sys->i_tex_height;
900 f_width = ( (float)p_vout->fmt_out.i_x_offset +
901 p_vout->fmt_out.i_visible_width ) / p_sys->i_tex_width;
902 f_height = ( (float)p_vout->fmt_out.i_y_offset +
903 p_vout->fmt_out.i_visible_height ) / p_sys->i_tex_height;
906 /* Why drawing here and not in Render()? Because this way, the
907 OpenGL providers can call pf_display to force redraw. Currently,
908 the OS X provider uses it to get a smooth window resizing */
910 glClear( GL_COLOR_BUFFER_BIT );
912 if( p_sys->i_effect == OPENGL_EFFECT_NONE )
914 glEnable( VLCGL_TARGET );
915 glBegin( GL_POLYGON );
916 glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
917 glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
918 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
919 glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
923 #ifdef OPENGL_MORE_EFFECT
924 if ((p_sys->i_effect > OPENGL_EFFECT_TRANSPARENT_CUBE) ||
925 ((p_sys->i_effect == OPENGL_EFFECT_NONE)))
927 unsigned int i_i, i_j;
928 unsigned int i_accuracy = config_GetInt( p_vout, "opengl-accuracy");
929 unsigned int i_n = pow(2, i_accuracy);
930 unsigned int i_n_x = (p_vout->fmt_out.i_visible_width / (i_n * 2));
931 unsigned int i_n_y = (p_vout->fmt_out.i_visible_height / i_n);
933 int i_distortion = p_sys->i_effect;
934 float f_p = p_sys->f_radius;
936 glEnable( VLCGL_TARGET );
938 for (i_i = 0; i_i < p_vout->fmt_out.i_visible_width; i_i += i_n_x)
940 if ( i_i == i_n_x * i_n / 2) i_n_x += p_vout->fmt_out.i_visible_width % i_n;
941 if ((i_i == (p_vout->fmt_out.i_visible_width / i_n) * i_n / 2 + i_n_x) &&
942 (p_vout->fmt_out.i_visible_width / i_n != i_n_x))
943 i_n_x -= p_vout->fmt_out.i_visible_width % i_n;
948 for (i_j = 0; i_j < p_vout->fmt_out.i_visible_height; i_j += i_n_y)
950 if ( i_j == i_n_y * i_n / 2) i_n_y += p_vout->fmt_out.i_visible_height % i_n;
951 if ((i_j == (p_vout->fmt_out.i_visible_height / i_n) * i_n / 2 + i_n_y) &&
952 (p_vout->fmt_out.i_visible_height / i_n != i_n_y))
953 i_n_y -= p_vout->fmt_out.i_visible_height % i_n;
955 for (i_m = i_index_max; i_m < i_index_max + 4; i_m++)
957 int i_k = ((i_m % 4) == 1) || ((i_m % 4) == 2);
958 int i_l = ((i_m % 4) == 2) || ((i_m % 4) == 3);
960 Transform( i_distortion, f_width, f_height, i_i + i_k * i_n_x, i_j + i_l * i_n_y, p_vout->fmt_out.i_visible_width, p_vout->fmt_out.i_visible_height, &d_x, &d_y);
961 glTexCoord2f(f_x + d_x, f_y + d_y);
962 d_x = - 1.0 + 2.0 * ((double)(i_k * i_n_x + i_i) / (double)p_vout->fmt_out.i_visible_width);
963 d_y = 1.0 - 2.0 * (((double)i_l * i_n_y + i_j) / (double)p_vout->fmt_out.i_visible_height);
964 glVertex3f((float)d_x, (float)d_y, Z_Compute(f_p, i_distortion, (float)d_x, (float)d_y));
973 glRotatef( 0.5 * p_sys->f_speed , 0.3, 0.5, 0.7 );
975 glEnable( VLCGL_TARGET );
979 glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
980 glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
981 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
982 glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, 1.0 );
985 glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
986 glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
987 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
988 glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
991 glTexCoord2f( f_x, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
992 glTexCoord2f( f_x, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
993 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
994 glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
997 glTexCoord2f( f_x, f_y ); glVertex3f( 1.0, 1.0, 1.0 );
998 glTexCoord2f( f_x, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
999 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
1000 glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
1003 glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
1004 glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
1005 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
1006 glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
1009 glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, - 1.0, 1.0 );
1010 glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
1011 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
1012 glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, - 1.0, 1.0 );
1016 glDisable( VLCGL_TARGET );
1018 p_sys->p_vout->pf_swap( p_sys->p_vout );
1020 if( p_sys->p_vout->pf_unlock )
1022 p_sys->p_vout->pf_unlock( p_sys->p_vout );
1026 int GetAlignedSize( int i_size )
1028 /* Return the nearest power of 2 */
1030 while( i_result < i_size )
1037 /*****************************************************************************
1038 * Control: control facility for the vout
1039 *****************************************************************************/
1040 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1042 vout_sys_t *p_sys = p_vout->p_sys;
1047 return vout_vaControlDefault( p_vout, i_query, args );
1050 if( p_sys->p_vout->pf_control )
1051 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
1053 return vout_vaControlDefault( p_vout, i_query, args );
1057 static int InitTextures( vout_thread_t *p_vout )
1059 vout_sys_t *p_sys = p_vout->p_sys;
1062 glDeleteTextures( 2, p_sys->p_textures );
1063 glGenTextures( 2, p_sys->p_textures );
1065 for( i_index = 0; i_index < 2; i_index++ )
1067 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
1069 /* Set the texture parameters */
1070 glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
1072 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
1073 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
1075 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
1076 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
1078 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1081 /* Tell the driver not to make a copy of the texture but to use
1083 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
1084 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
1087 /* Use VRAM texturing */
1088 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
1089 GL_STORAGE_CACHED_APPLE );
1091 /* Use AGP texturing */
1092 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
1093 GL_STORAGE_SHARED_APPLE );
1097 /* Call glTexImage2D only once, and use glTexSubImage2D later */
1098 glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
1099 p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
1100 p_sys->pp_buffer[i_index] );
1106 /*****************************************************************************
1107 * SendEvents: forward mouse and keyboard events to the parent p_vout
1108 *****************************************************************************/
1109 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1110 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1112 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
1113 return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );