Initial cut of osgimagesequence example

This commit is contained in:
Robert Osfield
2008-07-21 10:57:06 +00:00
parent d930308de4
commit ac61676368
5 changed files with 160 additions and 15 deletions

View File

@@ -40,6 +40,7 @@ IF(DYNAMIC_OPENSCENEGRAPH)
ADD_SUBDIRECTORY(osggeometryshaders)
ADD_SUBDIRECTORY(osghangglide)
ADD_SUBDIRECTORY(osghud)
ADD_SUBDIRECTORY(osgimagesequence)
ADD_SUBDIRECTORY(osgimpostor)
ADD_SUBDIRECTORY(osgintersection)
ADD_SUBDIRECTORY(osgkdtree)

View File

@@ -0,0 +1,4 @@
SET(TARGET_SRC osgimagesequence.cpp )
#### end var setup ###
SETUP_EXAMPLE(osgimagesequence)

View File

@@ -0,0 +1,136 @@
/* OpenSceneGraph example, osgtexture3D.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <osg/Node>
#include <osg/Geometry>
#include <osg/Notify>
#include <osg/Texture2D>
#include <osg/ImageSequence>
#include <osg/Geode>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <iostream>
//
// A simple demo demonstrating different texturing modes,
// including using of texture extensions.
//
typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
class MyGraphicsContext {
public:
MyGraphicsContext()
{
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)
{
traits->pbuffer = false;
_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
}
if (_gc.valid())
{
_gc->realize();
_gc->makeCurrent();
}
}
bool valid() const { return _gc.valid() && _gc->isRealized(); }
private:
osg::ref_ptr<osg::GraphicsContext> _gc;
};
osg::StateSet* createState()
{
// read 4 2d images
osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb");
osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb");
osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb");
osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg");
osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence;
imageSequence->addImage(image_0.get(), 0.25);
imageSequence->addImage(image_1.get(), 0.25);
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());
// create the StateSet to store the texture data
osg::StateSet* stateset = new osg::StateSet;
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
return stateset;
}
osg::Node* createModel()
{
// create the geometry of the model, just a simple 2d quad right now.
osg::Geode* geode = new osg::Geode;
geode->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0), osg::Vec3(1.0f,0.0f,0.0), osg::Vec3(0.0f,0.0f,1.0f)));
geode->setStateSet(createState());
return geode;
}
int main(int , char **)
{
// construct the viewer.
osgViewer::Viewer viewer;
// create a model from the images and pass it to the viewer.
viewer.setSceneData(createModel());
return viewer.run();
}

View File

@@ -17,7 +17,7 @@
#include <OpenThreads/Mutex>
#include <osg/ImageStream>
#include <map>
#include <list>
namespace osg {
@@ -48,14 +48,15 @@ class OSG_EXPORT ImageSequence : public ImageStream
virtual double getTimeMultiplier() const { return _timeMultiplier; }
typedef std::map<double, std::string> FileNameSequence;
typedef std::pair<double, osg::ref_ptr<osg::Image> > TimeImagePair;
typedef std::vector<TimeImagePair> TimeImageSequence;
typedef std::pair<osg::ref_ptr<osg::Image>, double> ImageDurationPair;
typedef std::pair<std::string, double> FileNameDurationPair;
void addImageFile(double time, std::string& fileName);
typedef std::list<ImageDurationPair> ImageDurationSequence;
typedef std::list<FileNameDurationPair> FileNameDurationSequence;
void addImage(double time, osg::Image* image);
void addImageFile(const std::string& fileName, double duration = 0.040);
void addImage(osg::Image* image, double duration = 0.040);
virtual void update(osg::FrameStamp* fs);
@@ -67,9 +68,9 @@ class OSG_EXPORT ImageSequence : public ImageStream
double _timeMultiplier;
OpenThreads::Mutex _mutex;
FileNameSequence _timeFileNameSequence;
TimeImageSequence _timeImageSequence;
OpenThreads::Mutex _mutex;
FileNameDurationSequence _fileNameDurationSequence;
ImageDurationSequence _imageDuationSequence;
};

View File

@@ -11,6 +11,7 @@
* OpenSceneGraph Public License for more details.
*/
#include <OpenThreads/ScopedLock>
#include <osg/ImageSequence>
using namespace osg;
@@ -33,17 +34,19 @@ int ImageSequence::compare(const Image& rhs) const
return ImageStream::compare(rhs);
}
void ImageSequence::addImageFile(double time, std::string& fileName)
void ImageSequence::addImageFile(const std::string& fileName, double duration)
{
_timeFileNameSequence[time] = fileName;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_fileNameDurationSequence.push_back(FileNameDurationPair(fileName, duration));
}
void ImageSequence::addImage(double time, osg::Image* image)
void ImageSequence::addImage(osg::Image* image, double duration)
{
_timeImageSequence.push_back(TimeImagePair(time,image));
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_imageDuationSequence.push_back(ImageDurationPair(image, duration));
}
void ImageSequence::update(osg::FrameStamp* fs)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
}