spt: Consistently return references in the loader.

This commit is contained in:
Mathias Froehlich 2013-01-20 15:33:25 +01:00
parent 55610bae70
commit f4e694afa7
2 changed files with 29 additions and 26 deletions

View File

@ -1,6 +1,6 @@
// ReaderWriterSPT.cxx -- Provide a paged database for flightgear scenery. // ReaderWriterSPT.cxx -- Provide a paged database for flightgear scenery.
// //
// Copyright (C) 2010 - 2011 Mathias Froehlich // Copyright (C) 2010 - 2013 Mathias Froehlich
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as // modify it under the terms of the GNU General Public License as
@ -30,6 +30,7 @@
#include <osg/Texture2D> #include <osg/Texture2D>
#include <osgDB/FileNameUtils> #include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/ReadFile> #include <osgDB/ReadFile>
#include <simgear/scene/util/OsgMath.hxx> #include <simgear/scene/util/OsgMath.hxx>
@ -130,9 +131,9 @@ ReaderWriterSPT::readObject(const std::string& fileName, const osgDB::Options* o
// We get called with different extensions. To make sure search continues, // We get called with different extensions. To make sure search continues,
// we need to return FILE_NOT_HANDLED in this case. // we need to return FILE_NOT_HANDLED in this case.
if (osgDB::getLowerCaseFileExtension(fileName) != "spt") if (osgDB::getLowerCaseFileExtension(fileName) != "spt")
return ReadResult(osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED); return ReadResult(ReadResult::FILE_NOT_HANDLED);
if (fileName != "state.spt") if (fileName != "state.spt")
return ReadResult(osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND); return ReadResult(ReadResult::FILE_NOT_FOUND);
osg::StateSet* stateSet = new osg::StateSet; osg::StateSet* stateSet = new osg::StateSet;
stateSet->setAttributeAndModes(new osg::CullFace); stateSet->setAttributeAndModes(new osg::CullFace);
@ -161,30 +162,30 @@ ReaderWriterSPT::readNode(const std::string& fileName, const osgDB::Options* opt
// The file name without path and without the spt extension // The file name without path and without the spt extension
std::string strippedFileName = osgDB::getStrippedName(fileName); std::string strippedFileName = osgDB::getStrippedName(fileName);
if (strippedFileName == "earth") if (strippedFileName == "earth")
return createTree(BucketBox(-180, -90, 360, 180), options, true); return ReadResult(createTree(BucketBox(-180, -90, 360, 180), options, true));
std::stringstream ss(strippedFileName); std::stringstream ss(strippedFileName);
BucketBox bucketBox; BucketBox bucketBox;
ss >> bucketBox; ss >> bucketBox;
if (ss.fail()) if (ss.fail())
return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND; return ReadResult::FILE_NOT_FOUND;
BucketBox bucketBoxList[2]; BucketBox bucketBoxList[2];
unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList); unsigned bucketBoxListSize = bucketBox.periodicSplit(bucketBoxList);
if (bucketBoxListSize == 0) if (bucketBoxListSize == 0)
return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND; return ReadResult::FILE_NOT_FOUND;
if (bucketBoxListSize == 1) if (bucketBoxListSize == 1)
return createTree(bucketBoxList[0], options, true); return ReadResult(createTree(bucketBoxList[0], options, true));
assert(bucketBoxListSize == 2); assert(bucketBoxListSize == 2);
osg::ref_ptr<osg::Group> group = new osg::Group; osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(createTree(bucketBoxList[0], options, true)); group->addChild(createTree(bucketBoxList[0], options, true));
group->addChild(createTree(bucketBoxList[1], options, true)); group->addChild(createTree(bucketBoxList[1], options, true));
return group.release(); return ReadResult(group);
} }
osg::Node* osg::ref_ptr<osg::Node>
ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const
{ {
if (bucketBox.getIsBucketSize()) { if (bucketBox.getIsBucketSize()) {
@ -203,19 +204,19 @@ ReaderWriterSPT::createTree(const BucketBox& bucketBox, const osgDB::Options* op
osg::ref_ptr<osg::Group> group = new osg::Group; osg::ref_ptr<osg::Group> group = new osg::Group;
for (unsigned i = 0; i < numTiles; ++i) { for (unsigned i = 0; i < numTiles; ++i) {
osg::Node* node = createTree(bucketBoxList[i], options, false); osg::ref_ptr<osg::Node> node = createTree(bucketBoxList[i], options, false);
if (!node) if (!node.valid())
continue; continue;
group->addChild(node); group->addChild(node.get());
} }
if (!group->getNumChildren()) if (!group->getNumChildren())
return 0; return 0;
return group.release(); return group;
} }
} }
osg::Node* osg::ref_ptr<osg::Node>
ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const
{ {
osg::PagedLOD* pagedLOD = new osg::PagedLOD; osg::PagedLOD* pagedLOD = new osg::PagedLOD;
@ -243,8 +244,9 @@ ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options
range = 1e6; range = 1e6;
// Add the static sea level textured shell // Add the static sea level textured shell
if (osg::Node* tile = createSeaLevelTile(bucketBox, options)) osg::ref_ptr<osg::Node> tile = createSeaLevelTile(bucketBox, options);
pagedLOD->addChild(tile, range, std::numeric_limits<float>::max()); if (tile.valid())
pagedLOD->addChild(tile.get(), range, std::numeric_limits<float>::max());
// Add the paged file name that creates the subtrees on demand // Add the paged file name that creates the subtrees on demand
if (bucketBox.getIsBucketSize()) { if (bucketBox.getIsBucketSize()) {
@ -261,7 +263,7 @@ ReaderWriterSPT::createPagedLOD(const BucketBox& bucketBox, const osgDB::Options
return pagedLOD; return pagedLOD;
} }
osg::Node* osg::ref_ptr<osg::Node>
ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const
{ {
if (options->getPluginStringData("SimGear::FG_EARTH") != "ON") if (options->getPluginStringData("SimGear::FG_EARTH") != "ON")
@ -310,23 +312,24 @@ ReaderWriterSPT::createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Opt
osg::Geode* geode = new osg::Geode; osg::Geode* geode = new osg::Geode;
geode->addDrawable(geometry); geode->addDrawable(geometry);
geode->setStateSet(getLowLODStateSet(options)); osg::ref_ptr<osg::StateSet> stateSet = getLowLODStateSet(options);
geode->setStateSet(stateSet.get());
return geode; return geode;
} }
osg::StateSet* osg::ref_ptr<osg::StateSet>
ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const ReaderWriterSPT::getLowLODStateSet(const osgDB::Options* options) const
{ {
osg::ref_ptr<osgDB::Options> localOptions; osg::ref_ptr<osgDB::Options> localOptions;
localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp())); localOptions = static_cast<osgDB::Options*>(options->clone(osg::CopyOp()));
localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL); localOptions->setObjectCacheHint(osgDB::Options::CACHE_ALL);
osg::ref_ptr<osg::Object> object = osgDB::readObjectFile("state.spt", localOptions.get()); osg::ref_ptr<osg::Object> object = osgDB::readRefObjectFile("state.spt", localOptions.get());
if (!dynamic_cast<osg::StateSet*>(object.get())) if (!dynamic_cast<osg::StateSet*>(object.get()))
return 0; return 0;
return static_cast<osg::StateSet*>(object.release()); return static_cast<osg::StateSet*>(object.get());
} }
} // namespace simgear } // namespace simgear

