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

Revision 1815, 7.8 kB (checked in by robert, 12 years ago)

Added the usage report to the examples for when no arguments are passed
to the examples.

  • 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 <osgProducer/Viewer>
10
11
12// Creates a stateset which contains a 1D texture which is populated by contour banded color
13// this is then used in conjunction with TexGen to create contoured models, either in
14// object linear coords - like contours on a map, or eye linear which contour the distance from
15// the eye. An app callback toggles between the two tex gen modes.
16osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel)
17{
18   
19    const osg::BoundingSphere& bs = loadedModel->getBound();
20   
21    osg::Image* image = new osg::Image;
22
23    int noPixels = 1024;
24   
25    // allocate the image data, noPixels x 1 x 1 with 4 rgba floats - equivilant to a Vec4!
26    image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT);
27    image->setInternalTextureFormat(GL_RGBA);
28   
29    typedef std::vector<osg::Vec4> ColorBands;
30    ColorBands colorbands;
31    colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f));
32    colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f));
33    colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f));
34    colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f));
35    colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f));
36    colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f));
37    colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f));
38    colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f));
39
40    float nobands = colorbands.size();
41    float delta = nobands/(float)noPixels;
42    float pos = 0.0f;
43
44    // fill in the image data.   
45    osg::Vec4* dataPtr = (osg::Vec4*)image->data();
46    for(int i=0;i<noPixels;++i,pos+=delta)
47    {
48        //float p = floorf(pos);
49        //float r = pos-p;
50        //osg::Vec4 color = colorbands[(int)p]*(1.0f-r);
51        //if (p+1<colorbands.size()) color += colorbands[(int)p+1]*r;
52        osg::Vec4 color = colorbands[(int)pos];
53        *dataPtr++ = color;
54    }
55   
56    osg::Texture1D* texture = new osg::Texture1D;
57    texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR);
58    texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR);
59    texture->setImage(image);
60
61    float zBase = bs.center().z()-bs.radius();
62    float zScale = 2.0f/bs.radius();
63   
64    osg::TexGen* texgen = new osg::TexGen;
65    texgen->setMode(osg::TexGen::OBJECT_LINEAR);
66    texgen->setPlane(osg::TexGen::S,osg::Vec4(0.0f,0.0f,zScale,-zBase));
67   
68    osg::Material* material = new osg::Material;
69   
70    osg::StateSet* stateset = new osg::StateSet;
71   
72    stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE);
73    stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
74    stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
75    stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
76
77    stateset->setTextureAttribute(0,texgen,osg::StateAttribute::OVERRIDE);
78    stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
79   
80    stateset->setAttribute(material,osg::StateAttribute::OVERRIDE);
81   
82    return stateset;
83}
84
85
86// An app callback which alternates the tex gen mode between object linear and eye linear to illustrate what differences it makes.
87class AnimateStateCallback : public osg::NodeCallback
88{
89    public:
90        AnimateStateCallback() {}
91       
92        void animateState(osg::StateSet* stateset,double time)
93        {
94            // here we simply get any existing texgen, and then increment its
95            // plane, pushing the R coordinate through the texture.
96            osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN);
97            osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute);
98            if (texgen)
99            {
100                const double timeInterval = 1.0f;
101               
102                static double previousTime = time;
103                static bool state = false;
104                while (time>previousTime+timeInterval)
105                {
106                    previousTime+=timeInterval;
107                    state = !state;
108                }
109           
110                if (state)
111                {
112                    texgen->setMode(osg::TexGen::OBJECT_LINEAR);
113                }
114                else
115                {
116                    texgen->setMode(osg::TexGen::EYE_LINEAR);
117                }
118            }
119           
120        }
121
122        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
123        {
124
125            osg::StateSet* stateset = node->getStateSet();
126            if (stateset && nv->getFrameStamp())
127            {
128                // we have an exisitng stateset, so lets animate it.
129                animateState(stateset,nv->getFrameStamp()->getReferenceTime());
130            }
131
132            // note, callback is repsonsible for scenegraph traversal so
133            // should always include call the traverse(node,nv) to ensure
134            // that the rest of cullbacks and the scene graph are traversed.
135            traverse(node,nv);
136        }
137};
138
139
140int main( int argc, char **argv )
141{
142
143    // use an ArgumentParser object to manage the program arguments.
144    osg::ArgumentParser arguments(&argc,argv);
145
146    // set up the usage document, in case we need to print out how to use this program.
147    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
148    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
149   
150    // construct the viewer.
151    osgProducer::Viewer viewer(arguments);
152
153    // set up the value with sensible default event handlers.
154    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
155
156    // get details on keyboard and mouse bindings used by the viewer.
157    viewer.getUsage(*arguments.getApplicationUsage());
158
159    // if user request help write it out to cout.
160    if (arguments.read("-h") || arguments.read("--help"))
161    {
162        arguments.getApplicationUsage()->write(std::cout);
163        return 1;
164    }
165
166    // any option left unread are converted into errors to write out later.
167    arguments.reportRemainingOptionsAsUnrecognized();
168
169    // report any errors if they have occured when parsing the program aguments.
170    if (arguments.errors())
171    {
172        arguments.writeErrorMessages(std::cout);
173        return 1;
174    }
175   
176    if (arguments.argc()<=1)
177    {
178        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
179        return 1;
180    }
181
182    // load the images specified on command line
183    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
184
185   
186    if (loadedModel)
187    {
188
189        osg::StateSet* stateset = create1DTextureStateToDecorate(loadedModel);
190        if (!stateset)
191        {
192            std::cout<<"Error: failed to create 1D texture state."<<std::endl;
193            return 1;
194        }
195
196
197        loadedModel->setStateSet(stateset);
198        loadedModel->setUpdateCallback(new AnimateStateCallback());
199
200        // add model to viewer.
201        viewer.setSceneData( loadedModel );
202
203        // create the windows and run the threads.
204        viewer.realize(Producer::CameraGroup::ThreadPerCamera);
205
206        while( !viewer.done() )
207        {
208            // wait for all cull and draw threads to complete.
209            viewer.sync();
210
211            // update the scene by traversing it with the the update visitor which will
212            // call all node update callbacks and animations.
213            viewer.update();
214
215            // fire off the cull and draw traversals of the scene.
216            viewer.frame();
217
218        }
219   
220        // wait for all cull and draw threads to complete before exit.
221        viewer.sync();
222    }
223    else
224    {
225        osg::notify(osg::NOTICE)<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
226        return 0;
227    }
228   
229   
230   
231    return 0;
232}
Note: See TracBrowser for help on using the browser.