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 */
36 #include <OpenGL/gl.h>
37 #include <OpenGL/glext.h>
39 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
40 This allows sizes which are not powers of 2 */
41 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
43 /* OS X OpenGL supports YUV. Hehe. */
44 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
45 #define VLCGL_TYPE GL_UNSIGNED_SHORT_8_8_APPLE
49 #define VLCGL_TARGET GL_TEXTURE_2D
52 #ifndef GL_UNSIGNED_SHORT_5_6_5
53 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
55 //#define VLCGL_RGB_FORMAT GL_RGB
56 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
59 //#define VLCGL_RGB_FORMAT GL_RGB
60 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
63 #define VLCGL_RGB_FORMAT GL_RGBA
64 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
68 #define YCBCR_MESA 0x8757
70 #ifndef UNSIGNED_SHORT_8_8_MESA
71 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
73 #define VLCGL_YUV_FORMAT YCBCR_MESA
74 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
76 /* Use RGB on Win32/GLX */
77 #define VLCGL_FORMAT VLCGL_RGB_FORMAT
78 #define VLCGL_TYPE VLCGL_RGB_TYPE
79 //#define VLCGL_FORMAT VLCGL_YUV_FORMAT
80 //#define VLCGL_TYPE VLCGL_YUV_TYPE
83 #ifndef GL_CLAMP_TO_EDGE
84 # define GL_CLAMP_TO_EDGE 0x812F
88 #define OPENGL_EFFECT_NONE 1
89 #define OPENGL_EFFECT_CUBE 2
90 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
91 #if defined( HAVE_GL_GLU_H ) || defined( __APPLE__ )
92 #define OPENGL_MORE_EFFECT 8
95 #ifdef OPENGL_MORE_EFFECT
98 #include <OpenGL/glu.h>
106 /*GRID2D TRANSFORMATION */
113 #define INIFILE 4096 // not used, just for mark end ...
114 #define SIGN(x) (x < 0 ? (-1) : 1)
115 #define PID2 1.570796326794896619231322
117 static const char *ppsz_effects[] = {
118 "none", "cube", "transparent-cube", "cylinder", "torus", "sphere","SQUAREXY","SQUARER", "ASINXY", "ASINR", "SINEXY", "SINER" };
119 static const char *ppsz_effects_text[] = {
120 N_("None"), N_("Cube"), N_("Transparent Cube"),
121 N_("Cylinder"), N_("Torus"), N_("Sphere"), N_("SQUAREXY"),N_("SQUARER"), N_("ASINXY"), N_("ASINR"), N_("SINEXY"), N_("SINER") };
124 /*****************************************************************************
126 *****************************************************************************/
127 static int CreateVout ( vlc_object_t * );
128 static void DestroyVout ( vlc_object_t * );
129 static int Init ( vout_thread_t * );
130 static void End ( vout_thread_t * );
131 static int Manage ( vout_thread_t * );
132 static void Render ( vout_thread_t *, picture_t * );
133 static void DisplayVideo ( vout_thread_t *, picture_t * );
134 static int Control ( vout_thread_t *, int, va_list );
136 static inline int GetAlignedSize( int );
138 static int InitTextures ( vout_thread_t * );
139 static int SendEvents ( vlc_object_t *, char const *,
140 vlc_value_t, vlc_value_t, void * );
142 #ifdef OPENGL_MORE_EFFECT
143 static float Z_Compute ( float, int, float, float );
144 static void Transform ( float, int, float, float, int, int, int, int, double *, double * );
146 /*****************************************************************************
148 *****************************************************************************/
149 #define ACCURACY_TEXT N_( "OpenGL sampling accuracy " )
150 #define ACCURACY_LONGTEXT N_( "Select the accuracy of 3D object sampling(1 = min and 10 = max)" )
151 #define RADIUS_TEXT N_( "OpenGL Cylinder radius" )
152 #define RADIUS_LONGTEXT N_( "Radius of the OpenGL cylinder effect, if enabled" )
153 #define POV_X_TEXT N_("Point of view x-coordinate")
154 #define POV_X_LONGTEXT N_("Point of view (X coordinate) of the cube/cylinder "\
155 "effect, if enabled.")
156 #define POV_Y_TEXT N_("Point of view y-coordinate")
157 #define POV_Y_LONGTEXT N_("Point of view (Y coordinate) of the cube/cylinder "\
158 "effect, if enabled.")
159 #define POV_Z_TEXT N_("Point of view z-coordinate")
160 #define POV_Z_LONGTEXT N_("Point of view (Z coordinate) of the cube/cylinder "\
161 "effect, if enabled.")
163 #define SPEED_TEXT N_( "OpenGL cube rotation speed" )
164 #define SPEED_LONGTEXT N_( "Rotation speed of the OpenGL cube effect, if " \
166 #define EFFECT_TEXT N_("Effect")
167 #define EFFECT_LONGTEXT N_( \
168 "Several visual OpenGL effects are available." )
170 #ifndef OPENGL_MORE_EFFECT
171 static const char *ppsz_effects[] = {
172 "none", "cube", "transparent-cube" };
173 static const char *ppsz_effects_text[] = {
174 N_("None"), N_("Cube"), N_("Transparent Cube") };
178 set_shortname( "OpenGL" );
179 set_category( CAT_VIDEO );
180 set_subcategory( SUBCAT_VIDEO_VOUT );
181 set_description( _("OpenGL video output") );
183 set_capability( "video output", 200 );
185 set_capability( "video output", 20 );
187 add_shortcut( "opengl" );
188 add_float( "opengl-cube-speed", 2.0, NULL, SPEED_TEXT,
189 SPEED_LONGTEXT, VLC_TRUE );
190 #ifdef OPENGL_MORE_EFFECT
191 add_integer_with_range( "opengl-accuracy", 4, 1, 10, NULL, ACCURACY_TEXT,
192 ACCURACY_LONGTEXT, VLC_TRUE );
193 add_float_with_range( "opengl-pov-x", 0.0, -1.0, 1.0, NULL, POV_X_TEXT,
194 POV_X_LONGTEXT, VLC_TRUE );
195 add_float_with_range( "opengl-pov-y", 0.0, -1.0, 1.0, NULL, POV_Y_TEXT,
196 POV_Y_LONGTEXT, VLC_TRUE );
197 add_float_with_range( "opengl-pov-z", -1.0, -1.0, 1.0, NULL, POV_Z_TEXT,
198 POV_Z_LONGTEXT, VLC_TRUE );
199 add_float( "opengl-cylinder-radius", -100.0, NULL, RADIUS_TEXT,
200 RADIUS_LONGTEXT, VLC_TRUE );
203 set_callbacks( CreateVout, DestroyVout );
204 add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
205 EFFECT_LONGTEXT, VLC_FALSE );
206 change_string_list( ppsz_effects, ppsz_effects_text, 0 );
209 /*****************************************************************************
210 * vout_sys_t: video output method descriptor
211 *****************************************************************************
212 * This structure is part of the video output thread descriptor.
213 * It describes the OpenGL specific properties of the output thread.
214 *****************************************************************************/
217 vout_thread_t *p_vout;
219 uint8_t *pp_buffer[2];
223 GLuint p_textures[2];
231 /*****************************************************************************
232 * CreateVout: This function allocates and initializes the OpenGL vout method.
233 *****************************************************************************/
234 static int CreateVout( vlc_object_t *p_this )
236 vout_thread_t *p_vout = (vout_thread_t *)p_this;
239 /* Allocate structure */
240 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
243 msg_Err( p_vout, "out of memory" );
247 var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
251 p_sys->i_tex_width = p_vout->fmt_in.i_width;
252 p_sys->i_tex_height = p_vout->fmt_in.i_height;
254 /* A texture must have a size aligned on a power of 2 */
255 p_sys->i_tex_width = GetAlignedSize( p_vout->fmt_in.i_width );
256 p_sys->i_tex_height = GetAlignedSize( p_vout->fmt_in.i_height );
259 msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
260 p_sys->i_tex_height );
264 (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
265 if( p_sys->p_vout == NULL )
267 msg_Err( p_vout, "out of memory" );
270 vlc_object_attach( p_sys->p_vout, p_this );
272 p_sys->p_vout->i_window_width = p_vout->i_window_width;
273 p_sys->p_vout->i_window_height = p_vout->i_window_height;
274 p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
275 p_sys->p_vout->render.i_width = p_vout->render.i_width;
276 p_sys->p_vout->render.i_height = p_vout->render.i_height;
277 p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
278 p_sys->p_vout->fmt_render = p_vout->fmt_render;
279 p_sys->p_vout->fmt_in = p_vout->fmt_in;
280 p_sys->p_vout->b_scale = p_vout->b_scale;
281 p_sys->p_vout->i_alignment = p_vout->i_alignment;
283 p_sys->p_vout->p_module =
284 module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
285 if( p_sys->p_vout->p_module == NULL )
287 msg_Warn( p_vout, "No OpenGL provider found" );
288 vlc_object_detach( p_sys->p_vout );
289 vlc_object_destroy( p_sys->p_vout );
293 p_sys->f_speed = var_CreateGetFloat( p_vout, "opengl-cube-speed" );
294 p_sys->f_radius = var_CreateGetFloat( p_vout, "opengl-cylinder-radius" );
296 p_vout->pf_init = Init;
297 p_vout->pf_end = End;
298 p_vout->pf_manage = Manage;
299 p_vout->pf_render = Render;
300 p_vout->pf_display = DisplayVideo;
301 p_vout->pf_control = Control;
303 /* Forward events from the opengl provider */
304 var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
305 var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
306 var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
307 var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_INTEGER );
308 var_Create( p_sys->p_vout, "mouse-button-down", VLC_VAR_INTEGER );
309 var_Create( p_sys->p_vout, "video-on-top",
310 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
312 var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
313 var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
314 var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
315 var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
316 var_AddCallback( p_sys->p_vout, "mouse-button-down", SendEvents, p_vout );
321 /*****************************************************************************
322 * Init: initialize the OpenGL video thread output method
323 *****************************************************************************/
324 static int Init( vout_thread_t *p_vout )
326 vout_sys_t *p_sys = p_vout->p_sys;
330 p_sys->p_vout->pf_init( p_sys->p_vout );
332 /* TODO: We use YCbCr on Mac which is Y422, but on OSX it seems to == YUY2. Verify */
333 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
334 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
337 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
338 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
341 #elif VLCGL_FORMAT == GL_RGB
342 # if VLCGL_TYPE == GL_UNSIGNED_BYTE
343 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
344 # if defined( WORDS_BIGENDIAN )
345 p_vout->output.i_rmask = 0x00ff0000;
346 p_vout->output.i_gmask = 0x0000ff00;
347 p_vout->output.i_bmask = 0x000000ff;
349 p_vout->output.i_rmask = 0x000000ff;
350 p_vout->output.i_gmask = 0x0000ff00;
351 p_vout->output.i_bmask = 0x00ff0000;
355 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
356 # if defined( WORDS_BIGENDIAN )
357 p_vout->output.i_rmask = 0x001f;
358 p_vout->output.i_gmask = 0x07e0;
359 p_vout->output.i_bmask = 0xf800;
361 p_vout->output.i_rmask = 0xf800;
362 p_vout->output.i_gmask = 0x07e0;
363 p_vout->output.i_bmask = 0x001f;
368 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
369 # if defined( WORDS_BIGENDIAN )
370 p_vout->output.i_rmask = 0xff000000;
371 p_vout->output.i_gmask = 0x00ff0000;
372 p_vout->output.i_bmask = 0x0000ff00;
374 p_vout->output.i_rmask = 0x000000ff;
375 p_vout->output.i_gmask = 0x0000ff00;
376 p_vout->output.i_bmask = 0x00ff0000;
381 /* Since OpenGL can do rescaling for us, stick to the default
382 * coordinates and aspect. */
383 p_vout->output.i_width = p_vout->render.i_width;
384 p_vout->output.i_height = p_vout->render.i_height;
385 p_vout->output.i_aspect = p_vout->render.i_aspect;
387 p_vout->fmt_out = p_vout->fmt_in;
388 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
390 /* We know the chroma, allocate one buffer which will be used
391 * directly by the decoder */
392 p_sys->pp_buffer[0] =
393 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
394 if( !p_sys->pp_buffer[0] )
396 msg_Err( p_vout, "out of memory" );
399 p_sys->pp_buffer[1] =
400 malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
401 if( !p_sys->pp_buffer[1] )
403 msg_Err( p_vout, "out of memory" );
407 p_vout->p_picture[0].i_planes = 1;
408 p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
409 p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
410 p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
411 p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
412 p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
413 p_vout->p_picture[0].p->i_pixel_pitch;
414 p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
415 p_vout->p_picture[0].p->i_pixel_pitch;
417 p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
418 p_vout->p_picture[0].i_type = DIRECT_PICTURE;
420 PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
422 I_OUTPUTPICTURES = 1;
424 if( p_sys->p_vout->pf_lock &&
425 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
427 msg_Warn( p_vout, "could not lock OpenGL provider" );
431 InitTextures( p_vout );
434 glDisable(GL_DEPTH_TEST);
435 glDepthMask(GL_FALSE);
436 glDisable(GL_CULL_FACE);
437 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
438 glClear( GL_COLOR_BUFFER_BIT );
440 /* Check if the user asked for useless visual effects */
441 var_Get( p_vout, "opengl-effect", &val );
442 if( !val.psz_string || !strcmp( val.psz_string, "none" ))
444 p_sys->i_effect = OPENGL_EFFECT_NONE;
446 else if( !strcmp( val.psz_string, "cube" ) )
448 p_sys->i_effect = OPENGL_EFFECT_CUBE;
450 glEnable( GL_CULL_FACE);
452 else if( !strcmp( val.psz_string, "transparent-cube" ) )
454 p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
456 glDisable( GL_DEPTH_TEST );
457 glEnable( GL_BLEND );
458 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
462 #ifdef OPENGL_MORE_EFFECT
464 while (( strcmp( val.psz_string, ppsz_effects[p_sys->i_effect]) ) && (pow(2,p_sys->i_effect) < INIFILE))
468 if (pow(2,p_sys->i_effect) < INIFILE)
469 p_sys->i_effect = pow(2,p_sys->i_effect);
470 else if ( strcmp( val.psz_string, ppsz_effects[p_sys->i_effect]))
472 msg_Warn( p_vout, "no valid opengl effect provided, using "
474 p_sys->i_effect = OPENGL_EFFECT_NONE;
477 msg_Warn( p_vout, "no valid opengl effect provided, using "
479 p_sys->i_effect = OPENGL_EFFECT_NONE;
482 if( val.psz_string ) free( val.psz_string );
484 if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
485 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
487 /* Set the perpective */
488 glMatrixMode( GL_PROJECTION );
490 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
491 glMatrixMode( GL_MODELVIEW );
493 glTranslatef( 0.0, 0.0, - 5.0 );
495 #ifdef OPENGL_MORE_EFFECT
498 /* Set the perpective */
499 glMatrixMode( GL_PROJECTION );
501 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
502 glMatrixMode( GL_MODELVIEW );
504 glTranslatef( 0.0, 0.0, -3.0 );
506 float f_pov_x, f_pov_y, f_pov_z;
507 f_pov_x = var_CreateGetFloat( p_vout, "opengl-pov-x");
508 f_pov_y = var_CreateGetFloat( p_vout, "opengl-pov-y");
509 f_pov_z = var_CreateGetFloat( p_vout, "opengl-pov-z");
510 gluLookAt(0, 0, 0, f_pov_x, f_pov_y, f_pov_z, 0, 1, 0);
513 if( p_sys->p_vout->pf_unlock )
515 p_sys->p_vout->pf_unlock( p_sys->p_vout );
521 /*****************************************************************************
522 * End: terminate GLX video thread output method
523 *****************************************************************************/
524 static void End( vout_thread_t *p_vout )
526 vout_sys_t *p_sys = p_vout->p_sys;
528 if( p_sys->p_vout->pf_lock &&
529 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
531 msg_Warn( p_vout, "could not lock OpenGL provider" );
538 /* Free the texture buffer*/
539 glDeleteTextures( 2, p_sys->p_textures );
540 if( p_sys->pp_buffer[0] ) free( p_sys->pp_buffer[0] );
541 if( p_sys->pp_buffer[1] ) free( p_sys->pp_buffer[1] );
543 if( p_sys->p_vout->pf_unlock )
545 p_sys->p_vout->pf_unlock( p_sys->p_vout );
549 /*****************************************************************************
550 * Destroy: destroy GLX video thread output method
551 *****************************************************************************
552 * Terminate an output method created by CreateVout
553 *****************************************************************************/
554 static void DestroyVout( vlc_object_t *p_this )
556 vout_thread_t *p_vout = (vout_thread_t *)p_this;
557 vout_sys_t *p_sys = p_vout->p_sys;
559 module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
560 vlc_object_detach( p_sys->p_vout );
561 vlc_object_destroy( p_sys->p_vout );
566 /*****************************************************************************
567 * Manage: handle Sys events
568 *****************************************************************************
569 * This function should be called regularly by video output thread. It returns
570 * a non null value if an error occurred.
571 *****************************************************************************/
572 static int Manage( vout_thread_t *p_vout )
574 vout_sys_t *p_sys = p_vout->p_sys;
575 int i_ret, i_fullscreen_change;
577 i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
579 p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
580 p_vout->fmt_in.i_x_offset;
581 p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
582 p_vout->fmt_in.i_y_offset;
583 p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
584 p_vout->fmt_in.i_visible_width;
585 p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
586 p_vout->fmt_in.i_visible_height;
587 p_vout->fmt_out.i_aspect = p_sys->p_vout->fmt_in.i_aspect =
588 p_vout->fmt_in.i_aspect;
589 p_vout->fmt_out.i_sar_num = p_sys->p_vout->fmt_in.i_sar_num =
590 p_vout->fmt_in.i_sar_num;
591 p_vout->fmt_out.i_sar_den = p_sys->p_vout->fmt_in.i_sar_den =
592 p_vout->fmt_in.i_sar_den;
593 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
595 p_sys->p_vout->i_changes = p_vout->i_changes;
596 i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
597 p_vout->i_changes = p_sys->p_vout->i_changes;
600 if( p_sys->p_vout->pf_lock &&
601 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
603 msg_Warn( p_vout, "could not lock OpenGL provider" );
607 /* On OS X, we create the window and the GL view when entering
608 fullscreen - the textures have to be inited again */
609 if( i_fullscreen_change )
611 InitTextures( p_vout );
613 switch( p_sys->i_effect )
615 case OPENGL_EFFECT_CUBE:
616 #ifdef OPENGL_MORE_EFFECT
627 glEnable( GL_CULL_FACE );
630 case OPENGL_EFFECT_TRANSPARENT_CUBE:
631 glDisable( GL_DEPTH_TEST );
632 glEnable( GL_BLEND );
633 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
637 if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
638 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
640 /* Set the perpective */
641 glMatrixMode( GL_PROJECTION );
643 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
644 glMatrixMode( GL_MODELVIEW );
646 glTranslatef( 0.0, 0.0, - 5.0 );
648 #ifdef OPENGL_MORE_EFFECT
651 glMatrixMode( GL_PROJECTION );
653 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
654 glMatrixMode( GL_MODELVIEW );
656 glTranslatef( 0.0, 0.0, -3.0 );
658 float f_pov_x, f_pov_y, f_pov_z;
659 f_pov_x = var_CreateGetFloat( p_vout, "opengl-pov-x");
660 f_pov_y = var_CreateGetFloat( p_vout, "opengl-pov-y");
661 f_pov_z = var_CreateGetFloat( p_vout, "opengl-pov-z");
662 gluLookAt(0, 0, 0, f_pov_x, f_pov_y, f_pov_z, 0, 1, 0);
667 if( p_sys->p_vout->pf_unlock )
669 p_sys->p_vout->pf_unlock( p_sys->p_vout );
672 // to align in real time in OPENGL
673 if (p_sys->p_vout->i_alignment != p_vout->i_alignment)
675 p_vout->i_changes = VOUT_CROP_CHANGE; //to force change
676 p_sys->p_vout->i_alignment = p_vout->i_alignment;
682 /*****************************************************************************
683 * Render: render previously calculated output
684 *****************************************************************************/
685 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
687 vout_sys_t *p_sys = p_vout->p_sys;
689 /* On Win32/GLX, we do this the usual way:
690 + Fill the buffer with new content,
691 + Reload the texture,
694 On OS X with VRAM or AGP texturing, the order has to be:
695 + Reload the texture,
696 + Fill the buffer with new content,
699 (Thanks to gcc from the Arstechnica forums for the tip)
701 Therefore, we have to use two buffers and textures. On Win32/GLX,
702 we reload the texture to be displayed and use it right away. On
703 OS X, we first render, then reload the texture to be used next
706 if( p_sys->p_vout->pf_lock &&
707 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
709 msg_Warn( p_vout, "could not lock OpenGL provider" );
715 i_new_index = ( p_sys->i_index + 1 ) & 1;
718 /* Update the texture */
719 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
720 glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
721 p_vout->fmt_out.i_width,
722 p_vout->fmt_out.i_height,
723 VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
725 /* Bind to the previous texture for drawing */
726 glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
729 p_sys->i_index = i_new_index;
730 p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
733 /* Update the texture */
734 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
735 p_vout->fmt_out.i_width,
736 p_vout->fmt_out.i_height,
737 VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[0] );
740 if( p_sys->p_vout->pf_unlock )
742 p_sys->p_vout->pf_unlock( p_sys->p_vout );
747 #ifdef OPENGL_MORE_EFFECT
748 /*****************************************************************************
749 * Transform: Calculate the distorted grid coordinates
750 *****************************************************************************/
751 static void Transform(float p, int distortion, float width, float height,int i, int j, int i_visible_width, int i_visible_height, double *ix,double *iy)
753 double x,y,xnew,ynew;
754 double r,theta,rnew,thetanew;
756 x = (double)i * (width / ((double)i_visible_width));
757 y = (double)j * (height / ((double)i_visible_height));
759 x = (2.0 * (double)x / width) - 1;
760 y = (2.0 * (double)y / height) - 1;
768 /* GRID2D TRANSFORMATION */
776 xnew = rnew * cos(thetanew);
777 ynew = rnew * sin(thetanew);
786 xnew = rnew * cos(thetanew);
787 ynew = rnew * sin(thetanew);
790 xnew = asin(x) / PID2;
791 ynew = asin(y) / PID2;
794 rnew = asin(r) / PID2;
796 xnew = rnew * cos(thetanew);
797 ynew = rnew * sin(thetanew);
799 /* OTHER WAY: 3D MODEL */
805 *ix = width * (xnew + 1) / (2.0);
806 *iy = height * (ynew + 1) / (2.0);
809 /*****************************************************************************
810 * Z_Compute: Calculate the Z-coordinate
811 *****************************************************************************/
812 static float Z_Compute(float p, int distortion, float x, float y)
815 double d_p = p / 100.0;
822 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));
824 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));
828 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));
830 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));
834 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));
836 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));
838 /* OTHER WAY: GRID2D TRANSFORMATION */
855 /*****************************************************************************
856 * DisplayVideo: displays previously rendered output
857 *****************************************************************************/
858 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
860 vout_sys_t *p_sys = p_vout->p_sys;
861 float f_width, f_height, f_x, f_y;
863 if( p_sys->p_vout->pf_lock &&
864 p_sys->p_vout->pf_lock( p_sys->p_vout ) )
866 msg_Warn( p_vout, "could not lock OpenGL provider" );
870 /* glTexCoord works differently with GL_TEXTURE_2D and
871 GL_TEXTURE_RECTANGLE_EXT */
873 f_x = (float)p_vout->fmt_out.i_x_offset;
874 f_y = (float)p_vout->fmt_out.i_y_offset;
875 f_width = (float)p_vout->fmt_out.i_x_offset +
876 (float)p_vout->fmt_out.i_visible_width;
877 f_height = (float)p_vout->fmt_out.i_y_offset +
878 (float)p_vout->fmt_out.i_visible_height;
880 f_x = (float)p_vout->fmt_out.i_x_offset / p_sys->i_tex_width;
881 f_y = (float)p_vout->fmt_out.i_y_offset / p_sys->i_tex_height;
882 f_width = ( (float)p_vout->fmt_out.i_x_offset +
883 p_vout->fmt_out.i_visible_width ) / p_sys->i_tex_width;
884 f_height = ( (float)p_vout->fmt_out.i_y_offset +
885 p_vout->fmt_out.i_visible_height ) / p_sys->i_tex_height;
888 /* Why drawing here and not in Render()? Because this way, the
889 OpenGL providers can call pf_display to force redraw. Currently,
890 the OS X provider uses it to get a smooth window resizing */
892 glClear( GL_COLOR_BUFFER_BIT );
894 if( p_sys->i_effect == OPENGL_EFFECT_NONE )
896 glEnable( VLCGL_TARGET );
897 glBegin( GL_POLYGON );
898 glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
899 glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
900 glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
901 glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
905 #ifdef OPENGL_MORE_EFFECT
906 if ((p_sys->i_effect > OPENGL_EFFECT_TRANSPARENT_CUBE) ||
907 ((p_sys->i_effect == OPENGL_EFFECT_NONE)))
909 unsigned int i_i, i_j;
910 unsigned int i_accuracy = config_GetInt( p_vout, "opengl-accuracy");
911 unsigned int i_n = pow(2, i_accuracy);
912 unsigned int i_n_x = (p_vout->fmt_out.i_visible_width / (i_n * 2));
913 unsigned int i_n_y = (p_vout->fmt_out.i_visible_height / i_n);
915 int i_distortion = p_sys->i_effect;
916 float f_p = p_sys->f_radius;
918 glEnable( VLCGL_TARGET );
920 for (i_i = 0; i_i < p_vout->fmt_out.i_visible_width; i_i += i_n_x)
922 if ( i_i == i_n_x * i_n / 2) i_n_x += p_vout->fmt_out.i_visible_width % i_n;
923 if ((i_i == (p_vout->fmt_out.i_visible_width / i_n) * i_n / 2 + i_n_x) &&
924 (p_vout->fmt_out.i_visible_width / i_n != i_n_x))
925 i_n_x -= p_vout->fmt_out.i_visible_width % i_n;
930 for (i_j = 0; i_j < p_vout->fmt_out.i_visible_height; i_j += i_n_y)
932 if ( i_j == i_n_y * i_n / 2) i_n_y += p_vout->fmt_out.i_visible_height % i_n;
933 if ((i_j == (p_vout->fmt_out.i_visible_height / i_n) * i_n / 2 + i_n_y) &&
934 (p_vout->fmt_out.i_visible_height / i_n != i_n_y))
935 i_n_y -= p_vout->fmt_out.i_visible_height % i_n;
937 for (i_m = i_index_max; i_m < i_index_max + 4; i_m++)
939 int i_k = ((i_m % 4) == 1) || ((i_m % 4) == 2);
940 int i_l = ((i_m % 4) == 2) || ((i_m % 4) == 3);
942 Transform(f_p, 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);
943 glTexCoord2f(f_x + d_x, f_y + d_y);
944 d_x = - 1.0 + 2.0 * ((double)(i_k * i_n_x + i_i) / (double)p_vout->fmt_out.i_visible_width);
945 d_y = 1.0 - 2.0 * (((double)i_l * i_n_y + i_j) / (double)p_vout->fmt_out.i_visible_height);
946 glVertex3f((float)d_x, (float)d_y, Z_Compute(f_p, i_distortion, (float)d_x, (float)d_y));
955 glRotatef( 0.5 * p_sys->f_speed , 0.3, 0.5, 0.7 );
957 glEnable( VLCGL_TARGET );
961 glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
962 glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
963 glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
964 glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, 1.0 );
967 glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
968 glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
969 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
970 glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
973 glTexCoord2f( f_x, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
974 glTexCoord2f( f_x, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
975 glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
976 glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
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 );
998 glDisable( VLCGL_TARGET );
1000 p_sys->p_vout->pf_swap( p_sys->p_vout );
1002 if( p_sys->p_vout->pf_unlock )
1004 p_sys->p_vout->pf_unlock( p_sys->p_vout );
1008 int GetAlignedSize( int i_size )
1010 /* Return the nearest power of 2 */
1012 while( i_result < i_size )
1019 /*****************************************************************************
1020 * Control: control facility for the vout
1021 *****************************************************************************/
1022 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1024 vout_sys_t *p_sys = p_vout->p_sys;
1029 return vout_vaControlDefault( p_vout, i_query, args );
1032 if( p_sys->p_vout->pf_control )
1033 return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
1035 return vout_vaControlDefault( p_vout, i_query, args );
1039 static int InitTextures( vout_thread_t *p_vout )
1041 vout_sys_t *p_sys = p_vout->p_sys;
1044 glDeleteTextures( 2, p_sys->p_textures );
1045 glGenTextures( 2, p_sys->p_textures );
1047 for( i_index = 0; i_index < 2; i_index++ )
1049 glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
1051 /* Set the texture parameters */
1052 glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
1054 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
1055 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
1057 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
1058 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
1060 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1063 /* Tell the driver not to make a copy of the texture but to use
1065 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
1066 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
1069 /* Use VRAM texturing */
1070 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
1071 GL_STORAGE_CACHED_APPLE );
1073 /* Use AGP texturing */
1074 glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
1075 GL_STORAGE_SHARED_APPLE );
1079 /* Call glTexImage2D only once, and use glTexSubImage2D later */
1080 glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
1081 p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
1082 p_sys->pp_buffer[i_index] );
1088 /*****************************************************************************
1089 * SendEvents: forward mouse and keyboard events to the parent p_vout
1090 *****************************************************************************/
1091 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1092 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1094 return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );