| [7902] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <osg/Notify> |
|---|
| 21 | |
|---|
| 22 | #include <osg/Texture2D> |
|---|
| [7903] | 23 | #include <osg/io_utils> |
|---|
| [7902] | 24 | |
|---|
| 25 | #include <osgDB/Registry> |
|---|
| 26 | #include <osgDB/ReadFile> |
|---|
| 27 | |
|---|
| 28 | #include <osgFX/MultiTextureControl> |
|---|
| 29 | |
|---|
| 30 | #include <osgGA/TerrainManipulator> |
|---|
| [7921] | 31 | #include <osgGA/StateSetManipulator> |
|---|
| [7987] | 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> |
|---|
| [7902] | 40 | |
|---|
| [8000] | 41 | #include <osgTerrain/Terrain> |
|---|
| [8840] | 42 | #include <osgTerrain/TerrainTile> |
|---|
| [7998] | 43 | |
|---|
| [7921] | 44 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 45 | #include <osgViewer/Viewer> |
|---|
| [7902] | 46 | |
|---|
| [7921] | 47 | |
|---|
| [7902] | 48 | #include <iostream> |
|---|
| 49 | |
|---|
| 50 | template<class T> |
|---|
| 51 | class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor |
|---|
| 52 | { |
|---|
| 53 | public: |
|---|
| 54 | FindTopMostNodeOfTypeVisitor(): |
|---|
| 55 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 56 | _foundNode(0) |
|---|
| 57 | {} |
|---|
| 58 | |
|---|
| 59 | void apply(osg::Node& node) |
|---|
| 60 | { |
|---|
| 61 | T* result = dynamic_cast<T*>(&node); |
|---|
| 62 | if (result) |
|---|
| 63 | { |
|---|
| 64 | _foundNode = result; |
|---|
| 65 | } |
|---|
| 66 | else |
|---|
| 67 | { |
|---|
| 68 | traverse(node); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | T* _foundNode; |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | template<class T> |
|---|
| 76 | T* findTopMostNodeOfType(osg::Node* node) |
|---|
| 77 | { |
|---|
| 78 | if (!node) return 0; |
|---|
| 79 | |
|---|
| 80 | FindTopMostNodeOfTypeVisitor<T> fnotv; |
|---|
| 81 | node->accept(fnotv); |
|---|
| 82 | |
|---|
| 83 | return fnotv._foundNode; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| [7903] | 86 | |
|---|
| 87 | class ElevationLayerBlendingCallback : public osg::NodeCallback |
|---|
| 88 | { |
|---|
| 89 | public: |
|---|
| 90 | |
|---|
| 91 | typedef std::vector<double> Elevations; |
|---|
| 92 | |
|---|
| 93 | ElevationLayerBlendingCallback(osgFX::MultiTextureControl* mtc, const Elevations& elevations, float animationTime=4.0f): |
|---|
| [12056] | 94 | _previousFrame(0), |
|---|
| [7903] | 95 | _previousTime(0.0), |
|---|
| [9395] | 96 | _animationTime(animationTime), |
|---|
| [8610] | 97 | _currentElevation(0.0), |
|---|
| [7903] | 98 | _mtc(mtc), |
|---|
| [9395] | 99 | _elevations(elevations) {} |
|---|
| [7903] | 100 | |
|---|
| 101 | |
|---|
| 102 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| [8610] | 103 | { |
|---|
| 104 | if (nv->getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| [7903] | 105 | { |
|---|
| 106 | |
|---|
| [8610] | 107 | float deltaTime = 0.01f; |
|---|
| [12056] | 108 | if (_previousFrame!=0) |
|---|
| [7903] | 109 | { |
|---|
| [8610] | 110 | deltaTime = float(nv->getFrameStamp()->getReferenceTime() - _previousTime); |
|---|
| [7903] | 111 | } |
|---|
| [8610] | 112 | |
|---|
| 113 | _previousTime = nv->getFrameStamp()->getReferenceTime(); |
|---|
| 114 | _previousFrame = nv->getFrameStamp()->getFrameNumber(); |
|---|
| 115 | |
|---|
| 116 | if (_mtc.valid() && !_elevations.empty()) |
|---|
| [7903] | 117 | { |
|---|
| [8610] | 118 | unsigned int index = _mtc->getNumTextureWeights()-1; |
|---|
| 119 | for(unsigned int i=0; i<_elevations.size(); ++i) |
|---|
| [7903] | 120 | { |
|---|
| [8610] | 121 | if (_currentElevation>_elevations[i]) |
|---|
| 122 | { |
|---|
| 123 | index = i; |
|---|
| 124 | break; |
|---|
| 125 | } |
|---|
| [7903] | 126 | } |
|---|
| [8610] | 127 | |
|---|
| 128 | float delta = std::min(deltaTime/_animationTime, 1.0f); |
|---|
| 129 | |
|---|
| 130 | for(unsigned int i=0; i<_mtc->getNumTextureWeights(); ++i) |
|---|
| [7903] | 131 | { |
|---|
| [8610] | 132 | float currentValue = _mtc->getTextureWeight(i); |
|---|
| 133 | float desiredValue = (i==index) ? 1.0f : 0.0f; |
|---|
| 134 | if (desiredValue != currentValue) |
|---|
| [7903] | 135 | { |
|---|
| [8610] | 136 | if (currentValue<desiredValue) |
|---|
| 137 | { |
|---|
| 138 | desiredValue = std::min(currentValue + delta, desiredValue); |
|---|
| 139 | } |
|---|
| 140 | else |
|---|
| 141 | { |
|---|
| 142 | desiredValue = std::max(currentValue - delta, desiredValue); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | _mtc->setTextureWeight(i, desiredValue); |
|---|
| [7903] | 146 | } |
|---|
| 147 | } |
|---|
| [8610] | 148 | |
|---|
| [7903] | 149 | } |
|---|
| 150 | } |
|---|
| [8610] | 151 | else if (nv->getVisitorType()==osg::NodeVisitor::CULL_VISITOR) |
|---|
| 152 | { |
|---|
| 153 | _currentElevation = nv->getViewPoint().z(); |
|---|
| 154 | |
|---|
| 155 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(node); |
|---|
| 156 | if (csn) |
|---|
| 157 | { |
|---|
| 158 | osg::EllipsoidModel* em = csn->getEllipsoidModel(); |
|---|
| 159 | if (em) |
|---|
| 160 | { |
|---|
| 161 | double X = nv->getViewPoint().x(); |
|---|
| 162 | double Y = nv->getViewPoint().y(); |
|---|
| 163 | double Z = nv->getViewPoint().z(); |
|---|
| 164 | double latitude, longitude; |
|---|
| 165 | em->convertXYZToLatLongHeight(X,Y,Z,latitude, longitude, _currentElevation); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| [7903] | 171 | traverse(node,nv); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| [12056] | 174 | unsigned int _previousFrame; |
|---|
| [7903] | 175 | double _previousTime; |
|---|
| 176 | float _animationTime; |
|---|
| [8610] | 177 | double _currentElevation; |
|---|
| 178 | |
|---|
| [7903] | 179 | osg::observer_ptr<osgFX::MultiTextureControl> _mtc; |
|---|
| 180 | Elevations _elevations; |
|---|
| 181 | }; |
|---|
| 182 | |
|---|
| 183 | |
|---|
| [8366] | 184 | |
|---|
| 185 | class TerrainHandler : public osgGA::GUIEventHandler { |
|---|
| 186 | public: |
|---|
| 187 | |
|---|
| 188 | TerrainHandler(osgTerrain::Terrain* terrain): |
|---|
| 189 | _terrain(terrain) {} |
|---|
| 190 | |
|---|
| 191 | bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) |
|---|
| 192 | { |
|---|
| 193 | switch(ea.getEventType()) |
|---|
| 194 | { |
|---|
| 195 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 196 | { |
|---|
| 197 | if (ea.getKey()=='r') |
|---|
| 198 | { |
|---|
| 199 | _terrain->setSampleRatio(_terrain->getSampleRatio()*0.5); |
|---|
| 200 | osg::notify(osg::NOTICE)<<"Sample ratio "<<_terrain->getSampleRatio()<<std::endl; |
|---|
| 201 | return true; |
|---|
| 202 | } |
|---|
| 203 | else if (ea.getKey()=='R') |
|---|
| 204 | { |
|---|
| 205 | _terrain->setSampleRatio(_terrain->getSampleRatio()/0.5); |
|---|
| 206 | osg::notify(osg::NOTICE)<<"Sample ratio "<<_terrain->getSampleRatio()<<std::endl; |
|---|
| 207 | return true; |
|---|
| 208 | } |
|---|
| 209 | else if (ea.getKey()=='v') |
|---|
| 210 | { |
|---|
| 211 | _terrain->setVerticalScale(_terrain->getVerticalScale()*1.25); |
|---|
| 212 | osg::notify(osg::NOTICE)<<"Vertical scale "<<_terrain->getVerticalScale()<<std::endl; |
|---|
| 213 | return true; |
|---|
| 214 | } |
|---|
| 215 | else if (ea.getKey()=='V') |
|---|
| 216 | { |
|---|
| 217 | _terrain->setVerticalScale(_terrain->getVerticalScale()/1.25); |
|---|
| 218 | osg::notify(osg::NOTICE)<<"Vertical scale "<<_terrain->getVerticalScale()<<std::endl; |
|---|
| 219 | return true; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | return false; |
|---|
| 223 | } |
|---|
| 224 | default: |
|---|
| 225 | return false; |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | protected: |
|---|
| 230 | |
|---|
| 231 | ~TerrainHandler() {} |
|---|
| 232 | |
|---|
| 233 | osg::ref_ptr<osgTerrain::Terrain> _terrain; |
|---|
| 234 | }; |
|---|
| 235 | |
|---|
| [7902] | 236 | int main( int argc, char **argv ) |
|---|
| 237 | { |
|---|
| 238 | |
|---|
| 239 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| [8639] | 240 | arguments.getApplicationUsage()->addCommandLineOption("-v","Set the terrain vertical scale."); |
|---|
| 241 | arguments.getApplicationUsage()->addCommandLineOption("-r","Set the terrain sample ratio."); |
|---|
| 242 | arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","Provide authentication information for http file access."); |
|---|
| [7902] | 243 | |
|---|
| [8844] | 244 | |
|---|
| 245 | osgViewer::Viewer viewer(arguments); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| [8840] | 248 | |
|---|
| [8844] | 249 | osg::ref_ptr<osgTerrain::WhiteListTileLoadedCallback> whiteList = new osgTerrain::WhiteListTileLoadedCallback; |
|---|
| [8843] | 250 | std::string setname; |
|---|
| 251 | while(arguments.read("--allow",setname)) |
|---|
| 252 | { |
|---|
| 253 | whiteList->allow(setname); |
|---|
| 254 | } |
|---|
| [8844] | 255 | while(arguments.read("--allow-all")) |
|---|
| 256 | { |
|---|
| 257 | whiteList->setAllowAll(true); |
|---|
| 258 | } |
|---|
| [8843] | 259 | osgTerrain::TerrainTile::setTileLoadedCallback(whiteList.get()); |
|---|
| [8844] | 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| [8366] | 263 | float verticalScale = 1.0f; |
|---|
| 264 | while(arguments.read("-v",verticalScale)) {} |
|---|
| 265 | |
|---|
| [8844] | 266 | |
|---|
| [8366] | 267 | float sampleRatio = 1.0f; |
|---|
| 268 | while(arguments.read("-r",sampleRatio)) {} |
|---|
| [7902] | 269 | |
|---|
| [8844] | 270 | |
|---|
| 271 | |
|---|
| [8639] | 272 | std::string url, username, password; |
|---|
| 273 | while(arguments.read("--login",url, username, password)) |
|---|
| 274 | { |
|---|
| 275 | if (!osgDB::Registry::instance()->getAuthenticationMap()) |
|---|
| 276 | { |
|---|
| 277 | osgDB::Registry::instance()->setAuthenticationMap(new osgDB::AuthenticationMap); |
|---|
| 278 | osgDB::Registry::instance()->getAuthenticationMap()->addAuthenticationDetails( |
|---|
| 279 | url, |
|---|
| 280 | new osgDB::AuthenticationDetails(username, password) |
|---|
| 281 | ); |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| [7902] | 284 | |
|---|
| [7921] | 285 | |
|---|
| 286 | { |
|---|
| 287 | |
|---|
| 288 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | viewer.addEventHandler(new osgViewer::LODScaleHandler); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| [8005] | 309 | |
|---|
| [7987] | 310 | { |
|---|
| 311 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 312 | |
|---|
| 313 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 314 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 315 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 316 | |
|---|
| 317 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 318 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 319 | |
|---|
| 320 | std::string pathfile; |
|---|
| 321 | char keyForAnimationPath = '5'; |
|---|
| 322 | while (arguments.read("-p",pathfile)) |
|---|
| 323 | { |
|---|
| 324 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 325 | if (apm || !apm->valid()) |
|---|
| 326 | { |
|---|
| 327 | num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 328 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 329 | ++keyForAnimationPath; |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 334 | |
|---|
| 335 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| [8005] | 338 | |
|---|
| 339 | { |
|---|
| 340 | |
|---|
| 341 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 342 | |
|---|
| 343 | if (!rootnode) |
|---|
| 344 | { |
|---|
| 345 | osg::notify(osg::NOTICE)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl; |
|---|
| 346 | return 1; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | osgTerrain::Terrain* terrain = findTopMostNodeOfType<osgTerrain::Terrain>(rootnode); |
|---|
| 350 | if (!terrain) |
|---|
| 351 | { |
|---|
| 352 | terrain = new osgTerrain::Terrain; |
|---|
| [11391] | 353 | |
|---|
| 354 | osg::CoordinateSystemNode* csn = findTopMostNodeOfType<osg::CoordinateSystemNode>(rootnode); |
|---|
| 355 | if (csn) |
|---|
| 356 | { |
|---|
| 357 | terrain->set(*csn); |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| [8005] | 360 | terrain->addChild(rootnode); |
|---|
| 361 | |
|---|
| 362 | rootnode = terrain; |
|---|
| [11391] | 363 | csn = terrain; |
|---|
| 364 | } |
|---|
| [8005] | 365 | |
|---|
| [8366] | 366 | terrain->setSampleRatio(sampleRatio); |
|---|
| 367 | terrain->setVerticalScale(verticalScale); |
|---|
| 368 | |
|---|
| [11391] | 369 | |
|---|
| [8366] | 370 | viewer.addEventHandler(new TerrainHandler(terrain)); |
|---|
| 371 | |
|---|
| [8005] | 372 | |
|---|
| 373 | unsigned int numLayers = 1; |
|---|
| 374 | osgFX::MultiTextureControl* mtc = findTopMostNodeOfType<osgFX::MultiTextureControl>(rootnode); |
|---|
| 375 | if (mtc) |
|---|
| 376 | { |
|---|
| [11391] | 377 | |
|---|
| [8005] | 378 | numLayers = mtc->getNumTextureWeights(); |
|---|
| [8844] | 379 | |
|---|
| 380 | |
|---|
| 381 | mtc->setTextureWeight(0,1.0f); |
|---|
| 382 | for(unsigned int i=1; i<numLayers; ++i) |
|---|
| 383 | { |
|---|
| 384 | mtc->setTextureWeight(i,0.0f); |
|---|
| 385 | } |
|---|
| [8005] | 386 | } |
|---|
| 387 | |
|---|
| 388 | if (numLayers<2) |
|---|
| 389 | { |
|---|
| 390 | osg::notify(osg::NOTICE)<<"Warning: scene must have MultiTextureControl node with at least 2 texture units defined."<<std::endl; |
|---|
| 391 | return 1; |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | double maxElevationTransition = 1e6; |
|---|
| 395 | ElevationLayerBlendingCallback::Elevations elevations; |
|---|
| 396 | for(unsigned int i=0; i<numLayers; ++i) |
|---|
| 397 | { |
|---|
| 398 | elevations.push_back(maxElevationTransition); |
|---|
| 399 | maxElevationTransition /= 2.0; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | |
|---|
| [11391] | 403 | |
|---|
| [8610] | 404 | |
|---|
| 405 | |
|---|
| [11391] | 406 | ElevationLayerBlendingCallback* elbc = new ElevationLayerBlendingCallback(mtc, elevations); |
|---|
| 407 | terrain->setUpdateCallback(elbc); |
|---|
| 408 | terrain->setCullCallback(elbc); |
|---|
| [8005] | 409 | |
|---|
| 410 | |
|---|
| 411 | viewer.setSceneData( rootnode ); |
|---|
| 412 | } |
|---|
| [7902] | 413 | |
|---|
| [8639] | 414 | |
|---|
| 415 | |
|---|
| [7902] | 416 | |
|---|
| 417 | viewer.realize(); |
|---|
| 418 | |
|---|
| 419 | return viewer.run(); |
|---|
| 420 | } |
|---|