root/OpenSceneGraph/trunk/examples/osgmultitexturecontrol/osgmultitexturecontrol.cpp @ 7998

Revision 7998, 10.2 kB (checked in by robert, 7 years ago)

Introduce TerrainSystem? node which decorates a complete terrain model made up of Terrain tiles.

Line 
1/* OpenSceneGraph example, osgmultitexture.
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
20#include <osg/Notify>
21
22#include <osg/Texture2D>
23#include <osg/io_utils>
24
25#include <osgDB/Registry>
26#include <osgDB/ReadFile>
27
28#include <osgFX/MultiTextureControl>
29
30#include <osgGA/TerrainManipulator>
31#include <osgGA/StateSetManipulator>
32#include <osgGA/AnimationPathManipulator>
33#include <osgGA/TrackballManipulator>
34#include <osgGA/FlightManipulator>
35#include <osgGA/DriveManipulator>
36#include <osgGA/KeySwitchMatrixManipulator>
37#include <osgGA/StateSetManipulator>
38#include <osgGA/AnimationPathManipulator>
39#include <osgGA/TerrainManipulator>
40
41#include <osgTerrain/TerrainSystem>
42
43#include <osgViewer/ViewerEventHandlers>
44#include <osgViewer/Viewer>
45
46
47#include <iostream>
48
49template<class T>
50class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor
51{
52public:
53    FindTopMostNodeOfTypeVisitor():
54        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
55        _foundNode(0)
56    {}
57   
58    void apply(osg::Node& node)
59    {
60        T* result = dynamic_cast<T*>(&node);
61        if (result)
62        {
63            _foundNode = result;
64        }
65        else
66        {
67            traverse(node);
68        }
69    }
70   
71    T* _foundNode;
72};
73
74template<class T>
75T* findTopMostNodeOfType(osg::Node* node)
76{
77    if (!node) return 0;
78
79    FindTopMostNodeOfTypeVisitor<T> fnotv;
80    node->accept(fnotv);
81   
82    return fnotv._foundNode;
83}
84
85/** Callback used to track the elevation of the camera and update the texture weights in an MultiTextureControl node.*/
86class ElevationLayerBlendingCallback : public osg::NodeCallback
87{
88    public:
89   
90        typedef std::vector<double> Elevations;
91
92        ElevationLayerBlendingCallback(osgFX::MultiTextureControl* mtc, const Elevations& elevations, float animationTime=4.0f):
93            _previousFrame(-1),
94            _previousTime(0.0),
95            _mtc(mtc),
96            _elevations(elevations),
97            _animationTime(animationTime) {}
98   
99        /** Callback method called by the NodeVisitor when visiting a node.*/
100        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
101        {
102            if (!nv->getFrameStamp() || _previousFrame==nv->getFrameStamp()->getFrameNumber())
103            {
104                // we've already updated for this frame so no need to do it again, just traverse children.
105                traverse(node,nv);
106                return;
107            }
108           
109            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
110           
111            float deltaTime = 0.01f;
112            if (_previousFrame!=-1)
113            {
114                deltaTime = float(nv->getFrameStamp()->getReferenceTime() - _previousTime);
115            }
116           
117            _previousTime = nv->getFrameStamp()->getReferenceTime();
118            _previousFrame = nv->getFrameStamp()->getFrameNumber();
119
120            double elevation = nv->getViewPoint().z();
121       
122            osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(node);
123            if (csn)
124            {
125                osg::EllipsoidModel* em = csn->getEllipsoidModel();
126                if (em)
127                {
128                    double X = nv->getViewPoint().x();
129                    double Y = nv->getViewPoint().y();
130                    double Z = nv->getViewPoint().z();
131                    double latitude, longitude;
132                    em->convertXYZToLatLongHeight(X,Y,Z,latitude, longitude, elevation);
133                }
134            }
135       
136            if (_mtc.valid() && !_elevations.empty())
137            {
138                unsigned int index = _mtc->getNumTextureWeights()-1;
139                for(unsigned int i=0; i<_elevations.size(); ++i)
140                {
141                    if (elevation>_elevations[i])
142                    {
143                        index = i;
144                        break;
145                    }
146                }
147               
148                float delta = std::min(deltaTime/_animationTime, 1.0f);
149               
150                for(unsigned int i=0; i<_mtc->getNumTextureWeights(); ++i)
151                {
152                    float currentValue = _mtc->getTextureWeight(i);
153                    float desiredValue = (i==index) ? 1.0f : 0.0f;
154                    if (desiredValue != currentValue)
155                    {
156                        if (currentValue<desiredValue)
157                        {
158                            desiredValue = std::min(currentValue + delta, desiredValue);
159                        }
160                        else
161                        {
162                            desiredValue = std::max(currentValue - delta, desiredValue);
163                        }
164                   
165                        _mtc->setTextureWeight(i, desiredValue);
166                    }
167                }
168               
169            }
170       
171            traverse(node,nv);
172        }
173
174        int                                             _previousFrame;
175        double                                          _previousTime;
176        float                                           _animationTime;
177        osg::observer_ptr<osgFX::MultiTextureControl>   _mtc;
178        Elevations                                      _elevations;
179       
180        OpenThreads::Mutex                              _mutex;
181};
182
183
184int main( int argc, char **argv )
185{
186    // use an ArgumentParser object to manage the program arguments.
187    osg::ArgumentParser arguments(&argc,argv);
188   
189    // construct the viewer.
190    osgViewer::Viewer viewer(arguments);
191
192    // load the nodes from the commandline arguments.
193    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
194
195    if (!rootnode)
196    {
197        osg::notify(osg::NOTICE)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl;
198        return 1;
199    }
200   
201    osgTerrain::TerrainSystem* terrain = findTopMostNodeOfType<osgTerrain::TerrainSystem>(rootnode);
202    if (!terrain)
203    {
204        terrain = new osgTerrain::TerrainSystem;
205        terrain->addChild(rootnode);
206       
207        rootnode = terrain;
208    }   
209   
210    osg::CoordinateSystemNode* csn = findTopMostNodeOfType<osg::CoordinateSystemNode>(rootnode);
211
212    unsigned int numLayers = 1;
213    osgFX::MultiTextureControl* mtc = findTopMostNodeOfType<osgFX::MultiTextureControl>(rootnode);
214    if (mtc)
215    {
216        numLayers = mtc->getNumTextureWeights();
217    }
218   
219    if (numLayers<2)
220    {
221        osg::notify(osg::NOTICE)<<"Warning: scene must have MultiTextureControl node with at least 2 texture units defined."<<std::endl;
222        return 1;
223    }
224   
225    double maxElevationTransition = 1e6;
226    ElevationLayerBlendingCallback::Elevations elevations;
227    for(unsigned int i=0; i<numLayers; ++i)
228    {
229        elevations.push_back(maxElevationTransition);
230        maxElevationTransition /= 2.0;
231    }
232   
233    ElevationLayerBlendingCallback* elbc = new ElevationLayerBlendingCallback(mtc, elevations);
234
235    // assign to the most appropriate node (the CoordinateSystemNode is best as it provides the elevation on the globe.)
236    if (csn) csn->setCullCallback(elbc);   
237    else if (mtc) mtc->setCullCallback(elbc);
238    else rootnode->setCullCallback(elbc);
239
240    // add all the event handlers to the viewer
241    {
242        // add the state manipulator
243        viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
244
245        // add the thread model handler
246        viewer.addEventHandler(new osgViewer::ThreadingHandler);
247
248        // add the window size toggle handler
249        viewer.addEventHandler(new osgViewer::WindowSizeHandler);
250
251        // add the stats handler
252        viewer.addEventHandler(new osgViewer::StatsHandler);
253
254        // add the help handler
255        viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
256
257        // add the record camera path handler
258        viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
259
260        // add the LOD Scale handler
261        viewer.addEventHandler(new osgViewer::LODScaleHandler);
262    }
263
264    {
265        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
266
267        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
268        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
269        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
270
271        unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
272        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
273
274        std::string pathfile;
275        char keyForAnimationPath = '5';
276        while (arguments.read("-p",pathfile))
277        {
278            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
279            if (apm || !apm->valid())
280            {
281                num = keyswitchManipulator->getNumMatrixManipulators();
282                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
283                ++keyForAnimationPath;
284            }
285        }
286
287        keyswitchManipulator->selectMatrixManipulator(num);
288
289        viewer.setCameraManipulator( keyswitchManipulator.get() );
290    }
291
292    // add a viewport to the viewer and attach the scene graph.
293    viewer.setSceneData( rootnode );
294   
295    // create the windows and run the threads.
296    viewer.realize();
297
298    return viewer.run();
299}
Note: See TracBrowser for help on using the browser.