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