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

Revision 8835, 9.1 kB (checked in by robert, 6 years ago)

Added playing of the imagesequence on creation of the imagesequence

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