2007-06-12 22:20:16 +08:00
/* OpenSceneGraph example, osgmovie.
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
*/
2004-01-30 22:06:29 +08:00
2007-01-08 18:00:16 +08:00
# include <osgViewer/Viewer>
2007-06-13 02:58:32 +08:00
# include <osgViewer/ViewerEventHandlers>
2004-01-30 22:06:29 +08:00
# include <osgDB/ReadFile>
# include <osg/Geode>
# include <osg/Geometry>
# include <osg/StateSet>
# include <osg/Material>
# include <osg/Texture2D>
# include <osg/TextureRectangle>
2007-05-08 20:18:57 +08:00
# include <osg/TextureCubeMap>
2004-01-30 22:06:29 +08:00
# include <osg/TexMat>
# include <osg/CullFace>
2004-03-13 05:04:20 +08:00
# include <osg/ImageStream>
2006-01-13 06:43:36 +08:00
# include <osg/io_utils>
2004-01-30 22:06:29 +08:00
# include <osgGA/TrackballManipulator>
2007-12-07 01:53:22 +08:00
# include <osgGA/StateSetManipulator>
2006-01-13 06:43:36 +08:00
# include <osgGA/EventVisitor>
2004-01-30 22:06:29 +08:00
2007-01-08 18:00:16 +08:00
# include <iostream>
2005-02-09 18:39:45 +08:00
2004-03-13 05:04:20 +08:00
class MovieEventHandler : public osgGA : : GUIEventHandler
{
public :
2009-11-20 18:54:39 +08:00
MovieEventHandler ( ) : _trackMouse ( false ) { }
2009-02-08 23:56:35 +08:00
2007-05-08 20:18:57 +08:00
void setMouseTracking ( bool track ) { _trackMouse = track ; }
bool getMouseTracking ( ) const { return _trackMouse ; }
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
void set ( osg : : Node * node ) ;
2006-01-13 06:43:36 +08:00
virtual bool handle ( const osgGA : : GUIEventAdapter & ea , osgGA : : GUIActionAdapter & aa , osg : : Object * , osg : : NodeVisitor * nv ) ;
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
virtual void getUsage ( osg : : ApplicationUsage & usage ) const ;
2007-12-11 01:30:18 +08:00
typedef std : : vector < osg : : observer_ptr < osg : : ImageStream > > ImageStreamList ;
2004-03-13 05:04:20 +08:00
protected :
virtual ~ MovieEventHandler ( ) { }
class FindImageStreamsVisitor : public osg : : NodeVisitor
{
public :
FindImageStreamsVisitor ( ImageStreamList & imageStreamList ) :
_imageStreamList ( imageStreamList ) { }
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
virtual void apply ( osg : : Geode & geode )
{
apply ( geode . getStateSet ( ) ) ;
for ( unsigned int i = 0 ; i < geode . getNumDrawables ( ) ; + + i )
{
apply ( geode . getDrawable ( i ) - > getStateSet ( ) ) ;
}
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
traverse ( geode ) ;
}
virtual void apply ( osg : : Node & node )
{
apply ( node . getStateSet ( ) ) ;
traverse ( node ) ;
}
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
inline void apply ( osg : : StateSet * stateset )
{
if ( ! stateset ) return ;
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
osg : : StateAttribute * attr = stateset - > getTextureAttribute ( 0 , osg : : StateAttribute : : TEXTURE ) ;
if ( attr )
{
osg : : Texture2D * texture2D = dynamic_cast < osg : : Texture2D * > ( attr ) ;
if ( texture2D ) apply ( dynamic_cast < osg : : ImageStream * > ( texture2D - > getImage ( ) ) ) ;
osg : : TextureRectangle * textureRec = dynamic_cast < osg : : TextureRectangle * > ( attr ) ;
if ( textureRec ) apply ( dynamic_cast < osg : : ImageStream * > ( textureRec - > getImage ( ) ) ) ;
}
}
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
inline void apply ( osg : : ImageStream * imagestream )
{
2005-02-09 18:39:45 +08:00
if ( imagestream )
{
2009-02-08 23:56:35 +08:00
_imageStreamList . push_back ( imagestream ) ;
2005-02-09 18:39:45 +08:00
}
2004-03-13 05:04:20 +08:00
}
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
ImageStreamList & _imageStreamList ;
2009-02-03 23:28:53 +08:00
protected :
2009-02-08 23:56:35 +08:00
2009-02-03 23:28:53 +08:00
FindImageStreamsVisitor & operator = ( const FindImageStreamsVisitor & ) { return * this ; }
2004-03-13 05:04:20 +08:00
} ;
2007-05-08 20:18:57 +08:00
bool _trackMouse ;
2004-03-13 05:04:20 +08:00
ImageStreamList _imageStreamList ;
2009-11-20 22:31:11 +08:00
unsigned int _seekIncr ;
2009-02-08 23:56:35 +08:00
2004-03-13 05:04:20 +08:00
} ;
void MovieEventHandler : : set ( osg : : Node * node )
{
_imageStreamList . clear ( ) ;
if ( node )
{
FindImageStreamsVisitor fisv ( _imageStreamList ) ;
node - > accept ( fisv ) ;
}
}
2006-01-13 06:43:36 +08:00
bool MovieEventHandler : : handle ( const osgGA : : GUIEventAdapter & ea , osgGA : : GUIActionAdapter & aa , osg : : Object * , osg : : NodeVisitor * nv )
2004-03-13 05:04:20 +08:00
{
switch ( ea . getEventType ( ) )
{
2006-01-13 06:43:36 +08:00
case ( osgGA : : GUIEventAdapter : : MOVE ) :
case ( osgGA : : GUIEventAdapter : : PUSH ) :
case ( osgGA : : GUIEventAdapter : : RELEASE ) :
{
2007-05-08 20:18:57 +08:00
if ( _trackMouse )
2006-01-13 06:43:36 +08:00
{
2007-05-08 20:18:57 +08:00
osgViewer : : View * view = dynamic_cast < osgViewer : : View * > ( & aa ) ;
osgUtil : : LineSegmentIntersector : : Intersections intersections ;
2008-04-11 18:10:40 +08:00
bool foundIntersection = view = = 0 ? false :
2013-05-04 03:26:27 +08:00
( nv = = 0 ? view - > computeIntersections ( ea , intersections ) :
view - > computeIntersections ( ea , nv - > getNodePath ( ) , intersections ) ) ;
2009-02-08 23:56:35 +08:00
2008-04-11 18:10:40 +08:00
if ( foundIntersection )
2006-01-13 06:43:36 +08:00
{
2009-02-08 23:56:35 +08:00
// use the nearest intersection
2007-05-08 20:18:57 +08:00
const osgUtil : : LineSegmentIntersector : : Intersection & intersection = * ( intersections . begin ( ) ) ;
osg : : Drawable * drawable = intersection . drawable . get ( ) ;
osg : : Geometry * geometry = drawable ? drawable - > asGeometry ( ) : 0 ;
osg : : Vec3Array * vertices = geometry ? dynamic_cast < osg : : Vec3Array * > ( geometry - > getVertexArray ( ) ) : 0 ;
if ( vertices )
2006-01-13 06:43:36 +08:00
{
2007-05-08 20:18:57 +08:00
// get the vertex indices.
const osgUtil : : LineSegmentIntersector : : Intersection : : IndexList & indices = intersection . indexList ;
const osgUtil : : LineSegmentIntersector : : Intersection : : RatioList & ratios = intersection . ratioList ;
2007-01-10 01:35:46 +08:00
2007-05-08 20:18:57 +08:00
if ( indices . size ( ) = = 3 & & ratios . size ( ) = = 3 )
2006-01-13 06:43:36 +08:00
{
2007-05-08 20:18:57 +08:00
unsigned int i1 = indices [ 0 ] ;
unsigned int i2 = indices [ 1 ] ;
unsigned int i3 = indices [ 2 ] ;
float r1 = ratios [ 0 ] ;
float r2 = ratios [ 1 ] ;
float r3 = ratios [ 2 ] ;
osg : : Array * texcoords = ( geometry - > getNumTexCoordArrays ( ) > 0 ) ? geometry - > getTexCoordArray ( 0 ) : 0 ;
osg : : Vec2Array * texcoords_Vec2Array = dynamic_cast < osg : : Vec2Array * > ( texcoords ) ;
if ( texcoords_Vec2Array )
{
2009-02-08 23:56:35 +08:00
// we have tex coord array so now we can compute the final tex coord at the point of intersection.
2007-05-08 20:18:57 +08:00
osg : : Vec2 tc1 = ( * texcoords_Vec2Array ) [ i1 ] ;
osg : : Vec2 tc2 = ( * texcoords_Vec2Array ) [ i2 ] ;
osg : : Vec2 tc3 = ( * texcoords_Vec2Array ) [ i3 ] ;
osg : : Vec2 tc = tc1 * r1 + tc2 * r2 + tc3 * r3 ;
osg : : notify ( osg : : NOTICE ) < < " We hit tex coords " < < tc < < std : : endl ;
}
}
else
{
osg : : notify ( osg : : NOTICE ) < < " Intersection has insufficient indices to work with " ;
2006-01-13 06:43:36 +08:00
}
2007-01-10 01:35:46 +08:00
2007-05-08 20:18:57 +08:00
}
}
else
{
osg : : notify ( osg : : NOTICE ) < < " No intersection " < < std : : endl ;
2007-01-10 01:35:46 +08:00
}
2006-01-13 06:43:36 +08:00
}
break ;
}
2004-03-13 05:04:20 +08:00
case ( osgGA : : GUIEventAdapter : : KEYDOWN ) :
{
2007-12-07 01:53:22 +08:00
if ( ea . getKey ( ) = = ' p ' )
2004-03-13 05:04:20 +08:00
{
for ( ImageStreamList : : iterator itr = _imageStreamList . begin ( ) ;
itr ! = _imageStreamList . end ( ) ;
+ + itr )
{
2009-11-20 18:54:39 +08:00
osg : : ImageStream : : StreamStatus playToggle = ( * itr ) - > getStatus ( ) ;
if ( playToggle ! = osg : : ImageStream : : PLAYING )
2007-12-07 01:53:22 +08:00
{
2009-11-20 18:54:39 +08:00
std : : cout < < ( * itr ) . get ( ) < < " Play " < < std : : endl ;
2007-12-07 01:53:22 +08:00
( * itr ) - > play ( ) ;
}
else
{
// playing, so pause
2009-11-20 18:54:39 +08:00
std : : cout < < ( * itr ) . get ( ) < < " Pause " < < std : : endl ;
2007-12-07 01:53:22 +08:00
( * itr ) - > pause ( ) ;
}
2007-11-05 00:08:05 +08:00
}
return true ;
}
2004-03-13 05:04:20 +08:00
else if ( ea . getKey ( ) = = ' r ' )
{
2005-09-12 03:05:30 +08:00
for ( ImageStreamList : : iterator itr = _imageStreamList . begin ( ) ;
itr ! = _imageStreamList . end ( ) ;
+ + itr )
{
2009-11-20 18:54:39 +08:00
std : : cout < < ( * itr ) . get ( ) < < " Restart " < < std : : endl ;
2005-09-12 03:05:30 +08:00
( * itr ) - > rewind ( ) ;
( * itr ) - > play ( ) ;
}
2004-03-13 05:04:20 +08:00
return true ;
}
2009-11-20 22:31:11 +08:00
else if ( ea . getKey ( ) = = ' > ' )
{
for ( ImageStreamList : : iterator itr = _imageStreamList . begin ( ) ;
itr ! = _imageStreamList . end ( ) ;
+ + itr )
{
std : : cout < < " Seeking " < < std : : endl ;
if ( _seekIncr > 3 ) _seekIncr = 0 ;
double length = ( * itr ) - > getLength ( ) ;
double t_pos = ( length / 4.0f ) * _seekIncr ;
//(*itr)->rewind();
( * itr ) - > seek ( t_pos ) ;
( * itr ) - > play ( ) ;
_seekIncr + + ;
}
return true ;
}
2007-12-07 01:53:22 +08:00
else if ( ea . getKey ( ) = = ' L ' )
2004-03-13 05:04:20 +08:00
{
2005-09-12 03:05:30 +08:00
for ( ImageStreamList : : iterator itr = _imageStreamList . begin ( ) ;
itr ! = _imageStreamList . end ( ) ;
+ + itr )
{
if ( ( * itr ) - > getLoopingMode ( ) = = osg : : ImageStream : : LOOPING )
{
2009-11-20 18:54:39 +08:00
std : : cout < < ( * itr ) . get ( ) < < " Toggle Looping Off " < < std : : endl ;
2005-09-12 03:05:30 +08:00
( * itr ) - > setLoopingMode ( osg : : ImageStream : : NO_LOOPING ) ;
}
else
{
2009-11-20 18:54:39 +08:00
std : : cout < < ( * itr ) . get ( ) < < " Toggle Looping On " < < std : : endl ;
2005-09-12 03:05:30 +08:00
( * itr ) - > setLoopingMode ( osg : : ImageStream : : LOOPING ) ;
}
}
2004-03-13 05:04:20 +08:00
return true ;
}
2009-11-20 18:54:39 +08:00
else if ( ea . getKey ( ) = = ' + ' )
{
for ( ImageStreamList : : iterator itr = _imageStreamList . begin ( ) ;
itr ! = _imageStreamList . end ( ) ;
+ + itr )
{
double tm = ( * itr ) - > getTimeMultiplier ( ) ;
tm + = 0.1 ;
( * itr ) - > setTimeMultiplier ( tm ) ;
std : : cout < < ( * itr ) . get ( ) < < " Increase speed rate " < < ( * itr ) - > getTimeMultiplier ( ) < < std : : endl ;
}
return true ;
}
else if ( ea . getKey ( ) = = ' - ' )
{
for ( ImageStreamList : : iterator itr = _imageStreamList . begin ( ) ;
itr ! = _imageStreamList . end ( ) ;
+ + itr )
{
double tm = ( * itr ) - > getTimeMultiplier ( ) ;
tm - = 0.1 ;
( * itr ) - > setTimeMultiplier ( tm ) ;
std : : cout < < ( * itr ) . get ( ) < < " Decrease speed rate " < < ( * itr ) - > getTimeMultiplier ( ) < < std : : endl ;
}
return true ;
}
else if ( ea . getKey ( ) = = ' o ' )
{
for ( ImageStreamList : : iterator itr = _imageStreamList . begin ( ) ;
itr ! = _imageStreamList . end ( ) ;
+ + itr )
{
std : : cout < < ( * itr ) . get ( ) < < " Frame rate " < < ( * itr ) - > getFrameRate ( ) < < std : : endl ;
}
return true ;
}
2004-03-13 05:04:20 +08:00
return false ;
}
default :
return false ;
}
2006-01-13 06:43:36 +08:00
return false ;
2004-03-13 05:04:20 +08:00
}
void MovieEventHandler : : getUsage ( osg : : ApplicationUsage & usage ) const
{
2007-12-07 01:53:22 +08:00
usage . addKeyboardMouseBinding ( " p " , " Play/Pause movie " ) ;
2005-09-12 03:05:30 +08:00
usage . addKeyboardMouseBinding ( " r " , " Restart movie " ) ;
2004-03-13 05:04:20 +08:00
usage . addKeyboardMouseBinding ( " l " , " Toggle looping of movie " ) ;
2009-11-20 18:54:39 +08:00
usage . addKeyboardMouseBinding ( " + " , " Increase speed of movie " ) ;
usage . addKeyboardMouseBinding ( " - " , " Decrease speed of movie " ) ;
usage . addKeyboardMouseBinding ( " o " , " Display frame rate of movie " ) ;
2009-11-20 22:31:11 +08:00
usage . addKeyboardMouseBinding ( " > " , " Advance the movie using seek " ) ;
2004-03-13 05:04:20 +08:00
}
2007-09-26 01:01:16 +08:00
osg : : Geometry * myCreateTexturedQuadGeometry ( const osg : : Vec3 & pos , float width , float height , osg : : Image * image , bool useTextureRectangle , bool xyPlane , bool option_flip )
2004-01-30 22:06:29 +08:00
{
2007-06-13 00:55:44 +08:00
bool flip = image - > getOrigin ( ) = = osg : : Image : : TOP_LEFT ;
2007-09-26 01:01:16 +08:00
if ( option_flip ) flip = ! flip ;
2009-02-08 23:56:35 +08:00
2004-03-13 04:23:55 +08:00
if ( useTextureRectangle )
{
2005-11-11 20:45:59 +08:00
osg : : Geometry * pictureQuad = osg : : createTexturedQuadGeometry ( pos ,
2004-03-13 04:23:55 +08:00
osg : : Vec3 ( width , 0.0f , 0.0f ) ,
2007-09-25 23:01:11 +08:00
xyPlane ? osg : : Vec3 ( 0.0f , height , 0.0f ) : osg : : Vec3 ( 0.0f , 0.0f , height ) ,
2007-06-13 00:55:44 +08:00
0.0f , flip ? image - > t ( ) : 0.0 , image - > s ( ) , flip ? 0.0 : image - > t ( ) ) ;
2005-06-08 23:57:05 +08:00
2007-06-12 22:20:16 +08:00
osg : : TextureRectangle * texture = new osg : : TextureRectangle ( image ) ;
texture - > setWrap ( osg : : Texture : : WRAP_S , osg : : Texture : : CLAMP_TO_EDGE ) ;
texture - > setWrap ( osg : : Texture : : WRAP_T , osg : : Texture : : CLAMP_TO_EDGE ) ;
2009-02-08 23:56:35 +08:00
2004-03-13 04:23:55 +08:00
pictureQuad - > getOrCreateStateSet ( ) - > setTextureAttributeAndModes ( 0 ,
2007-06-12 22:20:16 +08:00
texture ,
osg : : StateAttribute : : ON ) ;
2009-02-08 23:56:35 +08:00
2004-03-13 04:23:55 +08:00
return pictureQuad ;
}
else
{
2005-11-11 20:45:59 +08:00
osg : : Geometry * pictureQuad = osg : : createTexturedQuadGeometry ( pos ,
2004-03-13 04:23:55 +08:00
osg : : Vec3 ( width , 0.0f , 0.0f ) ,
2007-09-25 23:01:11 +08:00
xyPlane ? osg : : Vec3 ( 0.0f , height , 0.0f ) : osg : : Vec3 ( 0.0f , 0.0f , height ) ,
2007-06-13 00:55:44 +08:00
0.0f , flip ? 1.0f : 0.0f , 1.0f , flip ? 0.0f : 1.0f ) ;
2009-02-08 23:56:35 +08:00
2005-06-08 23:57:05 +08:00
osg : : Texture2D * texture = new osg : : Texture2D ( image ) ;
2008-09-16 17:31:29 +08:00
texture - > setResizeNonPowerOfTwoHint ( false ) ;
2007-06-12 22:20:16 +08:00
texture - > setFilter ( osg : : Texture : : MIN_FILTER , osg : : Texture : : LINEAR ) ;
texture - > setWrap ( osg : : Texture : : WRAP_S , osg : : Texture : : CLAMP_TO_EDGE ) ;
texture - > setWrap ( osg : : Texture : : WRAP_T , osg : : Texture : : CLAMP_TO_EDGE ) ;
2009-02-08 23:56:35 +08:00
2004-03-13 04:23:55 +08:00
pictureQuad - > getOrCreateStateSet ( ) - > setTextureAttributeAndModes ( 0 ,
2005-06-08 23:57:05 +08:00
texture ,
2004-03-13 04:23:55 +08:00
osg : : StateAttribute : : ON ) ;
return pictureQuad ;
2004-01-30 22:06:29 +08:00
}
}
2014-12-18 03:21:32 +08:00
# if USE_SDL || USE_SDL2
2009-03-04 01:37:48 +08:00
class SDLAudioSink : public osg : : AudioSink
2009-03-04 00:51:01 +08:00
{
public :
2009-05-12 21:24:08 +08:00
2009-03-04 01:37:48 +08:00
SDLAudioSink ( osg : : AudioStream * audioStream ) :
2010-01-07 22:35:17 +08:00
_started ( false ) ,
_paused ( false ) ,
2009-03-04 00:51:01 +08:00
_audioStream ( audioStream ) { }
2009-03-04 01:37:48 +08:00
2009-05-12 21:24:08 +08:00
~ SDLAudioSink ( ) ;
2009-03-04 01:37:48 +08:00
2010-01-07 22:35:17 +08:00
virtual void play ( ) ;
virtual void pause ( ) ;
virtual void stop ( ) ;
2009-03-04 00:51:01 +08:00
2010-01-07 22:35:17 +08:00
virtual bool playing ( ) const { return _started & & ! _paused ; }
2009-03-04 01:37:48 +08:00
2010-01-07 22:35:17 +08:00
bool _started ;
bool _paused ;
2009-03-04 00:51:01 +08:00
osg : : observer_ptr < osg : : AudioStream > _audioStream ;
} ;
2009-05-12 20:27:54 +08:00
# endif
2004-01-30 22:06:29 +08:00
int main ( int argc , char * * argv )
{
// use an ArgumentParser object to manage the program arguments.
osg : : ArgumentParser arguments ( & argc , argv ) ;
2009-02-08 23:56:35 +08:00
2004-01-30 22:06:29 +08:00
// set up the usage document, in case we need to print out how to use this program.
arguments . getApplicationUsage ( ) - > setApplicationName ( arguments . getApplicationName ( ) ) ;
2005-11-16 06:07:54 +08:00
arguments . getApplicationUsage ( ) - > setDescription ( arguments . getApplicationName ( ) + " example demonstrates the use of ImageStream for rendering movies as textures. " ) ;
2004-01-30 22:06:29 +08:00
arguments . getApplicationUsage ( ) - > setCommandLineUsage ( arguments . getApplicationName ( ) + " [options] filename ... " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " -h or --help " , " Display this information " ) ;
2007-01-08 18:00:16 +08:00
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --texture2D " , " Use Texture2D rather than TextureRectangle. " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --shader " , " Use shaders to post process the video. " ) ;
2007-09-26 01:02:22 +08:00
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --interactive " , " Use camera manipulator to allow movement around movie. " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --flip " , " Flip the movie so top becomes bottom. " ) ;
2009-02-08 23:56:35 +08:00
# if defined(WIN32) || defined(__APPLE__)
2007-11-17 19:50:13 +08:00
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --devices " , " Print the Video input capability via QuickTime and exit. " ) ;
2009-02-08 23:56:35 +08:00
# endif
2005-06-08 23:57:05 +08:00
bool useTextureRectangle = true ;
bool useShader = false ;
2004-01-30 22:06:29 +08:00
// construct the viewer.
2007-09-25 21:51:03 +08:00
osgViewer : : Viewer viewer ( arguments ) ;
2009-02-08 23:56:35 +08:00
2007-05-08 23:32:35 +08:00
if ( arguments . argc ( ) < = 1 )
{
arguments . getApplicationUsage ( ) - > write ( std : : cout , osg : : ApplicationUsage : : COMMAND_LINE_OPTION ) ;
return 1 ;
2007-11-17 19:50:13 +08:00
}
2009-02-08 23:56:35 +08:00
# if defined(WIN32) || defined(__APPLE__)
2007-11-17 19:50:13 +08:00
// if user requests devices video capability.
if ( arguments . read ( " -devices " ) | | arguments . read ( " --devices " ) )
{
// Force load QuickTime plugin, probe video capability, exit
osgDB : : readImageFile ( " devices.live " ) ;
return 1 ;
2007-05-08 23:32:35 +08:00
}
2009-02-08 23:56:35 +08:00
# endif
2007-05-08 23:32:35 +08:00
2005-06-08 23:57:05 +08:00
while ( arguments . read ( " --texture2D " ) ) useTextureRectangle = false ;
while ( arguments . read ( " --shader " ) ) useShader = true ;
2004-01-30 22:06:29 +08:00
2008-04-11 18:10:40 +08:00
bool mouseTracking = false ;
2009-02-08 23:56:35 +08:00
while ( arguments . read ( " --mouse " ) ) mouseTracking = true ;
2008-04-11 18:10:40 +08:00
2004-01-30 22:06:29 +08:00
// if user request help write it out to cout.
if ( arguments . read ( " -h " ) | | arguments . read ( " --help " ) )
{
arguments . getApplicationUsage ( ) - > write ( std : : cout ) ;
return 1 ;
}
2007-09-25 23:01:11 +08:00
bool fullscreen = ! arguments . read ( " --interactive " ) ;
2007-09-26 01:01:16 +08:00
bool flip = arguments . read ( " --flip " ) ;
2005-06-08 23:57:05 +08:00
2007-09-25 23:01:11 +08:00
osg : : ref_ptr < osg : : Geode > geode = new osg : : Geode ;
2007-05-08 23:32:35 +08:00
2007-09-25 23:01:11 +08:00
osg : : StateSet * stateset = geode - > getOrCreateStateSet ( ) ;
stateset - > setMode ( GL_LIGHTING , osg : : StateAttribute : : OFF ) ;
2007-05-08 23:32:35 +08:00
2007-09-25 23:01:11 +08:00
if ( useShader )
{
//useTextureRectangle = false;
static const char * shaderSourceTextureRec = {
" uniform vec4 cutoff_color; \n "
" uniform samplerRect movie_texture; \n "
" void main(void) \n "
" { \n "
2008-09-16 17:31:29 +08:00
" vec4 texture_color = textureRect(movie_texture, gl_TexCoord[0].st); \n "
2007-09-25 23:01:11 +08:00
" if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n "
" gl_FragColor = texture_color; \n "
" } \n "
} ;
static const char * shaderSourceTexture2D = {
" uniform vec4 cutoff_color; \n "
" uniform sampler2D movie_texture; \n "
" void main(void) \n "
" { \n "
2008-09-16 17:31:29 +08:00
" vec4 texture_color = texture2D(movie_texture, gl_TexCoord[0].st); \n "
2007-09-25 23:01:11 +08:00
" if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n "
" gl_FragColor = texture_color; \n "
" } \n "
} ;
osg : : Program * program = new osg : : Program ;
program - > addShader ( new osg : : Shader ( osg : : Shader : : FRAGMENT ,
useTextureRectangle ? shaderSourceTextureRec : shaderSourceTexture2D ) ) ;
stateset - > addUniform ( new osg : : Uniform ( " cutoff_color " , osg : : Vec4 ( 0.1f , 0.1f , 0.1f , 1.0f ) ) ) ;
stateset - > addUniform ( new osg : : Uniform ( " movie_texture " , 0 ) ) ;
stateset - > setAttribute ( program ) ;
2007-05-08 23:32:35 +08:00
2007-09-25 23:01:11 +08:00
}
2007-05-08 23:32:35 +08:00
2007-09-25 23:01:11 +08:00
osg : : Vec3 pos ( 0.0f , 0.0f , 0.0f ) ;
osg : : Vec3 topleft = pos ;
osg : : Vec3 bottomright = pos ;
2009-02-08 23:56:35 +08:00
2007-09-25 23:01:11 +08:00
bool xyPlane = fullscreen ;
2014-12-18 03:21:32 +08:00
2009-03-04 01:37:48 +08:00
bool useAudioSink = false ;
while ( arguments . read ( " --audio " ) ) { useAudioSink = true ; }
2014-12-18 03:21:32 +08:00
# if USE_SDL || USE_SDL2
2009-03-12 02:13:58 +08:00
unsigned int numAudioStreamsEnabled = 0 ;
2009-03-13 19:06:12 +08:00
# endif
2009-02-08 23:56:35 +08:00
2007-09-25 23:01:11 +08:00
for ( int i = 1 ; i < arguments . argc ( ) ; + + i )
{
if ( arguments . isString ( i ) )
2004-03-13 04:23:55 +08:00
{
2007-09-25 23:01:11 +08:00
osg : : Image * image = osgDB : : readImageFile ( arguments [ i ] ) ;
osg : : ImageStream * imagestream = dynamic_cast < osg : : ImageStream * > ( image ) ;
2014-12-18 03:21:32 +08:00
if ( imagestream )
2009-03-04 00:51:01 +08:00
{
osg : : ImageStream : : AudioStreams & audioStreams = imagestream - > getAudioStreams ( ) ;
2009-03-04 01:37:48 +08:00
if ( useAudioSink & & ! audioStreams . empty ( ) )
2009-03-04 00:51:01 +08:00
{
osg : : AudioStream * audioStream = audioStreams [ 0 ] . get ( ) ;
osg : : notify ( osg : : NOTICE ) < < " AudioStream read [ " < < audioStream - > getName ( ) < < " ] " < < std : : endl ;
2014-12-18 03:21:32 +08:00
# if USE_SDL || USE_SDL2
2009-03-12 02:13:58 +08:00
if ( numAudioStreamsEnabled = = 0 )
{
audioStream - > setAudioSink ( new SDLAudioSink ( audioStream ) ) ;
2014-12-18 03:21:32 +08:00
2009-03-12 02:13:58 +08:00
+ + numAudioStreamsEnabled ;
}
2009-03-04 01:37:48 +08:00
# endif
2009-03-04 00:51:01 +08:00
}
imagestream - > play ( ) ;
}
2007-05-08 20:18:57 +08:00
2007-09-25 23:01:11 +08:00
if ( image )
{
2009-03-11 23:43:13 +08:00
osg : : notify ( osg : : NOTICE ) < < " image->s() " < < image - > s ( ) < < " image-t()= " < < image - > t ( ) < < " aspectRatio= " < < image - > getPixelAspectRatio ( ) < < std : : endl ;
2009-02-08 23:56:35 +08:00
2009-03-12 01:57:33 +08:00
float width = image - > s ( ) * image - > getPixelAspectRatio ( ) ;
float height = image - > t ( ) ;
2009-02-08 23:56:35 +08:00
2009-03-11 23:43:13 +08:00
osg : : ref_ptr < osg : : Drawable > drawable = myCreateTexturedQuadGeometry ( pos , width , height , image , useTextureRectangle , xyPlane , flip ) ;
2014-12-18 03:21:32 +08:00
2009-03-11 23:43:13 +08:00
if ( image - > isImageTranslucent ( ) )
{
osg : : notify ( osg : : NOTICE ) < < " Transparent movie, enabling blending. " < < std : : endl ;
drawable - > getOrCreateStateSet ( ) - > setMode ( GL_BLEND , osg : : StateAttribute : : ON ) ;
drawable - > getOrCreateStateSet ( ) - > setRenderingHint ( osg : : StateSet : : TRANSPARENT_BIN ) ;
}
geode - > addDrawable ( drawable . get ( ) ) ;
bottomright = pos + osg : : Vec3 ( width , height , 0.0f ) ;
2007-05-08 20:18:57 +08:00
2009-03-11 23:43:13 +08:00
if ( xyPlane ) pos . y ( ) + = height * 1.05f ;
else pos . z ( ) + = height * 1.05f ;
2005-04-22 23:44:21 +08:00
}
2007-09-25 23:01:11 +08:00
else
{
std : : cout < < " Unable to read file " < < arguments [ i ] < < std : : endl ;
2009-02-08 23:56:35 +08:00
}
2005-04-22 23:44:21 +08:00
}
}
2009-02-08 23:56:35 +08:00
2007-09-25 23:01:11 +08:00
// set the scene to render
viewer . setSceneData ( geode . get ( ) ) ;
if ( viewer . getSceneData ( ) = = 0 )
{
arguments . getApplicationUsage ( ) - > write ( std : : cout ) ;
return 1 ;
}
2004-03-13 04:23:55 +08:00
2004-03-13 05:04:20 +08:00
// pass the model to the MovieEventHandler so it can pick out ImageStream's to manipulate.
2006-01-13 06:43:36 +08:00
MovieEventHandler * meh = new MovieEventHandler ( ) ;
2008-04-11 18:10:40 +08:00
meh - > setMouseTracking ( mouseTracking ) ;
2007-12-07 01:53:22 +08:00
meh - > set ( viewer . getSceneData ( ) ) ;
viewer . addEventHandler ( meh ) ;
viewer . addEventHandler ( new osgViewer : : StatsHandler ) ;
viewer . addEventHandler ( new osgGA : : StateSetManipulator ( viewer . getCamera ( ) - > getOrCreateStateSet ( ) ) ) ;
viewer . addEventHandler ( new osgViewer : : WindowSizeHandler ) ;
// add the record camera path handler
viewer . addEventHandler ( new osgViewer : : RecordCameraPathHandler ) ;
2004-03-13 04:23:55 +08:00
2007-12-11 01:30:18 +08:00
// report any errors if they have occurred when parsing the program arguments.
2004-01-30 22:06:29 +08:00
if ( arguments . errors ( ) )
{
arguments . writeErrorMessages ( std : : cout ) ;
return 1 ;
}
2007-09-25 23:01:11 +08:00
if ( fullscreen )
{
viewer . realize ( ) ;
2014-12-18 03:21:32 +08:00
2009-03-11 23:43:13 +08:00
viewer . getCamera ( ) - > setClearColor ( osg : : Vec4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
float screenAspectRatio = 1280.0f / 1024.0f ;
2007-05-08 20:18:57 +08:00
2009-03-11 23:43:13 +08:00
osg : : GraphicsContext : : WindowingSystemInterface * wsi = osg : : GraphicsContext : : getWindowingSystemInterface ( ) ;
2014-12-18 03:21:32 +08:00
if ( wsi )
2009-03-11 23:43:13 +08:00
{
unsigned int width , height ;
wsi - > getScreenResolution ( osg : : GraphicsContext : : ScreenIdentifier ( 0 ) , width , height ) ;
2014-12-18 03:21:32 +08:00
2009-03-11 23:43:13 +08:00
screenAspectRatio = float ( width ) / float ( height ) ;
}
2014-12-18 03:21:32 +08:00
2009-03-11 23:43:13 +08:00
float modelAspectRatio = ( bottomright . x ( ) - topleft . x ( ) ) / ( bottomright . y ( ) - topleft . y ( ) ) ;
2014-12-18 03:21:32 +08:00
2007-09-25 23:01:11 +08:00
viewer . getCamera ( ) - > setViewMatrix ( osg : : Matrix : : identity ( ) ) ;
2009-03-11 23:43:13 +08:00
osg : : Vec3 center = ( bottomright + topleft ) * 0.5f ;
osg : : Vec3 dx ( bottomright . x ( ) - center . x ( ) , 0.0f , 0.0f ) ;
osg : : Vec3 dy ( 0.0f , topleft . y ( ) - center . y ( ) , 0.0f ) ;
float ratio = modelAspectRatio / screenAspectRatio ;
if ( ratio > 1.0f )
{
// use model width as the control on model size.
bottomright = center + dx - dy * ratio ;
topleft = center - dx + dy * ratio ;
}
else
{
// use model height as the control on model size.
bottomright = center + dx / ratio - dy ;
topleft = center - dx / ratio + dy ;
}
2007-09-25 23:01:11 +08:00
viewer . getCamera ( ) - > setProjectionMatrixAsOrtho2D ( topleft . x ( ) , bottomright . x ( ) , topleft . y ( ) , bottomright . y ( ) ) ;
2004-01-30 22:06:29 +08:00
2007-09-25 23:01:11 +08:00
while ( ! viewer . done ( ) )
{
viewer . frame ( ) ;
}
return 0 ;
}
else
{
// create the windows and run the threads.
return viewer . run ( ) ;
}
2004-01-30 22:06:29 +08:00
}
2009-02-08 23:56:35 +08:00
2014-12-18 03:21:32 +08:00
# if USE_SDL || USE_SDL2
2009-05-12 21:24:08 +08:00
# include "SDL.h"
2009-05-13 16:40:10 +08:00
static void soundReadCallback ( void * user_data , uint8_t * data , int datalen )
{
SDLAudioSink * sink = reinterpret_cast < SDLAudioSink * > ( user_data ) ;
osg : : ref_ptr < osg : : AudioStream > as = sink - > _audioStream . get ( ) ;
if ( as . valid ( ) )
{
as - > consumeAudioBuffer ( data , datalen ) ;
}
}
2009-05-12 21:24:08 +08:00
SDLAudioSink : : ~ SDLAudioSink ( )
{
2010-01-07 22:35:17 +08:00
stop ( ) ;
2009-05-12 21:24:08 +08:00
}
2010-01-07 22:35:17 +08:00
void SDLAudioSink : : play ( )
2009-05-12 21:24:08 +08:00
{
2010-01-07 22:35:17 +08:00
if ( _started )
{
if ( _paused )
{
SDL_PauseAudio ( 0 ) ;
_paused = false ;
}
return ;
}
_started = true ;
_paused = false ;
2009-05-12 21:24:08 +08:00
osg : : notify ( osg : : NOTICE ) < < " SDLAudioSink()::startPlaying() " < < std : : endl ;
osg : : notify ( osg : : NOTICE ) < < " audioFrequency()= " < < _audioStream - > audioFrequency ( ) < < std : : endl ;
osg : : notify ( osg : : NOTICE ) < < " audioNbChannels()= " < < _audioStream - > audioNbChannels ( ) < < std : : endl ;
osg : : notify ( osg : : NOTICE ) < < " audioSampleFormat()= " < < _audioStream - > audioSampleFormat ( ) < < std : : endl ;
2014-12-18 03:21:32 +08:00
2009-05-12 21:24:08 +08:00
SDL_AudioSpec specs = { 0 } ;
SDL_AudioSpec wanted_specs = { 0 } ;
wanted_specs . freq = _audioStream - > audioFrequency ( ) ;
wanted_specs . channels = _audioStream - > audioNbChannels ( ) ;
wanted_specs . silence = 0 ;
wanted_specs . samples = 1024 ;
wanted_specs . callback = soundReadCallback ;
wanted_specs . userdata = this ;
2014-12-18 03:21:32 +08:00
wanted_specs . format = 0 ;
2009-05-12 21:24:08 +08:00
2014-12-18 03:21:32 +08:00
# if SDL_MAJOR_VERSION>=2
switch ( _audioStream - > audioSampleFormat ( ) )
{
case ( osg : : AudioStream : : SAMPLE_FORMAT_U8 ) : { wanted_specs . format = AUDIO_U8 ; OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_U8 " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_S16 ) : { wanted_specs . format = AUDIO_S16SYS ; OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_S16 " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_S24 ) : { OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_S24 NOT Supported by SDL. " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_S32 ) : { wanted_specs . format = AUDIO_S32SYS ; OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_S32 " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_F32 ) : { wanted_specs . format = AUDIO_F32SYS ; OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_F32 " < < std : : endl ; break ; }
}
# else
switch ( _audioStream - > audioSampleFormat ( ) )
{
case ( osg : : AudioStream : : SAMPLE_FORMAT_U8 ) : { wanted_specs . format = AUDIO_U8 ; OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_U8 " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_S16 ) : { wanted_specs . format = AUDIO_S16SYS ; OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_S16 " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_S24 ) : { OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_S24 NOT Supported by SDL1.x. " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_S32 ) : { OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_S32 NOT Supported by SDL1.x " < < std : : endl ; break ; }
case ( osg : : AudioStream : : SAMPLE_FORMAT_F32 ) : { OSG_NOTICE < < " SampleFormat = SAMPLE_FORMAT_F32 NOT Supported by SDL1.x " < < std : : endl ; break ; }
}
# endif
2009-05-12 21:24:08 +08:00
2014-12-18 03:21:32 +08:00
if ( wanted_specs . format ! = 0 )
{
2009-05-12 21:24:08 +08:00
2014-12-18 03:21:32 +08:00
if ( SDL_OpenAudio ( & wanted_specs , & specs ) < 0 )
throw " SDL_OpenAudio() failed ( " + std : : string ( SDL_GetError ( ) ) + " ) " ;
SDL_PauseAudio ( 0 ) ;
}
else
{
throw " SDL_OpenAudio() does not support audio format " ;
}
2009-05-12 21:24:08 +08:00
}
2010-01-07 22:35:17 +08:00
void SDLAudioSink : : pause ( )
{
if ( _started )
{
SDL_PauseAudio ( 1 ) ;
_paused = true ;
}
}
void SDLAudioSink : : stop ( )
{
if ( _started )
{
if ( ! _paused ) SDL_PauseAudio ( 1 ) ;
SDL_CloseAudio ( ) ;
osg : : notify ( osg : : NOTICE ) < < " ~SDLAudioSink() destructor, but still playing " < < std : : endl ;
}
}
2009-05-12 21:24:08 +08:00
# endif