]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/ModelRegistry.hxx
7b7ffe4eb2b43b180c623e388035df76e5654211
[simgear.git] / simgear / scene / model / ModelRegistry.hxx
1 // ModelRegistry.hxx -- interface to the OSG model registry
2 //
3 // Copyright (C) 2005-2007 Mathias Froehlich 
4 // Copyright (C) 2007  Tim Moore <timoore@redhat.com>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 #ifndef _SG_MODELREGISTRY_HXX
20 #define _SG_MODELREGISTRY_HXX 1
21
22 #include <OpenThreads/ReentrantMutex>
23
24 #include <osg/ref_ptr>
25 #include <osg/Node>
26 #include <osgDB/FileUtils>
27 #include <osgDB/FileNameUtils>
28 #include <osgDB/ReaderWriter>
29 #include <osgDB/Registry>
30
31 #include <simgear/compiler.h>
32 #include <simgear/structure/Singleton.hxx>
33
34 #include <string>
35 #include <map>
36
37 // Class to register per file extension read callbacks with the OSG
38 // registry, mostly to control caching and post load optimization /
39 // copying that happens above the level of the ReaderWriter.
40 namespace simgear
41 {
42
43 // Different caching and optimization strategies are needed for
44 // different file types. Most loaded files should be optimized and the
45 // optimized version should be cached. When an .osg file is
46 // substituted for another, it is assumed to be optimized already but
47 // it should be cached too (under the name of the original?). .stg
48 // files should not be cached (that's the pager's job) but the files
49 // it causes to be loaded should be. .btg files are already optimized
50 // and shouldn't be cached.
51 //
52 // Complicating this is the effect that removing CACHE_NODES has from
53 // the ReaderWriter options: it switches the object cache with an
54 // empty one, so that's not an option for the files that could be
55 // loaded from a .stg file. So, we'll let
56 // Registry::readNodeImplementation cache a loaded file and then add
57 // the optimized version to the cache ourselves, replacing the
58 // original subgraph.
59 //
60 // To support all these options with a minimum of duplication, the
61 // readNode function is specified as a template with a bunch of
62 // pluggable (and predefined) policies.
63 template <typename ProcessPolicy, typename CachePolicy, typename OptimizePolicy,
64           typename CopyPolicy, typename SubstitutePolicy>
65 class ModelRegistryCallback : public osgDB::Registry::ReadFileCallback {
66 public:
67     ModelRegistryCallback(const std::string& extension) :
68         _processPolicy(extension), _cachePolicy(extension),
69         _optimizePolicy(extension), _copyPolicy(extension),
70         _substitutePolicy(extension)
71     {
72     }
73     virtual osgDB::ReaderWriter::ReadResult
74     readNode(const std::string& fileName,
75              const osgDB::ReaderWriter::Options* opt)
76     {
77         using namespace osg;
78         using namespace osgDB;
79         using osgDB::ReaderWriter;
80         Registry* registry = Registry::instance();
81         ref_ptr<osg::Node> optimizedNode = _cachePolicy.find(fileName, opt);
82         if (!optimizedNode.valid()) {
83             std::string otherFileName = _substitutePolicy.substitute(fileName,
84                                                                      opt);
85             ReaderWriter::ReadResult res;
86             if (!otherFileName.empty()) {
87                 res = loadUsingReaderWriter(otherFileName, opt);
88                 if (res.validNode())
89                     optimizedNode = res.getNode();
90             }
91             if (!optimizedNode.valid()) {
92                 res = loadUsingReaderWriter(fileName, opt);
93                 if (!res.validNode())
94                     return res;
95                 ref_ptr<osg::Node> processedNode
96                     = _processPolicy.process(res.getNode(), fileName, opt);
97                 optimizedNode = _optimizePolicy.optimize(processedNode.get(),
98                                                          fileName, opt);
99             }
100             _cachePolicy.addToCache(fileName, optimizedNode.get());
101         }
102         return ReaderWriter::ReadResult(_copyPolicy.copy(optimizedNode.get(),
103                                                          fileName,
104                                                          opt));
105     }
106 protected:
107     static osgDB::ReaderWriter::ReadResult
108     loadUsingReaderWriter(const std::string& fileName,
109                           const osgDB::ReaderWriter::Options* opt)
110     {
111         using namespace osgDB;
112         ReaderWriter* rw = Registry::instance()
113             ->getReaderWriterForExtension(osgDB::getFileExtension(fileName));
114         if (!rw)
115             return ReaderWriter::ReadResult(); // FILE_NOT_HANDLED
116         return rw->readNode(fileName, opt);
117     }
118     
119     ProcessPolicy _processPolicy;
120     CachePolicy _cachePolicy;
121     OptimizePolicy _optimizePolicy;
122     CopyPolicy _copyPolicy;
123     SubstitutePolicy _substitutePolicy;
124     virtual ~ModelRegistryCallback() {}
125 };
126
127 // Predefined policies
128
129 struct DefaultProcessPolicy {
130     DefaultProcessPolicy(const std::string& extension) {}
131     osg::Node* process(osg::Node* node, const std::string& filename,
132                        const osgDB::ReaderWriter::Options* opt);
133 };
134
135 struct DefaultCachePolicy {
136     DefaultCachePolicy(const std::string& extension) {}
137     osg::Node* find(const std::string& fileName,
138                     const osgDB::ReaderWriter::Options* opt);
139     void addToCache(const std::string& filename, osg::Node* node);
140 };
141
142 struct NoCachePolicy {
143     NoCachePolicy(const std::string& extension) {}
144     osg::Node* find(const std::string& fileName,
145                     const osgDB::ReaderWriter::Options* opt)
146     {
147         return 0;
148     }
149     void addToCache(const std::string& filename, osg::Node* node) {}
150 };
151
152 class OptimizeModelPolicy {
153 public:
154     OptimizeModelPolicy(const std::string& extension);
155     osg::Node* optimize(osg::Node* node, const std::string& fileName,
156                         const osgDB::ReaderWriter::Options* opt);
157 protected:
158     unsigned _osgOptions;
159 };
160
161 struct NoOptimizePolicy {
162     NoOptimizePolicy(const std::string& extension) {}
163     osg::Node* optimize(osg::Node* node, const std::string& fileName,
164                         const osgDB::ReaderWriter::Options* opt)
165     {
166         return node;
167     }
168 };
169
170 struct DefaultCopyPolicy {
171     DefaultCopyPolicy(const std::string& extension) {}
172     osg::Node* copy(osg::Node* node, const std::string& fileName,
173                     const osgDB::ReaderWriter::Options* opt);
174 };
175
176 struct NoCopyPolicy {
177     NoCopyPolicy(const std::string& extension) {}
178     osg::Node* copy(osg::Node* node, const std::string& fileName,
179                     const osgDB::ReaderWriter::Options* opt)
180     {
181         return node;
182     }
183 };
184
185 struct OSGSubstitutePolicy {
186     OSGSubstitutePolicy(const std::string& extension) {}
187     std::string substitute(const std::string& name,
188                            const osgDB::ReaderWriter::Options* opt);
189 };
190
191 struct NoSubstitutePolicy {
192     NoSubstitutePolicy(const std::string& extension) {}
193     std::string substitute(const std::string& name,
194                            const osgDB::ReaderWriter::Options* opt)
195     {
196         return std::string();
197     }
198 };
199 typedef ModelRegistryCallback<DefaultProcessPolicy, DefaultCachePolicy,
200                               OptimizeModelPolicy, DefaultCopyPolicy,
201                               OSGSubstitutePolicy> DefaultCallback;
202
203 // The manager for the callbacks
204 class ModelRegistry : public osgDB::Registry::ReadFileCallback,
205                       public ReferencedSingleton<ModelRegistry> {
206 public:
207     ModelRegistry();
208     virtual osgDB::ReaderWriter::ReadResult
209     readImage(const std::string& fileName,
210               const osgDB::ReaderWriter::Options* opt);
211     virtual osgDB::ReaderWriter::ReadResult
212     readNode(const std::string& fileName,
213              const osgDB::ReaderWriter::Options* opt);
214     void addImageCallbackForExtension(const std::string& extension,
215                                       osgDB::Registry::ReadFileCallback*
216                                       callback);
217     void addNodeCallbackForExtension(const std::string& extension,
218                                      osgDB::Registry::ReadFileCallback*
219                                      callback);
220     virtual ~ModelRegistry() {}
221 protected:
222     typedef std::map<std::string, osg::ref_ptr<osgDB::Registry::ReadFileCallback> >
223     CallbackMap;
224     CallbackMap imageCallbackMap;
225     CallbackMap nodeCallbackMap;
226     osg::ref_ptr<DefaultCallback> _defaultCallback;
227     // Protect against simultaneous calls from main thread (MP models)
228     // and pager thread.
229     OpenThreads::ReentrantMutex readerMutex;
230 };
231
232 // Callback that only loads the file without any caching or
233 // postprocessing.
234 typedef ModelRegistryCallback<DefaultProcessPolicy, NoCachePolicy,
235                               NoOptimizePolicy, NoCopyPolicy,
236                               NoSubstitutePolicy> LoadOnlyCallback;
237
238 // Proxy for registering extension-based callbacks
239
240 template<typename T>
241 class ModelRegistryCallbackProxy
242 {
243 public:
244     ModelRegistryCallbackProxy(std::string extension)
245     {
246         ModelRegistry::instance()
247             ->addNodeCallbackForExtension(extension, new T(extension));
248     }
249 };
250 }
251 #endif // _SG_MODELREGISTRY_HXX