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

Revision 8642, 3.6 kB (checked in by robert, 6 years ago)

Added basic image sequencing

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/Texture2D>
23#include <osg/ImageSequence>
24#include <osg/Geode>
25
26#include <osgDB/Registry>
27#include <osgDB/ReadFile>
28
29#include <osgViewer/Viewer>
30
31#include <iostream>
32
33//
34// A simple demo demonstrating different texturing modes,
35// including using of texture extensions.
36//
37
38struct ImageUpdateCallback : public osg::StateAttribute::Callback
39{
40    /** do customized update code.*/
41    virtual void operator () (osg::StateAttribute* attr, osg::NodeVisitor* nv)
42    {
43        const osg::FrameStamp* fs = nv!=0 ? nv->getFrameStamp() : 0;
44        osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr);
45        if (texture2D && texture2D->getImage() && fs)
46        {
47            texture2D->getImage()->update(fs);
48        }
49    }
50};
51
52osg::StateSet* createState()
53{
54    // read 4 2d images
55    osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb");
56    osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb");
57    osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb");
58    osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg");
59
60    osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence;
61    imageSequence->addImage(image_0.get(), 0.25);
62    imageSequence->addImage(image_1.get(), 0.25);
63    imageSequence->addImage(image_2.get(), 0.25);
64    imageSequence->addImage(image_3.get(), 0.25);
65
66    osg::Texture2D* texture = new osg::Texture2D;
67    texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
68    texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
69    texture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT);
70    texture->setResizeNonPowerOfTwoHint(false);
71    texture->setImage(imageSequence.get());
72    //texture->setTextureSize(512,512);
73   
74    texture->setUpdateCallback(new ImageUpdateCallback);
75
76    // create the StateSet to store the texture data
77    osg::StateSet* stateset = new osg::StateSet;
78
79    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
80
81    return stateset;
82}
83
84osg::Node* createModel()
85{
86
87    // create the geometry of the model, just a simple 2d quad right now.   
88    osg::Geode* geode = new osg::Geode;
89    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)));
90
91    geode->setStateSet(createState());
92   
93    return geode;
94
95}
96
97
98int main(int , char **)
99{
100    // construct the viewer.
101    osgViewer::Viewer viewer;
102
103    // create a model from the images and pass it to the viewer.
104    viewer.setSceneData(createModel());
105
106    return viewer.run();
107}
Note: See TracBrowser for help on using the browser.