| | 28 | public: |
| | 29 | SceneGraphProcessor() |
| | 30 | { |
| | 31 | _init(); |
| | 32 | } |
| | 33 | |
| | 34 | SceneGraphProcessor(osg::ArgumentParser& arguments) |
| | 35 | { |
| | 36 | _init(); |
| | 37 | |
| | 38 | while (arguments.read("--vbo")) { modifyDrawableSettings = true; useVBO = true; } |
| | 39 | while (arguments.read("--dl")) { modifyDrawableSettings = true; useDisplayLists = true; } |
| | 40 | while (arguments.read("--simplify", simplificatioRatio)) {} |
| | 41 | |
| | 42 | while (arguments.read("--build-mipmaps")) { modifyTextureSettings = true; buildImageMipmaps = true; } |
| | 43 | while (arguments.read("--compress")) { modifyTextureSettings = true; compressImages = true; } |
| | 44 | while (arguments.read("--disable-mipmaps")) { modifyTextureSettings = true; disableMipmaps = true; } |
| | 45 | } |
| | 46 | |
| | 47 | virtual osg::Node* process(osg::Node* node) |
| | 48 | { |
| | 49 | if (!node) |
| | 50 | { |
| | 51 | OSG_NOTICE<<"SceneGraphProcessor::process(Node*) : error cannot process NULL Node."<<std::endl; |
| | 52 | return 0; |
| | 53 | } |
| | 54 | |
| | 55 | OSG_NOTICE<<"SceneGraphProcessor::process("<<node<<") : "<<node->getName()<<std::endl; |
| | 56 | |
| | 57 | return node; |
| | 58 | } |
| | 59 | |
| | 60 | protected: |
| | 61 | |
| | 62 | void _init() |
| | 63 | { |
| | 64 | modifyDrawableSettings = false; |
| | 65 | useVBO = false; |
| | 66 | useDisplayLists = false; |
| | 67 | simplificatioRatio = 1.0; |
| | 68 | |
| | 69 | modifyTextureSettings = false; |
| | 70 | buildImageMipmaps = false; |
| | 71 | compressImages = false; |
| | 72 | disableMipmaps = false; |
| | 73 | } |
| | 74 | |
| | 75 | bool modifyDrawableSettings; |
| | 76 | bool useVBO; |
| | 77 | bool useDisplayLists; |
| | 78 | bool simplificatioRatio; |
| | 79 | |
| | 80 | bool modifyTextureSettings; |
| | 81 | bool buildImageMipmaps; |
| | 82 | bool compressImages; |
| | 83 | bool disableMipmaps; |
| | 84 | |
| | 85 | }; |
| | 86 | |
| | 87 | |
| | 88 | class CustomCompileCompletedCallback : public osgUtil::IncrementalCompileOperation::CompileCompletedCallback |
| | 89 | { |
| | 90 | public: |
| | 104 | class DatabasePagingOperation : public osg::Operation, public osgUtil::IncrementalCompileOperation::CompileCompletedCallback |
| | 105 | { |
| | 106 | public: |
| | 107 | |
| | 108 | DatabasePagingOperation(const std::string& filename, |
| | 109 | SceneGraphProcessor* sceneGraphProcessor, |
| | 110 | osgUtil::IncrementalCompileOperation* ico): |
| | 111 | Operation("DatabasePaging Operation", false), |
| | 112 | _filename(filename), |
| | 113 | _modelReadyToMerge(false), |
| | 114 | _sceneGraphProcessor(sceneGraphProcessor), |
| | 115 | _incrementalCompileOperation(ico) {} |
| | 116 | |
| | 117 | virtual void operator () (osg::Object* object) |
| | 118 | { |
| | 119 | osg::notify(osg::NOTICE)<<"LoadAndCompileOperation "<<_filename<<std::endl; |
| | 120 | |
| | 121 | _modelReadyToMerge = false; |
| | 122 | _loadedModel = osgDB::readNodeFile(_filename); |
| | 123 | |
| | 124 | if (_loadedModel.valid()) |
| | 125 | { |
| | 126 | if (_sceneGraphProcessor.valid()) |
| | 127 | { |
| | 128 | _loadedModel = _sceneGraphProcessor->process(_loadedModel.get()); |
| | 129 | } |
| | 130 | } |
| | 131 | |
| | 132 | if (_loadedModel.valid()) |
| | 133 | { |
| | 134 | if (_incrementalCompileOperation.valid()) |
| | 135 | { |
| | 136 | osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet = |
| | 137 | new osgUtil::IncrementalCompileOperation::CompileSet(_loadedModel.get()); |
| | 138 | |
| | 139 | compileSet->_compileCompletedCallback = this; |
| | 140 | |
| | 141 | _incrementalCompileOperation->add(compileSet.get()); |
| | 142 | } |
| | 143 | else |
| | 144 | { |
| | 145 | _modelReadyToMerge = true; |
| | 146 | } |
| | 147 | } |
| | 148 | |
| | 149 | osg::notify(osg::NOTICE)<<"done LoadAndCompileOperation "<<_filename<<std::endl; |
| | 150 | } |
| | 151 | |
| | 152 | virtual bool compileCompleted(osgUtil::IncrementalCompileOperation::CompileSet* compileSet) |
| | 153 | { |
| | 154 | OSG_NOTICE<<"compileCompleted"<<std::endl; |
| | 155 | _modelReadyToMerge = true; |
| | 156 | return true; |
| | 157 | } |
| | 158 | |
| | 159 | std::string _filename; |
| | 160 | osg::ref_ptr<osg::Node> _loadedModel; |
| | 161 | bool _modelReadyToMerge; |
| | 162 | osg::ref_ptr<SceneGraphProcessor> _sceneGraphProcessor; |
| | 163 | osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation; |
| | 164 | }; |
| | 165 | |
| | 166 | |
| | 185 | if (arguments.read("--force") || arguments.read("-f")) |
| | 186 | { |
| | 187 | incrementalCompile->assignForceTextureDownloadGeometry(); |
| | 188 | } |
| | 189 | |
| | 190 | if (arguments.read("-a")) |
| | 191 | { |
| | 192 | incrementalCompile->setMinimumTimeAvailableForGLCompileAndDeletePerFrame(1); |
| | 193 | incrementalCompile->setConservativeTimeRatio(1); |
| | 194 | incrementalCompile->setMaximumNumOfObjectsToCompilePerFrame(100); |
| | 195 | } |
| | 196 | else if (arguments.read("-c")) |
| | 197 | { |
| | 198 | incrementalCompile->setMinimumTimeAvailableForGLCompileAndDeletePerFrame(0.0001); |
| | 199 | incrementalCompile->setConservativeTimeRatio(0.01); |
| | 200 | incrementalCompile->setMaximumNumOfObjectsToCompilePerFrame(1); |
| | 201 | } |
| | 202 | |
| | 203 | ///////////////////////////////////////////////////////////////////////////////// |
| | 204 | // |
| | 205 | // SceneGraph processing setup |
| | 206 | // |
| | 207 | osg::ref_ptr<SceneGraphProcessor> sceneGraphProcessor = new SceneGraphProcessor(arguments); |
| | 208 | |
| | 209 | ///////////////////////////////////////////////////////////////////////////////// |
| | 210 | // |
| | 211 | // Database settings |
| | 212 | // |
| 62 | | for(int pos=1;pos<arguments.argc();++pos) |
| 63 | | { |
| 64 | | if (!arguments.isOption(pos)) |
| | 239 | osg::ref_ptr<osg::Group> group = new osg::Group; |
| | 240 | viewer.setSceneData(group); |
| | 241 | |
| | 242 | osg::ref_ptr<osg::OperationThread> databasePagingThread; |
| | 243 | osg::ref_ptr<DatabasePagingOperation> databasePagingOperation; |
| | 244 | if (readDatabasesInPagingThread) |
| | 245 | { |
| | 246 | databasePagingThread = new osg::OperationThread; |
| | 247 | databasePagingThread->startThread(); |
| | 248 | |
| | 249 | databasePagingOperation = new DatabasePagingOperation( |
| | 250 | fileNames[modelIndex++], |
| | 251 | sceneGraphProcessor.get(), |
| | 252 | incrementalCompile.get()); |
| | 253 | |
| | 254 | databasePagingThread->add(databasePagingOperation.get()); |
| | 255 | |
| | 256 | } |
| | 257 | else |
| | 258 | { |
| | 259 | for(FileNames::iterator itr = fileNames.begin(); |
| | 260 | itr != fileNames.end(); |
| | 261 | ++itr) |
| 67 | | osg::Node *node = osgDB::readNodeFile( arguments[pos]); |
| 68 | | if(node) |
| 69 | | { |
| 70 | | if (node->getName().empty()) node->setName( arguments[pos] ); |
| 71 | | models.push_back(node); |
| 72 | | } |
| 73 | | } |
| 74 | | } |
| 75 | | |
| 76 | | OSG_NOTICE<<"models.size()="<<models.size()<<std::endl; |
| 77 | | |
| 78 | | osg::ref_ptr<osg::Group> group = new osg::Group; |
| 79 | | |
| 80 | | unsigned int modelIndex = 0; |
| 81 | | |
| 82 | | group->addChild(models[modelIndex++].get()); |
| 83 | | |
| 84 | | viewer.setSceneData(group); |
| | 264 | osg::ref_ptr<osg::Node> node = osgDB::readNodeFile( *itr ); |
| | 265 | if(node.valid()) |
| | 266 | { |
| | 267 | if (node->getName().empty()) node->setName( *itr ); |
| | 268 | |
| | 269 | node = sceneGraphProcessor->process(node.get()); |
| | 270 | |
| | 271 | models.push_back(node.get()); |
| | 272 | } |
| | 273 | } |
| | 274 | |
| | 275 | group->addChild(models[modelIndex++].get()); |
| | 276 | |
| | 277 | } |
| 98 | | if (!compileCompletedCallback && |
| 99 | | modelIndex<models.size() && |
| 100 | | (currentTime-timeOfLastMerge)>timeBetweenMerges) |
| 101 | | { |
| 102 | | OSG_NOTICE<<"Compiling model "<<modelIndex<<" at "<<currentTime<<std::endl; |
| 103 | | |
| 104 | | osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet = |
| 105 | | new osgUtil::IncrementalCompileOperation::CompileSet(models[modelIndex].get()); |
| 106 | | |
| 107 | | compileCompletedCallback = new CustomCompileCompletedCallback; |
| 108 | | |
| 109 | | compileSet->_compileCompletedCallback = compileCompletedCallback; |
| 110 | | |
| 111 | | incrementalCompile->add(compileSet.get()); |
| 112 | | } |
| 113 | | |
| 114 | | if (compileCompletedCallback.valid() && compileCompletedCallback->completed) |
| 115 | | { |
| 116 | | OSG_NOTICE<<"Merging model "<<modelIndex<<" at "<<currentTime<<std::endl; |
| 117 | | |
| 118 | | timeOfLastMerge = currentTime; |
| 119 | | |
| 120 | | compileCompletedCallback = 0; |
| 121 | | |
| 122 | | group->removeChildren(0,group->getNumChildren()); |
| 123 | | |
| 124 | | group->addChild(models[modelIndex++].get()); |
| 125 | | |
| 126 | | viewer.home(); |
| 127 | | } |
| 128 | | |
| | 291 | if (readDatabasesInPagingThread) |
| | 292 | { |
| | 293 | if (!databasePagingOperation && |
| | 294 | modelIndex<fileNames.size() && |
| | 295 | (currentTime-timeOfLastMerge)>timeBetweenMerges) |
| | 296 | { |
| | 297 | databasePagingOperation = new DatabasePagingOperation( |
| | 298 | fileNames[modelIndex++], |
| | 299 | sceneGraphProcessor.get(), |
| | 300 | incrementalCompile.get()); |
| | 301 | |
| | 302 | databasePagingThread->add(databasePagingOperation.get()); |
| | 303 | } |
| | 304 | |
| | 305 | if (databasePagingOperation.get() && databasePagingOperation->_modelReadyToMerge) |
| | 306 | { |
| | 307 | timeOfLastMerge = currentTime; |
| | 308 | |
| | 309 | group->removeChildren(0,group->getNumChildren()); |
| | 310 | |
| | 311 | group->addChild(databasePagingOperation->_loadedModel.get()); |
| | 312 | |
| | 313 | viewer.home(); |
| | 314 | |
| | 315 | // we no longer need the paging operation as it's done it's job. |
| | 316 | databasePagingOperation = 0; |
| | 317 | |
| | 318 | viewer.home(); |
| | 319 | } |
| | 320 | } |
| | 321 | else |
| | 322 | { |
| | 323 | if (!compileCompletedCallback && |
| | 324 | modelIndex<fileNames.size() && |
| | 325 | (currentTime-timeOfLastMerge)>timeBetweenMerges) |
| | 326 | { |
| | 327 | OSG_NOTICE<<"Compiling model "<<modelIndex<<" at "<<currentTime<<std::endl; |
| | 328 | |
| | 329 | osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet = |
| | 330 | new osgUtil::IncrementalCompileOperation::CompileSet(models[modelIndex].get()); |
| | 331 | |
| | 332 | compileCompletedCallback = new CustomCompileCompletedCallback; |
| | 333 | |
| | 334 | compileSet->_compileCompletedCallback = compileCompletedCallback; |
| | 335 | |
| | 336 | incrementalCompile->add(compileSet.get()); |
| | 337 | } |
| | 338 | |
| | 339 | if (compileCompletedCallback.valid() && compileCompletedCallback->completed) |
| | 340 | { |
| | 341 | OSG_NOTICE<<"Merging model "<<modelIndex<<" at "<<currentTime<<std::endl; |
| | 342 | |
| | 343 | timeOfLastMerge = currentTime; |
| | 344 | |
| | 345 | group->removeChildren(0,group->getNumChildren()); |
| | 346 | |
| | 347 | group->addChild(models[modelIndex++].get()); |
| | 348 | |
| | 349 | compileCompletedCallback = 0; |
| | 350 | |
| | 351 | viewer.home(); |
| | 352 | } |
| | 353 | } |