root/OpenSceneGraph/trunk/examples/osgtexture1D/osgtexture1D.cpp @ 6916

Revision 6916, 6.1 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, added fallbacks for when now command line args are provided.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Notify>
2#include <osg/Texture1D>
3#include <osg/TexGen>
4#include <osg/Material>
5
6#include <osgDB/Registry>
7#include <osgDB/ReadFile>
8
9#include <osgViewer/Viewer>
10
11#include <iostream>
12
13// Creates a stateset which contains a 1D texture which is populated by contour banded color
14// this is then used in conjunction with TexGen to create contoured models, either in
15// object linear coords - like contours on a map, or eye linear which contour the distance from
16// the eye. An app callback toggles between the two tex gen modes.
17osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel)
18{
19   
20    const osg::BoundingSphere& bs = loadedModel->getBound();
21   
22    osg::Image* image = new osg::Image;
23
24    int noPixels = 1024;
25   
26    // allocate the image data, noPixels x 1 x 1 with 4 rgba floats - equivilant to a Vec4!
27    image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT);
28    image->setInternalTextureFormat(GL_RGBA);
29   
30    typedef std::vector<osg::Vec4> ColorBands;
31    ColorBands colorbands;
32    colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f));
33    colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f));
34    colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f));
35    colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f));
36    colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f));
37    colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f));
38    colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f));
39    colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f));
40
41    float nobands = colorbands.size();
42    float delta = nobands/(float)noPixels;
43    float pos = 0.0f;
44
45    // fill in the image data.   
46    osg::Vec4* dataPtr = (osg::Vec4*)image->data();
47    for(int i=0;i<noPixels;++i,pos+=delta)
48    {
49        //float p = floorf(pos);
50        //float r = pos-p;
51        //osg::Vec4 color = colorbands[(int)p]*(1.0f-r);
52        //if (p+1<colorbands.size()) color += colorbands[(int)p+1]*r;
53        osg::Vec4 color = colorbands[(int)pos];
54        *dataPtr++ = color;
55    }
56   
57    osg::Texture1D* texture = new osg::Texture1D;
58    texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR);
59    texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR);
60    texture->setImage(image);
61
62    float zBase = bs.center().z()-bs.radius();
63    float zScale = 2.0f/bs.radius();
64   
65    osg::TexGen* texgen = new osg::TexGen;
66    texgen->setMode(osg::TexGen::OBJECT_LINEAR);
67    texgen->setPlane(osg::TexGen::S,osg::Plane(0.0f,0.0f,zScale,-zBase));
68   
69    osg::Material* material = new osg::Material;
70   
71    osg::StateSet* stateset = new osg::StateSet;
72   
73    stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE);
74    stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
75    stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
76    stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
77
78    stateset->setTextureAttribute(0,texgen,osg::StateAttribute::OVERRIDE);
79    stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
80   
81    stateset->setAttribute(material,osg::StateAttribute::OVERRIDE);
82   
83    return stateset;
84}
85
86
87// An app callback which alternates the tex gen mode between object linear and eye linear to illustrate what differences it makes.
88class AnimateStateCallback : public osg::NodeCallback
89{
90    public:
91        AnimateStateCallback() {}
92       
93        void animateState(osg::StateSet* stateset,double time)
94        {
95            // here we simply get any existing texgen, and then increment its
96            // plane, pushing the R coordinate through the texture.
97            osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN);
98            osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute);
99            if (texgen)
100            {
101                const double timeInterval = 1.0f;
102               
103                static double previousTime = time;
104                static bool state = false;
105                while (time>previousTime+timeInterval)
106                {
107                    previousTime+=timeInterval;
108                    state = !state;
109                }
110           
111                if (state)
112                {
113                    texgen->setMode(osg::TexGen::OBJECT_LINEAR);
114                }
115                else
116                {
117                    texgen->setMode(osg::TexGen::EYE_LINEAR);
118                }
119            }
120           
121        }
122
123        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
124        {
125
126            osg::StateSet* stateset = node->getStateSet();
127            if (stateset && nv->getFrameStamp())
128            {
129                // we have an exisitng stateset, so lets animate it.
130                animateState(stateset,nv->getFrameStamp()->getSimulationTime());
131            }
132
133            // note, callback is repsonsible for scenegraph traversal so
134            // should always include call the traverse(node,nv) to ensure
135            // that the rest of cullbacks and the scene graph are traversed.
136            traverse(node,nv);
137        }
138};
139
140
141int main( int argc, char **argv )
142{
143
144    // use an ArgumentParser object to manage the program arguments.
145    osg::ArgumentParser arguments(&argc,argv);
146   
147    // construct the viewer.
148    osgViewer::Viewer viewer;
149
150    // load the images specified on command line
151    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
152 
153    // if not loaded assume no arguments passed in, try use default mode instead.
154    if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osg");
155   
156    if (!loadedModel)
157    {
158        osg::notify(osg::NOTICE)<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
159        return 0;
160    }
161
162    osg::StateSet* stateset = create1DTextureStateToDecorate(loadedModel);
163    if (!stateset)
164    {
165        std::cout<<"Error: failed to create 1D texture state."<<std::endl;
166        return 1;
167    }
168
169
170    loadedModel->setStateSet(stateset);
171    loadedModel->setUpdateCallback(new AnimateStateCallback());
172
173    // add model to viewer.
174    viewer.setSceneData( loadedModel );
175
176    return viewer.run();
177}
Note: See TracBrowser for help on using the browser.