osg::StateSet* createState(osg::ArgumentParser& arguments) { osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence; bool preLoad = true; while (arguments.read("--page-and-discard")) { imageSequence->setMode(osg::ImageSequence::PAGE_AND_DISCARD_USED_IMAGES); preLoad = false; } while (arguments.read("--page-and-retain")) { imageSequence->setMode(osg::ImageSequence::PAGE_AND_RETAIN_IMAGES); preLoad = false; } while (arguments.read("--preload")) { imageSequence->setMode(osg::ImageSequence::PRE_LOAD_ALL_IMAGES); preLoad = true; } double length = -1.0; while (arguments.read("--length",length)) {} double fps = 30.0; while (arguments.read("--fps",fps)) {} osgDB::DirectoryContents files = getSuitableFiles(arguments); if (!files.empty()) { for(osgDB::DirectoryContents::iterator itr = files.begin(); itr != files.end(); ++itr) { const std::string& filename = *itr; if (preLoad) { osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(filename); if (image.valid()) { imageSequence->addImage(image.get()); } } else { imageSequence->addImageFile(filename); } } if (length>0.0) { imageSequence->setLength(length); } else { unsigned int maxNum = imageSequence->getNumImageData(); imageSequence->setLength(double(maxNum)*(1.0/fps)); } } else { if (length>0.0) { imageSequence->setLength(length); } else { imageSequence->setLength(4.0); } imageSequence->addImage(osgDB::readRefImageFile("Cubemap_axis/posx.png")); imageSequence->addImage(osgDB::readRefImageFile("Cubemap_axis/negx.png")); imageSequence->addImage(osgDB::readRefImageFile("Cubemap_axis/posy.png")); imageSequence->addImage(osgDB::readRefImageFile("Cubemap_axis/negy.png")); imageSequence->addImage(osgDB::readRefImageFile("Cubemap_axis/posz.png")); imageSequence->addImage(osgDB::readRefImageFile("Cubemap_axis/negz.png")); } // start the image sequence playing imageSequence->play(); #if 1 osg::Texture2D* texture = new osg::Texture2D; texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT); texture->setResizeNonPowerOfTwoHint(false); texture->setImage(imageSequence.get()); //texture->setTextureSize(512,512); #else osg::TextureRectangle* texture = new osg::TextureRectangle; texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT); texture->setImage(imageSequence.get()); //texture->setTextureSize(512,512); #endif // create the StateSet to store the texture data osg::StateSet* stateset = new osg::StateSet; stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); return stateset; }
int main(int argc, char** argv) { /* std::string plugin_to_use = "AVFoundation"; // "QTKit"; osgDB::Registry::instance()->addFileExtensionAlias("mov", plugin_to_use); osgDB::Registry::instance()->addFileExtensionAlias("mp4", plugin_to_use); osgDB::Registry::instance()->addFileExtensionAlias("m4v", plugin_to_use); */ // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); // set up the usage document, in case we need to print out how to use this program. arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example demonstrates the use of ImageStream for rendering movies as textures."); arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); arguments.getApplicationUsage()->addCommandLineOption("--disableCoreVideo","disable CoreVideo (QTKit+AVFoundation plugin)"); arguments.getApplicationUsage()->addCommandLineOption("--disableMultiThreadedFrameDispatching","disable frame dispatching via multiple threads (QTKit+AVFoundation plugin)"); arguments.getApplicationUsage()->addCommandLineOption("--maxVideos [numVideos]","max videos to open from a folder"); arguments.getApplicationUsage()->addCommandLineOption("--slideShow","present movies in a slide-show"); unsigned int max_videos(10); bool slide_show = false; std::string options_str(""); if (arguments.find("--slideShow") > 0) { slide_show = true; } if (arguments.find("--disableMultiThreadedFrameDispatching") > 0) { options_str += " disableMultiThreadedFrameDispatching"; } if (arguments.find("--disableCoreVideo") > 0) { options_str += " disableCoreVideo"; } if (int ndx = arguments.find("--numFrameDispatchThreads") > 0) { options_str += std::string(" numFrameDispatchThreads=") + arguments[ndx+1]; } if (int ndx = arguments.find("--maxVideos") > 0) { if (arguments.isNumber(ndx+1)) max_videos = atoi(arguments[ndx+1]); } // construct the viewer. osgViewer::Viewer viewer(arguments); if (arguments.argc()<=1) { arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); return 1; } // if user request help write it out to cout. if (arguments.read("-h") || arguments.read("--help")) { arguments.getApplicationUsage()->write(std::cout); return 1; } osg::ref_ptr<osg::Group> group = new osg::Group; osg::StateSet* stateset = group->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); osg::Vec3 pos(0.0f,0.0f,0.0f); static const float desired_height = 768.0f; osgDB::DirectoryContents files = getSuitableFiles(arguments); osgGA::GUIEventHandler* movie_event_handler(NULL); osg::ref_ptr<osgDB::ReaderWriter::Options> options = new osgDB::ReaderWriter::Options(options_str); if (slide_show) { osg::Node* node = readImageStream(files[0], pos, desired_height, options.get()); group->addChild(node); movie_event_handler = new SlideShowEventHandler(node, files, options.get()); } else { movie_event_handler = new MovieEventHandler(); unsigned int num_files_per_row = std::max(osg::round(sqrt(static_cast<double>(std::min(max_videos, static_cast<unsigned int>(files.size()))))), 1.0); static const float new_row_at = num_files_per_row * desired_height * 16 / 9.0; unsigned int num_videos = 0; for(osgDB::DirectoryContents::iterator i = files.begin(); (i != files.end()) && (num_videos < max_videos); ++i) { osg::Node* node = readImageStream(*i, pos, desired_height, options.get()); if (node) group->addChild(node); if (pos[0] > new_row_at) { pos[0] = 0; pos[2] += desired_height +10; } num_videos++; } } // set the scene to render viewer.setSceneData(group.get()); if (viewer.getSceneData()==0) { arguments.getApplicationUsage()->write(std::cout); return 1; } viewer.addEventHandler( movie_event_handler ); viewer.addEventHandler( new osgViewer::StatsHandler ); viewer.addEventHandler( new osgViewer::ToggleSyncToVBlankHandler()); viewer.addEventHandler( new osgGA::StateSetManipulator( viewer.getCamera()->getOrCreateStateSet() ) ); viewer.addEventHandler( new osgViewer::WindowSizeHandler ); // add the record camera path handler viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); // report any errors if they have occurred when parsing the program arguments. if (arguments.errors()) { arguments.writeErrorMessages(std::cout); return 1; } // create the windows and run the threads. return viewer.run(); }