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

Revision 1697, 7.7 kB (checked in by robert, 12 years ago)

Ported osgGLUT based src/Demos across to being osgProducer based, and placed
them in the new examples/ directory.

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