[250] | 1 | #include "ReaderWriterTerrainMod.h" |
---|
[284] | 2 | #include <osg/KdTree> |
---|
[250] | 3 | |
---|
| 4 | ReaderWriterTerrainMod::ReaderWriterTerrainMod() |
---|
| 5 | { |
---|
| 6 | supportsExtension( "terrainmod", "Terrain modification pseudo loader"); |
---|
[252] | 7 | |
---|
[253] | 8 | extensionToAdd = ".terrainmod"; |
---|
[250] | 9 | } |
---|
| 10 | |
---|
| 11 | ReaderWriterTerrainMod::ReadResult ReaderWriterTerrainMod::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const |
---|
| 12 | { |
---|
[252] | 13 | // Check if its a valid file for this pseudo loader |
---|
[250] | 14 | std::string ext = osgDB::getLowerCaseFileExtension( file ); |
---|
| 15 | if( !acceptsExtension(ext) ) |
---|
| 16 | return ReadResult::FILE_NOT_HANDLED; |
---|
| 17 | |
---|
[252] | 18 | // Strip the pseudo-loader extension |
---|
| 19 | std::string realName = osgDB::getNameLessExtension( file ); |
---|
| 20 | |
---|
| 21 | // check if stripped file exists |
---|
| 22 | std::string fileName = osgDB::findDataFile( realName, options ); |
---|
[250] | 23 | if( fileName.empty() ) |
---|
| 24 | return ReadResult::FILE_NOT_FOUND; |
---|
| 25 | |
---|
[284] | 26 | // Check and postphone KdTree creation |
---|
| 27 | bool doKdTreeBuilder = (options && options->getBuildKdTreesHint()!=Options::NO_PREFERENCE) ? |
---|
| 28 | options->getBuildKdTreesHint() == Options::BUILD_KDTREES : |
---|
| 29 | osgDB::Registry::instance()->getBuildKdTreesHint() == Options::BUILD_KDTREES; |
---|
[285] | 30 | // Save original KdTree Hint |
---|
| 31 | Options::BuildKdTreesHint originalHint = Options::NO_PREFERENCE; |
---|
| 32 | if(options) |
---|
| 33 | { |
---|
| 34 | originalHint = options->getBuildKdTreesHint(); |
---|
| 35 | const_cast<osgDB::ReaderWriter::Options*>(options)->setBuildKdTreesHint(Options::DO_NOT_BUILD_KDTREES); |
---|
| 36 | } |
---|
[284] | 37 | |
---|
[250] | 38 | // Load real file |
---|
[252] | 39 | osg::Node *node = osgDB::readNodeFile( fileName, options ); |
---|
[250] | 40 | if( !node ) |
---|
| 41 | { |
---|
| 42 | // propagate the read failure upwards |
---|
[252] | 43 | OSG_WARN << "Subfile \"" << fileName << "\" could not be loaded" << std::endl; |
---|
[250] | 44 | return ReadResult::FILE_NOT_HANDLED; |
---|
| 45 | } |
---|
| 46 | |
---|
[284] | 47 | // Traverse through loaded graph and |
---|
| 48 | // a) check for PagedLOD and modify all filenames of external references |
---|
| 49 | // b) modify tile if it is inside a modification ROI |
---|
[253] | 50 | // This ensures ensures that higher LODs are loaded via terrainmod pseudo loader as well. |
---|
[252] | 51 | ModificationVisitor modVisitor(extensionToAdd); |
---|
[251] | 52 | node->accept( modVisitor ); |
---|
| 53 | |
---|
[285] | 54 | // Finally create KdTree and restore options |
---|
[284] | 55 | if(doKdTreeBuilder) |
---|
| 56 | { |
---|
| 57 | osg::ref_ptr<osg::KdTreeBuilder> builder = osgDB::Registry::instance()->getKdTreeBuilder(); |
---|
| 58 | node->accept(*builder); |
---|
| 59 | } |
---|
[285] | 60 | // Restore original KdTree Hint |
---|
| 61 | if(options) const_cast<osgDB::ReaderWriter::Options*>(options)->setBuildKdTreesHint(originalHint); |
---|
[284] | 62 | |
---|
[250] | 63 | return node; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // Add ourself to the Registry to instantiate the reader/writer. |
---|
| 67 | REGISTER_OSGPLUGIN(terrainmod, ReaderWriterTerrainMod) |
---|