]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/ModelRegistry.cxx
Fix new livery code
[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 #include "ModelRegistry.hxx"
20
21 #include <algorithm>
22 #include <utility>
23 #include <vector>
24
25 #include <OpenThreads/ScopedLock>
26
27 #include <osg/observer_ptr>
28 #include <osg/ref_ptr>
29 #include <osg/Group>
30 #include <osg/NodeCallback>
31 #include <osg/Switch>
32 #include <osg/Material>
33 #include <osg/MatrixTransform>
34 #include <osgDB/Archive>
35 #include <osgDB/FileNameUtils>
36 #include <osgDB/FileUtils>
37 #include <osgDB/ReadFile>
38 #include <osgDB/WriteFile>
39 #include <osgDB/Registry>
40 #include <osgDB/SharedStateManager>
41 #include <osgUtil/Optimizer>
42
43 #include <simgear/scene/util/SGSceneFeatures.hxx>
44 #include <simgear/scene/util/SGStateAttributeVisitor.hxx>
45 #include <simgear/scene/util/SGTextureStateAttributeVisitor.hxx>
46 #include <simgear/scene/util/NodeAndDrawableVisitor.hxx>
47
48 #include <simgear/structure/exception.hxx>
49 #include <simgear/props/props.hxx>
50 #include <simgear/props/props_io.hxx>
51 #include <simgear/props/condition.hxx>
52
53 using namespace std;
54 using namespace osg;
55 using namespace osgUtil;
56 using namespace osgDB;
57 using namespace simgear;
58
59 // Little helper class that holds an extra reference to a
60 // loaded 3d model.
61 // Since we clone all structural nodes from our 3d models,
62 // the database pager will only see one single reference to
63 // top node of the model and expire it relatively fast.
64 // We attach that extra reference to every model cloned from
65 // a base model in the pager. When that cloned model is deleted
66 // this extra reference is deleted too. So if there are no
67 // cloned models left the model will expire.
68 namespace {
69 class SGDatabaseReference : public Observer {
70 public:
71   SGDatabaseReference(Referenced* referenced) :
72     mReferenced(referenced)
73   { }
74   virtual void objectDeleted(void*)
75   {
76     mReferenced = 0;
77   }
78 private:
79   ref_ptr<Referenced> mReferenced;
80 };
81
82 // Set the name of a Texture to the simple name of its image
83 // file. This can be used to do livery substitution after the image
84 // has been deallocated.
85 class TextureNameVisitor  : public NodeAndDrawableVisitor {
86 public:
87     TextureNameVisitor(NodeVisitor::TraversalMode tm = NodeVisitor::TRAVERSE_ALL_CHILDREN) :
88         NodeAndDrawableVisitor(tm)
89     {
90     }
91
92     virtual void apply(Node& node)
93     {
94         nameTextures(node.getStateSet());
95         traverse(node);
96     }
97
98     virtual void apply(Drawable& drawable)
99     {
100         nameTextures(drawable.getStateSet());
101     }
102 protected:
103     void nameTextures(StateSet* stateSet)
104     {
105         if (!stateSet)
106             return;
107         int numUnits = stateSet->getTextureAttributeList().size();
108         for (int i = 0; i < numUnits; ++i) {
109             StateAttribute* attr
110                 = stateSet->getTextureAttribute(i, StateAttribute::TEXTURE);
111             Texture2D* texture = dynamic_cast<Texture2D*>(attr);
112             if (!texture || !texture->getName().empty())
113                 continue;
114             const Image *image = texture->getImage();
115             if (!image)
116                 continue;
117             texture->setName(image->getFileName());
118         }
119     }
120 };
121
122 // Change the StateSets of a model to hold different textures based on
123 // a livery path.
124
125 class TextureUpdateVisitor : public NodeAndDrawableVisitor {
126 public:
127     TextureUpdateVisitor(const FilePathList& pathList) :
128         NodeAndDrawableVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN),
129         _pathList(pathList)
130     {
131     }
132     
133     virtual void apply(Node& node)
134     {
135         StateSet* stateSet = cloneStateSet(node.getStateSet());
136         if (stateSet)
137             node.setStateSet(stateSet);
138         traverse(node);
139     }
140
141     virtual void apply(Drawable& drawable)
142     {
143         StateSet* stateSet = cloneStateSet(drawable.getStateSet());
144         if (stateSet)
145             drawable.setStateSet(stateSet);
146     }
147     // Copied from Mathias' earlier SGTextureUpdateVisitor
148 protected:
149     Texture2D* textureReplace(int unit, const StateAttribute* attr)
150     {
151         const Texture2D* texture = dynamic_cast<const Texture2D*>(attr);
152
153         if (!texture)
154             return 0;
155     
156         const Image* image = texture->getImage();
157         const string* fullFilePath = 0;
158         if (image) {
159             // The currently loaded file name
160             fullFilePath = &image->getFileName();
161
162         } else {
163             fullFilePath = &texture->getName();
164         }
165         // The short name
166         string fileName = getSimpleFileName(*fullFilePath);
167         if (fileName.empty())
168             return 0;
169         // The name that should be found with the current database path
170         string fullLiveryFile = findFileInPath(fileName, _pathList);
171         // If it is empty or they are identical then there is nothing to do
172         if (fullLiveryFile.empty() || fullLiveryFile == *fullFilePath)
173             return 0;
174         Image* newImage = readImageFile(fullLiveryFile);
175         if (!newImage)
176             return 0;
177         CopyOp copyOp(CopyOp::DEEP_COPY_ALL & ~CopyOp::DEEP_COPY_IMAGES);
178         Texture2D* newTexture = static_cast<Texture2D*>(copyOp(texture));
179         if (!newTexture) {
180             return 0;
181         } else {
182             newTexture->setImage(newImage);
183             return newTexture;
184         }
185     }
186     
187     StateSet* cloneStateSet(const StateSet* stateSet)
188     {
189         typedef pair<int, Texture2D*> Tex2D;
190         vector<Tex2D> newTextures;
191         StateSet* result = 0;
192
193         if (!stateSet)
194             return 0;
195         int numUnits = stateSet->getTextureAttributeList().size();
196         if (numUnits > 0) {
197             for (int i = 0; i < numUnits; ++i) {
198                 const StateAttribute* attr
199                     = stateSet->getTextureAttribute(i, StateAttribute::TEXTURE);
200                 Texture2D* newTexture = textureReplace(i, attr);
201                 if (newTexture)
202                     newTextures.push_back(Tex2D(i, newTexture));
203             }
204             if (!newTextures.empty()) {
205                 result = static_cast<StateSet*>(stateSet->clone(CopyOp()));
206                 for (vector<Tex2D>::iterator i = newTextures.begin();
207                      i != newTextures.end();
208                      ++i) {
209                     result->setTextureAttribute(i->first, i->second);
210                 }
211             }
212         }
213         return result;
214     }
215 private:
216     FilePathList _pathList;
217 };
218
219
220 class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor {
221 public:
222   virtual void apply(int, StateSet::RefAttributePair& refAttr)
223   {
224     Texture2D* texture;
225     texture = dynamic_cast<Texture2D*>(refAttr.first.get());
226     if (!texture)
227       return;
228
229     // Hmm, true??
230     texture->setDataVariance(osg::Object::STATIC);
231
232     Image* image = texture->getImage(0);
233     if (!image)
234       return;
235
236     int s = image->s();
237     int t = image->t();
238
239     if (s <= t && 32 <= s) {
240       SGSceneFeatures::instance()->setTextureCompression(texture);
241     } else if (t < s && 32 <= t) {
242       SGSceneFeatures::instance()->setTextureCompression(texture);
243     }
244   }
245 };
246
247 class SGTexDataVarianceVisitor : public SGTextureStateAttributeVisitor {
248 public:
249   virtual void apply(int, StateSet::RefAttributePair& refAttr)
250   {
251     Texture* texture;
252     texture = dynamic_cast<Texture*>(refAttr.first.get());
253     if (!texture)
254       return;
255     
256     texture->setDataVariance(Object::STATIC);
257   }
258
259   virtual void apply(StateSet* stateSet)
260   {
261     if (!stateSet)
262       return;
263     SGTextureStateAttributeVisitor::apply(stateSet);
264     stateSet->setDataVariance(Object::STATIC);
265   }
266 };
267
268 class SGAcMaterialCrippleVisitor : public SGStateAttributeVisitor {
269 public:
270   virtual void apply(StateSet::RefAttributePair& refAttr)
271   {
272     Material* material;
273     material = dynamic_cast<Material*>(refAttr.first.get());
274     if (!material)
275       return;
276     material->setColorMode(Material::AMBIENT_AND_DIFFUSE);
277   }
278 };
279
280 // Work around an OSG bug - the file loaders don't use the file path
281 // in options while the file is being loaded.
282
283 struct OptionsPusher {
284     FilePathList localPathList;
285     bool validOptions;
286     OptionsPusher(const ReaderWriter::Options* options):
287         validOptions(false)
288     {
289         if (!options)
290             return;
291         Registry* registry = Registry::instance();
292         localPathList = registry->getDataFilePathList();
293         const FilePathList& regPathList = registry->getDataFilePathList();
294         const FilePathList& optionsPathList = options->getDatabasePathList();
295         for (FilePathList::const_iterator iter = optionsPathList.begin();
296              iter != optionsPathList.end();
297              ++iter) {
298             if (find(regPathList.begin(), regPathList.end(), *iter)
299                 == regPathList.end())
300                 localPathList.push_back(*iter);
301         }
302         // Save the current Registry path list and install the augmented one.
303         localPathList.swap(registry->getDataFilePathList());
304         validOptions = true;
305     }
306     ~OptionsPusher()
307     {
308         // Restore the old path list
309         if (validOptions)
310             localPathList.swap(Registry::instance()->getDataFilePathList());
311     }
312 };
313 } // namespace
314
315 Node* DefaultProcessPolicy::process(Node* node, const string& filename,
316                                     const ReaderWriter::Options* opt)
317 {
318     TextureNameVisitor nameVisitor;
319     node->accept(nameVisitor);
320     return node;
321 }
322
323 ReaderWriter::ReadResult
324 ModelRegistry::readImage(const string& fileName,
325                          const ReaderWriter::Options* opt)
326 {
327     OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(readerMutex);
328     CallbackMap::iterator iter
329         = imageCallbackMap.find(getFileExtension(fileName));
330     // XXX Workaround for OSG plugin bug
331     {
332         OptionsPusher pusher(opt);
333         if (iter != imageCallbackMap.end() && iter->second.valid())
334             return iter->second->readImage(fileName, opt);
335         string absFileName = findDataFile(fileName);
336         if (!fileExists(absFileName)) {
337             SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
338                    << fileName << "\"");
339             return ReaderWriter::ReadResult::FILE_NOT_FOUND;
340         }
341
342         Registry* registry = Registry::instance();
343         ReaderWriter::ReadResult res;
344         res = registry->readImageImplementation(absFileName, opt);
345         if (res.loadedFromCache())
346             SG_LOG(SG_IO, SG_INFO, "Returning cached image \""
347                    << res.getImage()->getFileName() << "\"");
348         else
349             SG_LOG(SG_IO, SG_INFO, "Reading image \""
350                    << res.getImage()->getFileName() << "\"");
351
352         return res;
353     }
354 }
355
356
357 osg::Node* DefaultCachePolicy::find(const string& fileName,
358                                     const ReaderWriter::Options* opt)
359 {
360     Registry* registry = Registry::instance();
361     osg::Node* cached
362         = dynamic_cast<Node*>(registry->getFromObjectCache(fileName));
363     if (cached)
364         SG_LOG(SG_IO, SG_INFO, "Got cached model \""
365                << fileName << "\"");
366     else
367         SG_LOG(SG_IO, SG_INFO, "Reading model \""
368                << fileName << "\"");
369     return cached;
370 }
371
372 void DefaultCachePolicy::addToCache(const string& fileName,
373                                     osg::Node* node)
374 {
375     Registry::instance()->addEntryToObjectCache(fileName, node);
376 }
377
378 // Optimizations we don't use:
379 // Don't use this one. It will break animation names ...
380 // opts |= osgUtil::Optimizer::REMOVE_REDUNDANT_NODES;
381 //
382 // opts |= osgUtil::Optimizer::REMOVE_LOADED_PROXY_NODES;
383 // opts |= osgUtil::Optimizer::COMBINE_ADJACENT_LODS;
384 // opts |= osgUtil::Optimizer::CHECK_GEOMETRY;
385 // opts |= osgUtil::Optimizer::SPATIALIZE_GROUPS;
386 // opts |= osgUtil::Optimizer::COPY_SHARED_NODES;
387 // opts |= osgUtil::Optimizer::TESSELATE_GEOMETRY;
388 // opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS;
389
390 OptimizeModelPolicy::OptimizeModelPolicy(const string& extension) :
391     _osgOptions(Optimizer::SHARE_DUPLICATE_STATE
392                 | Optimizer::MERGE_GEOMETRY
393                 | Optimizer::FLATTEN_STATIC_TRANSFORMS
394                 | Optimizer::TRISTRIP_GEOMETRY)
395 {
396 }
397
398 osg::Node* OptimizeModelPolicy::optimize(osg::Node* node,
399                                          const string& fileName,
400                                          const osgDB::ReaderWriter::Options* opt)
401 {
402     osgUtil::Optimizer optimizer;
403     optimizer.optimize(node, _osgOptions);
404
405     // Make sure the data variance of sharable objects is set to
406     // STATIC so that textures will be globally shared.
407     SGTexDataVarianceVisitor dataVarianceVisitor;
408     node->accept(dataVarianceVisitor);
409       
410     SGTexCompressionVisitor texComp;
411     node->accept(texComp);
412     return node;
413 }
414
415 osg::Node* DefaultCopyPolicy::copy(osg::Node* model, const string& fileName,
416                     const osgDB::ReaderWriter::Options* opt)
417 {
418     // Add an extra reference to the model stored in the database.
419     // That it to avoid expiring the object from the cache even if it is still
420     // in use. Note that the object cache will think that a model is unused
421     // if the reference count is 1. If we clone all structural nodes here
422     // we need that extra reference to the original object
423     SGDatabaseReference* databaseReference;
424     databaseReference = new SGDatabaseReference(model);
425     CopyOp::CopyFlags flags = CopyOp::DEEP_COPY_ALL;
426     flags &= ~CopyOp::DEEP_COPY_TEXTURES;
427     flags &= ~CopyOp::DEEP_COPY_IMAGES;
428     flags &= ~CopyOp::DEEP_COPY_STATESETS;
429     flags &= ~CopyOp::DEEP_COPY_STATEATTRIBUTES;
430     flags &= ~CopyOp::DEEP_COPY_ARRAYS;
431     flags &= ~CopyOp::DEEP_COPY_PRIMITIVES;
432     // This will safe display lists ...
433     flags &= ~CopyOp::DEEP_COPY_DRAWABLES;
434     flags &= ~CopyOp::DEEP_COPY_SHAPES;
435     osg::Node* res = CopyOp(flags)(model);
436     res->addObserver(databaseReference);
437
438     // Update liveries
439     TextureUpdateVisitor liveryUpdate(opt->getDatabasePathList());
440     res->accept(liveryUpdate);
441
442     return res;
443 }
444
445 string OSGSubstitutePolicy::substitute(const string& name,
446                                        const ReaderWriter::Options* opt)
447 {
448     string fileSansExtension = getNameLessExtension(name);
449     string osgFileName = fileSansExtension + ".osg";
450     string absFileName = findDataFile(osgFileName, opt);
451     return absFileName;
452 }
453
454 ModelRegistry::ModelRegistry() :
455     _defaultCallback(new DefaultCallback(""))
456 {
457 }
458
459 void
460 ModelRegistry::addImageCallbackForExtension(const string& extension,
461                                             Registry::ReadFileCallback* callback)
462 {
463     imageCallbackMap.insert(CallbackMap::value_type(extension, callback));
464 }
465
466 void
467 ModelRegistry::addNodeCallbackForExtension(const string& extension,
468                                            Registry::ReadFileCallback* callback)
469 {
470     nodeCallbackMap.insert(CallbackMap::value_type(extension, callback));
471 }
472
473 ref_ptr<ModelRegistry> ModelRegistry::instance;
474
475 ModelRegistry* ModelRegistry::getInstance()
476
477 {
478     if (!instance.valid())
479         instance = new ModelRegistry;
480     return instance.get();
481 }
482
483 ReaderWriter::ReadResult
484 ModelRegistry::readNode(const string& fileName,
485                         const ReaderWriter::Options* opt)
486 {
487     OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(readerMutex);
488     // XXX Workaround for OSG plugin bug.
489     OptionsPusher pusher(opt);
490     Registry* registry = Registry::instance();
491     ReaderWriter::ReadResult res;
492     Node* cached = 0;
493     CallbackMap::iterator iter
494         = nodeCallbackMap.find(getFileExtension(fileName));
495     if (iter != nodeCallbackMap.end() && iter->second.valid())
496         return iter->second->readNode(fileName, opt);
497     return _defaultCallback->readNode(fileName, opt);
498 }
499
500 class SGReadCallbackInstaller {
501 public:
502   SGReadCallbackInstaller()
503   {
504     // XXX I understand why we want this, but this seems like a weird
505     // place to set this option.
506     Referenced::setThreadSafeReferenceCounting(true);
507
508     Registry* registry = Registry::instance();
509     ReaderWriter::Options* options = new ReaderWriter::Options;
510     int cacheOptions = ReaderWriter::Options::CACHE_ALL;
511     options->
512       setObjectCacheHint((ReaderWriter::Options::CacheHintOptions)cacheOptions);
513     registry->setOptions(options);
514     registry->getOrCreateSharedStateManager()->
515       setShareMode(SharedStateManager::SHARE_STATESETS);
516     registry->setReadFileCallback(ModelRegistry::getInstance());
517   }
518 };
519
520 static SGReadCallbackInstaller readCallbackInstaller;
521
522 // we get optimal geometry from the loader.
523 struct ACOptimizePolicy : public OptimizeModelPolicy {
524     ACOptimizePolicy(const string& extension)  :
525         OptimizeModelPolicy(extension)
526     {
527         _osgOptions &= ~Optimizer::TRISTRIP_GEOMETRY;
528     }
529     Node* optimize(Node* node, const string& fileName,
530                    const ReaderWriter::Options* opt)
531     {
532         ref_ptr<Node> optimized
533             = OptimizeModelPolicy::optimize(node, fileName, opt);
534         MatrixTransform* transform
535             = dynamic_cast<MatrixTransform*>(optimized.get());
536         if (transform && transform->getMatrix().isIdentity()
537             && transform->getName().empty()
538             && transform->getNumChildren() == 1) {
539             optimized = static_cast<Node*>(transform->getChild(0));
540             Group* group = dynamic_cast<Group*>(optimized.get());
541             if (group && group->getName().empty()
542                 && group->getNumChildren() == 1)
543                 optimized = static_cast<Node*>(group->getChild(0));
544         }
545         return optimized.release();
546     }
547 };
548
549 struct ACProcessPolicy {
550     ACProcessPolicy(const string& extension) {}
551     Node* process(Node* node, const string& filename,
552                   const ReaderWriter::Options* opt)
553     {
554         Matrix m(1, 0, 0, 0,
555                  0, 0, 1, 0,
556                  0, -1, 0, 0,
557                  0, 0, 0, 1);
558         // XXX Does there need to be a Group node here to trick the
559         // optimizer into optimizing the static transform?
560         osg::Group* root = new Group;
561         MatrixTransform* transform = new MatrixTransform;
562         root->addChild(transform);
563         
564         transform->setDataVariance(Object::STATIC);
565         transform->setMatrix(m);
566         transform->addChild(node);
567         // Ok, this step is questionable.
568         // It is there to have the same visual appearance of ac objects for the
569         // first cut. Osg's ac3d loader will correctly set materials from the
570         // ac file. But the old plib loader used GL_AMBIENT_AND_DIFFUSE for the
571         // materials that in effect igored the ambient part specified in the
572         // file. We emulate that for the first cut here by changing all
573         // ac models here. But in the long term we should use the
574         // unchanged model and fix the input files instead ...
575         SGAcMaterialCrippleVisitor matCriple;
576         root->accept(matCriple);
577         return root;
578     }
579 };
580
581 typedef ModelRegistryCallback<ACProcessPolicy, DefaultCachePolicy,
582                               ACOptimizePolicy, DefaultCopyPolicy,
583                               OSGSubstitutePolicy> ACCallback;
584
585 namespace
586 {
587 ModelRegistryCallbackProxy<ACCallback> g_acRegister("ac");
588 }