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

Revision 7987, 9.8 kB (checked in by robert, 6 years ago)

Added full range of cameras manipulators to osgmultitexturecontrol example

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