| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgViewer/Viewer> |
|---|
| 20 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 21 | |
|---|
| 22 | #include <osgDB/ReadFile> |
|---|
| 23 | |
|---|
| 24 | #include <osg/Geode> |
|---|
| 25 | #include <osg/Geometry> |
|---|
| 26 | #include <osg/StateSet> |
|---|
| 27 | #include <osg/Material> |
|---|
| 28 | #include <osg/Texture2D> |
|---|
| 29 | #include <osg/TextureRectangle> |
|---|
| 30 | #include <osg/TextureCubeMap> |
|---|
| 31 | #include <osg/TexMat> |
|---|
| 32 | #include <osg/CullFace> |
|---|
| 33 | #include <osg/ImageStream> |
|---|
| 34 | #include <osg/io_utils> |
|---|
| 35 | |
|---|
| 36 | #include <osgGA/TrackballManipulator> |
|---|
| 37 | #include <osgGA/StateSetManipulator> |
|---|
| 38 | #include <osgGA/EventVisitor> |
|---|
| 39 | |
|---|
| 40 | #include <iostream> |
|---|
| 41 | |
|---|
| 42 | class MovieEventHandler : public osgGA::GUIEventHandler |
|---|
| 43 | { |
|---|
| 44 | public: |
|---|
| 45 | |
|---|
| 46 | MovieEventHandler():_playToggle(true),_trackMouse(false) {} |
|---|
| 47 | |
|---|
| 48 | void setMouseTracking(bool track) { _trackMouse = track; } |
|---|
| 49 | bool getMouseTracking() const { return _trackMouse; } |
|---|
| 50 | |
|---|
| 51 | void set(osg::Node* node); |
|---|
| 52 | |
|---|
| 53 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv); |
|---|
| 54 | |
|---|
| 55 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 56 | |
|---|
| 57 | typedef std::vector< osg::observer_ptr<osg::ImageStream> > ImageStreamList; |
|---|
| 58 | |
|---|
| 59 | protected: |
|---|
| 60 | |
|---|
| 61 | virtual ~MovieEventHandler() {} |
|---|
| 62 | |
|---|
| 63 | class FindImageStreamsVisitor : public osg::NodeVisitor |
|---|
| 64 | { |
|---|
| 65 | public: |
|---|
| 66 | FindImageStreamsVisitor(ImageStreamList& imageStreamList): |
|---|
| 67 | _imageStreamList(imageStreamList) {} |
|---|
| 68 | |
|---|
| 69 | virtual void apply(osg::Geode& geode) |
|---|
| 70 | { |
|---|
| 71 | apply(geode.getStateSet()); |
|---|
| 72 | |
|---|
| 73 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 74 | { |
|---|
| 75 | apply(geode.getDrawable(i)->getStateSet()); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | traverse(geode); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | virtual void apply(osg::Node& node) |
|---|
| 82 | { |
|---|
| 83 | apply(node.getStateSet()); |
|---|
| 84 | traverse(node); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | inline void apply(osg::StateSet* stateset) |
|---|
| 88 | { |
|---|
| 89 | if (!stateset) return; |
|---|
| 90 | |
|---|
| 91 | osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE); |
|---|
| 92 | if (attr) |
|---|
| 93 | { |
|---|
| 94 | osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr); |
|---|
| 95 | if (texture2D) apply(dynamic_cast<osg::ImageStream*>(texture2D->getImage())); |
|---|
| 96 | |
|---|
| 97 | osg::TextureRectangle* textureRec = dynamic_cast<osg::TextureRectangle*>(attr); |
|---|
| 98 | if (textureRec) apply(dynamic_cast<osg::ImageStream*>(textureRec->getImage())); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | inline void apply(osg::ImageStream* imagestream) |
|---|
| 103 | { |
|---|
| 104 | if (imagestream) |
|---|
| 105 | { |
|---|
| 106 | _imageStreamList.push_back(imagestream); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | ImageStreamList& _imageStreamList; |
|---|
| 111 | |
|---|
| 112 | protected: |
|---|
| 113 | |
|---|
| 114 | FindImageStreamsVisitor& operator = (const FindImageStreamsVisitor&) { return *this; } |
|---|
| 115 | |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | bool _playToggle; |
|---|
| 120 | bool _trackMouse; |
|---|
| 121 | ImageStreamList _imageStreamList; |
|---|
| 122 | |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | void MovieEventHandler::set(osg::Node* node) |
|---|
| 128 | { |
|---|
| 129 | _imageStreamList.clear(); |
|---|
| 130 | if (node) |
|---|
| 131 | { |
|---|
| 132 | FindImageStreamsVisitor fisv(_imageStreamList); |
|---|
| 133 | node->accept(fisv); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv) |
|---|
| 139 | { |
|---|
| 140 | switch(ea.getEventType()) |
|---|
| 141 | { |
|---|
| 142 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 143 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 144 | case(osgGA::GUIEventAdapter::RELEASE): |
|---|
| 145 | { |
|---|
| 146 | if (_trackMouse) |
|---|
| 147 | { |
|---|
| 148 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
|---|
| 149 | osgUtil::LineSegmentIntersector::Intersections intersections; |
|---|
| 150 | bool foundIntersection = view==0 ? false : |
|---|
| 151 | (nv==0 ? view->computeIntersections(ea.getX(), ea.getY(), intersections) : |
|---|
| 152 | view->computeIntersections(ea.getX(), ea.getY(), nv->getNodePath(), intersections)); |
|---|
| 153 | |
|---|
| 154 | if (foundIntersection) |
|---|
| 155 | { |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | const osgUtil::LineSegmentIntersector::Intersection& intersection = *(intersections.begin()); |
|---|
| 159 | osg::Drawable* drawable = intersection.drawable.get(); |
|---|
| 160 | osg::Geometry* geometry = drawable ? drawable->asGeometry() : 0; |
|---|
| 161 | osg::Vec3Array* vertices = geometry ? dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()) : 0; |
|---|
| 162 | if (vertices) |
|---|
| 163 | { |
|---|
| 164 | |
|---|
| 165 | const osgUtil::LineSegmentIntersector::Intersection::IndexList& indices = intersection.indexList; |
|---|
| 166 | const osgUtil::LineSegmentIntersector::Intersection::RatioList& ratios = intersection.ratioList; |
|---|
| 167 | |
|---|
| 168 | if (indices.size()==3 && ratios.size()==3) |
|---|
| 169 | { |
|---|
| 170 | unsigned int i1 = indices[0]; |
|---|
| 171 | unsigned int i2 = indices[1]; |
|---|
| 172 | unsigned int i3 = indices[2]; |
|---|
| 173 | |
|---|
| 174 | float r1 = ratios[0]; |
|---|
| 175 | float r2 = ratios[1]; |
|---|
| 176 | float r3 = ratios[2]; |
|---|
| 177 | |
|---|
| 178 | osg::Array* texcoords = (geometry->getNumTexCoordArrays()>0) ? geometry->getTexCoordArray(0) : 0; |
|---|
| 179 | osg::Vec2Array* texcoords_Vec2Array = dynamic_cast<osg::Vec2Array*>(texcoords); |
|---|
| 180 | if (texcoords_Vec2Array) |
|---|
| 181 | { |
|---|
| 182 | |
|---|
| 183 | osg::Vec2 tc1 = (*texcoords_Vec2Array)[i1]; |
|---|
| 184 | osg::Vec2 tc2 = (*texcoords_Vec2Array)[i2]; |
|---|
| 185 | osg::Vec2 tc3 = (*texcoords_Vec2Array)[i3]; |
|---|
| 186 | osg::Vec2 tc = tc1*r1 + tc2*r2 + tc3*r3; |
|---|
| 187 | |
|---|
| 188 | osg::notify(osg::NOTICE)<<"We hit tex coords "<<tc<<std::endl; |
|---|
| 189 | |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | else |
|---|
| 193 | { |
|---|
| 194 | osg::notify(osg::NOTICE)<<"Intersection has insufficient indices to work with"; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | else |
|---|
| 200 | { |
|---|
| 201 | osg::notify(osg::NOTICE)<<"No intersection"<<std::endl; |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 207 | { |
|---|
| 208 | if (ea.getKey()=='p') |
|---|
| 209 | { |
|---|
| 210 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 211 | itr!=_imageStreamList.end(); |
|---|
| 212 | ++itr) |
|---|
| 213 | { |
|---|
| 214 | _playToggle = !_playToggle; |
|---|
| 215 | if ( _playToggle ) |
|---|
| 216 | { |
|---|
| 217 | |
|---|
| 218 | std::cout<<"Play"<<std::endl; |
|---|
| 219 | (*itr)->play(); |
|---|
| 220 | } |
|---|
| 221 | else |
|---|
| 222 | { |
|---|
| 223 | |
|---|
| 224 | std::cout<<"Pause"<<std::endl; |
|---|
| 225 | (*itr)->pause(); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | return true; |
|---|
| 229 | } |
|---|
| 230 | else if (ea.getKey()=='r') |
|---|
| 231 | { |
|---|
| 232 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 233 | itr!=_imageStreamList.end(); |
|---|
| 234 | ++itr) |
|---|
| 235 | { |
|---|
| 236 | std::cout<<"Restart"<<std::endl; |
|---|
| 237 | (*itr)->rewind(); |
|---|
| 238 | (*itr)->play(); |
|---|
| 239 | } |
|---|
| 240 | return true; |
|---|
| 241 | } |
|---|
| 242 | else if (ea.getKey()=='L') |
|---|
| 243 | { |
|---|
| 244 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 245 | itr!=_imageStreamList.end(); |
|---|
| 246 | ++itr) |
|---|
| 247 | { |
|---|
| 248 | if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING) |
|---|
| 249 | { |
|---|
| 250 | std::cout<<"Toggle Looping Off"<<std::endl; |
|---|
| 251 | (*itr)->setLoopingMode( osg::ImageStream::NO_LOOPING ); |
|---|
| 252 | } |
|---|
| 253 | else |
|---|
| 254 | { |
|---|
| 255 | std::cout<<"Toggle Looping On"<<std::endl; |
|---|
| 256 | (*itr)->setLoopingMode( osg::ImageStream::LOOPING ); |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | return true; |
|---|
| 260 | } |
|---|
| 261 | return false; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | default: |
|---|
| 265 | return false; |
|---|
| 266 | } |
|---|
| 267 | return false; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 271 | { |
|---|
| 272 | usage.addKeyboardMouseBinding("p","Play/Pause movie"); |
|---|
| 273 | usage.addKeyboardMouseBinding("r","Restart movie"); |
|---|
| 274 | usage.addKeyboardMouseBinding("l","Toggle looping of movie"); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | osg::Geometry* myCreateTexturedQuadGeometry(const osg::Vec3& pos,float width,float height, osg::Image* image, bool useTextureRectangle, bool xyPlane, bool option_flip) |
|---|
| 279 | { |
|---|
| 280 | bool flip = image->getOrigin()==osg::Image::TOP_LEFT; |
|---|
| 281 | if (option_flip) flip = !flip; |
|---|
| 282 | |
|---|
| 283 | if (useTextureRectangle) |
|---|
| 284 | { |
|---|
| 285 | osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos, |
|---|
| 286 | osg::Vec3(width,0.0f,0.0f), |
|---|
| 287 | xyPlane ? osg::Vec3(0.0f,height,0.0f) : osg::Vec3(0.0f,0.0f,height), |
|---|
| 288 | 0.0f, flip ? image->t() : 0.0, image->s(), flip ? 0.0 : image->t()); |
|---|
| 289 | |
|---|
| 290 | osg::TextureRectangle* texture = new osg::TextureRectangle(image); |
|---|
| 291 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 292 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, |
|---|
| 296 | texture, |
|---|
| 297 | osg::StateAttribute::ON); |
|---|
| 298 | |
|---|
| 299 | return pictureQuad; |
|---|
| 300 | } |
|---|
| 301 | else |
|---|
| 302 | { |
|---|
| 303 | osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos, |
|---|
| 304 | osg::Vec3(width,0.0f,0.0f), |
|---|
| 305 | xyPlane ? osg::Vec3(0.0f,height,0.0f) : osg::Vec3(0.0f,0.0f,height), |
|---|
| 306 | 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); |
|---|
| 307 | |
|---|
| 308 | osg::Texture2D* texture = new osg::Texture2D(image); |
|---|
| 309 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 310 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 311 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 312 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, |
|---|
| 316 | texture, |
|---|
| 317 | osg::StateAttribute::ON); |
|---|
| 318 | |
|---|
| 319 | return pictureQuad; |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | #if USE_SDL |
|---|
| 324 | |
|---|
| 325 | #include "SDL.h" |
|---|
| 326 | |
|---|
| 327 | class SDLAudioSink : public osg::AudioSink |
|---|
| 328 | { |
|---|
| 329 | public: |
|---|
| 330 | |
|---|
| 331 | SDLAudioSink(osg::AudioStream* audioStream): |
|---|
| 332 | _playing(false), |
|---|
| 333 | _audioStream(audioStream) {} |
|---|
| 334 | |
|---|
| 335 | ~SDLAudioSink() |
|---|
| 336 | { |
|---|
| 337 | if (_playing) |
|---|
| 338 | { |
|---|
| 339 | |
|---|
| 340 | SDL_PauseAudio(1); |
|---|
| 341 | SDL_CloseAudio(); |
|---|
| 342 | |
|---|
| 343 | osg::notify(osg::NOTICE)<<"~SDLAudioSink() destructor, but still playing"<<std::endl; |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | virtual void startPlaying() |
|---|
| 348 | { |
|---|
| 349 | _playing = true; |
|---|
| 350 | osg::notify(osg::NOTICE)<<"SDLAudioSink()::startPlaying()"<<std::endl; |
|---|
| 351 | |
|---|
| 352 | osg::notify(osg::NOTICE)<<" audioFrequency()="<<_audioStream->audioFrequency()<<std::endl; |
|---|
| 353 | osg::notify(osg::NOTICE)<<" audioNbChannels()="<<_audioStream->audioNbChannels()<<std::endl; |
|---|
| 354 | osg::notify(osg::NOTICE)<<" audioSampleFormat()="<<_audioStream->audioSampleFormat()<<std::endl; |
|---|
| 355 | |
|---|
| 356 | SDL_AudioSpec specs = { 0 }; |
|---|
| 357 | SDL_AudioSpec wanted_specs = { 0 }; |
|---|
| 358 | |
|---|
| 359 | wanted_specs.freq = _audioStream->audioFrequency(); |
|---|
| 360 | wanted_specs.format = AUDIO_S16SYS; |
|---|
| 361 | wanted_specs.channels = _audioStream->audioNbChannels(); |
|---|
| 362 | wanted_specs.silence = 0; |
|---|
| 363 | wanted_specs.samples = 1024; |
|---|
| 364 | wanted_specs.callback = soundReadCallback; |
|---|
| 365 | wanted_specs.userdata = this; |
|---|
| 366 | |
|---|
| 367 | if (SDL_OpenAudio(&wanted_specs, &specs) < 0) |
|---|
| 368 | throw "SDL_OpenAudio() failed (" + std::string(SDL_GetError()) + ")"; |
|---|
| 369 | |
|---|
| 370 | SDL_PauseAudio(0); |
|---|
| 371 | |
|---|
| 372 | } |
|---|
| 373 | virtual bool playing() const { return _playing; } |
|---|
| 374 | |
|---|
| 375 | static void soundReadCallback(void * user_data, uint8_t * data, int datalen); |
|---|
| 376 | |
|---|
| 377 | bool _playing; |
|---|
| 378 | osg::observer_ptr<osg::AudioStream> _audioStream; |
|---|
| 379 | }; |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | void SDLAudioSink::soundReadCallback(void * const user_data, Uint8 * const data, const int datalen) |
|---|
| 383 | { |
|---|
| 384 | SDLAudioSink * sink = reinterpret_cast<SDLAudioSink*>(user_data); |
|---|
| 385 | osg::ref_ptr<osg::AudioStream> as = sink->_audioStream.get(); |
|---|
| 386 | if (as.valid()) |
|---|
| 387 | { |
|---|
| 388 | as->consumeAudioBuffer(data, datalen); |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | #endif |
|---|
| 393 | |
|---|
| 394 | int main(int argc, char** argv) |
|---|
| 395 | { |
|---|
| 396 | |
|---|
| 397 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 401 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example demonstrates the use of ImageStream for rendering movies as textures."); |
|---|
| 402 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 403 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 404 | arguments.getApplicationUsage()->addCommandLineOption("--texture2D","Use Texture2D rather than TextureRectangle."); |
|---|
| 405 | arguments.getApplicationUsage()->addCommandLineOption("--shader","Use shaders to post process the video."); |
|---|
| 406 | arguments.getApplicationUsage()->addCommandLineOption("--interactive","Use camera manipulator to allow movement around movie."); |
|---|
| 407 | arguments.getApplicationUsage()->addCommandLineOption("--flip","Flip the movie so top becomes bottom."); |
|---|
| 408 | #if defined(WIN32) || defined(__APPLE__) |
|---|
| 409 | arguments.getApplicationUsage()->addCommandLineOption("--devices","Print the Video input capability via QuickTime and exit."); |
|---|
| 410 | #endif |
|---|
| 411 | |
|---|
| 412 | bool useTextureRectangle = true; |
|---|
| 413 | bool useShader = false; |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | osgViewer::Viewer viewer(arguments); |
|---|
| 417 | |
|---|
| 418 | if (arguments.argc()<=1) |
|---|
| 419 | { |
|---|
| 420 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 421 | return 1; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | #if defined(WIN32) || defined(__APPLE__) |
|---|
| 425 | |
|---|
| 426 | if (arguments.read("-devices") || arguments.read("--devices")) |
|---|
| 427 | { |
|---|
| 428 | |
|---|
| 429 | osgDB::readImageFile("devices.live"); |
|---|
| 430 | return 1; |
|---|
| 431 | } |
|---|
| 432 | #endif |
|---|
| 433 | |
|---|
| 434 | while (arguments.read("--texture2D")) useTextureRectangle=false; |
|---|
| 435 | while (arguments.read("--shader")) useShader=true; |
|---|
| 436 | |
|---|
| 437 | bool mouseTracking = false; |
|---|
| 438 | while (arguments.read("--mouse")) mouseTracking=true; |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 443 | { |
|---|
| 444 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 445 | return 1; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | bool fullscreen = !arguments.read("--interactive"); |
|---|
| 449 | bool flip = arguments.read("--flip"); |
|---|
| 450 | |
|---|
| 451 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 452 | |
|---|
| 453 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 454 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 455 | |
|---|
| 456 | if (useShader) |
|---|
| 457 | { |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | static const char *shaderSourceTextureRec = { |
|---|
| 461 | "uniform vec4 cutoff_color;\n" |
|---|
| 462 | "uniform samplerRect movie_texture;\n" |
|---|
| 463 | "void main(void)\n" |
|---|
| 464 | "{\n" |
|---|
| 465 | " vec4 texture_color = textureRect(movie_texture, gl_TexCoord[0].st); \n" |
|---|
| 466 | " if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n" |
|---|
| 467 | " gl_FragColor = texture_color;\n" |
|---|
| 468 | "}\n" |
|---|
| 469 | }; |
|---|
| 470 | |
|---|
| 471 | static const char *shaderSourceTexture2D = { |
|---|
| 472 | "uniform vec4 cutoff_color;\n" |
|---|
| 473 | "uniform sampler2D movie_texture;\n" |
|---|
| 474 | "void main(void)\n" |
|---|
| 475 | "{\n" |
|---|
| 476 | " vec4 texture_color = texture2D(movie_texture, gl_TexCoord[0].st); \n" |
|---|
| 477 | " if (all(lessThanEqual(texture_color,cutoff_color))) discard; \n" |
|---|
| 478 | " gl_FragColor = texture_color;\n" |
|---|
| 479 | "}\n" |
|---|
| 480 | }; |
|---|
| 481 | |
|---|
| 482 | osg::Program* program = new osg::Program; |
|---|
| 483 | |
|---|
| 484 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, |
|---|
| 485 | useTextureRectangle ? shaderSourceTextureRec : shaderSourceTexture2D)); |
|---|
| 486 | |
|---|
| 487 | stateset->addUniform(new osg::Uniform("cutoff_color",osg::Vec4(0.1f,0.1f,0.1f,1.0f))); |
|---|
| 488 | stateset->addUniform(new osg::Uniform("movie_texture",0)); |
|---|
| 489 | |
|---|
| 490 | stateset->setAttribute(program); |
|---|
| 491 | |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | osg::Vec3 pos(0.0f,0.0f,0.0f); |
|---|
| 495 | osg::Vec3 topleft = pos; |
|---|
| 496 | osg::Vec3 bottomright = pos; |
|---|
| 497 | |
|---|
| 498 | bool xyPlane = fullscreen; |
|---|
| 499 | |
|---|
| 500 | bool useAudioSink = false; |
|---|
| 501 | while(arguments.read("--audio")) { useAudioSink = true; } |
|---|
| 502 | |
|---|
| 503 | for(int i=1;i<arguments.argc();++i) |
|---|
| 504 | { |
|---|
| 505 | if (arguments.isString(i)) |
|---|
| 506 | { |
|---|
| 507 | osg::Image* image = osgDB::readImageFile(arguments[i]); |
|---|
| 508 | osg::ImageStream* imagestream = dynamic_cast<osg::ImageStream*>(image); |
|---|
| 509 | if (imagestream) |
|---|
| 510 | { |
|---|
| 511 | osg::ImageStream::AudioStreams& audioStreams = imagestream->getAudioStreams(); |
|---|
| 512 | if (useAudioSink && !audioStreams.empty()) |
|---|
| 513 | { |
|---|
| 514 | osg::AudioStream* audioStream = audioStreams[0].get(); |
|---|
| 515 | osg::notify(osg::NOTICE)<<"AudioStream read ["<<audioStream->getName()<<"]"<<std::endl; |
|---|
| 516 | #if USE_SDL |
|---|
| 517 | audioStream->setAudioSink(new SDLAudioSink(audioStream)); |
|---|
| 518 | #endif |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | imagestream->play(); |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | if (image) |
|---|
| 526 | { |
|---|
| 527 | osg::notify(osg::NOTICE)<<"image->s()"<<image->s()<<" image-t()="<<image->t()<<" aspectRatio="<<image->getPixelAspectRatio()<<std::endl; |
|---|
| 528 | |
|---|
| 529 | float width = image->s(); |
|---|
| 530 | float height = image->t() * image->getPixelAspectRatio(); |
|---|
| 531 | |
|---|
| 532 | osg::ref_ptr<osg::Drawable> drawable = myCreateTexturedQuadGeometry(pos, width, height,image, useTextureRectangle, xyPlane, flip); |
|---|
| 533 | |
|---|
| 534 | if (image->isImageTranslucent()) |
|---|
| 535 | { |
|---|
| 536 | osg::notify(osg::NOTICE)<<"Transparent movie, enabling blending."<<std::endl; |
|---|
| 537 | |
|---|
| 538 | drawable->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); |
|---|
| 539 | drawable->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | geode->addDrawable(drawable.get()); |
|---|
| 543 | |
|---|
| 544 | bottomright = pos + osg::Vec3(width,height,0.0f); |
|---|
| 545 | |
|---|
| 546 | if (xyPlane) pos.y() += height*1.05f; |
|---|
| 547 | else pos.z() += height*1.05f; |
|---|
| 548 | } |
|---|
| 549 | else |
|---|
| 550 | { |
|---|
| 551 | std::cout<<"Unable to read file "<<arguments[i]<<std::endl; |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | viewer.setSceneData(geode.get()); |
|---|
| 558 | |
|---|
| 559 | if (viewer.getSceneData()==0) |
|---|
| 560 | { |
|---|
| 561 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 562 | return 1; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | MovieEventHandler* meh = new MovieEventHandler(); |
|---|
| 567 | meh->setMouseTracking( mouseTracking ); |
|---|
| 568 | meh->set( viewer.getSceneData() ); |
|---|
| 569 | viewer.addEventHandler( meh ); |
|---|
| 570 | |
|---|
| 571 | viewer.addEventHandler( new osgViewer::StatsHandler ); |
|---|
| 572 | viewer.addEventHandler( new osgGA::StateSetManipulator( viewer.getCamera()->getOrCreateStateSet() ) ); |
|---|
| 573 | viewer.addEventHandler( new osgViewer::WindowSizeHandler ); |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | if (arguments.errors()) |
|---|
| 580 | { |
|---|
| 581 | arguments.writeErrorMessages(std::cout); |
|---|
| 582 | return 1; |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | if (fullscreen) |
|---|
| 586 | { |
|---|
| 587 | viewer.realize(); |
|---|
| 588 | |
|---|
| 589 | viewer.getCamera()->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 590 | |
|---|
| 591 | float screenAspectRatio = 1280.0f/1024.0f; |
|---|
| 592 | |
|---|
| 593 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 594 | if (wsi) |
|---|
| 595 | { |
|---|
| 596 | unsigned int width, height; |
|---|
| 597 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 598 | |
|---|
| 599 | screenAspectRatio = float(width) / float(height); |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | float modelAspectRatio = (bottomright.x()-topleft.x())/(bottomright.y()-topleft.y()); |
|---|
| 603 | |
|---|
| 604 | viewer.getCamera()->setViewMatrix(osg::Matrix::identity()); |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | osg::Vec3 center = (bottomright + topleft)*0.5f; |
|---|
| 608 | osg::Vec3 dx(bottomright.x()-center.x(), 0.0f, 0.0f); |
|---|
| 609 | osg::Vec3 dy(0.0f, topleft.y()-center.y(), 0.0f); |
|---|
| 610 | |
|---|
| 611 | float ratio = modelAspectRatio/screenAspectRatio; |
|---|
| 612 | |
|---|
| 613 | if (ratio>1.0f) |
|---|
| 614 | { |
|---|
| 615 | |
|---|
| 616 | bottomright = center + dx - dy * ratio; |
|---|
| 617 | topleft = center - dx + dy * ratio; |
|---|
| 618 | } |
|---|
| 619 | else |
|---|
| 620 | { |
|---|
| 621 | |
|---|
| 622 | bottomright = center + dx / ratio - dy; |
|---|
| 623 | topleft = center - dx / ratio + dy; |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | viewer.getCamera()->setProjectionMatrixAsOrtho2D(topleft.x(),bottomright.x(),topleft.y(),bottomright.y()); |
|---|
| 627 | |
|---|
| 628 | while(!viewer.done()) |
|---|
| 629 | { |
|---|
| 630 | viewer.frame(); |
|---|
| 631 | } |
|---|
| 632 | return 0; |
|---|
| 633 | } |
|---|
| 634 | else |
|---|
| 635 | { |
|---|
| 636 | |
|---|
| 637 | return viewer.run(); |
|---|
| 638 | } |
|---|
| 639 | } |
|---|