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

Revision 8758, 9.0 kB (checked in by robert, 6 years ago)

Changed osg::ImageSequence::set/getDuration to set/getLength() to be in keeping with the
osg::ImageStream?'s getLength().

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()
45{
46    osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence;
47
48    imageSequence->setLength(4.0);
49    imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posx.png"));
50    imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negx.png"));
51    imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posy.png"));
52    imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negy.png"));
53    imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posz.png"));
54    imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negz.png"));
55
56#if 1
57    osg::Texture2D* texture = new osg::Texture2D;
58    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
59    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
60    texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT);
61    texture->setResizeNonPowerOfTwoHint(false);
62    texture->setImage(imageSequence.get());
63    //texture->setTextureSize(512,512);
64   
65    //texture->setUpdateCallback(new osg::ImageSequence::UpdateCallback);
66#else   
67    osg::TextureRectangle* texture = new osg::TextureRectangle;
68    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
69    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
70    texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT);
71    // texture->setResizeNonPowerOfTwoHint(false);
72    texture->setImage(imageSequence.get());
73    //texture->setTextureSize(512,512);
74   
75    //texture->setUpdateCallback(new osg::ImageSequence::UpdateCallback);
76#endif
77
78    // create the StateSet to store the texture data
79    osg::StateSet* stateset = new osg::StateSet;
80
81    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
82
83    return stateset;
84}
85
86osg::Node* createModel()
87{
88
89    // create the geometry of the model, just a simple 2d quad right now.   
90    osg::Geode* geode = new osg::Geode;
91    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)));
92
93    geode->setStateSet(createState());
94   
95    return geode;
96
97}
98
99
100osg::ImageStream* s_imageStream = 0;
101class MovieEventHandler : public osgGA::GUIEventHandler
102{
103public:
104
105    MovieEventHandler():_trackMouse(false),_playToggle(true) {}
106   
107    void setMouseTracking(bool track) { _trackMouse = track; }
108    bool getMouseTracking() const { return _trackMouse; }
109   
110    void set(osg::Node* node);
111
112    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv);
113   
114    virtual void getUsage(osg::ApplicationUsage& usage) const;
115
116    typedef std::vector< osg::observer_ptr<osg::ImageStream> > ImageStreamList;
117
118protected:
119
120    virtual ~MovieEventHandler() {}
121
122    class FindImageStreamsVisitor : public osg::NodeVisitor
123    {
124    public:
125        FindImageStreamsVisitor(ImageStreamList& imageStreamList):
126            _imageStreamList(imageStreamList) {}
127           
128        virtual void apply(osg::Geode& geode)
129        {
130            apply(geode.getStateSet());
131
132            for(unsigned int i=0;i<geode.getNumDrawables();++i)
133            {
134                apply(geode.getDrawable(i)->getStateSet());
135            }
136       
137            traverse(geode);
138        }
139
140        virtual void apply(osg::Node& node)
141        {
142            apply(node.getStateSet());
143            traverse(node);
144        }
145       
146        inline void apply(osg::StateSet* stateset)
147        {
148            if (!stateset) return;
149           
150            osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE);
151            if (attr)
152            {
153                osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr);
154                if (texture2D) apply(dynamic_cast<osg::ImageStream*>(texture2D->getImage()));
155
156                osg::TextureRectangle* textureRec = dynamic_cast<osg::TextureRectangle*>(attr);
157                if (textureRec) apply(dynamic_cast<osg::ImageStream*>(textureRec->getImage()));
158            }
159        }
160       
161        inline void apply(osg::ImageStream* imagestream)
162        {
163            if (imagestream)
164            {
165                _imageStreamList.push_back(imagestream);
166                s_imageStream = imagestream;
167            }
168        }
169       
170        ImageStreamList& _imageStreamList;
171    };
172
173
174    bool            _playToggle;
175    bool            _trackMouse;
176    ImageStreamList _imageStreamList;
177   
178};
179
180
181
182void MovieEventHandler::set(osg::Node* node)
183{
184    _imageStreamList.clear();
185    if (node)
186    {
187        FindImageStreamsVisitor fisv(_imageStreamList);
188        node->accept(fisv);
189    }
190}
191
192
193bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
194{
195    switch(ea.getEventType())
196    {
197        case(osgGA::GUIEventAdapter::KEYDOWN):
198        {
199            if (ea.getKey()=='p')
200            {
201                for(ImageStreamList::iterator itr=_imageStreamList.begin();
202                    itr!=_imageStreamList.end();
203                    ++itr)
204                {
205                    if ((*itr)->getStatus()==osg::ImageStream::PLAYING)
206                    {
207                        // playing, so pause
208                        std::cout<<"Pause"<<std::endl;
209                        (*itr)->pause();
210                    }
211                    else
212                    {
213                        // playing, so pause
214                        std::cout<<"Play"<<std::endl;
215                        (*itr)->play();
216                    }
217                }
218                return true;
219            }
220            else if (ea.getKey()=='r')
221            {
222                for(ImageStreamList::iterator itr=_imageStreamList.begin();
223                    itr!=_imageStreamList.end();
224                    ++itr)
225                {
226                    std::cout<<"Restart"<<std::endl;
227                    (*itr)->rewind();
228                }
229                return true;
230            }
231            else if (ea.getKey()=='L')
232            {
233                for(ImageStreamList::iterator itr=_imageStreamList.begin();
234                    itr!=_imageStreamList.end();
235                    ++itr)
236                {
237                    if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING)
238                    {
239                        std::cout<<"Toggle Looping Off"<<std::endl;
240                        (*itr)->setLoopingMode( osg::ImageStream::NO_LOOPING );
241                    }
242                    else
243                    {
244                        std::cout<<"Toggle Looping On"<<std::endl;
245                        (*itr)->setLoopingMode( osg::ImageStream::LOOPING );
246                    }
247                }
248                return true;
249            }
250            return false;
251        }
252
253        default:
254            return false;
255    }
256    return false;
257}
258
259void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const
260{
261    usage.addKeyboardMouseBinding("p","Play/Pause movie");
262    usage.addKeyboardMouseBinding("r","Restart movie");
263    usage.addKeyboardMouseBinding("l","Toggle looping of movie");
264}
265
266
267
268
269int main(int argc, char **argv)
270{
271    osg::ArgumentParser arguments(&argc,argv);
272
273    // construct the viewer.
274    osgViewer::Viewer viewer(arguments);
275
276    // create a model from the images and pass it to the viewer.
277    viewer.setSceneData(createModel());
278
279    // pass the model to the MovieEventHandler so it can pick out ImageStream's to manipulate.
280    MovieEventHandler* meh = new MovieEventHandler();
281    meh->set( viewer.getSceneData() );
282    viewer.addEventHandler( meh );
283
284
285    std::string filename;
286    if (arguments.read("-o",filename))
287    {
288        osgDB::writeNodeFile(*viewer.getSceneData(),filename);
289    }
290
291    return viewer.run();
292}
Note: See TracBrowser for help on using the browser.