1 | #include "ReaderWriterTerrainMod.h" |
---|
2 | #include <osg/KdTree> |
---|
3 | |
---|
4 | ReaderWriterTerrainMod::ReaderWriterTerrainMod() |
---|
5 | { |
---|
6 | supportsExtension( "terrainmod", "Terrain modification pseudo loader"); |
---|
7 | |
---|
8 | extensionToAdd = ".terrainmod"; |
---|
9 | } |
---|
10 | |
---|
11 | ReaderWriterTerrainMod::ReadResult ReaderWriterTerrainMod::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const |
---|
12 | { |
---|
13 | // Check if its a valid file for this pseudo loader |
---|
14 | std::string ext = osgDB::getLowerCaseFileExtension( file ); |
---|
15 | if( !acceptsExtension(ext) ) |
---|
16 | return ReadResult::FILE_NOT_HANDLED; |
---|
17 | |
---|
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 ); |
---|
23 | if( fileName.empty() ) |
---|
24 | return ReadResult::FILE_NOT_FOUND; |
---|
25 | |
---|
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; |
---|
30 | // If options are available: Save it'S 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 | } |
---|
37 | |
---|
38 | // Load real file |
---|
39 | osg::Node *node = osgDB::readNodeFile( fileName, options ); |
---|
40 | if( !node ) |
---|
41 | { |
---|
42 | // propagate the read failure upwards |
---|
43 | OSG_WARN << "Subfile \"" << fileName << "\" could not be loaded" << std::endl; |
---|
44 | return ReadResult::FILE_NOT_HANDLED; |
---|
45 | } |
---|
46 | |
---|
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 |
---|
50 | // This ensures ensures that higher LODs are loaded via terrainmod pseudo loader as well. |
---|
51 | ModificationVisitor modVisitor(extensionToAdd); |
---|
52 | node->accept( modVisitor ); |
---|
53 | |
---|
54 | // Finally create KdTree and restore options |
---|
55 | if(doKdTreeBuilder) |
---|
56 | { |
---|
57 | osg::ref_ptr<osg::KdTreeBuilder> builder = osgDB::Registry::instance()->getKdTreeBuilder(); |
---|
58 | node->accept(*builder); |
---|
59 | } |
---|
60 | // Restore original KdTree Hint |
---|
61 | if(options) const_cast<osgDB::ReaderWriter::Options*>(options)->setBuildKdTreesHint(originalHint); |
---|
62 | |
---|
63 | return node; |
---|
64 | } |
---|
65 | |
---|
66 | // Add ourself to the Registry to instantiate the reader/writer. |
---|
67 | REGISTER_OSGPLUGIN(terrainmod, ReaderWriterTerrainMod) |
---|