]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/ModelRegistry.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / model / ModelRegistry.cxx
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
20 #ifdef HAVE_CONFIG_H
21 #  include <simgear_config.h>
22 #endif
23
24 #include "ModelRegistry.hxx"
25
26 #include <algorithm>
27 #include <utility>
28 #include <vector>
29
30 #include <OpenThreads/ScopedLock>
31
32 #include <osg/ref_ptr>
33 #include <osg/Group>
34 #include <osg/NodeCallback>
35 #include <osg/Switch>
36 #include <osg/Material>
37 #include <osg/MatrixTransform>
38 #include <osgDB/Archive>
39 #include <osgDB/FileNameUtils>
40 #include <osgDB/FileUtils>
41 #include <osgDB/ReadFile>
42 #include <osgDB/WriteFile>
43 #include <osgDB/Registry>
44 #include <osgDB/SharedStateManager>
45 #include <osgUtil/Optimizer>
46
47 #include <simgear/scene/util/SGSceneFeatures.hxx>
48 #include <simgear/scene/util/SGStateAttributeVisitor.hxx>
49 #include <simgear/scene/util/SGTextureStateAttributeVisitor.hxx>
50 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
51 #include <simgear/scene/util/NodeAndDrawableVisitor.hxx>
52
53 #include <simgear/structure/exception.hxx>
54 #include <simgear/props/props.hxx>
55 #include <simgear/props/props_io.hxx>
56 #include <simgear/props/condition.hxx>
57
58 #include "BoundingVolumeBuildVisitor.hxx"
59 #include "model.hxx"
60
61 using namespace std;
62 using namespace osg;
63 using namespace osgUtil;
64 using namespace osgDB;
65 using namespace simgear;
66
67 namespace {
68 // Set the name of a Texture to the simple name of its image
69 // file. This can be used to do livery substitution after the image
70 // has been deallocated.
71 class TextureNameVisitor  : public NodeAndDrawableVisitor {
72 public:
73     TextureNameVisitor(NodeVisitor::TraversalMode tm = NodeVisitor::TRAVERSE_ALL_CHILDREN) :
74         NodeAndDrawableVisitor(tm)
75     {
76     }
77
78     virtual void apply(Node& node)
79     {
80         nameTextures(node.getStateSet());
81         traverse(node);
82     }
83
84     virtual void apply(Drawable& drawable)
85     {
86         nameTextures(drawable.getStateSet());
87     }
88 protected:
89     void nameTextures(StateSet* stateSet)
90     {
91         if (!stateSet)
92             return;
93         int numUnits = stateSet->getTextureAttributeList().size();
94         for (int i = 0; i < numUnits; ++i) {
95             StateAttribute* attr
96                 = stateSet->getTextureAttribute(i, StateAttribute::TEXTURE);
97             Texture2D* texture = dynamic_cast<Texture2D*>(attr);
98             if (!texture || !texture->getName().empty())
99                 continue;
100             const Image *image = texture->getImage();
101             if (!image)
102                 continue;
103             texture->setName(image->getFileName());
104         }
105     }
106 };
107
108
109 class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor {
110 public:
111   virtual void apply(int, StateSet::RefAttributePair& refAttr)
112   {
113     Texture2D* texture;
114     texture = dynamic_cast<Texture2D*>(refAttr.first.get());
115     if (!texture)
116       return;
117
118     // Do not touch dynamically generated textures.
119     if (texture->getReadPBuffer())
120       return;
121     if (texture->getDataVariance() == osg::Object::DYNAMIC)
122       return;
123
124     // If no image attached, we assume this one is dynamically generated
125     Image* image = texture->getImage(0);
126     if (!image)
127       return;
128
129     int s = image->s();
130     int t = image->t();
131
132     if (s <= t && 32 <= s) {
133       SGSceneFeatures::instance()->setTextureCompression(texture);
134     } else if (t < s && 32 <= t) {
135       SGSceneFeatures::instance()->setTextureCompression(texture);
136     }
137   }
138 };
139
140 class SGTexDataVarianceVisitor : public SGTextureStateAttributeVisitor {
141 public:
142   virtual void apply(int, StateSet::RefAttributePair& refAttr)
143   {
144     Texture* texture;
145     texture = dynamic_cast<Texture*>(refAttr.first.get());
146     if (!texture)
147       return;
148
149     // Cannot be static if this is a render to texture thing
150     if (texture->getReadPBuffer())
151       return;
152     if (texture->getDataVariance() == osg::Object::DYNAMIC)
153       return;
154     // If no image attached, we assume this one is dynamically generated
155     Image* image = texture->getImage(0);
156     if (!image)
157       return;
158     
159     texture->setDataVariance(Object::STATIC);
160   }
161
162   virtual void apply(StateSet* stateSet)
163   {
164     if (!stateSet)
165       return;
166     stateSet->setDataVariance(Object::STATIC);
167     SGTextureStateAttributeVisitor::apply(stateSet);
168   }
169 };
170
171 } // namespace
172
173 Node* DefaultProcessPolicy::process(Node* node, const string& filename,
174                                     const Options* opt)
175 {
176     TextureNameVisitor nameVisitor;
177     node->accept(nameVisitor);
178     return node;
179 }
180
181 ReaderWriter::ReadResult
182 ModelRegistry::readImage(const string& fileName,
183                          const Options* opt)
184 {
185     CallbackMap::iterator iter
186         = imageCallbackMap.find(getFileExtension(fileName));
187     {
188         if (iter != imageCallbackMap.end() && iter->second.valid())
189             return iter->second->readImage(fileName, opt);
190         string absFileName = SGModelLib::findDataFile(fileName, opt);
191         if (!fileExists(absFileName)) {
192             SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
193                    << fileName << "\"");
194             return ReaderWriter::ReadResult::FILE_NOT_FOUND;
195         }
196
197         Registry* registry = Registry::instance();
198         ReaderWriter::ReadResult res;
199         res = registry->readImageImplementation(absFileName, opt);
200         if (!res.success()) {
201           SG_LOG(SG_IO, SG_WARN, "Image loading failed:" << res.message());
202           return res;
203         }
204         
205         if (res.loadedFromCache())
206             SG_LOG(SG_IO, SG_BULK, "Returning cached image \""
207                    << res.getImage()->getFileName() << "\"");
208         else
209             SG_LOG(SG_IO, SG_BULK, "Reading image \""
210                    << res.getImage()->getFileName() << "\"");
211
212         // Check for precompressed textures that depend on an extension
213         switch (res.getImage()->getPixelFormat()) {
214
215             // GL_EXT_texture_compression_s3tc
216             // patented, no way to decompress these
217 #ifndef GL_EXT_texture_compression_s3tc
218 #define GL_COMPRESSED_RGB_S3TC_DXT1_EXT   0x83F0
219 #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT  0x83F1
220 #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT  0x83F2
221 #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT  0x83F3
222 #endif
223         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
224         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
225         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
226         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
227             
228             // GL_EXT_texture_sRGB
229             // patented, no way to decompress these
230 #ifndef GL_EXT_texture_sRGB
231 #define GL_COMPRESSED_SRGB_S3TC_DXT1_EXT  0x8C4C
232 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
233 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
234 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
235 #endif
236         case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
237         case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
238         case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
239         case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
240             
241             // GL_TDFX_texture_compression_FXT1
242             // can decompress these in software but
243             // no code present in simgear.
244 #ifndef GL_3DFX_texture_compression_FXT1
245 #define GL_COMPRESSED_RGB_FXT1_3DFX       0x86B0
246 #define GL_COMPRESSED_RGBA_FXT1_3DFX      0x86B1
247 #endif
248         case GL_COMPRESSED_RGB_FXT1_3DFX:
249         case GL_COMPRESSED_RGBA_FXT1_3DFX:
250             
251             // GL_EXT_texture_compression_rgtc
252             // can decompress these in software but
253             // no code present in simgear.
254 #ifndef GL_EXT_texture_compression_rgtc
255 #define GL_COMPRESSED_RED_RGTC1_EXT       0x8DBB
256 #define GL_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
257 #define GL_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
258 #define GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
259 #endif
260         case GL_COMPRESSED_RED_RGTC1_EXT:
261         case GL_COMPRESSED_SIGNED_RED_RGTC1_EXT:
262         case GL_COMPRESSED_RED_GREEN_RGTC2_EXT:
263         case GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT:
264
265             SG_LOG(SG_IO, SG_ALERT, "Image \"" << fileName << "\"\n"
266                    "uses compressed textures which cannot be supported on "
267                    "some systems.\n"
268                    "Please decompress this texture for improved portability.");
269             break;
270
271         default:
272             break;
273         }
274
275         return res;
276     }
277 }
278
279
280 osg::Node* DefaultCachePolicy::find(const string& fileName,
281                                     const Options* opt)
282 {
283     Registry* registry = Registry::instance();
284     osg::Node* cached
285         = dynamic_cast<Node*>(registry->getFromObjectCache(fileName));
286     if (cached)
287         SG_LOG(SG_IO, SG_BULK, "Got cached model \""
288                << fileName << "\"");
289     else
290         SG_LOG(SG_IO, SG_BULK, "Reading model \""
291                << fileName << "\"");
292     return cached;
293 }
294
295 void DefaultCachePolicy::addToCache(const string& fileName,
296                                     osg::Node* node)
297 {
298     Registry::instance()->addEntryToObjectCache(fileName, node);
299 }
300
301 // Optimizations we don't use:
302 // Don't use this one. It will break animation names ...
303 // opts |= osgUtil::Optimizer::REMOVE_REDUNDANT_NODES;
304 //
305 // opts |= osgUtil::Optimizer::REMOVE_LOADED_PROXY_NODES;
306 // opts |= osgUtil::Optimizer::COMBINE_ADJACENT_LODS;
307 // opts |= osgUtil::Optimizer::CHECK_GEOMETRY;
308 // opts |= osgUtil::Optimizer::SPATIALIZE_GROUPS;
309 // opts |= osgUtil::Optimizer::COPY_SHARED_NODES;
310 // opts |= osgUtil::Optimizer::TESSELATE_GEOMETRY;
311 // opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS;
312
313 OptimizeModelPolicy::OptimizeModelPolicy(const string& extension) :
314     _osgOptions(Optimizer::SHARE_DUPLICATE_STATE
315                 | Optimizer::MERGE_GEOMETRY
316                 | Optimizer::FLATTEN_STATIC_TRANSFORMS
317                 | Optimizer::INDEX_MESH
318                 | Optimizer::VERTEX_POSTTRANSFORM
319                 | Optimizer::VERTEX_PRETRANSFORM)
320 {
321 }
322
323 osg::Node* OptimizeModelPolicy::optimize(osg::Node* node,
324                                          const string& fileName,
325                                          const osgDB::Options* opt)
326 {
327     osgUtil::Optimizer optimizer;
328     optimizer.optimize(node, _osgOptions);
329
330     // Make sure the data variance of sharable objects is set to
331     // STATIC so that textures will be globally shared.
332     SGTexDataVarianceVisitor dataVarianceVisitor;
333     node->accept(dataVarianceVisitor);
334
335     SGTexCompressionVisitor texComp;
336     node->accept(texComp);
337     return node;
338 }
339
340 string OSGSubstitutePolicy::substitute(const string& name,
341                                        const Options* opt)
342 {
343     string fileSansExtension = getNameLessExtension(name);
344     string osgFileName = fileSansExtension + ".osg";
345     string absFileName = SGModelLib::findDataFile(osgFileName, opt);
346     return absFileName;
347 }
348
349
350 void
351 BuildLeafBVHPolicy::buildBVH(const std::string& fileName, osg::Node* node)
352 {
353     SG_LOG(SG_IO, SG_BULK, "Building leaf attached boundingvolume tree for \""
354            << fileName << "\".");
355     BoundingVolumeBuildVisitor bvBuilder(true);
356     node->accept(bvBuilder);
357 }
358
359 void
360 BuildGroupBVHPolicy::buildBVH(const std::string& fileName, osg::Node* node)
361 {
362     SG_LOG(SG_IO, SG_BULK, "Building group attached boundingvolume tree for \""
363            << fileName << "\".");
364     BoundingVolumeBuildVisitor bvBuilder(false);
365     node->accept(bvBuilder);
366 }
367
368 void
369 NoBuildBVHPolicy::buildBVH(const std::string& fileName, osg::Node*)
370 {
371     SG_LOG(SG_IO, SG_BULK, "Omitting boundingvolume tree for \""
372            << fileName << "\".");
373 }
374
375 ModelRegistry::ModelRegistry() :
376     _defaultCallback(new DefaultCallback(""))
377 {
378 }
379
380 void
381 ModelRegistry::addImageCallbackForExtension(const string& extension,
382                                             Registry::ReadFileCallback* callback)
383 {
384     imageCallbackMap.insert(CallbackMap::value_type(extension, callback));
385 }
386
387 void
388 ModelRegistry::addNodeCallbackForExtension(const string& extension,
389                                            Registry::ReadFileCallback* callback)
390 {
391     nodeCallbackMap.insert(CallbackMap::value_type(extension, callback));
392 }
393
394 ReaderWriter::ReadResult
395 ModelRegistry::readNode(const string& fileName,
396                         const Options* opt)
397 {
398     ReaderWriter::ReadResult res;
399     CallbackMap::iterator iter
400         = nodeCallbackMap.find(getFileExtension(fileName));
401     ReaderWriter::ReadResult result;
402     if (iter != nodeCallbackMap.end() && iter->second.valid())
403         result = iter->second->readNode(fileName, opt);
404     else
405         result = _defaultCallback->readNode(fileName, opt);
406
407     return result;
408 }
409
410 class SGReadCallbackInstaller {
411 public:
412   SGReadCallbackInstaller()
413   {
414     // XXX I understand why we want this, but this seems like a weird
415     // place to set this option.
416     Referenced::setThreadSafeReferenceCounting(true);
417
418     Registry* registry = Registry::instance();
419     Options* options = new Options;
420     int cacheOptions = Options::CACHE_ALL;
421     options->
422       setObjectCacheHint((Options::CacheHintOptions)cacheOptions);
423     registry->setOptions(options);
424     registry->getOrCreateSharedStateManager()->
425       setShareMode(SharedStateManager::SHARE_STATESETS);
426     registry->setReadFileCallback(ModelRegistry::instance());
427   }
428 };
429
430 static SGReadCallbackInstaller readCallbackInstaller;
431
432 // we get optimal geometry from the loader (Hah!).
433 struct ACOptimizePolicy : public OptimizeModelPolicy {
434     ACOptimizePolicy(const string& extension)  :
435         OptimizeModelPolicy(extension)
436     {
437         _osgOptions &= ~Optimizer::TRISTRIP_GEOMETRY;
438     }
439     Node* optimize(Node* node, const string& fileName,
440                    const Options* opt)
441     {
442         ref_ptr<Node> optimized
443             = OptimizeModelPolicy::optimize(node, fileName, opt);
444         Group* group = dynamic_cast<Group*>(optimized.get());
445         MatrixTransform* transform
446             = dynamic_cast<MatrixTransform*>(optimized.get());
447         if (((transform && transform->getMatrix().isIdentity()) || group)
448             && group->getName().empty()
449             && group->getNumChildren() == 1) {
450             optimized = static_cast<Node*>(group->getChild(0));
451             group = dynamic_cast<Group*>(optimized.get());
452             if (group && group->getName().empty()
453                 && group->getNumChildren() == 1)
454                 optimized = static_cast<Node*>(group->getChild(0));
455         }
456         const SGReaderWriterOptions* sgopt
457             = dynamic_cast<const SGReaderWriterOptions*>(opt);
458         if (sgopt && sgopt->getInstantiateEffects())
459             optimized = instantiateEffects(optimized.get(), sgopt);
460         return optimized.release();
461     }
462 };
463
464 struct ACProcessPolicy {
465     ACProcessPolicy(const string& extension) {}
466     Node* process(Node* node, const string& filename,
467                   const Options* opt)
468     {
469         Matrix m(1, 0, 0, 0,
470                  0, 0, 1, 0,
471                  0, -1, 0, 0,
472                  0, 0, 0, 1);
473         // XXX Does there need to be a Group node here to trick the
474         // optimizer into optimizing the static transform?
475         osg::Group* root = new Group;
476         MatrixTransform* transform = new MatrixTransform;
477         root->addChild(transform);
478         
479         transform->setDataVariance(Object::STATIC);
480         transform->setMatrix(m);
481         transform->addChild(node);
482
483         return root;
484     }
485 };
486
487 typedef ModelRegistryCallback<ACProcessPolicy, DefaultCachePolicy,
488                               ACOptimizePolicy,
489                               OSGSubstitutePolicy, BuildLeafBVHPolicy>
490 ACCallback;
491
492 namespace
493 {
494 ModelRegistryCallbackProxy<ACCallback> g_acRegister("ac");
495 }