View File

@ -1,6 +1,6 @@
// ReaderWriterSPT.cxx -- Provide a paged database for flightgear scenery. // ReaderWriterSPT.cxx -- Provide a paged database for flightgear scenery.
// //
// Copyright (C) 2010 - 2011 Mathias Froehlich // Copyright (C) 2010 - 2013 Mathias Froehlich
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as // modify it under the terms of the GNU General Public License as
@ -37,10 +37,10 @@ public:
virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& fileName, const osgDB::Options* options) const; virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& fileName, const osgDB::Options* options) const;
protected: protected:
osg::Node* createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const; osg::ref_ptr<osg::Node> createTree(const BucketBox& bucketBox, const osgDB::Options* options, bool topLevel) const;
osg::Node* createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const; osg::ref_ptr<osg::Node> createPagedLOD(const BucketBox& bucketBox, const osgDB::Options* options) const;
osg::Node* createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const; osg::ref_ptr<osg::Node> createSeaLevelTile(const BucketBox& bucketBox, const osgDB::Options* options) const;
osg::StateSet* getLowLODStateSet(const osgDB::Options* options) const; osg::ref_ptr<osg::StateSet> getLowLODStateSet(const osgDB::Options* options) const;
private: private:
struct CullCallback; struct CullCallback;