source: experimental/TerrainTest/ReaderWriterTerrainMod.cpp @ 385

Last change on this file since 385 was 296, checked in by Torben Dannhauer, 13 years ago
File size: 2.6 KB
Line 
1#include "ReaderWriterTerrainMod.h"
2#include <osg/KdTree>
3
4ReaderWriterTerrainMod::ReaderWriterTerrainMod()
5{
6        supportsExtension( "terrainmod", "Terrain modification pseudo loader");
7        //supportsExtension( "*", "Terrain modification pseudo loader");
8
9
10        extensionToAdd = ".terrainmod";
11}
12
13ReaderWriterTerrainMod::ReadResult ReaderWriterTerrainMod::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const
14{
15        // Check if its a valid file for this pseudo loader
16        std::string ext = osgDB::getLowerCaseFileExtension( file );
17        if( !acceptsExtension(ext) )
18                return ReadResult::FILE_NOT_HANDLED;
19
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 );
25        if( fileName.empty() )
26                return ReadResult::FILE_NOT_FOUND;
27
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;
32        // If options are available: Save it'S original KdTree Hint.
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        }
39
40        // Load real file
41        osg::Node *node = osgDB::readNodeFile( fileName, options );
42    if( !node )
43    {
44        // propagate the read failure upwards
45        OSG_WARN << "Subfile \"" << fileName << "\" could not be loaded" << std::endl;
46        return ReadResult::FILE_NOT_HANDLED;
47    }
48
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
52        // This ensures ensures that higher LODs are loaded via terrainmod pseudo loader as well.
53        ModificationVisitor modVisitor(extensionToAdd);
54        node->accept( modVisitor );
55
56        // Finally create KdTree and restore options
57        if(doKdTreeBuilder)
58        {
59                osg::ref_ptr<osg::KdTreeBuilder> builder = osgDB::Registry::instance()->getKdTreeBuilder();
60                node->accept(*builder);
61        }
62        // Restore original KdTree Hint
63        if(options) const_cast<osgDB::ReaderWriter::Options*>(options)->setBuildKdTreesHint(originalHint);
64
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.