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

Revision 8918, 10.8 kB (checked in by robert, 6 years ago)

Added --page-and-discard, --page-and-retain, --preload and --length command line options

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