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

Revision 8637, 4.4 kB (checked in by robert, 6 years ago)

Initial cut of osgimagesequence example

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
38
39typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
40
41
42class MyGraphicsContext {
43    public:
44        MyGraphicsContext()
45        {
46            osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
47            traits->x = 0;
48            traits->y = 0;
49            traits->width = 1;
50            traits->height = 1;
51            traits->windowDecoration = false;
52            traits->doubleBuffer = false;
53            traits->sharedContext = 0;
54            traits->pbuffer = true;
55
56            _gc = osg::GraphicsContext::createGraphicsContext(traits.get());
57
58            if (!_gc)
59            {
60                traits->pbuffer = false;
61                _gc = osg::GraphicsContext::createGraphicsContext(traits.get());
62            }
63
64            if (_gc.valid())
65            {
66                _gc->realize();
67                _gc->makeCurrent();
68            }
69        }
70       
71        bool valid() const { return _gc.valid() && _gc->isRealized(); }
72       
73    private:
74        osg::ref_ptr<osg::GraphicsContext> _gc;
75};
76
77
78osg::StateSet* createState()
79{
80    // read 4 2d images
81    osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb");
82    osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb");
83    osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb");
84    osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg");
85
86    osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence;
87    imageSequence->addImage(image_0.get(), 0.25);
88    imageSequence->addImage(image_1.get(), 0.25);
89    imageSequence->addImage(image_2.get(), 0.25);
90    imageSequence->addImage(image_3.get(), 0.25);
91
92    imageSequence->setImage(image_0->s(),image_0->t(),image_0->r(),
93                  image_0->getInternalTextureFormat(),
94                  image_0->getPixelFormat(),image_0->getDataType(),
95                  image_0->data(),
96                  osg::Image::NO_DELETE,
97                  image_0->getPacking());
98
99    osg::Texture2D* texture = new osg::Texture2D;
100    texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
101    texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
102    texture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT);
103    texture->setResizeNonPowerOfTwoHint(false);
104    texture->setImage(imageSequence.get());
105
106    // create the StateSet to store the texture data
107    osg::StateSet* stateset = new osg::StateSet;
108    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
109
110    return stateset;
111}
112
113osg::Node* createModel()
114{
115
116    // create the geometry of the model, just a simple 2d quad right now.   
117    osg::Geode* geode = new osg::Geode;
118    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)));
119
120    geode->setStateSet(createState());
121   
122    return geode;
123
124}
125
126
127int main(int , char **)
128{
129    // construct the viewer.
130    osgViewer::Viewer viewer;
131
132    // create a model from the images and pass it to the viewer.
133    viewer.setSceneData(createModel());
134
135    return viewer.run();
136}
Note: See TracBrowser for help on using the browser.