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.
21 # include <simgear_config.h>
24 #include "ModelRegistry.hxx"
30 #include <OpenThreads/ScopedLock>
32 #include <osg/ref_ptr>
34 #include <osg/NodeCallback>
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>
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/NodeAndDrawableVisitor.hxx>
52 #include <simgear/structure/exception.hxx>
53 #include <simgear/props/props.hxx>
54 #include <simgear/props/props_io.hxx>
55 #include <simgear/props/condition.hxx>
57 #include "BoundingVolumeBuildVisitor.hxx"
59 #include "SGReaderWriterXMLOptions.hxx"
63 using namespace osgUtil;
64 using namespace osgDB;
65 using namespace simgear;
67 // Little helper class that holds an extra reference to a
69 // Since we clone all structural nodes from our 3d models,
70 // the database pager will only see one single reference to
71 // top node of the model and expire it relatively fast.
72 // We attach that extra reference to every model cloned from
73 // a base model in the pager. When that cloned model is deleted
74 // this extra reference is deleted too. So if there are no
75 // cloned models left the model will expire.
77 class SGDatabaseReference : public Observer {
79 SGDatabaseReference(Referenced* referenced) :
80 mReferenced(referenced)
82 virtual void objectDeleted(void*)
87 ref_ptr<Referenced> mReferenced;
90 // Set the name of a Texture to the simple name of its image
91 // file. This can be used to do livery substitution after the image
92 // has been deallocated.
93 class TextureNameVisitor : public NodeAndDrawableVisitor {
95 TextureNameVisitor(NodeVisitor::TraversalMode tm = NodeVisitor::TRAVERSE_ALL_CHILDREN) :
96 NodeAndDrawableVisitor(tm)
100 virtual void apply(Node& node)
102 nameTextures(node.getStateSet());
106 virtual void apply(Drawable& drawable)
108 nameTextures(drawable.getStateSet());
111 void nameTextures(StateSet* stateSet)
115 int numUnits = stateSet->getTextureAttributeList().size();
116 for (int i = 0; i < numUnits; ++i) {
118 = stateSet->getTextureAttribute(i, StateAttribute::TEXTURE);
119 Texture2D* texture = dynamic_cast<Texture2D*>(attr);
120 if (!texture || !texture->getName().empty())
122 const Image *image = texture->getImage();
125 texture->setName(image->getFileName());
131 class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor {
133 virtual void apply(int, StateSet::RefAttributePair& refAttr)
136 texture = dynamic_cast<Texture2D*>(refAttr.first.get());
140 // Do not touch dynamically generated textures.
141 if (texture->getReadPBuffer())
143 if (texture->getDataVariance() == osg::Object::DYNAMIC)
146 // If no image attached, we assume this one is dynamically generated
147 Image* image = texture->getImage(0);
154 if (s <= t && 32 <= s) {
155 SGSceneFeatures::instance()->setTextureCompression(texture);
156 } else if (t < s && 32 <= t) {
157 SGSceneFeatures::instance()->setTextureCompression(texture);
162 class SGTexDataVarianceVisitor : public SGTextureStateAttributeVisitor {
164 virtual void apply(int, StateSet::RefAttributePair& refAttr)
167 texture = dynamic_cast<Texture*>(refAttr.first.get());
171 // Cannot be static if this is a render to texture thing
172 if (texture->getReadPBuffer())
174 if (texture->getDataVariance() == osg::Object::DYNAMIC)
176 // If no image attached, we assume this one is dynamically generated
177 Image* image = texture->getImage(0);
181 texture->setDataVariance(Object::STATIC);
184 virtual void apply(StateSet* stateSet)
188 stateSet->setDataVariance(Object::STATIC);
189 SGTextureStateAttributeVisitor::apply(stateSet);
195 Node* DefaultProcessPolicy::process(Node* node, const string& filename,
196 const ReaderWriter::Options* opt)
198 TextureNameVisitor nameVisitor;
199 node->accept(nameVisitor);
203 ReaderWriter::ReadResult
204 ModelRegistry::readImage(const string& fileName,
205 const ReaderWriter::Options* opt)
207 CallbackMap::iterator iter
208 = imageCallbackMap.find(getFileExtension(fileName));
210 if (iter != imageCallbackMap.end() && iter->second.valid())
211 return iter->second->readImage(fileName, opt);
212 string absFileName = SGModelLib::findDataFile(fileName, opt);
213 if (!fileExists(absFileName)) {
214 SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
215 << fileName << "\"");
216 return ReaderWriter::ReadResult::FILE_NOT_FOUND;
219 Registry* registry = Registry::instance();
220 ReaderWriter::ReadResult res;
221 res = registry->readImageImplementation(absFileName, opt);
222 if (!res.success()) {
223 SG_LOG(SG_IO, SG_WARN, "Image loading failed:" << res.message());
227 if (res.loadedFromCache())
228 SG_LOG(SG_IO, SG_BULK, "Returning cached image \""
229 << res.getImage()->getFileName() << "\"");
231 SG_LOG(SG_IO, SG_BULK, "Reading image \""
232 << res.getImage()->getFileName() << "\"");
239 osg::Node* DefaultCachePolicy::find(const string& fileName,
240 const ReaderWriter::Options* opt)
242 Registry* registry = Registry::instance();
244 = dynamic_cast<Node*>(registry->getFromObjectCache(fileName));
246 SG_LOG(SG_IO, SG_BULK, "Got cached model \""
247 << fileName << "\"");
249 SG_LOG(SG_IO, SG_BULK, "Reading model \""
250 << fileName << "\"");
254 void DefaultCachePolicy::addToCache(const string& fileName,
257 Registry::instance()->addEntryToObjectCache(fileName, node);
260 // Optimizations we don't use:
261 // Don't use this one. It will break animation names ...
262 // opts |= osgUtil::Optimizer::REMOVE_REDUNDANT_NODES;
264 // opts |= osgUtil::Optimizer::REMOVE_LOADED_PROXY_NODES;
265 // opts |= osgUtil::Optimizer::COMBINE_ADJACENT_LODS;
266 // opts |= osgUtil::Optimizer::CHECK_GEOMETRY;
267 // opts |= osgUtil::Optimizer::SPATIALIZE_GROUPS;
268 // opts |= osgUtil::Optimizer::COPY_SHARED_NODES;
269 // opts |= osgUtil::Optimizer::TESSELATE_GEOMETRY;
270 // opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS;
272 OptimizeModelPolicy::OptimizeModelPolicy(const string& extension) :
273 _osgOptions(Optimizer::SHARE_DUPLICATE_STATE
274 | Optimizer::MERGE_GEOMETRY
275 | Optimizer::FLATTEN_STATIC_TRANSFORMS
276 | Optimizer::TRISTRIP_GEOMETRY)
280 osg::Node* OptimizeModelPolicy::optimize(osg::Node* node,
281 const string& fileName,
282 const osgDB::ReaderWriter::Options* opt)
284 osgUtil::Optimizer optimizer;
285 optimizer.optimize(node, _osgOptions);
287 // Make sure the data variance of sharable objects is set to
288 // STATIC so that textures will be globally shared.
289 SGTexDataVarianceVisitor dataVarianceVisitor;
290 node->accept(dataVarianceVisitor);
292 SGTexCompressionVisitor texComp;
293 node->accept(texComp);
297 string OSGSubstitutePolicy::substitute(const string& name,
298 const ReaderWriter::Options* opt)
300 string fileSansExtension = getNameLessExtension(name);
301 string osgFileName = fileSansExtension + ".osg";
302 string absFileName = SGModelLib::findDataFile(osgFileName, opt);
308 BuildLeafBVHPolicy::buildBVH(const std::string& fileName, osg::Node* node)
310 SG_LOG(SG_IO, SG_BULK, "Building leaf attached boundingvolume tree for \""
311 << fileName << "\".");
312 BoundingVolumeBuildVisitor bvBuilder(true);
313 node->accept(bvBuilder);
317 BuildGroupBVHPolicy::buildBVH(const std::string& fileName, osg::Node* node)
319 SG_LOG(SG_IO, SG_BULK, "Building group attached boundingvolume tree for \""
320 << fileName << "\".");
321 BoundingVolumeBuildVisitor bvBuilder(false);
322 node->accept(bvBuilder);
326 NoBuildBVHPolicy::buildBVH(const std::string& fileName, osg::Node*)
328 SG_LOG(SG_IO, SG_BULK, "Omitting boundingvolume tree for \""
329 << fileName << "\".");
332 ModelRegistry::ModelRegistry() :
333 _defaultCallback(new DefaultCallback(""))
338 ModelRegistry::addImageCallbackForExtension(const string& extension,
339 Registry::ReadFileCallback* callback)
341 imageCallbackMap.insert(CallbackMap::value_type(extension, callback));
345 ModelRegistry::addNodeCallbackForExtension(const string& extension,
346 Registry::ReadFileCallback* callback)
348 nodeCallbackMap.insert(CallbackMap::value_type(extension, callback));
351 ReaderWriter::ReadResult
352 ModelRegistry::readNode(const string& fileName,
353 const ReaderWriter::Options* opt)
355 ReaderWriter::ReadResult res;
356 CallbackMap::iterator iter
357 = nodeCallbackMap.find(getFileExtension(fileName));
358 ReaderWriter::ReadResult result;
359 if (iter != nodeCallbackMap.end() && iter->second.valid())
360 result = iter->second->readNode(fileName, opt);
362 result = _defaultCallback->readNode(fileName, opt);
367 class SGReadCallbackInstaller {
369 SGReadCallbackInstaller()
371 // XXX I understand why we want this, but this seems like a weird
372 // place to set this option.
373 Referenced::setThreadSafeReferenceCounting(true);
375 Registry* registry = Registry::instance();
376 ReaderWriter::Options* options = new ReaderWriter::Options;
377 int cacheOptions = ReaderWriter::Options::CACHE_ALL;
379 setObjectCacheHint((ReaderWriter::Options::CacheHintOptions)cacheOptions);
380 registry->setOptions(options);
381 registry->getOrCreateSharedStateManager()->
382 setShareMode(SharedStateManager::SHARE_STATESETS);
383 registry->setReadFileCallback(ModelRegistry::instance());
387 static SGReadCallbackInstaller readCallbackInstaller;
389 // we get optimal geometry from the loader (Hah!).
390 struct ACOptimizePolicy : public OptimizeModelPolicy {
391 ACOptimizePolicy(const string& extension) :
392 OptimizeModelPolicy(extension)
394 _osgOptions &= ~Optimizer::TRISTRIP_GEOMETRY;
396 Node* optimize(Node* node, const string& fileName,
397 const ReaderWriter::Options* opt)
399 ref_ptr<Node> optimized
400 = OptimizeModelPolicy::optimize(node, fileName, opt);
401 Group* group = dynamic_cast<Group*>(optimized.get());
402 MatrixTransform* transform
403 = dynamic_cast<MatrixTransform*>(optimized.get());
404 if (((transform && transform->getMatrix().isIdentity()) || group)
405 && group->getName().empty()
406 && group->getNumChildren() == 1) {
407 optimized = static_cast<Node*>(group->getChild(0));
408 group = dynamic_cast<Group*>(optimized.get());
409 if (group && group->getName().empty()
410 && group->getNumChildren() == 1)
411 optimized = static_cast<Node*>(group->getChild(0));
413 const SGReaderWriterXMLOptions* sgopt
414 = dynamic_cast<const SGReaderWriterXMLOptions*>(opt);
415 if (sgopt && sgopt->getInstantiateEffects())
416 optimized = instantiateEffects(optimized.get(), sgopt);
417 return optimized.release();
421 struct ACProcessPolicy {
422 ACProcessPolicy(const string& extension) {}
423 Node* process(Node* node, const string& filename,
424 const ReaderWriter::Options* opt)
430 // XXX Does there need to be a Group node here to trick the
431 // optimizer into optimizing the static transform?
432 osg::Group* root = new Group;
433 MatrixTransform* transform = new MatrixTransform;
434 root->addChild(transform);
436 transform->setDataVariance(Object::STATIC);
437 transform->setMatrix(m);
438 transform->addChild(node);
444 typedef ModelRegistryCallback<ACProcessPolicy, DefaultCachePolicy,
446 OSGSubstitutePolicy, BuildLeafBVHPolicy>
451 ModelRegistryCallbackProxy<ACCallback> g_acRegister("ac");