| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <OpenThreads/Block> |
|---|
| 20 | |
|---|
| 21 | #include <osg/Group> |
|---|
| 22 | #include <osg/Geode> |
|---|
| 23 | #include <osg/ShapeDrawable> |
|---|
| 24 | #include <osg/Texture2D> |
|---|
| 25 | #include <osg/PositionAttitudeTransform> |
|---|
| 26 | #include <osg/MatrixTransform> |
|---|
| 27 | #include <osg/CoordinateSystemNode> |
|---|
| 28 | #include <osg/ClusterCullingCallback> |
|---|
| 29 | #include <osg/ArgumentParser> |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | #include <osgDB/FileUtils> |
|---|
| 33 | #include <osgDB/fstream> |
|---|
| 34 | #include <osgDB/ReadFile> |
|---|
| 35 | |
|---|
| 36 | #include <osgUtil/IncrementalCompileOperation> |
|---|
| 37 | |
|---|
| 38 | #include <osgText/FadeText> |
|---|
| 39 | |
|---|
| 40 | #include <osgViewer/Viewer> |
|---|
| 41 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 42 | |
|---|
| 43 | #include <osgGA/TrackballManipulator> |
|---|
| 44 | #include <osgGA/FlightManipulator> |
|---|
| 45 | #include <osgGA/DriveManipulator> |
|---|
| 46 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 47 | #include <osgGA/StateSetManipulator> |
|---|
| 48 | #include <osgGA/AnimationPathManipulator> |
|---|
| 49 | #include <osgGA/TerrainManipulator> |
|---|
| 50 | |
|---|
| 51 | #include <osgTerrain/TerrainTile> |
|---|
| 52 | #include <osgTerrain/GeometryTechnique> |
|---|
| 53 | #include <osgTerrain/Layer> |
|---|
| 54 | |
|---|
| 55 | #include <iostream> |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | typedef std::vector< osg::ref_ptr<osg::GraphicsThread> > GraphicsThreads; |
|---|
| 59 | |
|---|
| 60 | struct ReleaseBlockOnCompileCompleted : public osgUtil::IncrementalCompileOperation::CompileCompletedCallback |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | ReleaseBlockOnCompileCompleted(osg::RefBlockCount* block): |
|---|
| 64 | _block(block) {} |
|---|
| 65 | |
|---|
| 66 | virtual bool compileCompleted(osgUtil::IncrementalCompileOperation::CompileSet* compileSet) |
|---|
| 67 | { |
|---|
| 68 | if (_block.valid()) _block->completed(); |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | osg::notify(osg::NOTICE)<<"compileCompleted("<<compileSet<<")"<<std::endl; |
|---|
| 73 | |
|---|
| 74 | return true; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | osg::ref_ptr<osg::RefBlockCount> _block; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | class LoadAndCompileOperation : public osg::Operation |
|---|
| 82 | { |
|---|
| 83 | public: |
|---|
| 84 | |
|---|
| 85 | LoadAndCompileOperation(const std::string& filename, osgUtil::IncrementalCompileOperation* ico , osg::RefBlockCount* block): |
|---|
| 86 | Operation("Load and compile Operation", false), |
|---|
| 87 | _filename(filename), |
|---|
| 88 | _incrementalCompileOperation(ico), |
|---|
| 89 | _block(block) {} |
|---|
| 90 | |
|---|
| 91 | virtual void operator () (osg::Object* object) |
|---|
| 92 | { |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | _loadedModel = osgDB::readNodeFile(_filename); |
|---|
| 96 | |
|---|
| 97 | if (_loadedModel.valid() && _incrementalCompileOperation.valid()) |
|---|
| 98 | { |
|---|
| 99 | osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet = |
|---|
| 100 | new osgUtil::IncrementalCompileOperation::CompileSet(_loadedModel.get()); |
|---|
| 101 | |
|---|
| 102 | compileSet->_compileCompletedCallback = new ReleaseBlockOnCompileCompleted(_block.get()); |
|---|
| 103 | |
|---|
| 104 | _incrementalCompileOperation->add(compileSet.get()); |
|---|
| 105 | } |
|---|
| 106 | else |
|---|
| 107 | { |
|---|
| 108 | if (_block.valid()) _block->completed(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | std::string _filename; |
|---|
| 115 | osg::ref_ptr<osg::Node> _loadedModel; |
|---|
| 116 | osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation; |
|---|
| 117 | osg::ref_ptr<osg::RefBlockCount> _block; |
|---|
| 118 | |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | class MasterOperation : public osg::Operation |
|---|
| 122 | { |
|---|
| 123 | public: |
|---|
| 124 | |
|---|
| 125 | typedef std::set<std::string> Files; |
|---|
| 126 | typedef std::map<std::string, osg::ref_ptr<osg::Node> > FilenameNodeMap; |
|---|
| 127 | typedef std::vector< osg::ref_ptr<osg::Node> > Nodes; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | MasterOperation(const std::string& filename, osgUtil::IncrementalCompileOperation* ico): |
|---|
| 131 | Operation("Master reading operation",true), |
|---|
| 132 | _filename(filename), |
|---|
| 133 | _incrementalCompileOperation(ico) |
|---|
| 134 | { |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | void setOperationQueue(osg::OperationQueue* oq) { _operationQueue = oq; } |
|---|
| 139 | |
|---|
| 140 | osg::OperationQueue* getOperationQueue() { return _operationQueue.get(); } |
|---|
| 141 | |
|---|
| 142 | bool readMasterFile(Files& files) const |
|---|
| 143 | { |
|---|
| 144 | osgDB::ifstream fin(_filename.c_str()); |
|---|
| 145 | if (fin) |
|---|
| 146 | { |
|---|
| 147 | osgDB::Input fr; |
|---|
| 148 | fr.attach(&fin); |
|---|
| 149 | |
|---|
| 150 | bool readFilename = false; |
|---|
| 151 | |
|---|
| 152 | while(!fr.eof()) |
|---|
| 153 | { |
|---|
| 154 | bool itrAdvanced = false; |
|---|
| 155 | if (fr.matchSequence("file %s") || fr.matchSequence("file %w") ) |
|---|
| 156 | { |
|---|
| 157 | files.insert(fr[1].getStr()); |
|---|
| 158 | fr += 2; |
|---|
| 159 | itrAdvanced = true; |
|---|
| 160 | readFilename = true; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | if (!itrAdvanced) |
|---|
| 164 | { |
|---|
| 165 | ++fr; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | return readFilename; |
|---|
| 170 | } |
|---|
| 171 | return false; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | bool open(osg::Group* group) |
|---|
| 175 | { |
|---|
| 176 | Files files; |
|---|
| 177 | readMasterFile(files); |
|---|
| 178 | for(Files::iterator itr = files.begin(); |
|---|
| 179 | itr != files.end(); |
|---|
| 180 | ++itr) |
|---|
| 181 | { |
|---|
| 182 | osg::Node* model = osgDB::readNodeFile(*itr); |
|---|
| 183 | if (model) |
|---|
| 184 | { |
|---|
| 185 | osg::notify(osg::NOTICE)<<"open: Loaded file "<<*itr<<std::endl; |
|---|
| 186 | group->addChild(model); |
|---|
| 187 | _existingFilenameNodeMap[*itr] = model; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | return true; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | virtual void operator () (osg::Object* callingObject) |
|---|
| 196 | { |
|---|
| 197 | |
|---|
| 198 | osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(callingObject); |
|---|
| 199 | |
|---|
| 200 | if (viewer) update(viewer->getSceneData()); |
|---|
| 201 | else load(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | void load() |
|---|
| 205 | { |
|---|
| 206 | |
|---|
| 207 | Files filesA; |
|---|
| 208 | Files filesB; |
|---|
| 209 | |
|---|
| 210 | readMasterFile(filesB); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | do |
|---|
| 215 | { |
|---|
| 216 | OpenThreads::Thread::microSleep(100000); |
|---|
| 217 | |
|---|
| 218 | filesB.swap(filesA); |
|---|
| 219 | filesB.clear(); |
|---|
| 220 | readMasterFile(filesB); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | } while (filesA!=filesB); |
|---|
| 225 | |
|---|
| 226 | Files files; |
|---|
| 227 | files.swap(filesB); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | Files newFiles; |
|---|
| 233 | Files removedFiles; |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | { |
|---|
| 237 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 238 | |
|---|
| 239 | for(Files::iterator fitr = files.begin(); |
|---|
| 240 | fitr != files.end(); |
|---|
| 241 | ++fitr) |
|---|
| 242 | { |
|---|
| 243 | if (_existingFilenameNodeMap.count(*fitr)==0) newFiles.insert(*fitr); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | for(FilenameNodeMap::iterator litr = _existingFilenameNodeMap.begin(); |
|---|
| 247 | litr != _existingFilenameNodeMap.end(); |
|---|
| 248 | ++litr) |
|---|
| 249 | { |
|---|
| 250 | if (files.count(litr->first)==0) |
|---|
| 251 | { |
|---|
| 252 | removedFiles.insert(litr->first); |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | #if 0 |
|---|
| 258 | if (!newFiles.empty() || !removedFiles.empty()) |
|---|
| 259 | { |
|---|
| 260 | osg::notify(osg::NOTICE)<<std::endl<<std::endl<<"void operator () files.size()="<<files.size()<<std::endl; |
|---|
| 261 | } |
|---|
| 262 | #endif |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | FilenameNodeMap nodesToAdd; |
|---|
| 266 | if (!newFiles.empty()) |
|---|
| 267 | { |
|---|
| 268 | |
|---|
| 269 | typedef std::vector< osg::ref_ptr<osg::GraphicsThread> > GraphicsThreads; |
|---|
| 270 | GraphicsThreads threads; |
|---|
| 271 | |
|---|
| 272 | for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i) |
|---|
| 273 | { |
|---|
| 274 | osg::GraphicsContext* gc = osg::GraphicsContext::getCompileContext(i); |
|---|
| 275 | osg::GraphicsThread* gt = gc ? gc->getGraphicsThread() : 0; |
|---|
| 276 | if (gt) threads.push_back(gt); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | if (_operationQueue.valid()) |
|---|
| 280 | { |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | _endOfLoadBlock = new osg::RefBlockCount(newFiles.size()); |
|---|
| 284 | |
|---|
| 285 | _endOfLoadBlock->reset(); |
|---|
| 286 | |
|---|
| 287 | typedef std::list< osg::ref_ptr<LoadAndCompileOperation> > LoadAndCompileList; |
|---|
| 288 | LoadAndCompileList loadAndCompileList; |
|---|
| 289 | |
|---|
| 290 | for(Files::iterator nitr = newFiles.begin(); |
|---|
| 291 | nitr != newFiles.end(); |
|---|
| 292 | ++nitr) |
|---|
| 293 | { |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | osg::ref_ptr<LoadAndCompileOperation> loadAndCompile = new LoadAndCompileOperation( *nitr, _incrementalCompileOperation.get(), _endOfLoadBlock.get() ); |
|---|
| 297 | loadAndCompileList.push_back(loadAndCompile); |
|---|
| 298 | _operationQueue->add( loadAndCompile.get() ); |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | #if 1 |
|---|
| 302 | osg::ref_ptr<osg::Operation> operation; |
|---|
| 303 | while ((operation=_operationQueue->getNextOperation()).valid()) |
|---|
| 304 | { |
|---|
| 305 | |
|---|
| 306 | (*operation)(0); |
|---|
| 307 | } |
|---|
| 308 | #endif |
|---|
| 309 | |
|---|
| 310 | _endOfLoadBlock->block(); |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | for(LoadAndCompileList::iterator litr = loadAndCompileList.begin(); |
|---|
| 314 | litr != loadAndCompileList.end(); |
|---|
| 315 | ++litr) |
|---|
| 316 | { |
|---|
| 317 | if ((*litr)->_loadedModel.valid()) |
|---|
| 318 | { |
|---|
| 319 | nodesToAdd[(*litr)->_filename] = (*litr)->_loadedModel; |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | else |
|---|
| 326 | { |
|---|
| 327 | |
|---|
| 328 | _endOfLoadBlock = new osg::RefBlockCount(newFiles.size()); |
|---|
| 329 | |
|---|
| 330 | _endOfLoadBlock->reset(); |
|---|
| 331 | |
|---|
| 332 | for(Files::iterator nitr = newFiles.begin(); |
|---|
| 333 | nitr != newFiles.end(); |
|---|
| 334 | ++nitr) |
|---|
| 335 | { |
|---|
| 336 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(*nitr); |
|---|
| 337 | |
|---|
| 338 | if (loadedModel.get()) |
|---|
| 339 | { |
|---|
| 340 | nodesToAdd[*nitr] = loadedModel; |
|---|
| 341 | |
|---|
| 342 | if (_incrementalCompileOperation.valid()) |
|---|
| 343 | { |
|---|
| 344 | osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet = |
|---|
| 345 | new osgUtil::IncrementalCompileOperation::CompileSet(loadedModel.get()); |
|---|
| 346 | |
|---|
| 347 | compileSet->_compileCompletedCallback = new ReleaseBlockOnCompileCompleted(_endOfLoadBlock.get()); |
|---|
| 348 | |
|---|
| 349 | _incrementalCompileOperation->add(compileSet.get()); |
|---|
| 350 | } |
|---|
| 351 | else |
|---|
| 352 | { |
|---|
| 353 | _endOfLoadBlock->completed(); |
|---|
| 354 | } |
|---|
| 355 | } |
|---|
| 356 | else |
|---|
| 357 | { |
|---|
| 358 | _endOfLoadBlock->completed(); |
|---|
| 359 | } |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | _endOfLoadBlock->block(); |
|---|
| 363 | |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | bool requiresBlock = false; |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | if (!removedFiles.empty() || !nodesToAdd.empty()) |
|---|
| 374 | { |
|---|
| 375 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 376 | _nodesToRemove.swap(removedFiles); |
|---|
| 377 | _nodesToAdd.swap(nodesToAdd); |
|---|
| 378 | requiresBlock = true; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | if (requiresBlock) |
|---|
| 384 | { |
|---|
| 385 | _updatesMergedBlock.block(); |
|---|
| 386 | } |
|---|
| 387 | else |
|---|
| 388 | { |
|---|
| 389 | OpenThreads::Thread::YieldCurrentThread(); |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | void update(osg::Node* scene) |
|---|
| 396 | { |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | osg::Group* group = dynamic_cast<osg::Group*>(scene); |
|---|
| 400 | if (!group) |
|---|
| 401 | { |
|---|
| 402 | osg::notify(osg::NOTICE)<<"Error, MasterOperation::update(Node*) can only work with a Group as Viewer::getSceneData()."<<std::endl; |
|---|
| 403 | return; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 407 | |
|---|
| 408 | if (!_nodesToRemove.empty() || !_nodesToAdd.empty()) |
|---|
| 409 | { |
|---|
| 410 | osg::notify(osg::NOTICE)<<"update().................. "<<std::endl; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | if (!_nodesToRemove.empty()) |
|---|
| 414 | { |
|---|
| 415 | for(Files::iterator itr = _nodesToRemove.begin(); |
|---|
| 416 | itr != _nodesToRemove.end(); |
|---|
| 417 | ++itr) |
|---|
| 418 | { |
|---|
| 419 | FilenameNodeMap::iterator fnmItr = _existingFilenameNodeMap.find(*itr); |
|---|
| 420 | if (fnmItr != _existingFilenameNodeMap.end()) |
|---|
| 421 | { |
|---|
| 422 | osg::notify(osg::NOTICE)<<" update():removing "<<*itr<<std::endl; |
|---|
| 423 | |
|---|
| 424 | group->removeChild(fnmItr->second.get()); |
|---|
| 425 | _existingFilenameNodeMap.erase(fnmItr); |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | _nodesToRemove.clear(); |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | if (!_nodesToAdd.empty()) |
|---|
| 433 | { |
|---|
| 434 | for(FilenameNodeMap::iterator itr = _nodesToAdd.begin(); |
|---|
| 435 | itr != _nodesToAdd.end(); |
|---|
| 436 | ++itr) |
|---|
| 437 | { |
|---|
| 438 | osg::notify(osg::NOTICE)<<" update():inserting "<<itr->first<<std::endl; |
|---|
| 439 | group->addChild(itr->second.get()); |
|---|
| 440 | _existingFilenameNodeMap[itr->first] = itr->second; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | _nodesToAdd.clear(); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | _updatesMergedBlock.release(); |
|---|
| 447 | |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | virtual void release() |
|---|
| 453 | { |
|---|
| 454 | if (_operationQueue.valid()) _operationQueue->removeAllOperations(); |
|---|
| 455 | |
|---|
| 456 | _updatesMergedBlock.release(); |
|---|
| 457 | if (_endOfCompilebarrier.valid()) _endOfCompilebarrier.release(); |
|---|
| 458 | if (_endOfLoadBlock.valid()) _endOfLoadBlock.release(); |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | std::string _filename; |
|---|
| 463 | |
|---|
| 464 | OpenThreads::Mutex _mutex; |
|---|
| 465 | FilenameNodeMap _existingFilenameNodeMap; |
|---|
| 466 | Files _nodesToRemove; |
|---|
| 467 | FilenameNodeMap _nodesToAdd; |
|---|
| 468 | OpenThreads::Block _updatesMergedBlock; |
|---|
| 469 | |
|---|
| 470 | osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation; |
|---|
| 471 | osg::ref_ptr<osg::BarrierOperation> _endOfCompilebarrier; |
|---|
| 472 | osg::ref_ptr<osg::RefBlockCount> _endOfLoadBlock; |
|---|
| 473 | |
|---|
| 474 | osg::ref_ptr<osg::OperationQueue> _operationQueue; |
|---|
| 475 | }; |
|---|
| 476 | |
|---|
| 477 | class FilterHandler : public osgGA::GUIEventHandler |
|---|
| 478 | { |
|---|
| 479 | public: |
|---|
| 480 | |
|---|
| 481 | FilterHandler(osgTerrain::GeometryTechnique* gt): |
|---|
| 482 | _gt(gt) {} |
|---|
| 483 | |
|---|
| 484 | bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa) |
|---|
| 485 | { |
|---|
| 486 | if (!_gt) return false; |
|---|
| 487 | |
|---|
| 488 | switch(ea.getEventType()) |
|---|
| 489 | { |
|---|
| 490 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 491 | { |
|---|
| 492 | if (ea.getKey() == 'g') |
|---|
| 493 | { |
|---|
| 494 | osg::notify(osg::NOTICE)<<"Gaussian"<<std::endl; |
|---|
| 495 | _gt->setFilterMatrixAs(osgTerrain::GeometryTechnique::GAUSSIAN); |
|---|
| 496 | return true; |
|---|
| 497 | } |
|---|
| 498 | else if (ea.getKey() == 's') |
|---|
| 499 | { |
|---|
| 500 | osg::notify(osg::NOTICE)<<"Smooth"<<std::endl; |
|---|
| 501 | _gt->setFilterMatrixAs(osgTerrain::GeometryTechnique::SMOOTH); |
|---|
| 502 | return true; |
|---|
| 503 | } |
|---|
| 504 | else if (ea.getKey() == 'S') |
|---|
| 505 | { |
|---|
| 506 | osg::notify(osg::NOTICE)<<"Sharpen"<<std::endl; |
|---|
| 507 | _gt->setFilterMatrixAs(osgTerrain::GeometryTechnique::SHARPEN); |
|---|
| 508 | return true; |
|---|
| 509 | } |
|---|
| 510 | else if (ea.getKey() == '+') |
|---|
| 511 | { |
|---|
| 512 | _gt->setFilterWidth(_gt->getFilterWidth()*1.1); |
|---|
| 513 | osg::notify(osg::NOTICE)<<"Filter width = "<<_gt->getFilterWidth()<<std::endl; |
|---|
| 514 | return true; |
|---|
| 515 | } |
|---|
| 516 | else if (ea.getKey() == '-') |
|---|
| 517 | { |
|---|
| 518 | _gt->setFilterWidth(_gt->getFilterWidth()/1.1); |
|---|
| 519 | osg::notify(osg::NOTICE)<<"Filter width = "<<_gt->getFilterWidth()<<std::endl; |
|---|
| 520 | return true; |
|---|
| 521 | } |
|---|
| 522 | else if (ea.getKey() == '>') |
|---|
| 523 | { |
|---|
| 524 | _gt->setFilterBias(_gt->getFilterBias()+0.1); |
|---|
| 525 | osg::notify(osg::NOTICE)<<"Filter bias = "<<_gt->getFilterBias()<<std::endl; |
|---|
| 526 | return true; |
|---|
| 527 | } |
|---|
| 528 | else if (ea.getKey() == '<') |
|---|
| 529 | { |
|---|
| 530 | _gt->setFilterBias(_gt->getFilterBias()-0.1); |
|---|
| 531 | osg::notify(osg::NOTICE)<<"Filter bias = "<<_gt->getFilterBias()<<std::endl; |
|---|
| 532 | return true; |
|---|
| 533 | } |
|---|
| 534 | break; |
|---|
| 535 | } |
|---|
| 536 | default: |
|---|
| 537 | break; |
|---|
| 538 | } |
|---|
| 539 | return false; |
|---|
| 540 | |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | protected: |
|---|
| 544 | |
|---|
| 545 | osg::observer_ptr<osgTerrain::GeometryTechnique> _gt; |
|---|
| 546 | |
|---|
| 547 | }; |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | class LayerHandler : public osgGA::GUIEventHandler |
|---|
| 552 | { |
|---|
| 553 | public: |
|---|
| 554 | |
|---|
| 555 | LayerHandler(osgTerrain::Layer* layer): |
|---|
| 556 | _layer(layer) {} |
|---|
| 557 | |
|---|
| 558 | bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa) |
|---|
| 559 | { |
|---|
| 560 | if (!_layer) return false; |
|---|
| 561 | |
|---|
| 562 | float scale = 1.2; |
|---|
| 563 | |
|---|
| 564 | switch(ea.getEventType()) |
|---|
| 565 | { |
|---|
| 566 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 567 | { |
|---|
| 568 | if (ea.getKey() == 'q') |
|---|
| 569 | { |
|---|
| 570 | _layer->transform(0.0, scale); |
|---|
| 571 | return true; |
|---|
| 572 | } |
|---|
| 573 | else if (ea.getKey() == 'a') |
|---|
| 574 | { |
|---|
| 575 | _layer->transform(0.0, 1.0f/scale); |
|---|
| 576 | return true; |
|---|
| 577 | } |
|---|
| 578 | break; |
|---|
| 579 | } |
|---|
| 580 | default: |
|---|
| 581 | break; |
|---|
| 582 | } |
|---|
| 583 | return false; |
|---|
| 584 | |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | protected: |
|---|
| 588 | |
|---|
| 589 | osg::observer_ptr<osgTerrain::Layer> _layer; |
|---|
| 590 | }; |
|---|
| 591 | |
|---|
| 592 | int main(int argc, char** argv) |
|---|
| 593 | { |
|---|
| 594 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | osgViewer::Viewer viewer(arguments); |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | { |
|---|
| 601 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 602 | |
|---|
| 603 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 604 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 605 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 606 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 607 | |
|---|
| 608 | std::string pathfile; |
|---|
| 609 | char keyForAnimationPath = '5'; |
|---|
| 610 | while (arguments.read("-p",pathfile)) |
|---|
| 611 | { |
|---|
| 612 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 613 | if (apm || !apm->valid()) |
|---|
| 614 | { |
|---|
| 615 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 616 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 617 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 618 | ++keyForAnimationPath; |
|---|
| 619 | } |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | viewer.setIncrementalCompileOperation(new osgUtil::IncrementalCompileOperation()); |
|---|
| 638 | |
|---|
| 639 | double x = 0.0; |
|---|
| 640 | double y = 0.0; |
|---|
| 641 | double w = 1.0; |
|---|
| 642 | double h = 1.0; |
|---|
| 643 | |
|---|
| 644 | unsigned int numLoadThreads = 1; |
|---|
| 645 | while (arguments.read("--load-threads",numLoadThreads)) { } |
|---|
| 646 | |
|---|
| 647 | osg::ref_ptr<MasterOperation> masterOperation; |
|---|
| 648 | std::string masterFilename; |
|---|
| 649 | while(arguments.read("-m",masterFilename)) |
|---|
| 650 | { |
|---|
| 651 | masterOperation = new MasterOperation(masterFilename, viewer.getIncrementalCompileOperation()); |
|---|
| 652 | } |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | osg::ref_ptr<osgTerrain::TerrainTile> terrainTile = new osgTerrain::TerrainTile; |
|---|
| 656 | osg::ref_ptr<osgTerrain::Locator> locator = new osgTerrain::Locator; |
|---|
| 657 | osg::ref_ptr<osgTerrain::ValidDataOperator> validDataOperator = new osgTerrain::NoDataValue(0.0); |
|---|
| 658 | osg::ref_ptr<osgTerrain::Layer> lastAppliedLayer; |
|---|
| 659 | |
|---|
| 660 | locator->setCoordinateSystemType(osgTerrain::Locator::GEOCENTRIC); |
|---|
| 661 | locator->setTransformAsExtents(-osg::PI, -osg::PI*0.5, osg::PI, osg::PI*0.5); |
|---|
| 662 | |
|---|
| 663 | unsigned int layerNum = 0; |
|---|
| 664 | |
|---|
| 665 | std::string filterName; |
|---|
| 666 | |
|---|
| 667 | osg::Texture::FilterMode filter = osg::Texture::LINEAR; |
|---|
| 668 | |
|---|
| 669 | float minValue, maxValue; |
|---|
| 670 | float scale = 1.0f; |
|---|
| 671 | float offset = 0.0f; |
|---|
| 672 | |
|---|
| 673 | int pos = 1; |
|---|
| 674 | while(pos<arguments.argc()) |
|---|
| 675 | { |
|---|
| 676 | std::string filename; |
|---|
| 677 | |
|---|
| 678 | if (arguments.read(pos, "--layer",layerNum)) |
|---|
| 679 | { |
|---|
| 680 | osg::notify(osg::NOTICE)<<"Set layer number to "<<layerNum<<std::endl; |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | else if (arguments.read(pos, "-b")) |
|---|
| 684 | { |
|---|
| 685 | terrainTile->setTreatBoundariesToValidDataAsDefaultValue(true); |
|---|
| 686 | } |
|---|
| 687 | |
|---|
| 688 | else if (arguments.read(pos, "-e",x,y,w,h)) |
|---|
| 689 | { |
|---|
| 690 | |
|---|
| 691 | locator->setCoordinateSystemType(osgTerrain::Locator::GEOCENTRIC); |
|---|
| 692 | locator->setTransformAsExtents(x,y,x+w,y+h); |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | else if (arguments.read(pos, "--transform",offset, scale) || arguments.read(pos, "-t",offset, scale)) |
|---|
| 696 | { |
|---|
| 697 | |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | else if (arguments.read(pos, "--cartesian",x,y,w,h)) |
|---|
| 701 | { |
|---|
| 702 | |
|---|
| 703 | locator->setCoordinateSystemType(osgTerrain::Locator::PROJECTED); |
|---|
| 704 | locator->setTransformAsExtents(x,y,x+w,y+h); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | else if (arguments.read(pos, "--hf",filename)) |
|---|
| 708 | { |
|---|
| 709 | osg::notify(osg::NOTICE)<<"--hf "<<filename<<std::endl; |
|---|
| 710 | |
|---|
| 711 | osg::ref_ptr<osg::HeightField> hf = osgDB::readHeightFieldFile(filename); |
|---|
| 712 | if (hf.valid()) |
|---|
| 713 | { |
|---|
| 714 | osg::ref_ptr<osgTerrain::HeightFieldLayer> hfl = new osgTerrain::HeightFieldLayer; |
|---|
| 715 | hfl->setHeightField(hf.get()); |
|---|
| 716 | |
|---|
| 717 | hfl->setLocator(locator.get()); |
|---|
| 718 | hfl->setValidDataOperator(validDataOperator.get()); |
|---|
| 719 | hfl->setMagFilter(filter); |
|---|
| 720 | |
|---|
| 721 | if (offset!=0.0f || scale!=1.0f) |
|---|
| 722 | { |
|---|
| 723 | hfl->transform(offset,scale); |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | terrainTile->setElevationLayer(hfl.get()); |
|---|
| 727 | |
|---|
| 728 | lastAppliedLayer = hfl.get(); |
|---|
| 729 | |
|---|
| 730 | osg::notify(osg::NOTICE)<<"created osgTerrain::HeightFieldLayer"<<std::endl; |
|---|
| 731 | } |
|---|
| 732 | else |
|---|
| 733 | { |
|---|
| 734 | osg::notify(osg::NOTICE)<<"failed to create osgTerrain::HeightFieldLayer"<<std::endl; |
|---|
| 735 | } |
|---|
| 736 | |
|---|
| 737 | scale = 1.0f; |
|---|
| 738 | offset = 0.0f; |
|---|
| 739 | |
|---|
| 740 | } |
|---|
| 741 | |
|---|
| 742 | else if (arguments.read(pos, "-d",filename) || arguments.read(pos, "--elevation-image",filename)) |
|---|
| 743 | { |
|---|
| 744 | osg::notify(osg::NOTICE)<<"--elevation-image "<<filename<<std::endl; |
|---|
| 745 | |
|---|
| 746 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename); |
|---|
| 747 | if (image.valid()) |
|---|
| 748 | { |
|---|
| 749 | osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer; |
|---|
| 750 | imageLayer->setImage(image.get()); |
|---|
| 751 | imageLayer->setLocator(locator.get()); |
|---|
| 752 | imageLayer->setValidDataOperator(validDataOperator.get()); |
|---|
| 753 | imageLayer->setMagFilter(filter); |
|---|
| 754 | |
|---|
| 755 | if (offset!=0.0f || scale!=1.0f) |
|---|
| 756 | { |
|---|
| 757 | imageLayer->transform(offset,scale); |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | terrainTile->setElevationLayer(imageLayer.get()); |
|---|
| 761 | |
|---|
| 762 | lastAppliedLayer = imageLayer.get(); |
|---|
| 763 | |
|---|
| 764 | osg::notify(osg::NOTICE)<<"created Elevation osgTerrain::ImageLayer"<<std::endl; |
|---|
| 765 | } |
|---|
| 766 | else |
|---|
| 767 | { |
|---|
| 768 | osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl; |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | scale = 1.0f; |
|---|
| 772 | offset = 0.0f; |
|---|
| 773 | |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | else if (arguments.read(pos, "-c",filename) || arguments.read(pos, "--image",filename)) |
|---|
| 777 | { |
|---|
| 778 | osg::notify(osg::NOTICE)<<"--image "<<filename<<" x="<<x<<" y="<<y<<" w="<<w<<" h="<<h<<std::endl; |
|---|
| 779 | |
|---|
| 780 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename); |
|---|
| 781 | if (image.valid()) |
|---|
| 782 | { |
|---|
| 783 | osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer; |
|---|
| 784 | imageLayer->setImage(image.get()); |
|---|
| 785 | imageLayer->setLocator(locator.get()); |
|---|
| 786 | imageLayer->setValidDataOperator(validDataOperator.get()); |
|---|
| 787 | imageLayer->setMagFilter(filter); |
|---|
| 788 | |
|---|
| 789 | if (offset!=0.0f || scale!=1.0f) |
|---|
| 790 | { |
|---|
| 791 | imageLayer->transform(offset,scale); |
|---|
| 792 | } |
|---|
| 793 | |
|---|
| 794 | terrainTile->setColorLayer(layerNum, imageLayer.get()); |
|---|
| 795 | |
|---|
| 796 | lastAppliedLayer = imageLayer.get(); |
|---|
| 797 | |
|---|
| 798 | osg::notify(osg::NOTICE)<<"created Color osgTerrain::ImageLayer"<<std::endl; |
|---|
| 799 | } |
|---|
| 800 | else |
|---|
| 801 | { |
|---|
| 802 | osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl; |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | scale = 1.0f; |
|---|
| 806 | offset = 0.0f; |
|---|
| 807 | |
|---|
| 808 | } |
|---|
| 809 | |
|---|
| 810 | else if (arguments.read(pos, "--filter",filterName)) |
|---|
| 811 | { |
|---|
| 812 | if (filterName=="NEAREST") |
|---|
| 813 | { |
|---|
| 814 | osg::notify(osg::NOTICE)<<"--filter "<<filterName<<std::endl; |
|---|
| 815 | filter = osg::Texture::NEAREST; |
|---|
| 816 | } |
|---|
| 817 | else if (filterName=="LINEAR") |
|---|
| 818 | { |
|---|
| 819 | filter = osg::Texture::LINEAR; |
|---|
| 820 | osg::notify(osg::NOTICE)<<"--filter "<<filterName<<std::endl; |
|---|
| 821 | } |
|---|
| 822 | else |
|---|
| 823 | { |
|---|
| 824 | osg::notify(osg::NOTICE)<<"--filter "<<filterName<<" unrecognized filter name, please use LINEAER or NEAREST."<<std::endl; |
|---|
| 825 | } |
|---|
| 826 | |
|---|
| 827 | if (terrainTile->getColorLayer(layerNum)) |
|---|
| 828 | { |
|---|
| 829 | terrainTile->getColorLayer(layerNum)->setMagFilter(filter); |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | else if (arguments.read(pos, "--tf",minValue, maxValue)) |
|---|
| 835 | { |
|---|
| 836 | osg::ref_ptr<osg::TransferFunction1D> tf = new osg::TransferFunction1D; |
|---|
| 837 | |
|---|
| 838 | unsigned int numCells = 6; |
|---|
| 839 | float delta = (maxValue-minValue)/float(numCells-1); |
|---|
| 840 | float v = minValue; |
|---|
| 841 | |
|---|
| 842 | tf->allocate(6); |
|---|
| 843 | tf->setColor(v, osg::Vec4(1.0,1.0,1.0,1.0)); v += delta; |
|---|
| 844 | tf->setColor(v, osg::Vec4(1.0,0.0,1.0,1.0)); v += delta; |
|---|
| 845 | tf->setColor(v, osg::Vec4(1.0,0.0,0.0,1.0)); v += delta; |
|---|
| 846 | tf->setColor(v, osg::Vec4(1.0,1.0,0.0,1.0)); v += delta; |
|---|
| 847 | tf->setColor(v, osg::Vec4(0.0,1.0,1.0,1.0)); v += delta; |
|---|
| 848 | tf->setColor(v, osg::Vec4(0.0,1.0,0.0,1.0)); |
|---|
| 849 | |
|---|
| 850 | osg::notify(osg::NOTICE)<<"--tf "<<minValue<<" "<<maxValue<<std::endl; |
|---|
| 851 | |
|---|
| 852 | terrainTile->setColorLayer(layerNum, new osgTerrain::ContourLayer(tf.get())); |
|---|
| 853 | } |
|---|
| 854 | else |
|---|
| 855 | { |
|---|
| 856 | ++pos; |
|---|
| 857 | } |
|---|
| 858 | |
|---|
| 859 | } |
|---|
| 860 | |
|---|
| 861 | |
|---|
| 862 | osg::ref_ptr<osg::Group> scene = new osg::Group; |
|---|
| 863 | |
|---|
| 864 | if (terrainTile.valid() && (terrainTile->getElevationLayer() || terrainTile->getColorLayer(0))) |
|---|
| 865 | { |
|---|
| 866 | osg::notify(osg::NOTICE)<<"Terrain created"<<std::endl; |
|---|
| 867 | |
|---|
| 868 | scene->addChild(terrainTile.get()); |
|---|
| 869 | |
|---|
| 870 | osg::ref_ptr<osgTerrain::GeometryTechnique> geometryTechnique = new osgTerrain::GeometryTechnique; |
|---|
| 871 | terrainTile->setTerrainTechnique(geometryTechnique.get()); |
|---|
| 872 | viewer.addEventHandler(new FilterHandler(geometryTechnique.get())); |
|---|
| 873 | viewer.addEventHandler(new LayerHandler(lastAppliedLayer.get())); |
|---|
| 874 | } |
|---|
| 875 | |
|---|
| 876 | if (masterOperation.valid()) |
|---|
| 877 | { |
|---|
| 878 | osg::notify(osg::NOTICE)<<"Master operation created"<<std::endl; |
|---|
| 879 | |
|---|
| 880 | masterOperation->open(scene.get()); |
|---|
| 881 | } |
|---|
| 882 | |
|---|
| 883 | if (scene->getNumChildren()==0) |
|---|
| 884 | { |
|---|
| 885 | osg::notify(osg::NOTICE)<<"No model created, please specify terrain or master file on command line."<<std::endl; |
|---|
| 886 | return 0; |
|---|
| 887 | } |
|---|
| 888 | |
|---|
| 889 | viewer.setSceneData(scene.get()); |
|---|
| 890 | |
|---|
| 891 | |
|---|
| 892 | |
|---|
| 893 | osg::ref_ptr<osg::OperationThread> masterOperationThread; |
|---|
| 894 | |
|---|
| 895 | typedef std::list< osg::ref_ptr<osg::OperationThread> > OperationThreadList; |
|---|
| 896 | OperationThreadList generalThreadList; |
|---|
| 897 | |
|---|
| 898 | if (masterOperation.valid()) |
|---|
| 899 | { |
|---|
| 900 | masterOperationThread = new osg::OperationThread; |
|---|
| 901 | masterOperationThread->startThread(); |
|---|
| 902 | |
|---|
| 903 | masterOperationThread->add(masterOperation.get()); |
|---|
| 904 | |
|---|
| 905 | |
|---|
| 906 | { |
|---|
| 907 | osg::ref_ptr<osg::OperationQueue> operationQueue = new osg::OperationQueue; |
|---|
| 908 | masterOperation->setOperationQueue(operationQueue.get()); |
|---|
| 909 | |
|---|
| 910 | for(unsigned int i=0; i<numLoadThreads; ++i) |
|---|
| 911 | { |
|---|
| 912 | osg::ref_ptr<osg::OperationThread> thread = new osg::OperationThread; |
|---|
| 913 | thread->setOperationQueue(operationQueue.get()); |
|---|
| 914 | thread->startThread(); |
|---|
| 915 | generalThreadList.push_back(thread); |
|---|
| 916 | } |
|---|
| 917 | } |
|---|
| 918 | |
|---|
| 919 | viewer.addUpdateOperation(masterOperation.get()); |
|---|
| 920 | } |
|---|
| 921 | |
|---|
| 922 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | |
|---|
| 926 | |
|---|
| 927 | |
|---|
| 928 | viewer.realize(); |
|---|
| 929 | |
|---|
| 930 | |
|---|
| 931 | return viewer.run(); |
|---|
| 932 | |
|---|
| 933 | } |
|---|