source: experimental/TerrainTest/ReaderWriterTerrainMod.cpp @ 395

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