2008-07-21 17:46:53 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2008-07-21 18:57:06 +08:00
|
|
|
#include <OpenThreads/ScopedLock>
|
2008-07-21 17:46:53 +08:00
|
|
|
#include <osg/ImageSequence>
|
2008-07-22 01:28:22 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/Camera>
|
2008-07-22 05:00:57 +08:00
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/Texture2D>
|
2008-07-22 01:28:22 +08:00
|
|
|
|
|
|
|
#include <math.h>
|
2008-07-21 17:46:53 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
void ImageSequence::UpdateCallback::operator () (osg::StateAttribute* attr, osg::NodeVisitor* nv)
|
|
|
|
{
|
2008-07-22 20:28:46 +08:00
|
|
|
osg::Texture* texture = attr ? attr->asTexture() : 0;
|
|
|
|
|
2008-07-22 22:47:59 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<"ImageSequence::UpdateCallback::"<<texture<<std::endl;
|
2008-07-22 20:28:46 +08:00
|
|
|
if (texture)
|
2008-07-22 05:00:57 +08:00
|
|
|
{
|
2008-07-22 20:28:46 +08:00
|
|
|
for(unsigned int i=0; i<texture->getNumImages(); ++i)
|
|
|
|
{
|
|
|
|
texture->getImage(i)->update(nv);
|
|
|
|
}
|
2008-07-22 05:00:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-21 17:46:53 +08:00
|
|
|
ImageSequence::ImageSequence()
|
|
|
|
{
|
2008-07-22 01:28:22 +08:00
|
|
|
_referenceTime = DBL_MAX;
|
2008-07-21 17:46:53 +08:00
|
|
|
_timeMultiplier = 1.0;
|
2008-07-22 05:00:57 +08:00
|
|
|
|
2008-07-26 00:11:51 +08:00
|
|
|
_mode = PRE_LOAD_ALL_IMAGES;
|
2008-07-22 05:00:57 +08:00
|
|
|
_duration = 1.0;
|
2008-07-23 00:44:49 +08:00
|
|
|
_timePerImage = 1.0;
|
2008-07-22 05:00:57 +08:00
|
|
|
|
2008-07-23 04:20:16 +08:00
|
|
|
_fileNamesIterator = _fileNames.end();
|
|
|
|
_fileNamesIteratorTime = DBL_MAX;
|
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
_imageIterator = _images.end();
|
2008-07-23 04:20:16 +08:00
|
|
|
_imageIteratorTime = DBL_MAX;
|
2008-07-21 17:46:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageSequence::ImageSequence(const ImageSequence& is,const CopyOp& copyop):
|
|
|
|
osg::ImageStream(is,copyop),
|
|
|
|
_referenceTime(is._referenceTime),
|
2008-07-22 05:00:57 +08:00
|
|
|
_timeMultiplier(is._timeMultiplier),
|
2008-07-26 00:11:51 +08:00
|
|
|
_mode(is._mode),
|
2008-07-22 05:00:57 +08:00
|
|
|
_duration(is._duration),
|
2008-07-26 00:11:51 +08:00
|
|
|
_timePerImage(is._timePerImage)
|
2008-07-21 17:46:53 +08:00
|
|
|
{
|
2008-07-23 04:20:16 +08:00
|
|
|
_fileNamesIterator = _fileNames.end();
|
|
|
|
_fileNamesIteratorTime = DBL_MAX;
|
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
_imageIteratorTime = DBL_MAX;
|
|
|
|
_imageIterator = _images.end();
|
2008-07-21 17:46:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ImageSequence::compare(const Image& rhs) const
|
|
|
|
{
|
|
|
|
return ImageStream::compare(rhs);
|
|
|
|
}
|
|
|
|
|
2008-07-23 00:44:49 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
void ImageSequence::addImageFile(const std::string& fileName)
|
2008-07-21 17:46:53 +08:00
|
|
|
{
|
2008-07-21 18:57:06 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
2008-07-22 05:00:57 +08:00
|
|
|
_fileNames.push_back(fileName);
|
2008-07-23 00:44:49 +08:00
|
|
|
computeTimePerImage();
|
2008-07-23 04:20:16 +08:00
|
|
|
|
|
|
|
if (_fileNamesIterator==_fileNames.end())
|
|
|
|
{
|
|
|
|
_fileNamesIterator = _fileNames.begin();
|
|
|
|
_fileNamesIteratorTime = _referenceTime;
|
|
|
|
}
|
2008-07-21 17:46:53 +08:00
|
|
|
}
|
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
void ImageSequence::addImage(osg::Image* image)
|
2008-07-21 17:46:53 +08:00
|
|
|
{
|
2008-07-21 18:57:06 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
2008-07-22 01:28:22 +08:00
|
|
|
|
2008-07-25 21:45:07 +08:00
|
|
|
if (!_filesRequested.empty())
|
|
|
|
{
|
|
|
|
// follows is a mechanism that ensures that requested images
|
|
|
|
// get merged in the correct time order.
|
|
|
|
if (_filesRequested.front().first != image->getFileName())
|
|
|
|
{
|
|
|
|
for(FileNameImageList::iterator itr = _filesRequested.begin();
|
|
|
|
itr != _filesRequested.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (itr->first == image->getFileName())
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"inserting image into waiting stake : "<<image->getFileName()<<std::endl;
|
|
|
|
itr->second = image;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// osg::notify(osg::NOTICE)<<"image not expected : "<<image->getFileName()<<std::endl;
|
|
|
|
_images.push_back(image);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// osg::notify(osg::NOTICE)<<"merging image in order expected : "<<image->getFileName()<<std::endl;
|
|
|
|
_images.push_back(image);
|
|
|
|
|
|
|
|
_filesRequested.pop_front();
|
|
|
|
|
|
|
|
FileNameImageList::iterator itr;
|
|
|
|
for(itr = _filesRequested.begin();
|
|
|
|
itr != _filesRequested.end() && itr->second.valid();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
// osg::notify(osg::NOTICE)<<" merging previously loaded, but out of order file : "<<itr->first<<std::endl;
|
|
|
|
_images.push_back(itr->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
_filesRequested.erase(_filesRequested.begin(), itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_images.push_back(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
computeTimePerImage();
|
|
|
|
|
|
|
|
if (data()==0)
|
2008-07-22 01:28:22 +08:00
|
|
|
{
|
2008-07-22 05:00:57 +08:00
|
|
|
_imageIterator = _images.begin();
|
2008-07-22 01:28:22 +08:00
|
|
|
_imageIteratorTime = _referenceTime;
|
2008-07-22 05:00:57 +08:00
|
|
|
setImageToChild(_imageIterator->get());
|
2008-07-22 01:28:22 +08:00
|
|
|
}
|
2008-07-21 17:46:53 +08:00
|
|
|
}
|
|
|
|
|
2008-07-22 01:28:22 +08:00
|
|
|
void ImageSequence::setImageToChild(const osg::Image* image)
|
|
|
|
{
|
2008-07-22 05:00:57 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<"setImageToChild("<<image<<")"<<std::endl;
|
|
|
|
|
2008-07-26 00:11:51 +08:00
|
|
|
if (image==0) return;
|
|
|
|
|
2008-07-22 01:28:22 +08:00
|
|
|
setImage(image->s(),image->t(),image->r(),
|
|
|
|
image->getInternalTextureFormat(),
|
|
|
|
image->getPixelFormat(),image->getDataType(),
|
|
|
|
const_cast<unsigned char*>(image->data()),
|
|
|
|
osg::Image::NO_DELETE,
|
|
|
|
image->getPacking());
|
|
|
|
}
|
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
void ImageSequence::update(osg::NodeVisitor* nv)
|
2008-07-21 17:46:53 +08:00
|
|
|
{
|
2008-07-21 18:57:06 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
2008-07-22 05:00:57 +08:00
|
|
|
|
|
|
|
osg::NodeVisitor::ImageRequestHandler* irh = nv->getImageRequestHandler();
|
|
|
|
const osg::FrameStamp* fs = nv->getFrameStamp();
|
|
|
|
|
|
|
|
// osg::notify(osg::NOTICE)<<"ImageSequence::update("<<fs<<", "<<irh<<")"<<std::endl;
|
2008-07-22 01:28:22 +08:00
|
|
|
|
|
|
|
if (_referenceTime == DBL_MAX)
|
|
|
|
{
|
|
|
|
_referenceTime = fs->getSimulationTime();
|
|
|
|
}
|
|
|
|
|
2008-07-23 04:20:16 +08:00
|
|
|
if (_fileNamesIteratorTime == DBL_MAX)
|
|
|
|
{
|
|
|
|
_fileNamesIteratorTime = _referenceTime;
|
|
|
|
}
|
|
|
|
|
2008-07-22 01:28:22 +08:00
|
|
|
if (_imageIteratorTime == DBL_MAX)
|
|
|
|
{
|
|
|
|
_imageIteratorTime = _referenceTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
double time = (fs->getSimulationTime() - _referenceTime)*_timeMultiplier;
|
2008-07-22 05:00:57 +08:00
|
|
|
|
2008-07-24 03:04:46 +08:00
|
|
|
FileNames::iterator previous_fileNamesIterator = _fileNamesIterator;
|
2008-07-22 05:00:57 +08:00
|
|
|
Images::iterator previous_imageIterator = _imageIterator;
|
2008-07-22 01:28:22 +08:00
|
|
|
|
2008-07-26 00:11:51 +08:00
|
|
|
bool pruneOldImages = false;
|
|
|
|
|
|
|
|
bool looping = getLoopingMode()==LOOPING;
|
|
|
|
|
|
|
|
switch(_mode)
|
|
|
|
{
|
|
|
|
case(PRE_LOAD_ALL_IMAGES):
|
|
|
|
{
|
|
|
|
if (_fileNames.size()>_images.size())
|
|
|
|
{
|
|
|
|
for(unsigned int i=_images.size(); i<_fileNames.size(); ++i)
|
|
|
|
{
|
|
|
|
osg::Image* image = irh->readImageFile(_fileNames[i]);
|
|
|
|
_images.push_back(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
irh = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(PAGE_AND_RETAIN_IMAGES):
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(PAGE_AND_DISCARD_USED_IMAGES):
|
|
|
|
{
|
|
|
|
pruneOldImages = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-22 01:28:22 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<"time = "<<time<<std::endl;
|
|
|
|
|
2008-07-24 03:04:46 +08:00
|
|
|
if (irh)
|
2008-07-22 01:28:22 +08:00
|
|
|
{
|
2008-07-26 00:11:51 +08:00
|
|
|
double preLoadTime = (time+irh->getPreLoadTime())*_timeMultiplier;
|
|
|
|
|
2008-07-23 04:20:16 +08:00
|
|
|
//
|
|
|
|
// Advance imageIterator
|
|
|
|
//
|
|
|
|
if (_fileNamesIterator!=_fileNames.end())
|
2008-07-22 01:28:22 +08:00
|
|
|
{
|
2008-07-23 04:20:16 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<" _fileNamesIteratorTime = "<<_fileNamesIteratorTime<<std::endl;
|
|
|
|
while(preLoadTime > (_fileNamesIteratorTime + _timePerImage))
|
2008-07-22 01:28:22 +08:00
|
|
|
{
|
2008-07-23 04:20:16 +08:00
|
|
|
_fileNamesIteratorTime += _timePerImage;
|
2008-07-25 21:45:07 +08:00
|
|
|
//osg::notify(osg::NOTICE)<<" _fileNamesIteratorTime = "<<_fileNamesIteratorTime<<std::endl;
|
|
|
|
//osg::notify(osg::NOTICE)<<" need to preLoad = "<<*_fileNamesIterator<<std::endl;
|
2008-07-23 04:20:16 +08:00
|
|
|
++_fileNamesIterator;
|
2008-07-23 00:44:49 +08:00
|
|
|
|
2008-07-24 03:04:46 +08:00
|
|
|
if (previous_fileNamesIterator==_fileNamesIterator) break;
|
|
|
|
|
2008-07-23 04:20:16 +08:00
|
|
|
if (_fileNamesIterator ==_fileNames.end())
|
2008-07-23 00:44:49 +08:00
|
|
|
{
|
2008-07-23 04:20:16 +08:00
|
|
|
// return iterator to begining of set.
|
|
|
|
_fileNamesIterator = _fileNames.begin();
|
2008-07-23 00:44:49 +08:00
|
|
|
}
|
2008-07-24 03:04:46 +08:00
|
|
|
|
2008-07-25 21:45:07 +08:00
|
|
|
_filesRequested.push_back(FileNameImagePair(*_fileNamesIterator,0));
|
2008-07-24 03:04:46 +08:00
|
|
|
irh->requestImageFile(*_fileNamesIterator, this, _fileNamesIteratorTime, fs);
|
|
|
|
|
2008-07-22 01:28:22 +08:00
|
|
|
}
|
|
|
|
}
|
2008-07-23 04:20:16 +08:00
|
|
|
|
|
|
|
if (_fileNamesIterator==_fileNames.end())
|
|
|
|
{
|
|
|
|
_fileNamesIterator = _fileNames.begin();
|
|
|
|
}
|
2008-07-22 01:28:22 +08:00
|
|
|
}
|
2008-07-23 04:20:16 +08:00
|
|
|
|
2008-07-22 01:28:22 +08:00
|
|
|
{
|
2008-07-24 03:04:46 +08:00
|
|
|
|
2008-07-23 04:20:16 +08:00
|
|
|
//
|
|
|
|
// Advance imageIterator
|
|
|
|
//
|
|
|
|
if (_imageIterator!=_images.end())
|
|
|
|
{
|
|
|
|
// osg::notify(osg::NOTICE)<<" _imageIteratorTime = "<<_imageIteratorTime<<std::endl;
|
|
|
|
while(time > (_imageIteratorTime + _timePerImage))
|
|
|
|
{
|
|
|
|
_imageIteratorTime += _timePerImage;
|
|
|
|
// osg::notify(osg::NOTICE)<<" _imageIteratorTime = "<<_imageIteratorTime<<std::endl;
|
|
|
|
++_imageIterator;
|
|
|
|
|
|
|
|
if (_imageIterator ==_images.end())
|
|
|
|
{
|
|
|
|
|
2008-07-26 00:11:51 +08:00
|
|
|
if (pruneOldImages)
|
2008-07-23 04:20:16 +08:00
|
|
|
{
|
|
|
|
_images.erase(previous_imageIterator, _imageIterator);
|
2008-07-24 03:04:46 +08:00
|
|
|
previous_imageIterator = _images.begin();
|
2008-07-23 04:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// return iterator to begining of set.
|
|
|
|
_imageIterator = _images.begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_imageIterator==_images.end())
|
|
|
|
{
|
|
|
|
_imageIterator = _images.begin();
|
|
|
|
}
|
2008-07-22 01:28:22 +08:00
|
|
|
}
|
2008-07-23 04:20:16 +08:00
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
if (_imageIterator!=_images.end() && previous_imageIterator != _imageIterator)
|
2008-07-22 01:28:22 +08:00
|
|
|
{
|
2008-07-26 00:11:51 +08:00
|
|
|
if (pruneOldImages)
|
2008-07-23 00:44:49 +08:00
|
|
|
{
|
|
|
|
_images.erase(previous_imageIterator, _imageIterator);
|
|
|
|
}
|
|
|
|
|
2008-07-22 05:00:57 +08:00
|
|
|
setImageToChild(_imageIterator->get());
|
2008-07-22 01:28:22 +08:00
|
|
|
}
|
2008-07-21 17:46:53 +08:00
|
|
|
}
|