1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
7 #include <simgear_config.h>
10 #include <osg/observer_ptr>
11 #include <osg/ref_ptr>
13 #include <osg/NodeCallback>
15 #include <osg/MatrixTransform>
16 #include <osgDB/Archive>
17 #include <osgDB/FileNameUtils>
18 #include <osgDB/FileUtils>
19 #include <osgDB/ReadFile>
20 #include <osgDB/WriteFile>
21 #include <osgDB/Registry>
22 #include <osgDB/SharedStateManager>
23 #include <osgUtil/Optimizer>
25 #include <simgear/scene/util/SGSceneFeatures.hxx>
26 #include <simgear/scene/util/SGStateAttributeVisitor.hxx>
27 #include <simgear/scene/util/SGTextureStateAttributeVisitor.hxx>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/props/props.hxx>
31 #include <simgear/props/props_io.hxx>
32 #include <simgear/props/condition.hxx>
34 #include "animation.hxx"
40 // Little helper class that holds an extra reference to a
42 // Since we clone all structural nodes from our 3d models,
43 // the database pager will only see one single reference to
44 // top node of the model and expire it relatively fast.
45 // We attach that extra reference to every model cloned from
46 // a base model in the pager. When that cloned model is deleted
47 // this extra reference is deleted too. So if there are no
48 // cloned models left the model will expire.
49 class SGDatabaseReference : public osg::Observer {
51 SGDatabaseReference(osg::Referenced* referenced) :
52 mReferenced(referenced)
54 virtual void objectDeleted(void*)
59 osg::ref_ptr<osg::Referenced> mReferenced;
63 class SGTextureUpdateVisitor : public SGTextureStateAttributeVisitor {
65 SGTextureUpdateVisitor(const osgDB::FilePathList& pathList) :
68 osg::Texture2D* textureReplace(int unit,
69 osg::StateSet::RefAttributePair& refAttr)
71 osg::Texture2D* texture;
72 texture = dynamic_cast<osg::Texture2D*>(refAttr.first.get());
76 osg::ref_ptr<osg::Image> image = texture->getImage(0);
80 // The currently loaded file name
81 std::string fullFilePath = image->getFileName();
83 std::string fileName = osgDB::getSimpleFileName(fullFilePath);
84 // The name that should be found with the current database path
85 std::string fullLiveryFile = osgDB::findFileInPath(fileName, mPathList);
86 // If they are identical then there is nothing to do
87 if (fullLiveryFile == fullFilePath)
90 image = osgDB::readImageFile(fullLiveryFile);
94 osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_ALL &
95 ~osg::CopyOp::DEEP_COPY_IMAGES);
96 texture = static_cast<osg::Texture2D*>(copyOp(texture));
99 texture->setImage(image.get());
102 virtual void apply(osg::StateSet* stateSet)
107 // get a copy that we can safely modify the statesets values.
108 osg::StateSet::TextureAttributeList attrList;
109 attrList = stateSet->getTextureAttributeList();
110 for (unsigned unit = 0; unit < attrList.size(); ++unit) {
111 osg::StateSet::AttributeList::iterator i;
112 i = attrList[unit].begin();
113 while (i != attrList[unit].end()) {
114 osg::Texture2D* texture = textureReplace(unit, i->second);
116 stateSet->removeTextureAttribute(unit, i->second.first.get());
117 stateSet->setTextureAttribute(unit, texture, i->second.second);
118 stateSet->setTextureMode(unit, GL_TEXTURE_2D,
119 osg::StateAttribute::ON);
127 osgDB::FilePathList mPathList;
130 class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor {
132 virtual void apply(int, osg::StateSet::RefAttributePair& refAttr)
134 osg::Texture2D* texture;
135 texture = dynamic_cast<osg::Texture2D*>(refAttr.first.get());
140 texture->setDataVariance(osg::Object::STATIC);
142 osg::Image* image = texture->getImage(0);
149 if (s <= t && 32 <= s) {
150 SGSceneFeatures::instance()->setTextureCompression(texture);
151 } else if (t < s && 32 <= t) {
152 SGSceneFeatures::instance()->setTextureCompression(texture);
157 class SGTexDataVarianceVisitor : public SGTextureStateAttributeVisitor {
159 virtual void apply(int, osg::StateSet::RefAttributePair& refAttr)
161 osg::Texture* texture;
162 texture = dynamic_cast<osg::Texture*>(refAttr.first.get());
166 texture->setDataVariance(osg::Object::STATIC);
170 class SGAcMaterialCrippleVisitor : public SGStateAttributeVisitor {
172 virtual void apply(osg::StateSet::RefAttributePair& refAttr)
174 osg::Material* material;
175 material = dynamic_cast<osg::Material*>(refAttr.first.get());
178 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
182 class SGReadFileCallback :
183 public osgDB::Registry::ReadFileCallback {
185 virtual osgDB::ReaderWriter::ReadResult
186 readImage(const std::string& fileName,
187 const osgDB::ReaderWriter::Options* opt)
189 std::string absFileName = osgDB::findDataFile(fileName);
190 if (!osgDB::fileExists(absFileName)) {
191 SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
192 << fileName << "\"");
193 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
196 osgDB::Registry* registry = osgDB::Registry::instance();
197 osgDB::ReaderWriter::ReadResult res;
198 res = registry->readImageImplementation(absFileName, opt);
199 if (res.loadedFromCache())
200 SG_LOG(SG_IO, SG_INFO, "Returning cached image \""
201 << res.getImage()->getFileName() << "\"");
203 SG_LOG(SG_IO, SG_INFO, "Reading image \""
204 << res.getImage()->getFileName() << "\"");
209 virtual osgDB::ReaderWriter::ReadResult
210 readNode(const std::string& fileName,
211 const osgDB::ReaderWriter::Options* opt)
213 osgDB::Registry* registry = osgDB::Registry::instance();
214 osgDB::ReaderWriter::ReadResult res;
215 osg::Node* cached = 0;
216 // The BTG loader automatically looks for ".btg.gz" if a file with
217 // the .btg extension doesn't exist. Also, we don't want to add
218 // nodes, run the optimizer, etc. on the btg model.So, let it do
220 if (osgDB::equalCaseInsensitive(osgDB::getFileExtension(fileName), "btg")) {
221 return registry->readNodeImplementation(fileName, opt);
223 // First, look for a file with the same name, and the extension
224 // ".osg" and, if it exists, load it instead. This allows for
225 // substitution of optimized models for ones named in the scenery.
226 bool optimizeModel = true;
227 std::string fileSansExtension = osgDB::getNameLessExtension(fileName);
228 std::string osgFileName = fileSansExtension + ".osg";
229 std::string absFileName = osgDB::findDataFile(osgFileName);
230 if (osgDB::fileExists(absFileName)) {
231 optimizeModel = false;
233 absFileName = osgDB::findDataFile(fileName);
235 if (!osgDB::fileExists(absFileName)) {
236 SG_LOG(SG_IO, SG_ALERT, "Cannot find model file \""
237 << fileName << "\"");
238 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
241 = dynamic_cast<osg::Node*>(registry->getFromObjectCache(absFileName));
243 SG_LOG(SG_IO, SG_INFO, "Got cached model \""
244 << absFileName << "\"");
246 SG_LOG(SG_IO, SG_INFO, "Reading model \""
247 << absFileName << "\"");
248 res = registry->readNodeImplementation(absFileName, opt);
249 if (!res.validNode())
252 bool needTristrip = true;
253 if (osgDB::getLowerCaseFileExtension(fileName) == "ac") {
254 // we get optimal geometry from the loader.
255 needTristrip = false;
256 osg::Matrix m(1, 0, 0, 0,
261 osg::ref_ptr<osg::Group> root = new osg::Group;
262 osg::MatrixTransform* transform = new osg::MatrixTransform;
263 root->addChild(transform);
265 transform->setDataVariance(osg::Object::STATIC);
266 transform->setMatrix(m);
267 transform->addChild(res.getNode());
269 res = osgDB::ReaderWriter::ReadResult(0);
272 osgUtil::Optimizer optimizer;
273 unsigned opts = osgUtil::Optimizer::FLATTEN_STATIC_TRANSFORMS;
274 optimizer.optimize(root.get(), opts);
277 // strip away unneeded groups
278 if (root->getNumChildren() == 1 && root->getName().empty()) {
279 res = osgDB::ReaderWriter::ReadResult(root->getChild(0));
281 res = osgDB::ReaderWriter::ReadResult(root.get());
283 // Ok, this step is questionable.
284 // It is there to have the same visual appearance of ac objects for the
285 // first cut. Osg's ac3d loader will correctly set materials from the
286 // ac file. But the old plib loader used GL_AMBIENT_AND_DIFFUSE for the
287 // materials that in effect igored the ambient part specified in the
288 // file. We emulate that for the first cut here by changing all
289 // ac models here. But in the long term we should use the
290 // unchanged model and fix the input files instead ...
291 SGAcMaterialCrippleVisitor matCriple;
292 res.getNode()->accept(matCriple);
296 osgUtil::Optimizer optimizer;
298 // Don't use this one. It will break animation names ...
299 // opts |= osgUtil::Optimizer::REMOVE_REDUNDANT_NODES;
301 // opts |= osgUtil::Optimizer::REMOVE_LOADED_PROXY_NODES;
302 // opts |= osgUtil::Optimizer::COMBINE_ADJACENT_LODS;
303 // opts |= osgUtil::Optimizer::SHARE_DUPLICATE_STATE;
304 opts |= osgUtil::Optimizer::MERGE_GEOMETRY;
305 // opts |= osgUtil::Optimizer::CHECK_GEOMETRY;
306 // opts |= osgUtil::Optimizer::SPATIALIZE_GROUPS;
307 // opts |= osgUtil::Optimizer::COPY_SHARED_NODES;
308 opts |= osgUtil::Optimizer::FLATTEN_STATIC_TRANSFORMS;
310 opts |= osgUtil::Optimizer::TRISTRIP_GEOMETRY;
311 // opts |= osgUtil::Optimizer::TESSELATE_GEOMETRY;
312 // opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS;
313 optimizer.optimize(res.getNode(), opts);
315 // Make sure the data variance of sharable objects is set to STATIC ...
316 SGTexDataVarianceVisitor dataVarianceVisitor;
317 res.getNode()->accept(dataVarianceVisitor);
318 // ... so that textures are now globally shared
319 registry->getSharedStateManager()->share(res.getNode());
321 SGTexCompressionVisitor texComp;
322 res.getNode()->accept(texComp);
323 cached = res.getNode();
324 registry->addEntryToObjectCache(absFileName, cached);
326 // Add an extra reference to the model stored in the database.
327 // That it to avoid expiring the object from the cache even if it is still
328 // in use. Note that the object cache will think that a model is unused
329 // if the reference count is 1. If we clone all structural nodes here
330 // we need that extra reference to the original object
331 SGDatabaseReference* databaseReference;
332 databaseReference = new SGDatabaseReference(cached);
333 osg::CopyOp::CopyFlags flags = osg::CopyOp::DEEP_COPY_ALL;
334 flags &= ~osg::CopyOp::DEEP_COPY_TEXTURES;
335 flags &= ~osg::CopyOp::DEEP_COPY_IMAGES;
336 flags &= ~osg::CopyOp::DEEP_COPY_ARRAYS;
337 flags &= ~osg::CopyOp::DEEP_COPY_PRIMITIVES;
338 // This will safe display lists ...
339 flags &= ~osg::CopyOp::DEEP_COPY_DRAWABLES;
340 flags &= ~osg::CopyOp::DEEP_COPY_SHAPES;
341 res = osgDB::ReaderWriter::ReadResult(osg::CopyOp(flags)(cached));
342 res.getNode()->addObserver(databaseReference);
345 SGTextureUpdateVisitor liveryUpdate(osgDB::getDataFilePathList());
346 res.getNode()->accept(liveryUpdate);
348 // Make sure the data variance of sharable objects is set to STATIC ...
349 SGTexDataVarianceVisitor dataVarianceVisitor;
350 res.getNode()->accept(dataVarianceVisitor);
351 // ... so that textures are now globally shared
352 registry->getOrCreateSharedStateManager()->share(res.getNode(), 0);
358 class SGReadCallbackInstaller {
360 SGReadCallbackInstaller()
362 osg::Referenced::setThreadSafeReferenceCounting(true);
364 osgDB::Registry* registry = osgDB::Registry::instance();
365 osgDB::ReaderWriter::Options* options = new osgDB::ReaderWriter::Options;
366 // We manage node caching ourselves
367 int cacheOptions = osgDB::ReaderWriter::Options::CACHE_ALL
368 & ~osgDB::ReaderWriter::Options::CACHE_NODES;
370 setObjectCacheHint((osgDB::ReaderWriter::Options::CacheHintOptions)cacheOptions);
371 registry->setOptions(options);
372 registry->getOrCreateSharedStateManager()->setShareMode(osgDB::SharedStateManager::SHARE_TEXTURES);
373 registry->setReadFileCallback(new SGReadFileCallback);
377 static SGReadCallbackInstaller readCallbackInstaller;
380 SGLoadTexture2D(const std::string& path, bool wrapu, bool wrapv, int)
382 osg::Image* image = osgDB::readImageFile(path);
383 osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
384 texture->setImage(image);
385 texture->setDataVariance(osg::Object::STATIC);
387 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
389 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
391 texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
393 texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
399 if (s <= t && 32 <= s) {
400 SGSceneFeatures::instance()->setTextureCompression(texture.get());
401 } else if (t < s && 32 <= t) {
402 SGSceneFeatures::instance()->setTextureCompression(texture.get());
406 // Make sure the texture is shared if we already have the same texture
409 osg::ref_ptr<osg::Node> tmpNode = new osg::Node;
410 osg::StateSet* stateSet = tmpNode->getOrCreateStateSet();
411 stateSet->setTextureAttribute(0, texture.get());
413 // OSGFIXME: don't forget that mutex here
414 osgDB::Registry* registry = osgDB::Registry::instance();
415 registry->getSharedStateManager()->share(tmpNode.get(), 0);
417 // should be the same, but be paranoid ...
418 stateSet = tmpNode->getStateSet();
419 osg::StateAttribute* stateAttr;
420 stateAttr = stateSet->getTextureAttribute(0, osg::StateAttribute::TEXTURE);
421 osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(stateAttr);
426 return texture.release();
429 class SGSwitchUpdateCallback : public osg::NodeCallback {
431 SGSwitchUpdateCallback(SGCondition* condition) :
432 mCondition(condition) {}
433 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
435 assert(dynamic_cast<osg::Switch*>(node));
436 osg::Switch* s = static_cast<osg::Switch*>(node);
438 if (mCondition && mCondition->test()) {
439 s->setAllChildrenOn();
440 // note, callback is responsible for scenegraph traversal so
441 // should always include call traverse(node,nv) to ensure
442 // that the rest of cullbacks and the scene graph are traversed.
445 s->setAllChildrenOff();
449 SGSharedPtr<SGCondition> mCondition;
453 ////////////////////////////////////////////////////////////////////////
455 ////////////////////////////////////////////////////////////////////////
458 sgLoad3DModel( const string &fg_root, const string &path,
459 SGPropertyNode *prop_root,
460 double sim_time_sec, osg::Node *(*load_panel)(SGPropertyNode *),
462 const SGPath& externalTexturePath )
464 osg::ref_ptr<osg::Node> model;
465 SGPropertyNode props;
467 // Load the 3D aircraft object itself
468 SGPath modelpath = path, texturepath = path;
469 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
470 SGPath tmp = fg_root;
471 tmp.append(modelpath.str());
472 modelpath = texturepath = tmp;
475 // Check for an XML wrapper
476 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
477 readProperties(modelpath.str(), &props);
478 if (props.hasValue("/path")) {
479 modelpath = modelpath.dir();
480 modelpath.append(props.getStringValue("/path"));
481 if (props.hasValue("/texture-path")) {
482 texturepath = texturepath.dir();
483 texturepath.append(props.getStringValue("/texture-path"));
487 model = new osg::Switch;
491 osgDB::FilePathList pathList = osgDB::getDataFilePathList();
492 osgDB::Registry::instance()->initFilePathLists();
494 // Assume that textures are in
495 // the same location as the XML file.
497 if (texturepath.extension() != "")
498 texturepath = texturepath.dir();
500 osgDB::Registry::instance()->getDataFilePathList().push_front(texturepath.str());
502 model = osgDB::readNodeFile(modelpath.str());
504 throw sg_io_exception("Failed to load 3D model",
505 sg_location(modelpath.str()));
508 osgDB::Registry::instance()->getDataFilePathList().push_front(externalTexturePath.str());
510 // Set up the alignment node
511 osg::ref_ptr<osg::MatrixTransform> alignmainmodel = new osg::MatrixTransform;
512 alignmainmodel->addChild(model.get());
513 osg::Matrix res_matrix;
514 res_matrix.makeRotate(
515 props.getFloatValue("/offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
517 props.getFloatValue("/offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
519 props.getFloatValue("/offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
523 tmat.makeTranslate(props.getFloatValue("/offsets/x-m", 0.0),
524 props.getFloatValue("/offsets/y-m", 0.0),
525 props.getFloatValue("/offsets/z-m", 0.0));
526 alignmainmodel->setMatrix(res_matrix*tmat);
529 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
530 for (unsigned i = 0; i < model_nodes.size(); i++) {
531 SGPropertyNode_ptr node = model_nodes[i];
532 osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
533 res_matrix.makeIdentity();
534 res_matrix.makeRotate(
535 node->getDoubleValue("offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
537 node->getDoubleValue("offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
539 node->getDoubleValue("offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
543 tmat.makeTranslate(node->getDoubleValue("offsets/x-m", 0),
544 node->getDoubleValue("offsets/y-m", 0),
545 node->getDoubleValue("offsets/z-m", 0));
546 align->setMatrix(res_matrix*tmat);
548 osg::ref_ptr<osg::Node> kid;
549 const char* submodel = node->getStringValue("path");
551 kid = sgLoad3DModel( fg_root, submodel, prop_root, sim_time_sec, load_panel );
553 } catch (const sg_throwable &t) {
554 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
557 align->addChild(kid.get());
559 align->setName(node->getStringValue("name", ""));
561 SGPropertyNode *cond = node->getNode("condition", false);
563 osg::ref_ptr<osg::Switch> sw = new osg::Switch;
564 sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
565 alignmainmodel->addChild(sw.get());
566 sw->addChild(align.get());
567 sw->setName("submodel condition switch");
569 alignmainmodel->addChild(align.get());
575 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
576 for (unsigned i = 0; i < panel_nodes.size(); i++) {
577 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
578 osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
579 if (panel_nodes[i]->hasValue("name"))
580 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
581 alignmainmodel->addChild(panel.get());
586 alignmainmodel->setUserData(data);
587 data->modelLoaded(path, &props, alignmainmodel.get());
590 std::vector<SGPropertyNode_ptr> animation_nodes;
591 animation_nodes = props.getChildren("animation");
592 for (unsigned i = 0; i < animation_nodes.size(); ++i)
593 /// OSGFIXME: duh, why not only model?????
594 SGAnimation::animate(alignmainmodel.get(), animation_nodes[i], prop_root);
596 // restore old path list
597 osgDB::setDataFilePathList(pathList);
599 if (props.hasChild("debug-outfile")) {
600 std::string outputfile = props.getStringValue("debug-outfile",
602 osgDB::writeNodeFile(*alignmainmodel, outputfile);
605 return alignmainmodel.release();