diff --git a/examples/osgmovie/CMakeLists.txt b/examples/osgmovie/CMakeLists.txt index c0cdafbef..157bac8a1 100644 --- a/examples/osgmovie/CMakeLists.txt +++ b/examples/osgmovie/CMakeLists.txt @@ -1,9 +1,19 @@ # INCLUDE_DIRECTORIES( ${OPENAL_INCLUDE_DIR} ) +# SET(TARGET_EXTERNAL_LIBRARIES ${OPENAL_LIBRARY} alut) + +IF (SDL_FOUND) + SET(TARGET_EXTERNAL_LIBRARIES ${SDL_LIBRARY} ) + INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR} ) + ADD_DEFINITIONS(-DUSE_SDL) + IF (MINGW) + SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} winmm dinput ddraw dxguid) + ENDIF() +ENDIF(SDL_FOUND) SET(TARGET_SRC osgmovie.cpp ) SET(TARGET_ADDED_LIBRARIES osgGA ) -# SET(TARGET_EXTERNAL_LIBRARIES ${OPENAL_LIBRARY} alut) + #### end var setup ### SETUP_EXAMPLE(osgmovie) diff --git a/examples/osgmovie/osgmovie.cpp b/examples/osgmovie/osgmovie.cpp index 3bea15fc1..23961e776 100644 --- a/examples/osgmovie/osgmovie.cpp +++ b/examples/osgmovie/osgmovie.cpp @@ -43,7 +43,7 @@ class MovieEventHandler : public osgGA::GUIEventHandler { public: - MovieEventHandler():_playToggle(true),_trackMouse(false) {} + MovieEventHandler():_trackMouse(false) {} void setMouseTracking(bool track) { _trackMouse = track; } bool getMouseTracking() const { return _trackMouse; } @@ -116,7 +116,6 @@ protected: }; - bool _playToggle; bool _trackMouse; ImageStreamList _imageStreamList; unsigned int _seekIncr; @@ -212,17 +211,16 @@ bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIAction itr!=_imageStreamList.end(); ++itr) { - _playToggle = !_playToggle; - if ( _playToggle ) + osg::ImageStream::StreamStatus playToggle = (*itr)->getStatus(); + if (playToggle != osg::ImageStream::PLAYING) { - // playing, so pause - std::cout<<"Play"<play(); } else { // playing, so pause - std::cout<<"Pause"<pause(); } } @@ -234,7 +232,7 @@ bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIAction itr!=_imageStreamList.end(); ++itr) { - std::cout<<"Restart"<rewind(); (*itr)->play(); } @@ -265,17 +263,53 @@ bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIAction { if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING) { - std::cout<<"Toggle Looping Off"<setLoopingMode( osg::ImageStream::NO_LOOPING ); } else { - std::cout<<"Toggle Looping On"<setLoopingMode( osg::ImageStream::LOOPING ); } } 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() << " 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() <","Advance the movie using seek"); } @@ -339,26 +376,24 @@ osg::Geometry* myCreateTexturedQuadGeometry(const osg::Vec3& pos,float width,flo } } -class CustomAudioSink : public osg::AudioSink +#if USE_SDL + +class SDLAudioSink : public osg::AudioSink { public: - - CustomAudioSink(osg::AudioStream* audioStream): + + SDLAudioSink(osg::AudioStream* audioStream): _started(false), _paused(false), _audioStream(audioStream) {} - - virtual void startPlaying() - { - _playing = true; - osg::notify(osg::NOTICE)<<"CustomAudioSink()::startPlaying()"<getAudioStreams(); - if (useOpenALAudio && !audioStreams.empty()) + if (useAudioSink && !audioStreams.empty()) { osg::AudioStream* audioStream = audioStreams[0].get(); osg::notify(osg::NOTICE)<<"AudioStream read ["<getName()<<"]"<setAudioSink(new CustomAudioSink(audioStream)); +#if USE_SDL + if (numAudioStreamsEnabled==0) + { + audioStream->setAudioSink(new SDLAudioSink(audioStream)); + + ++numAudioStreamsEnabled; + } +#endif } @@ -497,14 +545,27 @@ int main(int argc, char** argv) if (image) { - osg::notify(osg::NOTICE)<<"image->s()"<s()<<" image-t()="<t()<s()"<s()<<" image-t()="<t()<<" aspectRatio="<getPixelAspectRatio()<addDrawable(myCreateTexturedQuadGeometry(pos,image->s(),image->t(),image, useTextureRectangle, xyPlane, flip)); + float width = image->s() * image->getPixelAspectRatio(); + float height = image->t(); - bottomright = pos + osg::Vec3(static_cast(image->s()),static_cast(image->t()),0.0f); + osg::ref_ptr drawable = myCreateTexturedQuadGeometry(pos, width, height,image, useTextureRectangle, xyPlane, flip); + + if (image->isImageTranslucent()) + { + osg::notify(osg::NOTICE)<<"Transparent movie, enabling blending."<t()*1.05f; - else pos.z() += image->t()*1.05f; + 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); + + if (xyPlane) pos.y() += height*1.05f; + else pos.z() += height*1.05f; } else { @@ -545,8 +606,44 @@ int main(int argc, char** argv) if (fullscreen) { viewer.realize(); + + viewer.getCamera()->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); + float screenAspectRatio = 1280.0f/1024.0f; + + osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); + if (wsi) + { + unsigned int width, height; + wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); + + screenAspectRatio = float(width) / float(height); + } + + float modelAspectRatio = (bottomright.x()-topleft.x())/(bottomright.y()-topleft.y()); + viewer.getCamera()->setViewMatrix(osg::Matrix::identity()); + + + 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; + } + viewer.getCamera()->setProjectionMatrixAsOrtho2D(topleft.x(),bottomright.x(),topleft.y(),bottomright.y()); while(!viewer.done()) @@ -562,3 +659,83 @@ int main(int argc, char** argv) } } +#if USE_SDL + +#include "SDL.h" + +static void soundReadCallback(void * user_data, uint8_t * data, int datalen) +{ + SDLAudioSink * sink = reinterpret_cast(user_data); + osg::ref_ptr as = sink->_audioStream.get(); + if (as.valid()) + { + as->consumeAudioBuffer(data, datalen); + } +} + +SDLAudioSink::~SDLAudioSink() +{ + stop(); +} + +void SDLAudioSink::play() +{ + if (_started) + { + if (_paused) + { + SDL_PauseAudio(0); + _paused = false; + } + return; + } + + _started = true; + _paused = false; + + osg::notify(osg::NOTICE)<<"SDLAudioSink()::startPlaying()"<