root/OpenSceneGraph/trunk/examples/osgimagesequence/osgimagesequence.cpp @ 8841

Revision 8841, 9.6 kB (checked in by robert, 6 years ago)

Added support for reading source image file names from the command line.

Line 
1/* OpenSceneGraph example, osgtexture3D.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Node>
20#include <osg/Geometry>
21#include <osg/Notify>
22#include <osg/Texture1D>
23#include <osg/Texture2D>
24#include <osg/Texture3D>
25#include <osg/TextureRectangle>
26#include <osg/ImageSequence>
27#include <osg/Geode>
28
29#include <osgDB/Registry>
30#include <osgDB/ReadFile>
31#include <osgDB/WriteFile>
32
33#include <osgViewer/Viewer>
34
35#include <iostream>
36
37
38
39
40//
41// A simple demo demonstrating how to set on an animated texture using an osg::ImageSequence
42//
43
44osg::StateSet* createState(osg::ArgumentParser& arguments)
45{
46    osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence;
47   
48    if (arguments.argc()>1)
49    {
50        for(unsigned int i=1; i<arguments.argc(); ++i)
51        {
52            osg::ref_ptr<osg::Image> image = osgDB::readImageFile(arguments[i]);
53            if (image.valid())
54            {
55                imageSequence->addImage(image.get());
56            }
57        }
58        imageSequence->setLength(float(imageSequence->getImages().size())*0.1f);
59    }
60    else
61    {
62        imageSequence->setLength(4.0);
63        imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posx.png"));
64        imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negx.png"));
65        imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posy.png"));
66        imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negy.png"));
67        imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posz.png"));
68        imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negz.png"));
69    }
70       
71    // start the image sequence playing
72    imageSequence->play();
73
74#if 1
75    osg::Texture2D* texture = new osg::Texture2D;
76    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
77    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
78    texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT);
79    texture->setResizeNonPowerOfTwoHint(false);
80    texture->setImage(imageSequence.get());
81    //texture->setTextureSize(512,512);
82   
83    //texture->setUpdateCallback(new osg::ImageSequence::UpdateCallback);
84#else   
85    osg::TextureRectangle* texture = new osg::TextureRectangle;
86    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
87    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
88    texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT);
89    // texture->setResizeNonPowerOfTwoHint(false);
90    texture->setImage(imageSequence.get());
91    //texture->setTextureSize(512,512);
92   
93    //texture->setUpdateCallback(new osg::ImageSequence::UpdateCallback);
94#endif
95
96    // create the StateSet to store the texture data
97    osg::StateSet* stateset = new osg::StateSet;
98
99    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
100
101    return stateset;
102}
103
104osg::Node* createModel(osg::ArgumentParser& arguments)
105{
106
107    // create the geometry of the model, just a simple 2d quad right now.   
108    osg::Geode* geode = new osg::Geode;
109    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)));
110
111    geode->setStateSet(createState(arguments));
112   
113    return geode;
114
115}
116
117
118osg::ImageStream* s_imageStream = 0;
119class MovieEventHandler : public osgGA::GUIEventHandler
120{
121public:
122
123    MovieEventHandler():_trackMouse(false),_playToggle(true) {}
124   
125    void setMouseTracking(bool track) { _trackMouse = track; }
126    bool getMouseTracking() const { return _trackMouse; }
127   
128    void set(osg::Node* node);
129
130    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv);
131   
132    virtual void getUsage(osg::ApplicationUsage& usage) const;
133
134    typedef std::vector< osg::observer_ptr<osg::ImageStream> > ImageStreamList;
135
136protected:
137
138    virtual ~MovieEventHandler() {}
139
140    class FindImageStreamsVisitor : public osg::NodeVisitor
141    {
142    public:
143        FindImageStreamsVisitor(ImageStreamList& imageStreamList):
144            _imageStreamList(imageStreamList) {}
145           
146        virtual void apply(osg::Geode& geode)
147        {
148            apply(geode.getStateSet());
149
150            for(unsigned int i=0;i<geode.getNumDrawables();++i)
151            {
152                apply(geode.getDrawable(i)->getStateSet());
153            }
154       
155            traverse(geode);
156        }
157
158        virtual void apply(osg::Node& node)
159        {
160            apply(node.getStateSet());
161            traverse(node);
162        }
163       
164        inline void apply(osg::StateSet* stateset)
165        {
166            if (!stateset) return;
167           
168            osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE);
169            if (attr)
170            {
171                osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr);
172                if (texture2D) apply(dynamic_cast<osg::ImageStream*>(texture2D->getImage()));
173
174                osg::TextureRectangle* textureRec = dynamic_cast<osg::TextureRectangle*>(attr);
175                if (textureRec) apply(dynamic_cast<osg::ImageStream*>(textureRec->getImage()));
176            }
177        }
178       
179        inline void apply(osg::ImageStream* imagestream)
180        {
181            if (imagestream)
182            {
183                _imageStreamList.push_back(imagestream);
184                s_imageStream = imagestream;
185            }
186        }
187       
188        ImageStreamList& _imageStreamList;
189    };
190
191
192    bool            _playToggle;
193    bool            _trackMouse;
194    ImageStreamList _imageStreamList;
195   
196};
197
198
199
200void MovieEventHandler::set(osg::Node* node)
201{
202    _imageStreamList.clear();
203    if (node)
204    {
205        FindImageStreamsVisitor fisv(_imageStreamList);
206        node->accept(fisv);
207    }
208}
209
210
211bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
212{
213    switch(ea.getEventType())
214    {
215        case(osgGA::GUIEventAdapter::KEYDOWN):
216        {
217            if (ea.getKey()=='p')
218            {
219                for(ImageStreamList::iterator itr=_imageStreamList.begin();
220                    itr!=_imageStreamList.end();
221                    ++itr)
222                {
223                    if ((*itr)->getStatus()==osg::ImageStream::PLAYING)
224                    {
225                        // playing, so pause
226                        std::cout<<"Pause"<<std::endl;
227                        (*itr)->pause();
228                    }
229                    else
230                    {
231                        // playing, so pause
232                        std::cout<<"Play"<<std::endl;
233                        (*itr)->play();
234                    }
235                }
236                return true;
237            }
238            else if (ea.getKey()=='r')
239            {
240                for(ImageStreamList::iterator itr=_imageStreamList.begin();
241                    itr!=_imageStreamList.end();
242                    ++itr)
243                {
244                    std::cout<<"Restart"<<std::endl;
245                    (*itr)->rewind();
246                }
247                return true;
248            }
249            else if (ea.getKey()=='L')
250            {
251                for(ImageStreamList::iterator itr=_imageStreamList.begin();
252                    itr!=_imageStreamList.end();
253                    ++itr)
254                {
255                    if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING)
256                    {
257                        std::cout<<"Toggle Looping Off"<<std::endl;
258                        (*itr)->setLoopingMode( osg::ImageStream::NO_LOOPING );
259                    }
260                    else
261                    {
262                        std::cout<<"Toggle Looping On"<<std::endl;
263                        (*itr)->setLoopingMode( osg::ImageStream::LOOPING );
264                    }
265                }
266                return true;
267            }
268            return false;
269        }
270
271        default:
272            return false;
273    }
274    return false;
275}
276
277void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const
278{
279    usage.addKeyboardMouseBinding("p","Play/Pause movie");
280    usage.addKeyboardMouseBinding("r","Restart movie");
281    usage.addKeyboardMouseBinding("l","Toggle looping of movie");
282}
283
284
285
286
287int main(int argc, char **argv)
288{
289    osg::ArgumentParser arguments(&argc,argv);
290
291    // construct the viewer.
292    osgViewer::Viewer viewer(arguments);
293
294    std::string filename;
295    arguments.read("-o",filename);
296
297    // create a model from the images and pass it to the viewer.
298    viewer.setSceneData(createModel(arguments));
299
300    // pass the model to the MovieEventHandler so it can pick out ImageStream's to manipulate.
301    MovieEventHandler* meh = new MovieEventHandler();
302    meh->set( viewer.getSceneData() );
303    viewer.addEventHandler( meh );
304
305
306    if (!filename.empty())
307    {
308        osgDB::writeNodeFile(*viewer.getSceneData(),filename);
309    }
310
311    return viewer.run();
312}
Note: See TracBrowser for help on using the browser.