Added basic image sequencing

This commit is contained in:
Robert Osfield 2008-07-21 17:28:22 +00:00
parent f088d113f6
commit acd7e65687
7 changed files with 119 additions and 60 deletions

View File

@ -35,46 +35,20 @@
// including using of texture extensions.
//
typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
class MyGraphicsContext {
public:
MyGraphicsContext()
struct ImageUpdateCallback : public osg::StateAttribute::Callback
{
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 0;
traits->y = 0;
traits->width = 1;
traits->height = 1;
traits->windowDecoration = false;
traits->doubleBuffer = false;
traits->sharedContext = 0;
traits->pbuffer = true;
_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
if (!_gc)
/** do customized update code.*/
virtual void operator () (osg::StateAttribute* attr, osg::NodeVisitor* nv)
{
traits->pbuffer = false;
_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
}
if (_gc.valid())
const osg::FrameStamp* fs = nv!=0 ? nv->getFrameStamp() : 0;
osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr);
if (texture2D && texture2D->getImage() && fs)
{
_gc->realize();
_gc->makeCurrent();
texture2D->getImage()->update(fs);
}
}
bool valid() const { return _gc.valid() && _gc->isRealized(); }
private:
osg::ref_ptr<osg::GraphicsContext> _gc;
};
osg::StateSet* createState()
{
// read 4 2d images
@ -89,22 +63,19 @@ osg::StateSet* createState()
imageSequence->addImage(image_2.get(), 0.25);
imageSequence->addImage(image_3.get(), 0.25);
imageSequence->setImage(image_0->s(),image_0->t(),image_0->r(),
image_0->getInternalTextureFormat(),
image_0->getPixelFormat(),image_0->getDataType(),
image_0->data(),
osg::Image::NO_DELETE,
image_0->getPacking());
osg::Texture2D* texture = new osg::Texture2D;
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
texture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT);
texture->setResizeNonPowerOfTwoHint(false);
texture->setImage(imageSequence.get());
//texture->setTextureSize(512,512);
texture->setUpdateCallback(new ImageUpdateCallback);
// create the StateSet to store the texture data
osg::StateSet* stateset = new osg::StateSet;
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
return stateset;

View File

@ -18,6 +18,7 @@
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/FrameStamp>
#include <string>
#include <vector>
@ -294,6 +295,8 @@ class OSG_EXPORT Image : public Object
/** Get the const PixelBufferObject.*/
const PixelBufferObject* getPixelBufferObject() const { return _bufferObject.get(); }
virtual void update(const osg::FrameStamp* fs) {}
protected :
virtual ~Image();

View File

@ -58,20 +58,24 @@ class OSG_EXPORT ImageSequence : public ImageStream
void addImage(osg::Image* image, double duration = 0.040);
virtual void update(osg::FrameStamp* fs);
virtual void update(const osg::FrameStamp* fs);
protected:
virtual ~ImageSequence() {}
void setImageToChild(const osg::Image* image);
double _referenceTime;
double _timeMultiplier;
OpenThreads::Mutex _mutex;
FileNameDurationSequence _fileNameDurationSequence;
ImageDurationSequence _imageDuationSequence;
ImageDurationSequence::iterator _imageIterator;
double _imageIteratorTime;
};

View File

