#ifndef _SG_MODELREGISTRY_HXX
#define _SG_MODELREGISTRY_HXX 1
+#include <OpenThreads/ReentrantMutex>
+
#include <osg/ref_ptr>
#include <osg/Node>
#include <osgDB/FileUtils>
#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 CopyPolicy, 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)
+ _substitutePolicy(extension), _bvhPolicy(extension)
{
}
virtual osgDB::ReaderWriter::ReadResult
optimizedNode = _optimizePolicy.optimize(processedNode.get(),
fileName, opt);
}
+ _bvhPolicy.buildBVH(fileName, optimizedNode.get());
_cachePolicy.addToCache(fileName, optimizedNode.get());
}
- return ReaderWriter::ReadResult(_copyPolicy.copy(optimizedNode.get(),
- fileName,
- opt));
+ osg::ref_ptr<osg::Node> copyNode;
+ copyNode = _copyPolicy.copy(optimizedNode.get(), fileName, opt);
+ return ReaderWriter::ReadResult(copyNode);
}
protected:
static osgDB::ReaderWriter::ReadResult
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 {
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;
+ 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;
CallbackMap nodeCallbackMap;
osg::ref_ptr<DefaultCallback> _defaultCallback;
+ // Protect against simultaneous calls from main thread (MP models)
+ // and pager thread.
+ OpenThreads::ReentrantMutex readerMutex;
};
// Callback that only loads the file without any caching or
// postprocessing.
typedef ModelRegistryCallback<DefaultProcessPolicy, NoCachePolicy,
NoOptimizePolicy, NoCopyPolicy,
- NoSubstitutePolicy> LoadOnlyCallback;
+ 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