diff --git a/examples/osgimagesequence/osgimagesequence.cpp b/examples/osgimagesequence/osgimagesequence.cpp index dccf15b1c..1109713b7 100644 --- a/examples/osgimagesequence/osgimagesequence.cpp +++ b/examples/osgimagesequence/osgimagesequence.cpp @@ -104,7 +104,11 @@ int main(int argc, char **argv) // create a model from the images and pass it to the viewer. viewer.setSceneData(createModel()); - //osgDB::writeNodeFile(*viewer.getSceneData(),"test.osg"); + std::string filename; + if (arguments.read("-o",filename)) + { + osgDB::writeNodeFile(*viewer.getSceneData(),filename); + } return viewer.run(); } diff --git a/include/osg/ImageSequence b/include/osg/ImageSequence index a59c91d98..49a825620 100644 --- a/include/osg/ImageSequence +++ b/include/osg/ImageSequence @@ -51,7 +51,7 @@ class OSG_EXPORT ImageSequence : public ImageStream typedef std::list< osg::ref_ptr > Images; typedef std::list< std::string > FileNames; - void setDuration(double duration) { _duration = duration; } + void setDuration(double duration); double getDuration() const { return _duration; } void setPruneOldImages(bool prune) { _pruneOldImages = prune; } @@ -80,6 +80,8 @@ class OSG_EXPORT ImageSequence : public ImageStream virtual ~ImageSequence() {} void setImageToChild(const osg::Image* image); + + void computeTimePerImage(); double _referenceTime; double _timeMultiplier; @@ -89,6 +91,8 @@ class OSG_EXPORT ImageSequence : public ImageStream double _duration; bool _pruneOldImages; + + double _timePerImage; double _imageHeadTime; Images _images; diff --git a/src/osg/ImageSequence.cpp b/src/osg/ImageSequence.cpp index 95d780370..beba1ff5a 100644 --- a/src/osg/ImageSequence.cpp +++ b/src/osg/ImageSequence.cpp @@ -42,6 +42,7 @@ ImageSequence::ImageSequence() _timeMultiplier = 1.0; _duration = 1.0; + _timePerImage = 1.0; _pruneOldImages = false; _imageIteratorTime = DBL_MAX; @@ -64,16 +65,31 @@ int ImageSequence::compare(const Image& rhs) const return ImageStream::compare(rhs); } +void ImageSequence::setDuration(double duration) +{ + _duration = duration; + computeTimePerImage(); +} + +void ImageSequence::computeTimePerImage() +{ + if (!_fileNames.empty()) _timePerImage = _duration / double(_fileNames.size()); + else if (!_images.empty()) _timePerImage = _duration / double(_images.size()); + else _timePerImage = _duration; +} + void ImageSequence::addImageFile(const std::string& fileName) { OpenThreads::ScopedLock lock(_mutex); _fileNames.push_back(fileName); + computeTimePerImage(); } void ImageSequence::addImage(osg::Image* image) { OpenThreads::ScopedLock lock(_mutex); _images.push_back(image); + computeTimePerImage(); if (_imageIterator==_images.end()) { @@ -115,9 +131,6 @@ void ImageSequence::update(osg::NodeVisitor* nv) } double time = (fs->getSimulationTime() - _referenceTime)*_timeMultiplier; - double delta = _fileNames.empty() ? - _duration / _images.size() : - _duration / _fileNames.size(); Images::iterator previous_imageIterator = _imageIterator; @@ -126,14 +139,20 @@ void ImageSequence::update(osg::NodeVisitor* nv) if (_imageIterator!=_images.end()) { // osg::notify(osg::NOTICE)<<" _imageIteratorTime = "<<_imageIteratorTime< (_imageIteratorTime + delta)) + while(time > (_imageIteratorTime + _timePerImage)) { - _imageIteratorTime += delta; + _imageIteratorTime += _timePerImage; // osg::notify(osg::NOTICE)<<" _imageIteratorTime = "<<_imageIteratorTime<get()); } } diff --git a/src/osgPlugins/osg/ImageSequence.cpp b/src/osgPlugins/osg/ImageSequence.cpp index 41c672081..fe8e736a1 100644 --- a/src/osgPlugins/osg/ImageSequence.cpp +++ b/src/osgPlugins/osg/ImageSequence.cpp @@ -27,6 +27,14 @@ bool ImageSequence_readLocalData(Object& obj, Input& fr) ImageSequence& is = static_cast(obj); + unsigned int numFilesToPreLoad = 1; + + double duration; + if (fr.read("Duration", duration)) + { + is.setDuration(duration); + } + if (fr.matchSequence("FileNames {")) { fr += 2; @@ -36,12 +44,13 @@ bool ImageSequence_readLocalData(Object& obj, Input& fr) { if (fr[0].getStr()) { -#if 1 is.addImageFile(fr[0].getStr()); -#else - osg::ref_ptr image = fr.readImage(fr[0].getStr()); - if (image.valid()) is.addImage(image.get()); -#endif + + if (is.getImages().size() < numFilesToPreLoad) + { + osg::ref_ptr image = fr.readImage(fr[0].getStr()); + if (image.valid()) is.addImage(image.get()); + } } ++fr; } @@ -74,6 +83,8 @@ bool ImageSequence_writeLocalData(const Object& obj, Output& fw) // no current image writing code here // as it is all handled by osg::Registry::writeImage() via plugins. + fw.indent()<<"Duration "<