]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/ModelRegistry.hxx
Add preliminary spot light animation
[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 <osg/ref_ptr>
23 #include <osg/Node>
24 #include <osgDB/FileUtils>
25 #include <osgDB/FileNameUtils>
26 #include <osgDB/ReaderWriter>
27 #include <osgDB/Registry>
28
29 #include <simgear/compiler.h>
30 #include <simgear/scene/util/OsgSingleton.hxx>
31
32 #include <string>
33 #include <map>
34
35 // Class to register per file extension read callbacks with the OSG
36 // registry, mostly to control caching and post load optimization /
37 // copying that happens above the level of the ReaderWriter.
38 namespace simgear
39 {
40
41 // Different caching and optimization strategies are needed for
42 // different file types. Most loaded files should be optimized and the
43 // optimized version should be cached. When an .osg file is
44 // substituted for another, it is assumed to be optimized already but
45 // it should be cached too (under the name of the original?). .stg
46 // files should not be cached (that's the pager's job) but the files
47 // it causes to be loaded should be. .btg files are already optimized
48 // and shouldn't be cached.
49 //
50 // Complicating this is the effect that removing CACHE_NODES has from
51 // the ReaderWriter options: it switches the object cache with an
52 // empty one, so that's not an option for the files that could be
53 // loaded from a .stg file. So, we'll let
54 // Registry::readNodeImplementation cache a loaded file and then add
55 // the optimized version to the cache ourselves, replacing the
56 // original subgraph.
57 //
58 // To support all these options with a minimum of duplication, the
59 // readNode function is specified as a template with a bunch of
60 // pluggable (and predefined) policies.
61 template <typename ProcessPolicy, typename CachePolicy, typename OptimizePolicy,
62           typename SubstitutePolicy, typename BVHPolicy>
63 class ModelRegistryCallback : public osgDB::Registry::ReadFileCallback {
64 public:
65     ModelRegistryCallback(const std::string& extension) :
66         _processPolicy(extension), _cachePolicy(extension),
67         _optimizePolicy(extension),
68         _substitutePolicy(extension), _bvhPolicy(extension)
69     {
70     }
71     virtual osgDB::ReaderWriter::ReadResult
72     readNode(const std::string& fileName,
73              const osgDB::Options* opt)
74     {
75         using namespace osg;
76         using namespace osgDB;
77         using osgDB::ReaderWriter;
78 //        Registry* registry = Registry::instance();
79         ref_ptr<osg::Node> optimizedNode = _cachePolicy.find(fileName, opt);
80         if (!optimizedNode.valid()) {
81             std::string otherFileName = _substitutePolicy.substitute(fileName,
82                                                                      opt);
83             ReaderWriter::ReadResult res;
84             if (!otherFileName.empty()) {
85                 res = loadUsingReaderWriter(otherFileName, opt);
86                 if (res.validNode())
87                     optimizedNode = res.getNode();
88             }
89             if (!optimizedNode.valid()) {
90                 res = loadUsingReaderWriter(fileName, opt);
91                 if (!res.validNode())
92                     return res;
93                 ref_ptr<osg::Node> processedNode
94                     = _processPolicy.process(res.getNode(), fileName, opt);
95                 optimizedNode = _optimizePolicy.optimize(processedNode.get(),
96                                                          fileName, opt);
97             }
98             _bvhPolicy.buildBVH(fileName, optimizedNode.get());
99             _cachePolicy.addToCache(fileName, optimizedNode.get());
100         }
101         return ReaderWriter::ReadResult(optimizedNode.get());
102     }
103 protected:
104     static osgDB::ReaderWriter::ReadResult
105     loadUsingReaderWriter(const std::string& fileName,
106                           const osgDB::Options* opt)
107     {
108         using namespace osgDB;
109         ReaderWriter* rw = Registry::instance()
110             ->getReaderWriterForExtension(osgDB::getFileExtension(fileName));
111         if (!rw)
112             return ReaderWriter::ReadResult(); // FILE_NOT_HANDLED
113         return rw->readNode(fileName, opt);
114     }
115     
116     ProcessPolicy _processPolicy;
117     CachePolicy _cachePolicy;
118     OptimizePolicy _optimizePolicy;
119     SubstitutePolicy _substitutePolicy;
120     BVHPolicy _bvhPolicy;
121     virtual ~ModelRegistryCallback() {}
122 };
123
124 // Predefined policies
125
126 struct DefaultProcessPolicy {
127     DefaultProcessPolicy(const std::string& extension) {}
128     osg::Node* process(osg::Node* node, const std::string& filename,
129                        const osgDB::Options* opt);
130 };
131
132 struct DefaultCachePolicy {
133     DefaultCachePolicy(const std::string& extension) {}
134     osg::Node* find(const std::string& fileName,
135                     const osgDB::Options* opt);
136     void addToCache(const std::string& filename, osg::Node* node);
137 };
138
139 struct NoCachePolicy {
140     NoCachePolicy(const std::string& extension) {}
141     osg::Node* find(const std::string& fileName,
142                     const osgDB::Options* opt)
143     {
144         return 0;
145     }
146     void addToCache(const std::string& filename, osg::Node* node) {}
147 };
148
149 class OptimizeModelPolicy {
150 public:
151     OptimizeModelPolicy(const std::string& extension);
152     osg::Node* optimize(osg::Node* node, const std::string& fileName,
153                         const osgDB::Options* opt);
154 protected:
155     unsigned _osgOptions;
156 };
157
158 struct NoOptimizePolicy {
159     NoOptimizePolicy(const std::string& extension) {}
160     osg::Node* optimize(osg::Node* node, const std::string& fileName,
161                         const osgDB::Options* opt)
162     {
163         return node;
164     }
165 };
166
167 struct OSGSubstitutePolicy {
168     OSGSubstitutePolicy(const std::string& extension) {}
169     std::string substitute(const std::string& name,
170                            const osgDB::Options* opt);
171 };
172
173 struct NoSubstitutePolicy {
174     NoSubstitutePolicy(const std::string& extension) {}
175     std::string substitute(const std::string& name,
176                            const osgDB::Options* opt)
177     {
178         return std::string();
179     }
180 };
181
182 struct BuildLeafBVHPolicy {
183     BuildLeafBVHPolicy(const std::string& extension) {}
184     void buildBVH(const std::string& fileName, osg::Node* node);
185 };
186
187 struct BuildGroupBVHPolicy {
188     BuildGroupBVHPolicy(const std::string& extension) {}
189     void buildBVH(const std::string& fileName, osg::Node* node);
190 };
191
192 struct NoBuildBVHPolicy {
193     NoBuildBVHPolicy(const std::string& extension) {}
194     void buildBVH(const std::string& fileName, osg::Node* node);
195 };
196
197 typedef ModelRegistryCallback<DefaultProcessPolicy, DefaultCachePolicy,
198                               OptimizeModelPolicy,
199                               OSGSubstitutePolicy, BuildLeafBVHPolicy>
200 DefaultCallback;
201
202 // The manager for the callbacks
203 class ModelRegistry : public osgDB::Registry::ReadFileCallback,
204                       public ReferencedSingleton<ModelRegistry> {
205 public:
206     ModelRegistry();
207     virtual osgDB::ReaderWriter::ReadResult
208     readImage(const std::string& fileName,
209               const osgDB::Options* opt);
210     virtual osgDB::ReaderWriter::ReadResult
211     readNode(const std::string& fileName,
212              const osgDB::Options* opt);
213     void addImageCallbackForExtension(const std::string& extension,
214                                       osgDB::Registry::ReadFileCallback*
215                                       callback);
216     void addNodeCallbackForExtension(const std::string& extension,
217                                      osgDB::Registry::ReadFileCallback*
218                                      callback);
219     virtual ~ModelRegistry() {}
220 protected:
221     typedef std::map<std::string, osg::ref_ptr<osgDB::Registry::ReadFileCallback> >
222     CallbackMap;
223     CallbackMap imageCallbackMap;
224     CallbackMap nodeCallbackMap;
225     osg::ref_ptr<DefaultCallback> _defaultCallback;
226 };
227
228 // Callback that only loads the file without any caching or
229 // postprocessing.
230 typedef ModelRegistryCallback<DefaultProcessPolicy, NoCachePolicy,
231                               NoOptimizePolicy,
232                               NoSubstitutePolicy, BuildLeafBVHPolicy>
233 LoadOnlyCallback;
234
235 // Proxy for registering extension-based callbacks
236
237 template<typename T>
238 class ModelRegistryCallbackProxy
239 {
240 public:
241     ModelRegistryCallbackProxy(std::string extension)
242     {
243         ModelRegistry::instance()
244             ->addNodeCallbackForExtension(extension, new T(extension));
245     }
246 };
247 }
248 #endif // _SG_MODELREGISTRY_HXX