]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/ModelRegistry.hxx
Provide something more sensible for the properties root
[simgear.git] / simgear / scene / model / ModelRegistry.hxx
index 2c0102976c2815096a09a880ceecd99bc4137e39..998d2f2fca5ea17fe0476cfe3d0c43f344e1b2fe 100644 (file)
@@ -19,6 +19,8 @@
 #ifndef _SG_MODELREGISTRY_HXX
 #define _SG_MODELREGISTRY_HXX 1
 
+#include <OpenThreads/ReentrantMutex>
+
 #include <osg/ref_ptr>
 #include <osg/Node>
 #include <osgDB/FileUtils>
@@ -27,8 +29,9 @@
 #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
@@ -58,13 +61,13 @@ namespace simgear
 // 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
@@ -94,11 +97,12 @@ public:
                 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
@@ -118,6 +122,7 @@ protected:
     OptimizePolicy _optimizePolicy;
     CopyPolicy _copyPolicy;
     SubstitutePolicy _substitutePolicy;
+    BVHPolicy _bvhPolicy;
     virtual ~ModelRegistryCallback() {}
 };
 
@@ -126,10 +131,7 @@ protected:
 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 {
@@ -196,12 +198,30 @@ struct NoSubstitutePolicy {
         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
@@ -216,22 +236,24 @@ public:
     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
 
@@ -241,23 +263,9 @@ class ModelRegistryCallbackProxy
 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