OpenSceneGraph/src/osg/ImageSequence.cpp

442 lines
13 KiB
C++
Raw Normal View History

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2008-07-21 17:46:53 +08:00
*
* 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
2008-07-21 17:46:53 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
2008-07-21 17:46:53 +08:00
* 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
2008-07-21 17:46:53 +08:00
* OpenSceneGraph Public License for more details.
*/
#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>
#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;
ImageSequence::ImageData::ImageData()
{
}
ImageSequence::ImageData::ImageData(const ImageData& id):
_filename(id._filename),
_image(id._image),
_imageRequest(id._imageRequest)
{
}
ImageSequence::ImageData& ImageSequence::ImageData::operator = (const ImageSequence::ImageData& rhs)
{
if (&rhs!=this)
{
_filename = rhs._filename;
_image = rhs._image;
_imageRequest = rhs._imageRequest;
}
return *this;
}
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;
_mode = PRE_LOAD_ALL_IMAGES;
_length = 1.0;
_timePerImage = 1.0;
_seekTime = 0.0;
_seekTimeSet = false;
_previousAppliedImageIndex = -1;
2008-07-21 17:46:53 +08:00
}
ImageSequence::ImageSequence(const ImageSequence& is,const CopyOp& copyop):
osg::ImageStream(is,copyop),
_referenceTime(is._referenceTime),
_timeMultiplier(is._timeMultiplier),
_mode(is._mode),
_length(is._length),
_timePerImage(is._timePerImage)
2008-07-21 17:46:53 +08:00
{
_seekTime = is._seekTime;
_seekTimeSet = is._seekTimeSet;
_previousAppliedImageIndex = -1;
2008-07-21 17:46:53 +08:00
}
int ImageSequence::compare(const Image& rhs) const
{
return ImageStream::compare(rhs);
}
void ImageSequence::seek(double time)
{
_seekTime = time;
_seekTimeSet = true;
}
void ImageSequence::play()
{
_status=PLAYING;
}
void ImageSequence::pause()
{
_status=PAUSED;
}
void ImageSequence::rewind()
{
seek(0.0f);
}
2008-07-26 00:23:00 +08:00
void ImageSequence::setMode(Mode mode)
{
_mode = mode;
}
void ImageSequence::setLength(double length)
{
if (length<=0.0)
{
OSG_NOTICE<<"ImageSequence::setLength("<<length<<") invalid length value, must be greater than 0."<<std::endl;
return;
}
_length = length;
computeTimePerImage();
}
void ImageSequence::computeTimePerImage()
{
if (!_imageDataList.empty()) _timePerImage = _length / double(_imageDataList.size());
else _timePerImage = _length;
}
void ImageSequence::setImageFile(unsigned int pos, const std::string& fileName)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (pos>=_imageDataList.size()) _imageDataList.resize(pos);
_imageDataList[pos]._filename = fileName;
}
std::string ImageSequence::getImageFile(unsigned int pos) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return pos<_imageDataList.size() ? _imageDataList[pos]._filename : std::string();
}
void ImageSequence::addImageFile(const std::string& fileName)
2008-07-21 17:46:53 +08:00
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_imageDataList.push_back(ImageData());
_imageDataList.back()._filename = fileName;
computeTimePerImage();
}
void ImageSequence::setImage(unsigned int pos, osg::Image* image)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_setImage(pos,image);
}
void ImageSequence::_setImage(unsigned int pos, osg::Image* image)
{
if (pos>=_imageDataList.size()) _imageDataList.resize(pos+1);
_imageDataList[pos]._image = image;
_imageDataList[pos]._filename = image->getFileName();
2008-07-21 17:46:53 +08:00
}
Image* ImageSequence::getImage(unsigned int pos)
2008-07-21 17:46:53 +08:00
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return pos<_imageDataList.size() ? _imageDataList[pos]._image.get() : 0;
}
2008-07-22 01:28:22 +08:00
const Image* ImageSequence::getImage(unsigned int pos) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return pos<_imageDataList.size() ? _imageDataList[pos]._image.get() : 0;
}
void ImageSequence::addImage(osg::Image* image)
{
if (image==0) return;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
// OSG_NOTICE<<"merging image in order expected : "<<image->getFileName()<<std::endl;
_imageDataList.push_back(ImageData());
_imageDataList.back()._image = image;
computeTimePerImage();
if (data()==0)
2008-07-22 01:28:22 +08:00
{
setImageToChild(_imageDataList.size()-1);
2008-07-22 01:28:22 +08:00
}
2008-07-21 17:46:53 +08:00
}
void ImageSequence::setImageToChild(int pos)
2008-07-22 01:28:22 +08:00
{
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
const osg::Image* image = (pos>=0 && pos<static_cast<int>(_imageDataList.size())) ? _imageDataList[pos]._image.get() : 0;
if (image==0) return;
// check to see if data is changing, if not don't apply
if (image->data() == data())
{
return;
}
bool discardOldImages = _mode==PAGE_AND_DISCARD_USED_IMAGES || _mode==LOAD_AND_DISCARD_IN_UPDATE_TRAVERSAL;
if (discardOldImages && _previousAppliedImageIndex>=0)
{
if (_previousAppliedImageIndex<pos)
{
OSG_INFO<<"Moving forward from "<<_previousAppliedImageIndex<<" to "<<pos<<std::endl;
while(_previousAppliedImageIndex<pos)
{
_imageDataList[_previousAppliedImageIndex]._image = 0;
OSG_INFO<<" deleting "<<_previousAppliedImageIndex<<std::endl;
++_previousAppliedImageIndex;
}
}
else if (_previousAppliedImageIndex>pos)
{
OSG_INFO<<"Moving back from "<<_previousAppliedImageIndex<<" to "<<pos<<std::endl;
while(_previousAppliedImageIndex>pos)
{
_imageDataList[_previousAppliedImageIndex]._image = 0;
OSG_INFO<<" deleting "<<_previousAppliedImageIndex<<std::endl;
--_previousAppliedImageIndex;
}
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
}
}
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
_previousAppliedImageIndex = pos;
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());
setMipmapLevels(image->getMipmapLevels());
2008-07-22 01:28:22 +08:00
}
void ImageSequence::applyLoopingMode()
{
}
int ImageSequence::imageIndex(double time)
{
if (getLoopingMode()==LOOPING)
{
double positionRatio = time/_length;
time = (positionRatio - floor(positionRatio))*_length;
}
if (time<0.0) return 0;
int index = int(time/_timePerImage);
if (index>=int(_imageDataList.size())) return int(_imageDataList.size())-1;
return index;
}
void ImageSequence::update(osg::NodeVisitor* nv)
2008-07-21 17:46:53 +08:00
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
// if imageDataList is empty then there is nothing update can do.
if (_imageDataList.empty()) return;
osg::NodeVisitor::ImageRequestHandler* irh = nv->getImageRequestHandler();
const osg::FrameStamp* fs = nv->getFrameStamp();
// OSG_NOTICE<<"ImageSequence::update("<<fs<<", "<<irh<<")"<<std::endl;
2008-07-22 01:28:22 +08:00
if (_referenceTime == DBL_MAX)
{
_referenceTime = fs->getSimulationTime();
}
bool looping = getLoopingMode()==LOOPING;
double time = (fs->getSimulationTime() - _referenceTime)*_timeMultiplier;
bool useDirectTimeRequest = _seekTimeSet;
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
if (_seekTimeSet || _status==PAUSED || _status==INVALID)
2008-07-22 01:28:22 +08:00
{
time = _seekTime;
useDirectTimeRequest = true;
_referenceTime = fs->getSimulationTime() - time/_timeMultiplier;
2008-07-22 01:28:22 +08:00
}
else
{
if (looping)
{
while (time>_length)
{
_referenceTime += _length/_timeMultiplier;
time -= _length;
}
}
else
{
if (time>_length)
{
_referenceTime = fs->getSimulationTime() - _length/_timeMultiplier;
time = _length;
}
}
}
_seekTime = time;
_seekTimeSet = false;
if (irh && _mode==PRE_LOAD_ALL_IMAGES)
{
for(ImageDataList::iterator itr = _imageDataList.begin();
itr != _imageDataList.end();
++itr)
{
if (!(itr->_image) && !(itr->_filename.empty()))
{
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
itr->_image = irh->readRefImageFile(itr->_filename, _readOptions.get());
}
}
}
int index = int(time/_timePerImage);
// OSG_NOTICE<<"time= "<<time<<" _timePerImage="<<_timePerImage<<" index="<<index<<" _length="<<_length<<std::endl;
2008-07-22 01:28:22 +08:00
if (index>=int(_imageDataList.size())) index = int(_imageDataList.size())-1;
if (index>=0 && index<int(_imageDataList.size()))
2008-07-22 01:28:22 +08:00
{
// need to find the nearest relevant change.
if (!_imageDataList[index]._image)
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
{
if (_previousAppliedImageIndex<index)
{
2012-11-05 20:21:34 +08:00
OSG_DEBUG<<"ImageSequence::update(..) Moving forward by "<<index-_previousAppliedImageIndex<<std::endl;
while (index>=0 && !_imageDataList[index]._image.valid())
{
--index;
}
}
else if (_previousAppliedImageIndex>index)
{
2012-11-05 20:21:34 +08:00
OSG_DEBUG<<"ImageSequence::update(..) Moving back by "<<_previousAppliedImageIndex-index<<std::endl;
while (index<static_cast<int>(_imageDataList.size()) && !_imageDataList[index]._image.valid())
{
++index;
}
}
}
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
if (index>=0 && index!=_previousAppliedImageIndex)
2008-07-22 01:28:22 +08:00
{
setImageToChild(index);
2008-07-22 01:28:22 +08:00
}
}
// OSG_NOTICE<<"time = "<<time<<std::endl;
if (!irh) return;
bool loadDirectly = (_mode==LOAD_AND_RETAIN_IN_UPDATE_TRAVERSAL) || (_mode==LOAD_AND_DISCARD_IN_UPDATE_TRAVERSAL);
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
if (useDirectTimeRequest)
{
int i = osg::maximum<int>(0, int(time/_timePerImage));
if ((i>=int(_imageDataList.size()) || !_imageDataList[i]._image))
{
i = osg::minimum<int>(i, _imageDataList.size()-1);
if (loadDirectly)
{
OSG_NOTICE<<"Reading file, entry="<<i<<" : _fileNames[i]="<<_imageDataList[i]._filename<<std::endl;
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
osg::ref_ptr<osg::Image> image = irh->readRefImageFile(_imageDataList[i]._filename, _readOptions.get());
if (image.valid())
{
OSG_NOTICE<<" Assigning image "<<_imageDataList[i]._filename<<std::endl;
_setImage(i, image.get());
setImageToChild(i);
}
else
{
OSG_NOTICE<<" Failed to read file "<<_imageDataList[i]._filename<<std::endl;
}
}
else
{
OSG_NOTICE<<"Requesting file, entry="<<i<<" : _fileNames[i]="<<_imageDataList[i]._filename<<std::endl;
irh->requestImageFile(_imageDataList[i]._filename, this, i, time, fs, _imageDataList[i]._imageRequest, _readOptions.get());
}
}
}
else
2008-07-22 01:28:22 +08:00
{
double preLoadTime = time + osg::minimum(irh->getPreLoadTime()*_timeMultiplier, _length);
int startLoadIndex = int(time/_timePerImage);
if (startLoadIndex>=int(_imageDataList.size())) startLoadIndex = int(_imageDataList.size())-1;
if (startLoadIndex<0) startLoadIndex = 0;
int endLoadIndex = int(preLoadTime/_timePerImage);
if (looping && (endLoadIndex>=int(_imageDataList.size()))) endLoadIndex -= int(_imageDataList.size());
if (endLoadIndex>=int(_imageDataList.size())) endLoadIndex = int(_imageDataList.size())-1;
if (endLoadIndex<0) endLoadIndex = 0;
double requestTime = time;
if (endLoadIndex<startLoadIndex)
2008-07-23 04:20:16 +08:00
{
for(int i=startLoadIndex; i<int(_imageDataList.size()); ++i)
2008-07-23 04:20:16 +08:00
{
if (!_imageDataList[i]._image)
2008-07-23 04:20:16 +08:00
{
irh->requestImageFile(_imageDataList[i]._filename, this, i, requestTime, fs, _imageDataList[i]._imageRequest, _readOptions.get());
2008-07-23 04:20:16 +08:00
}
requestTime += _timePerImage;
2008-07-23 04:20:16 +08:00
}
for(int i=0; i<=endLoadIndex; ++i)
{
if (!_imageDataList[i]._image)
{
irh->requestImageFile(_imageDataList[i]._filename, this, i, requestTime, fs, _imageDataList[i]._imageRequest, _readOptions.get());
}
requestTime += _timePerImage;
}
2008-07-23 04:20:16 +08:00
}
else
{
for(int i=startLoadIndex; i<=endLoadIndex; ++i)
{
if (!_imageDataList[i]._image)
{
irh->requestImageFile(_imageDataList[i]._filename, this, i, requestTime, fs, _imageDataList[i]._imageRequest, _readOptions.get());
}
requestTime += _timePerImage;
}
}
2008-07-22 01:28:22 +08:00
}
2008-07-21 17:46:53 +08:00
}