1
/* OpenSceneGraph example, osgmultitexture.
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:
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
22
#include <osg/Texture2D>
23
#include <osg/io_utils>
25
#include <osgDB/Registry>
26
#include <osgDB/ReadFile>
28
#include <osgFX/MultiTextureControl>
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>
41
#include <osgTerrain/Terrain>
43
#include <osgViewer/ViewerEventHandlers>
44
#include <osgViewer/Viewer>
50
class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor
53
FindTopMostNodeOfTypeVisitor():
54
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
58
void apply(osg::Node& node)
60
T* result = dynamic_cast<T*>(&node);
75
T* findTopMostNodeOfType(osg::Node* node)
79
FindTopMostNodeOfTypeVisitor<T> fnotv;
82
return fnotv._foundNode;
85
/** Callback used to track the elevation of the camera and update the texture weights in an MultiTextureControl node.*/
86
class ElevationLayerBlendingCallback : public osg::NodeCallback
90
typedef std::vector<double> Elevations;
92
ElevationLayerBlendingCallback(osgFX::MultiTextureControl* mtc, const Elevations& elevations, float animationTime=4.0f):
96
_elevations(elevations),
97
_animationTime(animationTime) {}
99
/** Callback method called by the NodeVisitor when visiting a node.*/
100
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
102
if (!nv->getFrameStamp() || _previousFrame==nv->getFrameStamp()->getFrameNumber())
104
// we've already updated for this frame so no need to do it again, just traverse children.
109
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
111
float deltaTime = 0.01f;
112
if (_previousFrame!=-1)
114
deltaTime = float(nv->getFrameStamp()->getReferenceTime() - _previousTime);
117
_previousTime = nv->getFrameStamp()->getReferenceTime();
118
_previousFrame = nv->getFrameStamp()->getFrameNumber();
120
double elevation = nv->getViewPoint().z();
122
osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(node);
125
osg::EllipsoidModel* em = csn->getEllipsoidModel();
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);
136
if (_mtc.valid() && !_elevations.empty())
138
unsigned int index = _mtc->getNumTextureWeights()-1;
139
for(unsigned int i=0; i<_elevations.size(); ++i)
141
if (elevation>_elevations[i])
148
float delta = std::min(deltaTime/_animationTime, 1.0f);
150
for(unsigned int i=0; i<_mtc->getNumTextureWeights(); ++i)
152
float currentValue = _mtc->getTextureWeight(i);
153
float desiredValue = (i==index) ? 1.0f : 0.0f;
154
if (desiredValue != currentValue)
156
if (currentValue<desiredValue)
158
desiredValue = std::min(currentValue + delta, desiredValue);
162
desiredValue = std::max(currentValue - delta, desiredValue);
165
_mtc->setTextureWeight(i, desiredValue);
175
double _previousTime;
176
float _animationTime;
177
osg::observer_ptr<osgFX::MultiTextureControl> _mtc;
178
Elevations _elevations;
180
OpenThreads::Mutex _mutex;
184
int main( int argc, char **argv )
186
// use an ArgumentParser object to manage the program arguments.
187
osg::ArgumentParser arguments(&argc,argv);
189
// construct the viewer.
190
osgViewer::Viewer viewer(arguments);
193
// add all the event handlers to the viewer
195
// add the state manipulator
196
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
198
// add the thread model handler
199
viewer.addEventHandler(new osgViewer::ThreadingHandler);
201
// add the window size toggle handler
202
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
204
// add the stats handler
205
viewer.addEventHandler(new osgViewer::StatsHandler);
207
// add the help handler
208
viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
210
// add the record camera path handler
211
viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
213
// add the LOD Scale handler
214
viewer.addEventHandler(new osgViewer::LODScaleHandler);
217
// add all the camera manipulators
219
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
221
keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
222
keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
223
keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
225
unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
226
keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
228
std::string pathfile;
229
char keyForAnimationPath = '5';
230
while (arguments.read("-p",pathfile))
232
osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
233
if (apm || !apm->valid())
235
num = keyswitchManipulator->getNumMatrixManipulators();
236
keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
237
++keyForAnimationPath;
241
keyswitchManipulator->selectMatrixManipulator(num);
243
viewer.setCameraManipulator( keyswitchManipulator.get() );
246
// set up the scene graph
248
// load the nodes from the commandline arguments.
249
osg::Node* rootnode = osgDB::readNodeFiles(arguments);
253
osg::notify(osg::NOTICE)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl;
257
osgTerrain::Terrain* terrain = findTopMostNodeOfType<osgTerrain::Terrain>(rootnode);
260
terrain = new osgTerrain::Terrain;
261
terrain->addChild(rootnode);
262
terrain->setSampleRatio(0.25f);
267
osg::CoordinateSystemNode* csn = findTopMostNodeOfType<osg::CoordinateSystemNode>(rootnode);
269
unsigned int numLayers = 1;
270
osgFX::MultiTextureControl* mtc = findTopMostNodeOfType<osgFX::MultiTextureControl>(rootnode);
273
numLayers = mtc->getNumTextureWeights();
278
osg::notify(osg::NOTICE)<<"Warning: scene must have MultiTextureControl node with at least 2 texture units defined."<<std::endl;
282
double maxElevationTransition = 1e6;
283
ElevationLayerBlendingCallback::Elevations elevations;
284
for(unsigned int i=0; i<numLayers; ++i)
286
elevations.push_back(maxElevationTransition);
287
maxElevationTransition /= 2.0;
290
ElevationLayerBlendingCallback* elbc = new ElevationLayerBlendingCallback(mtc, elevations);
292
// assign to the most appropriate node (the CoordinateSystemNode is best as it provides the elevation on the globe.)
293
if (csn) csn->setCullCallback(elbc);
294
else if (mtc) mtc->setCullCallback(elbc);
295
else rootnode->setCullCallback(elbc);
297
// add a viewport to the viewer and attach the scene graph.
298
viewer.setSceneData( rootnode );
301
// create the windows and run the threads.