#include <osgDB/Registry>
#include <simgear/compiler.h>
+#include <simgear/structure/Singleton.hxx>
-#include STL_STRING
+#include <string>
#include <map>
// Class to register per file extension read callbacks with the OSG
// readNode function is specified as a template with a bunch of
// pluggable (and predefined) policies.
template <typename ProcessPolicy, typename CachePolicy, typename OptimizePolicy,
- typename CopyPolicy, typename SubstitutePolicy>
+ typename SubstitutePolicy, typename BVHPolicy>
class ModelRegistryCallback : public osgDB::Registry::ReadFileCallback {
public:
ModelRegistryCallback(const std::string& extension) :
_processPolicy(extension), _cachePolicy(extension),
- _optimizePolicy(extension), _copyPolicy(extension),
- _substitutePolicy(extension)
+ _optimizePolicy(extension),
+ _substitutePolicy(extension), _bvhPolicy(extension)
{
}
virtual osgDB::ReaderWriter::ReadResult
using namespace osg;
using namespace osgDB;
using osgDB::ReaderWriter;
- Registry* registry = Registry::instance();
- std::string usedFileName = _substitutePolicy.substitute(fileName, opt);
- if (usedFileName.empty())
- usedFileName = fileName;
- ref_ptr<osg::Node> loadedNode = _cachePolicy.find(usedFileName, opt);
- if (!loadedNode.valid()) {
- ReaderWriter* rw = registry ->getReaderWriterForExtension(osgDB::getFileExtension(usedFileName));
- if (!rw)
- return ReaderWriter::ReadResult(); // FILE_NOT_HANDLED
- ReaderWriter::ReadResult res = rw->readNode(usedFileName, opt);
- if (!res.validNode())
- return res;
- ref_ptr<osg::Node> processedNode
- = _processPolicy.process(res.getNode(), usedFileName, opt);
- ref_ptr<osg::Node> optimizedNode
- = _optimizePolicy.optimize(processedNode.get(), usedFileName,
- opt);
- _cachePolicy.addToCache(usedFileName, optimizedNode.get());
- loadedNode = optimizedNode;
+// Registry* registry = Registry::instance();
+ ref_ptr<osg::Node> optimizedNode = _cachePolicy.find(fileName, opt);
+ if (!optimizedNode.valid()) {
+ std::string otherFileName = _substitutePolicy.substitute(fileName,
+ opt);
+ ReaderWriter::ReadResult res;
+ if (!otherFileName.empty()) {
+ res = loadUsingReaderWriter(otherFileName, opt);
+ if (res.validNode())
+ optimizedNode = res.getNode();
+ }
+ if (!optimizedNode.valid()) {
+ res = loadUsingReaderWriter(fileName, opt);
+ if (!res.validNode())
+ return res;
+ ref_ptr<osg::Node> processedNode
+ = _processPolicy.process(res.getNode(), fileName, opt);
+ optimizedNode = _optimizePolicy.optimize(processedNode.get(),
+ fileName, opt);
+ }
+ _bvhPolicy.buildBVH(fileName, optimizedNode.get());
+ _cachePolicy.addToCache(fileName, optimizedNode.get());
}
- return ReaderWriter::ReadResult(_copyPolicy.copy(loadedNode.get(),
- usedFileName,
- opt));
+ return ReaderWriter::ReadResult(optimizedNode.get());
}
protected:
+ static osgDB::ReaderWriter::ReadResult
+ loadUsingReaderWriter(const std::string& fileName,
+ const osgDB::ReaderWriter::Options* opt)
+ {
+ using namespace osgDB;
+ ReaderWriter* rw = Registry::instance()
+ ->getReaderWriterForExtension(osgDB::getFileExtension(fileName));
+ if (!rw)
+ return ReaderWriter::ReadResult(); // FILE_NOT_HANDLED
+ return rw->readNode(fileName, opt);
+ }
+
ProcessPolicy _processPolicy;
CachePolicy _cachePolicy;
OptimizePolicy _optimizePolicy;
- CopyPolicy _copyPolicy;
SubstitutePolicy _substitutePolicy;
+ BVHPolicy _bvhPolicy;
virtual ~ModelRegistryCallback() {}
};
struct DefaultProcessPolicy {
DefaultProcessPolicy(const std::string& extension) {}
osg::Node* process(osg::Node* node, const std::string& filename,
- const osgDB::ReaderWriter::Options* opt)
- {
- return node;
- }
+ const osgDB::ReaderWriter::Options* opt);
};
struct DefaultCachePolicy {
}
};
-struct DefaultCopyPolicy {
- DefaultCopyPolicy(const std::string& extension) {}
- osg::Node* copy(osg::Node* node, const std::string& fileName,
- const osgDB::ReaderWriter::Options* opt);
-};
-
-struct NoCopyPolicy {
- NoCopyPolicy(const std::string& extension) {}
- osg::Node* copy(osg::Node* node, const std::string& fileName,
- const osgDB::ReaderWriter::Options* opt)
- {
- return node;
- }
-};
-
struct OSGSubstitutePolicy {
OSGSubstitutePolicy(const std::string& extension) {}
std::string substitute(const std::string& name,
return std::string();
}
};
+
+struct BuildLeafBVHPolicy {
+ BuildLeafBVHPolicy(const std::string& extension) {}
+ void buildBVH(const std::string& fileName, osg::Node* node);
+};
+
+struct BuildGroupBVHPolicy {
+ BuildGroupBVHPolicy(const std::string& extension) {}
+ void buildBVH(const std::string& fileName, osg::Node* node);
+};
+
+struct NoBuildBVHPolicy {
+ NoBuildBVHPolicy(const std::string& extension) {}
+ void buildBVH(const std::string& fileName, osg::Node* node);
+};
+
typedef ModelRegistryCallback<DefaultProcessPolicy, DefaultCachePolicy,
- OptimizeModelPolicy, DefaultCopyPolicy,
- OSGSubstitutePolicy> DefaultCallback;
+ OptimizeModelPolicy,
+ OSGSubstitutePolicy, BuildLeafBVHPolicy>
+DefaultCallback;
// The manager for the callbacks
-class ModelRegistry : public osgDB::Registry::ReadFileCallback {
+class ModelRegistry : public osgDB::Registry::ReadFileCallback,
+ public ReferencedSingleton<ModelRegistry> {
public:
ModelRegistry();
virtual osgDB::ReaderWriter::ReadResult
void addNodeCallbackForExtension(const std::string& extension,
osgDB::Registry::ReadFileCallback*
callback);
- static ModelRegistry* getInstance();
virtual ~ModelRegistry() {}
protected:
- static osg::ref_ptr<ModelRegistry> instance;
typedef std::map<std::string, osg::ref_ptr<osgDB::Registry::ReadFileCallback> >
CallbackMap;
CallbackMap imageCallbackMap;
// Callback that only loads the file without any caching or
// postprocessing.
typedef ModelRegistryCallback<DefaultProcessPolicy, NoCachePolicy,
- NoOptimizePolicy, NoCopyPolicy,
- NoSubstitutePolicy> LoadOnlyCallback;
+ NoOptimizePolicy,
+ NoSubstitutePolicy, BuildLeafBVHPolicy>
+LoadOnlyCallback;
// Proxy for registering extension-based callbacks
public:
ModelRegistryCallbackProxy(std::string extension)
{
- ModelRegistry::getInstance()
+ ModelRegistry::instance()
->addNodeCallbackForExtension(extension, new T(extension));
}
};
-
-// Callback for file extensions that load files using the default OSG
-// implementation.
-
-class OSGFileCallback : public osgDB::Registry::ReadFileCallback {
-public:
- virtual osgDB::ReaderWriter::ReadResult
- readImage(const std::string& fileName,
- const osgDB::ReaderWriter::Options* opt);
- virtual osgDB::ReaderWriter::ReadResult
- readNode(const std::string& fileName,
- const osgDB::ReaderWriter::Options* opt);
-};
-
}
#endif // _SG_MODELREGISTRY_HXX