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

Revision 12529, 7.0 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • 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/TexGenNode>
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// and allows tex gen to override the S texture coordinate
33osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel)
34{
35    osg::Image* image = new osg::Image;
36
37    int noPixels = 1024;
38   
39    // allocate the image data, noPixels x 1 x 1 with 4 rgba floats - equivalent to a Vec4!
40    image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT);
41    image->setInternalTextureFormat(GL_RGBA);
42   
43    typedef std::vector<osg::Vec4> ColorBands;
44    ColorBands colorbands;
45    colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f));
46    colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f));
47    colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f));
48    colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f));
49    colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f));
50    colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f));
51    colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f));
52    colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f));
53
54    float nobands = colorbands.size();
55    float delta = nobands/(float)noPixels;
56    float pos = 0.0f;
57
58    // fill in the image data.   
59    osg::Vec4* dataPtr = (osg::Vec4*)image->data();
60    for(int i=0;i<noPixels;++i,pos+=delta)
61    {
62        //float p = floorf(pos);
63        //float r = pos-p;
64        //osg::Vec4 color = colorbands[(int)p]*(1.0f-r);
65        //if (p+1<colorbands.size()) color += colorbands[(int)p+1]*r;
66        osg::Vec4 color = colorbands[(int)pos];
67        *dataPtr++ = color;
68    }
69   
70    osg::Texture1D* texture = new osg::Texture1D;
71    texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR);
72    texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR);
73    texture->setImage(image);
74   
75    osg::Material* material = new osg::Material;
76   
77    osg::StateSet* stateset = new osg::StateSet;
78   
79    stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE);
80    stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
81    stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
82    stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
83
84    stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
85   
86    stateset->setAttribute(material,osg::StateAttribute::OVERRIDE);
87   
88    return stateset;
89}
90
91
92// An app callback which alternates the tex gen mode between object linear and eye linear to illustrate what differences it makes.
93class AnimateTexGenCallback : public osg::NodeCallback
94{
95    public:
96        AnimateTexGenCallback() {}
97       
98        void animateTexGen(osg::TexGenNode* texgenNode,double time)
99        {
100            // here we simply get any existing texgen, and then increment its
101            // plane, pushing the R coordinate through the texture.
102            const double timeInterval = 2.0f;
103           
104            static double previousTime = time;
105            static bool state = false;
106            while (time>previousTime+timeInterval)
107            {
108                previousTime+=timeInterval;
109                state = !state;
110            }
111       
112            if (state)
113            {
114                texgenNode->getTexGen()->setMode(osg::TexGen::OBJECT_LINEAR);
115            }
116            else
117            {
118                texgenNode->getTexGen()->setMode(osg::TexGen::EYE_LINEAR);
119            }
120           
121        }
122
123        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
124        {
125            osg::TexGenNode* texgenNode = dynamic_cast<osg::TexGenNode*>(node);
126
127            if (texgenNode && nv->getFrameStamp())
128            {
129                // we have an exisitng stateset, so lets animate it.
130                animateTexGen(texgenNode,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.osgt");
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
172    osg:: Group *root = new osg:: Group;
173    root -> addChild( loadedModel );
174
175    // The contour banded color texture is used in conjunction with TexGenNode
176    // to create contoured models, either in object linear coords - like
177    // contours on a map, or eye linear which contour the distance from
178    // the eye. An app callback toggles between the two tex gen modes.
179    osg::TexGenNode* texgenNode = new osg::TexGenNode;
180    texgenNode->setReferenceFrame( osg::TexGenNode::ABSOLUTE_RF );
181    texgenNode->getTexGen()->setMode( osg::TexGen::OBJECT_LINEAR );
182
183    const osg::BoundingSphere& bs = loadedModel->getBound();
184    float zBase = bs.center().z()-bs.radius();
185    float zScale = 2.0f/bs.radius();
186    texgenNode->getTexGen()->setPlane(osg::TexGen::S,osg::Plane(0.0f,0.0f,zScale,-zBase));
187
188    texgenNode->setUpdateCallback(new AnimateTexGenCallback());
189
190    root -> addChild( texgenNode );
191
192
193    viewer.setSceneData( root );
194
195    return viewer.run();
196}
Note: See TracBrowser for help on using the browser.