| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <osgViewer/Viewer> |
|---|
| 21 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 22 | #include <osgDB/ReadFile> |
|---|
| 23 | #include <osgGA/TrackballManipulator> |
|---|
| 24 | #include <osgUtil/IncrementalCompileOperation> |
|---|
| 25 | |
|---|
| 26 | class SceneGraphProcessor : public osg::Referenced |
|---|
| 27 | { |
|---|
| 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: |
|---|
| 91 | CustomCompileCompletedCallback(): |
|---|
| 92 | completed(false) {} |
|---|
| 93 | |
|---|
| 94 | virtual bool compileCompleted(osgUtil::IncrementalCompileOperation::CompileSet* compileSet) |
|---|
| 95 | { |
|---|
| 96 | OSG_NOTICE<<"compileCompleted"<<std::endl; |
|---|
| 97 | completed = true; |
|---|
| 98 | return true; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | bool completed; |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 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 | |
|---|
| 167 | int main(int argc, char** argv) |
|---|
| 168 | { |
|---|
| 169 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | osgViewer::Viewer viewer(arguments); |
|---|
| 173 | |
|---|
| 174 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| 175 | viewer.addEventHandler(new osgViewer::StatsHandler()); |
|---|
| 176 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | osg::ref_ptr<osgUtil::IncrementalCompileOperation> incrementalCompile = new osgUtil::IncrementalCompileOperation; |
|---|
| 183 | viewer.setIncrementalCompileOperation(incrementalCompile.get()); |
|---|
| 184 | |
|---|
| 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 | |
|---|
| 206 | |
|---|
| 207 | osg::ref_ptr<SceneGraphProcessor> sceneGraphProcessor = new SceneGraphProcessor(arguments); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | double timeBetweenMerges = 2.0; |
|---|
| 214 | while(arguments.read("--interval",timeBetweenMerges)) {} |
|---|
| 215 | |
|---|
| 216 | bool readDatabasesInPagingThread = false; |
|---|
| 217 | while(arguments.read("--paging")) { readDatabasesInPagingThread = true; } |
|---|
| 218 | |
|---|
| 219 | typedef std::vector< std::string > FileNames; |
|---|
| 220 | FileNames fileNames; |
|---|
| 221 | for(int pos=1;pos<arguments.argc();++pos) |
|---|
| 222 | { |
|---|
| 223 | if (!arguments.isOption(pos)) |
|---|
| 224 | { |
|---|
| 225 | fileNames.push_back(arguments[pos]); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | if (fileNames.empty()) |
|---|
| 230 | { |
|---|
| 231 | OSG_NOTICE<<"No files loaded, please specifies files on commandline."<<std::endl; |
|---|
| 232 | return 1; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | typedef std::vector< osg::ref_ptr<osg::Node> > Models; |
|---|
| 236 | unsigned int modelIndex = 0; |
|---|
| 237 | Models models; |
|---|
| 238 | |
|---|
| 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) |
|---|
| 262 | { |
|---|
| 263 | |
|---|
| 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 | } |
|---|
| 278 | |
|---|
| 279 | viewer.realize(); |
|---|
| 280 | |
|---|
| 281 | double timeOfLastMerge = viewer.getFrameStamp()->getReferenceTime(); |
|---|
| 282 | |
|---|
| 283 | osg::ref_ptr<CustomCompileCompletedCallback> compileCompletedCallback; |
|---|
| 284 | |
|---|
| 285 | while(!viewer.done()) |
|---|
| 286 | { |
|---|
| 287 | viewer.frame(); |
|---|
| 288 | |
|---|
| 289 | double currentTime = viewer.getFrameStamp()->getReferenceTime(); |
|---|
| 290 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | return 0; |
|---|
| 357 | } |
|---|