@ -15,7 +15,6 @@
#define OSG_IMAGESTREAM 1
#include <osg/Image>
#include <osg/FrameStamp>
namespace osg {
@ -79,7 +78,6 @@ class OSG_EXPORT ImageStream : public Image
virtual void setVolume(float) {}
virtual float getVolume() const { return 0.0f; }
virtual void update(osg::FrameStamp* fs) {}
protected:
virtual void applyLoopingMode() {}

View File

@ -13,13 +13,19 @@
#include <OpenThreads/ScopedLock>
#include <osg/ImageSequence>
#include <osg/Notify>
#include <osg/Camera>
#include <math.h>
using namespace osg;
ImageSequence::ImageSequence()
{
_referenceTime = 0.0;
_referenceTime = DBL_MAX;
_imageIteratorTime = DBL_MAX;
_timeMultiplier = 1.0;
_imageIterator = _imageDuationSequence.end();
}
ImageSequence::ImageSequence(const ImageSequence& is,const CopyOp& copyop):
@ -36,17 +42,81 @@ int ImageSequence::compare(const Image& rhs) const
void ImageSequence::addImageFile(const std::string& fileName, double duration)
{
if (duration<0.01) duration = 0.01;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_fileNameDurationSequence.push_back(FileNameDurationPair(fileName, duration));
}
void ImageSequence::addImage(osg::Image* image, double duration)
{
if (duration<0.01) duration = 0.01;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_imageDuationSequence.push_back(ImageDurationPair(image, duration));
if (_imageIterator==_imageDuationSequence.end())
{
_imageIterator = _imageDuationSequence.begin();
_imageIteratorTime = _referenceTime;
setImageToChild(_imageIterator->first.get());
}
void ImageSequence::update(osg::FrameStamp* fs)
}
void ImageSequence::setImageToChild(const osg::Image* image)
{
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());
}
void ImageSequence::update(const osg::FrameStamp* fs)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
// osg::notify(osg::NOTICE)<<"ImageSequence::update("<<fs<<")"<<std::endl;
if (_referenceTime == DBL_MAX)
{
_referenceTime = fs->getSimulationTime();
}
if (_imageIteratorTime == DBL_MAX)
{
_imageIteratorTime = _referenceTime;
}
double time = (fs->getSimulationTime() - _referenceTime)*_timeMultiplier;
// osg::notify(osg::NOTICE)<<"time = "<<time<<std::endl;
if (_imageIterator!=_imageDuationSequence.end())
{
// osg::notify(osg::NOTICE)<<" _imageIteratorTime = "<<_imageIteratorTime<<std::endl;
while(time > (_imageIteratorTime + _imageIterator->second))
{
_imageIteratorTime += _imageIterator->second;
// osg::notify(osg::NOTICE)<<" _imageIteratorTime = "<<_imageIteratorTime<<std::endl;
++_imageIterator;
if (_imageIterator ==_imageDuationSequence.end())
{
// return iterator to begining of set.
_imageIterator = _imageDuationSequence.begin();
}
}
}
if (_imageIterator==_imageDuationSequence.end())
{
_imageIterator = _imageDuationSequence.begin();
}
if (_imageIterator!=_imageDuationSequence.end())
{
setImageToChild(_imageIterator->first.get());
}
}

View File

@ -12,6 +12,7 @@
#include <osg/BufferObject>
#include <osg/CopyOp>
#include <osg/FrameStamp>
#include <osg/Image>
#include <osg/Object>
#include <osg/Vec2>
@ -351,6 +352,21 @@ BEGIN_OBJECT_REFLECTOR(osg::Image)
__C5_PixelBufferObject_P1__getPixelBufferObject,
"Get the const PixelBufferObject. ",
"");
I_Method1(void, setCallUpdateInDraw, IN, bool, flag,
Properties::NON_VIRTUAL,
__void__setCallUpdateInDraw__bool,
"",
"");
I_Method0(bool, getCallUpdateInDraw,
Properties::NON_VIRTUAL,
__bool__getCallUpdateInDraw,
"",
"");
I_Method1(void, update, IN, const osg::FrameStamp *, fs,
Properties::VIRTUAL,
__void__update__C5_osg_FrameStamp_P1,
"",
"");
I_StaticMethod1(bool, isPackedType, IN, GLenum, type,
__bool__isPackedType__GLenum_S,
"",
@ -394,6 +410,9 @@ BEGIN_OBJECT_REFLECTOR(osg::Image)
I_SimpleProperty(osg::Image::AllocationMode, AllocationMode,
__AllocationMode__getAllocationMode,
__void__setAllocationMode__AllocationMode);
I_SimpleProperty(bool, CallUpdateInDraw,
__bool__getCallUpdateInDraw,
__void__setCallUpdateInDraw__bool);
I_SimpleProperty(GLenum, DataType,
__GLenum__getDataType,
__void__setDataType__GLenum);

View File

@ -11,7 +11,6 @@
#include <osgIntrospection/Attributes>
#include <osg/CopyOp>
#include <osg/FrameStamp>
#include <osg/Image>
#include <osg/ImageStream>
#include <osg/Object>
@ -148,11 +147,6 @@ BEGIN_OBJECT_REFLECTOR(osg::ImageStream)
__float__getVolume,
"",
"");
I_Method1(void, update, IN, osg::FrameStamp *, fs,
Properties::VIRTUAL,
__void__update__osg_FrameStamp_P1,
"",
"");
I_ProtectedMethod0(void, applyLoopingMode,
Properties::VIRTUAL,
Properties::NON_CONST,