root/OpenSceneGraph/trunk/examples/osgtexture3D/osgtexture3D.cpp @ 5962

Revision 5962, 8.1 kB (checked in by robert, 7 years ago)

Ported following examples to osgViewer:

osgteapot
osgtessellate
osgtext
osgtexture1D
osgtexture2D
osgtexture3D
osgtexturerectangle
osgvertexprogram
osgvolume
osgwindows
osgsimple
osgkeyboardmouse

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Node>
2#include <osg/Geometry>
3#include <osg/Notify>
4#include <osg/Texture3D>
5#include <osg/TexGen>
6#include <osg/Geode>
7
8#include <osgDB/Registry>
9#include <osgDB/ReadFile>
10
11#include <osgViewer/Viewer>
12
13#include <iostream>
14
15//
16// A simple demo demonstrating different texturing modes,
17// including using of texture extensions.
18//
19
20
21typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
22
23
24class MyGraphicsContext {
25    public:
26        MyGraphicsContext()
27        {
28            osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
29            traits->x = 0;
30            traits->y = 0;
31            traits->width = 1;
32            traits->height = 1;
33            traits->windowDecoration = false;
34            traits->doubleBuffer = false;
35            traits->sharedContext = 0;
36            traits->pbuffer = true;
37
38            _gc = osg::GraphicsContext::createGraphicsContext(traits.get());
39
40            if (!_gc)
41            {
42                traits->pbuffer = false;
43                _gc = osg::GraphicsContext::createGraphicsContext(traits.get());
44            }
45
46            if (_gc.valid())
47            {
48                _gc->realize();
49                _gc->makeCurrent();
50            }
51        }
52       
53        bool valid() const { return _gc.valid() && _gc->isRealized(); }
54       
55    private:
56        osg::ref_ptr<osg::GraphicsContext> _gc;
57};
58
59
60osg::StateSet* createState()
61{
62    MyGraphicsContext gc;
63    if (!gc.valid())
64    {
65        osg::notify(osg::NOTICE)<<"Unable to create the graphics context required to build 3d image."<<std::endl;
66        return 0;
67    }
68
69    // read 4 2d images
70    osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb");
71    osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb");
72    osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb");
73    osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg");
74
75    if (!image_0 || !image_1 || !image_2 || !image_3)
76    {
77        std::cout << "Warning: could not open files."<<std::endl;
78        return new osg::StateSet;
79    }
80
81    if (image_0->getPixelFormat()!=image_1->getPixelFormat() || image_0->getPixelFormat()!=image_2->getPixelFormat() || image_0->getPixelFormat()!=image_3->getPixelFormat())
82    {
83        std::cout << "Warning: image pixel formats not compatible."<<std::endl;
84        return new osg::StateSet;
85    }
86
87    // get max 3D texture size
88    GLint textureSize = osg::Texture3D::getExtensions(0,true)->maxTexture3DSize();
89    if (textureSize > 256)
90        textureSize = 256;
91
92    // scale them all to the same size.
93    image_0->scaleImage(textureSize,textureSize,1);
94    image_1->scaleImage(textureSize,textureSize,1);
95    image_2->scaleImage(textureSize,textureSize,1);
96    image_3->scaleImage(textureSize,textureSize,1);
97
98
99    // then allocated a 3d image to use for texturing.
100    osg::Image* image_3d = new osg::Image;
101    image_3d->allocateImage(textureSize,textureSize,4,
102                            image_0->getPixelFormat(),image_0->getDataType());
103
104    // copy the 2d images into the 3d image.
105    image_3d->copySubImage(0,0,0,image_0.get());
106    image_3d->copySubImage(0,0,1,image_1.get());
107    image_3d->copySubImage(0,0,2,image_2.get());
108    image_3d->copySubImage(0,0,3,image_3.get());
109
110    image_3d->setInternalTextureFormat(image_0->getInternalTextureFormat());       
111
112    // set up the 3d texture itself,
113    // note, well set the filtering up so that mip mapping is disabled,
114    // gluBuild3DMipsmaps doesn't do a very good job of handled the
115    // inbalanced dimensions of the 256x256x4 texture.
116    osg::Texture3D* texture3D = new osg::Texture3D;
117    texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR);
118    texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR);
119    texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::REPEAT);
120    texture3D->setImage(image_3d);
121
122
123    // create a texgen to generate a R texture coordinate, the geometry
124    // itself will supply the S & T texture coordinates.
125    // in the animateStateSet callback well alter this R value to
126    // move the texture through the 3d texture, 3d texture filtering
127    // will do the blending for us.
128    osg::TexGen* texgen = new osg::TexGen;
129    texgen->setMode(osg::TexGen::OBJECT_LINEAR);
130    texgen->setPlane(osg::TexGen::R, osg::Plane(0.0f,0.0f,0.0f,0.2f));
131
132    // create the StateSet to store the texture data
133    osg::StateSet* stateset = new osg::StateSet;
134    stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON);
135    stateset->setTextureAttribute(0,texgen);
136    stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON);
137
138    return stateset;
139}
140
141
142class UpdateStateCallback : public osg::NodeCallback
143{
144    public:
145        UpdateStateCallback() {}
146       
147        void animateState(osg::StateSet* stateset)
148        {
149            // here we simply get any existing texgen, and then increment its
150            // plane, pushing the R coordinate through the texture.
151            osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN);
152            osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute);
153            if (texgen)
154            {
155                texgen->getPlane(osg::TexGen::R)[3] += 0.001f;
156            }
157
158        }
159
160        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
161        {
162
163            osg::StateSet* stateset = node->getStateSet();
164            if (stateset)
165            {
166                // we have an exisitng stateset, so lets animate it.
167                animateState(stateset);
168            }
169
170            // note, callback is repsonsible for scenegraph traversal so
171            // should always include call the traverse(node,nv) to ensure
172            // that the rest of cullbacks and the scene graph are traversed.
173            traverse(node,nv);
174        }     
175};
176
177/** create 2,2 square with center at 0,0,0 and aligned along the XZ plan */
178osg::Drawable* createSquare(float textureCoordMax=1.0f)
179{
180    // set up the Geometry.
181    osg::Geometry* geom = new osg::Geometry;
182
183    osg::Vec3Array* coords = new osg::Vec3Array(4);
184    (*coords)[0].set(-1.0f,0.0f,1.0f);
185    (*coords)[1].set(-1.0f,0.0f,-1.0f);
186    (*coords)[2].set(1.0f,0.0f,-1.0f);
187    (*coords)[3].set(1.0f,0.0f,1.0f);
188    geom->setVertexArray(coords);
189
190    osg::Vec3Array* norms = new osg::Vec3Array(1);
191    (*norms)[0].set(0.0f,-1.0f,0.0f);
192    geom->setNormalArray(norms);
193    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
194
195    osg::Vec2Array* tcoords = new osg::Vec2Array(4);
196    (*tcoords)[0].set(0.0f,textureCoordMax);
197    (*tcoords)[1].set(0.0f,0.0f);
198    (*tcoords)[2].set(textureCoordMax,0.0f);
199    (*tcoords)[3].set(textureCoordMax,textureCoordMax);
200    geom->setTexCoordArray(0,tcoords);
201   
202    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
203
204    return geom;
205}
206
207osg::Node* createModel()
208{
209
210    // create the geometry of the model, just a simple 2d quad right now.   
211    osg::Geode* geode = new osg::Geode;
212    geode->addDrawable(createSquare());
213
214    // normally we'd create the stateset's to contain all the textures
215    // etc here, but, the above technique uses osg::Image::scaleImage and
216    // osg::Image::copySubImage() which are implemented with OpenGL utility
217    // library, which unfortunately can't be used until we have a valid
218    // OpenGL context, and at this point in initilialization we don't have
219    // a valid OpenGL context, so we have to delay creation of state until
220    // there is a valid OpenGL context.  I'll manage this by using an
221    // app callback which will create the state during the first traversal.
222    // A bit hacky, and my plan is to reimplement the osg::scaleImage and
223    // osg::Image::copySubImage() without using GLU which will get round
224    // this current limitation.
225    geode->setUpdateCallback(new UpdateStateCallback());
226    geode->setStateSet(createState());
227   
228    return geode;
229
230}
231
232
233int main(int , char **)
234{
235    // construct the viewer.
236    osgViewer::Viewer viewer;
237
238    // create a model from the images and pass it to the viewer.
239    viewer.setSceneData(createModel());
240
241    return viewer.run();
242}
Note: See TracBrowser for help on using the browser.