From ac616763685e605e3fa71825b29c3abc51d83c34 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 21 Jul 2008 10:57:06 +0000 Subject: [PATCH] Initial cut of osgimagesequence example --- examples/CMakeLists.txt | 1 + examples/osgimagesequence/CMakeLists.txt | 4 + .../osgimagesequence/osgimagesequence.cpp | 136 ++++++++++++++++++ include/osg/ImageSequence | 21 +-- src/osg/ImageSequence.cpp | 13 +- 5 files changed, 160 insertions(+), 15 deletions(-) create mode 100644 examples/osgimagesequence/CMakeLists.txt create mode 100644 examples/osgimagesequence/osgimagesequence.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1fb4c3ea2..16c41a8d8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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) diff --git a/examples/osgimagesequence/CMakeLists.txt b/examples/osgimagesequence/CMakeLists.txt new file mode 100644 index 000000000..d4763bea7 --- /dev/null +++ b/examples/osgimagesequence/CMakeLists.txt @@ -0,0 +1,4 @@ +SET(TARGET_SRC osgimagesequence.cpp ) + +#### end var setup ### +SETUP_EXAMPLE(osgimagesequence) diff --git a/examples/osgimagesequence/osgimagesequence.cpp b/examples/osgimagesequence/osgimagesequence.cpp new file mode 100644 index 000000000..41bb3dc7b --- /dev/null +++ b/examples/osgimagesequence/osgimagesequence.cpp @@ -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 +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include + +// +// A simple demo demonstrating different texturing modes, +// including using of texture extensions. +// + + +typedef std::vector< osg::ref_ptr > ImageList; + + +class MyGraphicsContext { + public: + MyGraphicsContext() + { + osg::ref_ptr 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 _gc; +}; + + +osg::StateSet* createState() +{ + // read 4 2d images + osg::ref_ptr image_0 = osgDB::readImageFile("Images/lz.rgb"); + osg::ref_ptr image_1 = osgDB::readImageFile("Images/reflect.rgb"); + osg::ref_ptr image_2 = osgDB::readImageFile("Images/tank.rgb"); + osg::ref_ptr image_3 = osgDB::readImageFile("Images/skymap.jpg"); + + osg::ref_ptr 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(); +} diff --git a/include/osg/ImageSequence b/include/osg/ImageSequence index 962db5655..49a11d5eb 100644 --- a/include/osg/ImageSequence +++ b/include/osg/ImageSequence @@ -17,7 +17,7 @@ #include #include -#include +#include namespace osg { @@ -48,14 +48,15 @@ class OSG_EXPORT ImageSequence : public ImageStream virtual double getTimeMultiplier() const { return _timeMultiplier; } - typedef std::map FileNameSequence; - - typedef std::pair > TimeImagePair; - typedef std::vector TimeImageSequence; + typedef std::pair, double> ImageDurationPair; + typedef std::pair FileNameDurationPair; - void addImageFile(double time, std::string& fileName); + typedef std::list ImageDurationSequence; + typedef std::list 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; }; diff --git a/src/osg/ImageSequence.cpp b/src/osg/ImageSequence.cpp index 50dc03aad..ee4417482 100644 --- a/src/osg/ImageSequence.cpp +++ b/src/osg/ImageSequence.cpp @@ -11,6 +11,7 @@ * OpenSceneGraph Public License for more details. */ +#include #include 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 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 lock(_mutex); + _imageDuationSequence.push_back(ImageDurationPair(image, duration)); } void ImageSequence::update(osg::FrameStamp* fs) { - + OpenThreads::ScopedLock lock(_mutex); }