1 // ModelRegistry.hxx -- interface to the OSG model registry
3 // Copyright (C) 2005-2007 Mathias Froehlich
4 // Copyright (C) 2007 Tim Moore <timoore@redhat.com>
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.
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.
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"
25 #include <OpenThreads/ScopedLock>
27 #include <osg/observer_ptr>
28 #include <osg/ref_ptr>
30 #include <osg/NodeCallback>
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>
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>
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>
55 using namespace osgUtil;
56 using namespace osgDB;
57 using namespace simgear;
59 using OpenThreads::ReentrantMutex;
60 using OpenThreads::ScopedLock;
62 // Little helper class that holds an extra reference to a
64 // Since we clone all structural nodes from our 3d models,
65 // the database pager will only see one single reference to
66 // top node of the model and expire it relatively fast.
67 // We attach that extra reference to every model cloned from
68 // a base model in the pager. When that cloned model is deleted
69 // this extra reference is deleted too. So if there are no
70 // cloned models left the model will expire.
72 class SGDatabaseReference : public Observer {
74 SGDatabaseReference(Referenced* referenced) :
75 mReferenced(referenced)
77 virtual void objectDeleted(void*)
82 ref_ptr<Referenced> mReferenced;
85 // Set the name of a Texture to the simple name of its image
86 // file. This can be used to do livery substitution after the image
87 // has been deallocated.
88 class TextureNameVisitor : public NodeAndDrawableVisitor {
90 TextureNameVisitor(NodeVisitor::TraversalMode tm = NodeVisitor::TRAVERSE_ALL_CHILDREN) :
91 NodeAndDrawableVisitor(tm)
95 virtual void apply(Node& node)
97 nameTextures(node.getStateSet());
101 virtual void apply(Drawable& drawable)
103 nameTextures(drawable.getStateSet());
106 void nameTextures(StateSet* stateSet)
110 int numUnits = stateSet->getTextureAttributeList().size();
111 for (int i = 0; i < numUnits; ++i) {
113 = stateSet->getTextureAttribute(i, StateAttribute::TEXTURE);
114 Texture2D* texture = dynamic_cast<Texture2D*>(attr);
115 if (!texture || !texture->getName().empty())
117 const Image *image = texture->getImage();
120 texture->setName(image->getFileName());
125 // Change the StateSets of a model to hold different textures based on
128 class TextureUpdateVisitor : public NodeAndDrawableVisitor {
130 TextureUpdateVisitor(const FilePathList& pathList) :
131 NodeAndDrawableVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN),
136 virtual void apply(Node& node)
138 StateSet* stateSet = cloneStateSet(node.getStateSet());
140 node.setStateSet(stateSet);
144 virtual void apply(Drawable& drawable)
146 StateSet* stateSet = cloneStateSet(drawable.getStateSet());
148 drawable.setStateSet(stateSet);
150 // Copied from Mathias' earlier SGTextureUpdateVisitor
152 Texture2D* textureReplace(int unit, const StateAttribute* attr)
154 const Texture2D* texture = dynamic_cast<const Texture2D*>(attr);
159 const Image* image = texture->getImage();
160 const string* fullFilePath = 0;
162 // The currently loaded file name
163 fullFilePath = &image->getFileName();
166 fullFilePath = &texture->getName();
169 string fileName = getSimpleFileName(*fullFilePath);
170 if (fileName.empty())
172 // The name that should be found with the current database path
173 string fullLiveryFile = findFileInPath(fileName, _pathList);
174 // If it is empty or they are identical then there is nothing to do
175 if (fullLiveryFile.empty() || fullLiveryFile == *fullFilePath)
177 Image* newImage = readImageFile(fullLiveryFile);
180 CopyOp copyOp(CopyOp::DEEP_COPY_ALL & ~CopyOp::DEEP_COPY_IMAGES);
181 Texture2D* newTexture = static_cast<Texture2D*>(copyOp(texture));
185 newTexture->setImage(newImage);
190 StateSet* cloneStateSet(const StateSet* stateSet)
192 typedef pair<int, Texture2D*> Tex2D;
193 vector<Tex2D> newTextures;
194 StateSet* result = 0;
198 int numUnits = stateSet->getTextureAttributeList().size();
200 for (int i = 0; i < numUnits; ++i) {
201 const StateAttribute* attr
202 = stateSet->getTextureAttribute(i, StateAttribute::TEXTURE);
203 Texture2D* newTexture = textureReplace(i, attr);
205 newTextures.push_back(Tex2D(i, newTexture));
207 if (!newTextures.empty()) {
208 result = static_cast<StateSet*>(stateSet->clone(CopyOp()));
209 for (vector<Tex2D>::iterator i = newTextures.begin();
210 i != newTextures.end();
212 result->setTextureAttribute(i->first, i->second);
219 FilePathList _pathList;
223 class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor {
225 virtual void apply(int, StateSet::RefAttributePair& refAttr)
228 texture = dynamic_cast<Texture2D*>(refAttr.first.get());
233 texture->setDataVariance(osg::Object::STATIC);
235 Image* image = texture->getImage(0);
242 if (s <= t && 32 <= s) {
243 SGSceneFeatures::instance()->setTextureCompression(texture);
244 } else if (t < s && 32 <= t) {
245 SGSceneFeatures::instance()->setTextureCompression(texture);
250 class SGTexDataVarianceVisitor : public SGTextureStateAttributeVisitor {
252 virtual void apply(int, StateSet::RefAttributePair& refAttr)
255 texture = dynamic_cast<Texture*>(refAttr.first.get());
259 texture->setDataVariance(Object::STATIC);
262 virtual void apply(StateSet* stateSet)
266 SGTextureStateAttributeVisitor::apply(stateSet);
267 stateSet->setDataVariance(Object::STATIC);
271 class SGAcMaterialCrippleVisitor : public SGStateAttributeVisitor {
273 virtual void apply(StateSet::RefAttributePair& refAttr)
276 material = dynamic_cast<Material*>(refAttr.first.get());
279 material->setColorMode(Material::AMBIENT_AND_DIFFUSE);
283 // Work around an OSG bug - the file loaders don't use the file path
284 // in options while the file is being loaded.
286 struct OptionsPusher {
287 FilePathList localPathList;
289 OptionsPusher(const ReaderWriter::Options* options):
294 Registry* registry = Registry::instance();
295 localPathList = registry->getDataFilePathList();
296 const FilePathList& regPathList = registry->getDataFilePathList();
297 const FilePathList& optionsPathList = options->getDatabasePathList();
298 for (FilePathList::const_iterator iter = optionsPathList.begin();
299 iter != optionsPathList.end();
301 if (find(regPathList.begin(), regPathList.end(), *iter)
302 == regPathList.end())
303 localPathList.push_back(*iter);
305 // Save the current Registry path list and install the augmented one.
306 localPathList.swap(registry->getDataFilePathList());
311 // Restore the old path list
313 localPathList.swap(Registry::instance()->getDataFilePathList());
318 Node* DefaultProcessPolicy::process(Node* node, const string& filename,
319 const ReaderWriter::Options* opt)
321 TextureNameVisitor nameVisitor;
322 node->accept(nameVisitor);
326 ReaderWriter::ReadResult
327 ModelRegistry::readImage(const string& fileName,
328 const ReaderWriter::Options* opt)
330 ScopedLock<ReentrantMutex> lock(readerMutex);
331 CallbackMap::iterator iter
332 = imageCallbackMap.find(getFileExtension(fileName));
333 // XXX Workaround for OSG plugin bug
335 OptionsPusher pusher(opt);
336 if (iter != imageCallbackMap.end() && iter->second.valid())
337 return iter->second->readImage(fileName, opt);
338 string absFileName = findDataFile(fileName);
339 if (!fileExists(absFileName)) {
340 SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
341 << fileName << "\"");
342 return ReaderWriter::ReadResult::FILE_NOT_FOUND;
345 Registry* registry = Registry::instance();
346 ReaderWriter::ReadResult res;
347 res = registry->readImageImplementation(absFileName, opt);
348 if (res.loadedFromCache())
349 SG_LOG(SG_IO, SG_INFO, "Returning cached image \""
350 << res.getImage()->getFileName() << "\"");
352 SG_LOG(SG_IO, SG_INFO, "Reading image \""
353 << res.getImage()->getFileName() << "\"");
360 osg::Node* DefaultCachePolicy::find(const string& fileName,
361 const ReaderWriter::Options* opt)
363 Registry* registry = Registry::instance();
365 = dynamic_cast<Node*>(registry->getFromObjectCache(fileName));
367 SG_LOG(SG_IO, SG_INFO, "Got cached model \""
368 << fileName << "\"");
370 SG_LOG(SG_IO, SG_INFO, "Reading model \""
371 << fileName << "\"");
375 void DefaultCachePolicy::addToCache(const string& fileName,
378 Registry::instance()->addEntryToObjectCache(fileName, node);
381 // Optimizations we don't use:
382 // Don't use this one. It will break animation names ...
383 // opts |= osgUtil::Optimizer::REMOVE_REDUNDANT_NODES;
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;
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)
401 osg::Node* OptimizeModelPolicy::optimize(osg::Node* node,
402 const string& fileName,
403 const osgDB::ReaderWriter::Options* opt)
405 osgUtil::Optimizer optimizer;
406 optimizer.optimize(node, _osgOptions);
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);
413 SGTexCompressionVisitor texComp;
414 node->accept(texComp);
418 osg::Node* DefaultCopyPolicy::copy(osg::Node* model, const string& fileName,
419 const osgDB::ReaderWriter::Options* opt)
421 // Add an extra reference to the model stored in the database.
422 // That it to avoid expiring the object from the cache even if it is still
423 // in use. Note that the object cache will think that a model is unused
424 // if the reference count is 1. If we clone all structural nodes here
425 // we need that extra reference to the original object
426 SGDatabaseReference* databaseReference;
427 databaseReference = new SGDatabaseReference(model);
428 CopyOp::CopyFlags flags = CopyOp::DEEP_COPY_ALL;
429 flags &= ~CopyOp::DEEP_COPY_TEXTURES;
430 flags &= ~CopyOp::DEEP_COPY_IMAGES;
431 flags &= ~CopyOp::DEEP_COPY_STATESETS;
432 flags &= ~CopyOp::DEEP_COPY_STATEATTRIBUTES;
433 flags &= ~CopyOp::DEEP_COPY_ARRAYS;
434 flags &= ~CopyOp::DEEP_COPY_PRIMITIVES;
435 // This will safe display lists ...
436 flags &= ~CopyOp::DEEP_COPY_DRAWABLES;
437 flags &= ~CopyOp::DEEP_COPY_SHAPES;
438 osg::Node* res = CopyOp(flags)(model);
439 res->addObserver(databaseReference);
442 TextureUpdateVisitor liveryUpdate(opt->getDatabasePathList());
443 res->accept(liveryUpdate);
448 string OSGSubstitutePolicy::substitute(const string& name,
449 const ReaderWriter::Options* opt)
451 string fileSansExtension = getNameLessExtension(name);
452 string osgFileName = fileSansExtension + ".osg";
453 string absFileName = findDataFile(osgFileName, opt);
457 ModelRegistry::ModelRegistry() :
458 _defaultCallback(new DefaultCallback(""))
463 ModelRegistry::addImageCallbackForExtension(const string& extension,
464 Registry::ReadFileCallback* callback)
466 imageCallbackMap.insert(CallbackMap::value_type(extension, callback));
470 ModelRegistry::addNodeCallbackForExtension(const string& extension,
471 Registry::ReadFileCallback* callback)
473 nodeCallbackMap.insert(CallbackMap::value_type(extension, callback));
476 ref_ptr<ModelRegistry> ModelRegistry::instance;
478 ModelRegistry* ModelRegistry::getInstance()
481 if (!instance.valid())
482 instance = new ModelRegistry;
483 return instance.get();
486 ReaderWriter::ReadResult
487 ModelRegistry::readNode(const string& fileName,
488 const ReaderWriter::Options* opt)
490 ScopedLock<ReentrantMutex> lock(readerMutex);
491 // XXX Workaround for OSG plugin bug.
492 OptionsPusher pusher(opt);
493 Registry* registry = Registry::instance();
494 ReaderWriter::ReadResult res;
496 CallbackMap::iterator iter
497 = nodeCallbackMap.find(getFileExtension(fileName));
498 if (iter != nodeCallbackMap.end() && iter->second.valid())
499 return iter->second->readNode(fileName, opt);
500 return _defaultCallback->readNode(fileName, opt);
503 class SGReadCallbackInstaller {
505 SGReadCallbackInstaller()
507 // XXX I understand why we want this, but this seems like a weird
508 // place to set this option.
509 Referenced::setThreadSafeReferenceCounting(true);
511 Registry* registry = Registry::instance();
512 ReaderWriter::Options* options = new ReaderWriter::Options;
513 int cacheOptions = ReaderWriter::Options::CACHE_ALL;
515 setObjectCacheHint((ReaderWriter::Options::CacheHintOptions)cacheOptions);
516 registry->setOptions(options);
517 registry->getOrCreateSharedStateManager()->
518 setShareMode(SharedStateManager::SHARE_STATESETS);
519 registry->setReadFileCallback(ModelRegistry::getInstance());
523 static SGReadCallbackInstaller readCallbackInstaller;
525 // we get optimal geometry from the loader.
526 struct ACOptimizePolicy : public OptimizeModelPolicy {
527 ACOptimizePolicy(const string& extension) :
528 OptimizeModelPolicy(extension)
530 _osgOptions &= ~Optimizer::TRISTRIP_GEOMETRY;
532 Node* optimize(Node* node, const string& fileName,
533 const ReaderWriter::Options* opt)
535 ref_ptr<Node> optimized
536 = OptimizeModelPolicy::optimize(node, fileName, opt);
537 MatrixTransform* transform
538 = dynamic_cast<MatrixTransform*>(optimized.get());
539 if (transform && transform->getMatrix().isIdentity()
540 && transform->getName().empty()
541 && transform->getNumChildren() == 1) {
542 optimized = static_cast<Node*>(transform->getChild(0));
543 Group* group = dynamic_cast<Group*>(optimized.get());
544 if (group && group->getName().empty()
545 && group->getNumChildren() == 1)
546 optimized = static_cast<Node*>(group->getChild(0));
548 return optimized.release();
552 struct ACProcessPolicy {
553 ACProcessPolicy(const string& extension) {}
554 Node* process(Node* node, const string& filename,
555 const ReaderWriter::Options* opt)
561 // XXX Does there need to be a Group node here to trick the
562 // optimizer into optimizing the static transform?
563 osg::Group* root = new Group;
564 MatrixTransform* transform = new MatrixTransform;
565 root->addChild(transform);
567 transform->setDataVariance(Object::STATIC);
568 transform->setMatrix(m);
569 transform->addChild(node);
570 // Ok, this step is questionable.
571 // It is there to have the same visual appearance of ac objects for the
572 // first cut. Osg's ac3d loader will correctly set materials from the
573 // ac file. But the old plib loader used GL_AMBIENT_AND_DIFFUSE for the
574 // materials that in effect igored the ambient part specified in the
575 // file. We emulate that for the first cut here by changing all
576 // ac models here. But in the long term we should use the
577 // unchanged model and fix the input files instead ...
578 SGAcMaterialCrippleVisitor matCriple;
579 root->accept(matCriple);
584 typedef ModelRegistryCallback<ACProcessPolicy, DefaultCachePolicy,
585 ACOptimizePolicy, DefaultCopyPolicy,
586 OSGSubstitutePolicy> ACCallback;
590 ModelRegistryCallbackProxy<ACCallback> g_acRegister("ac");