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

Revision 7648, 7.1 kB (checked in by robert, 6 years ago)

From Roland Smeenk, "Attached you will find a large set of small typo fixes (mainly in the comments)."

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