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 <string.h> // for strcmp()
12 #include <osg/observer_ptr>
13 #include <osg/ref_ptr>
15 #include <osg/NodeCallback>
17 #include <osg/MatrixTransform>
18 #include <osgDB/Archive>
19 #include <osgDB/FileNameUtils>
20 #include <osgDB/FileUtils>
21 #include <osgDB/ReadFile>
22 #include <osgDB/Registry>
23 #include <osgDB/SharedStateManager>
24 #include <osgUtil/Optimizer>
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"
52 // Little helper class that holds an extra reference to a
54 // Since we clone all structural nodes from our 3d models,
55 // the database pager will only see one single reference to
56 // top node of the model and expire it relatively fast.
57 // We attach that extra reference to every model cloned from
58 // a base model in the pager. When that cloned model is deleted
59 // this extra reference is deleted too. So if there are no
60 // cloned models left the model will expire.
61 class SGDatabaseReference : public osg::Observer {
63 SGDatabaseReference(osg::Referenced* referenced) :
64 mReferenced(referenced)
66 virtual void objectDeleted(void*)
71 osg::ref_ptr<osg::Referenced> mReferenced;
75 class SGTextureUpdateVisitor : public SGTextureStateAttributeVisitor {
77 SGTextureUpdateVisitor(const osgDB::FilePathList& pathList) :
80 osg::Texture2D* textureReplace(int unit, osg::StateSet::RefAttributePair& refAttr)
82 osg::Texture2D* texture = dynamic_cast<osg::Texture2D*>(refAttr.first.get());
86 osg::ref_ptr<osg::Image> image = texture->getImage(0);
90 // The currently loaded file name
91 std::string fullFilePath = image->getFileName();
93 std::string fileName = osgDB::getSimpleFileName(fullFilePath);
94 // The name that should be found with the current database path
95 std::string fullLiveryFile = osgDB::findFileInPath(fileName, mPathList);
96 // If they are identical then there is nothing to do
97 if (fullLiveryFile == fullFilePath)
100 image = osgDB::readImageFile(fullLiveryFile);
104 osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_ALL &
105 ~osg::CopyOp::DEEP_COPY_IMAGES);
106 texture = static_cast<osg::Texture2D*>(copyOp(texture));
109 texture->setImage(image.get());
112 virtual void apply(osg::StateSet* stateSet)
117 // get a copy that we can safely modify the statesets values.
118 osg::StateSet::TextureAttributeList attrList;
119 attrList = stateSet->getTextureAttributeList();
120 for (unsigned unit = 0; unit < attrList.size(); ++unit) {
121 osg::StateSet::AttributeList::iterator i;
122 i = attrList[unit].begin();
123 while (i != attrList[unit].end()) {
124 osg::Texture2D* texture = textureReplace(unit, i->second);
126 stateSet->removeTextureAttribute(unit, i->second.first.get());
127 stateSet->setTextureAttribute(unit, texture, i->second.second);
128 stateSet->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::ON);
136 osgDB::FilePathList mPathList;
139 class SGTexCompressionVisitor : public SGTextureStateAttributeVisitor {
141 SGTexCompressionVisitor(osg::Texture::InternalFormatMode formatMode) :
142 mFormatMode(formatMode)
145 virtual void apply(int, osg::StateSet::RefAttributePair& refAttr)
147 osg::Texture2D* texture = dynamic_cast<osg::Texture2D*>(refAttr.first.get());
151 osg::Image* image = texture->getImage(0);
158 int mipmaplevels = 0;
160 mipmaplevels = nMipMaps(s);
162 mipmaplevels = nMipMaps(t);
164 texture->setNumMipmapLevels(mipmaplevels);
166 if (s <= t && 32 <= s) {
167 texture->setInternalFormatMode(mFormatMode);
168 } else if (t < s && 32 <= t) {
169 texture->setInternalFormatMode(mFormatMode);
174 osg::Texture::InternalFormatMode mFormatMode;
177 class SGAcMaterialCrippleVisitor : public SGStateAttributeVisitor {
179 virtual void apply(osg::StateSet::RefAttributePair& refAttr)
181 osg::Material* material = dynamic_cast<osg::Material*>(refAttr.first.get());
184 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
188 class SGReadFileCallback :
189 public osgDB::Registry::ReadFileCallback {
191 virtual osgDB::ReaderWriter::ReadResult
192 readImage(const std::string& fileName, const osgDB::ReaderWriter::Options* opt)
194 std::string absFileName = osgDB::findDataFile(fileName);
195 if (!osgDB::fileExists(absFileName)) {
196 SG_LOG(SG_IO, SG_ALERT, "Cannot find image file \""
197 << fileName << "\"");
198 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
201 osgDB::Registry* registry = osgDB::Registry::instance();
202 osgDB::ReaderWriter::ReadResult res = registry->readImageImplementation(absFileName, opt);
203 if (res.loadedFromCache())
204 SG_LOG(SG_IO, SG_INFO, "Returning cached image \""
205 << res.getImage()->getFileName() << "\"");
207 SG_LOG(SG_IO, SG_INFO, "Reading image \""
208 << res.getImage()->getFileName() << "\"");
213 virtual osgDB::ReaderWriter::ReadResult
214 readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* opt)
216 std::string absFileName = osgDB::findDataFile(fileName);
217 if (!osgDB::fileExists(absFileName)) {
218 SG_LOG(SG_IO, SG_ALERT, "Cannot find model file \""
219 << fileName << "\"");
220 return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
223 osgDB::Registry* registry = osgDB::Registry::instance();
224 osgDB::ReaderWriter::ReadResult res;
225 res = registry->readNodeImplementation(absFileName, opt);
226 if (!res.validNode())
229 if (res.loadedFromCache()) {
230 SG_LOG(SG_IO, SG_INFO, "Returning cached model \""
231 << absFileName << "\"");
233 SG_LOG(SG_IO, SG_INFO, "Reading model \""
234 << absFileName << "\"");
236 if (osgDB::getLowerCaseFileExtension(absFileName) == "ac") {
237 osg::Matrix m(1, 0, 0, 0,
242 osg::ref_ptr<osg::Group> root = new osg::Group;
243 osg::MatrixTransform* transform = new osg::MatrixTransform;
244 root->addChild(transform);
246 transform->setDataVariance(osg::Object::STATIC);
247 transform->setMatrix(m);
248 transform->addChild(res.getNode());
250 res = osgDB::ReaderWriter::ReadResult(0);
252 osgUtil::Optimizer optimizer;
253 unsigned opts = osgUtil::Optimizer::FLATTEN_STATIC_TRANSFORMS;
254 optimizer.optimize(root.get(), opts);
256 // Ok, this step is questionable.
257 // It is there to have the same visual appearance of ac objects for the
258 // first cut. Osg's ac3d loader will correctly set materials from the
259 // ac file. But the old plib loader used GL_AMBIENT_AND_DIFFUSE for the
260 // materials that in effect igored the ambient part specified in the
261 // file. We emulate that for the first cut here by changing all ac models
262 // here. But in the long term we should use the unchanged model and fix
263 // the input files instead ...
264 SGAcMaterialCrippleVisitor matCriple;
265 root->accept(matCriple);
267 res = osgDB::ReaderWriter::ReadResult(root.get());
270 osgUtil::Optimizer optimizer;
272 // Don't use this one. It will break animation names ...
273 // opts |= osgUtil::Optimizer::REMOVE_REDUNDANT_NODES;
275 // opts |= osgUtil::Optimizer::REMOVE_LOADED_PROXY_NODES;
276 // opts |= osgUtil::Optimizer::COMBINE_ADJACENT_LODS;
277 // opts |= osgUtil::Optimizer::SHARE_DUPLICATE_STATE;
278 opts |= osgUtil::Optimizer::MERGE_GEOMETRY;
279 // opts |= osgUtil::Optimizer::CHECK_GEOMETRY;
280 // opts |= osgUtil::Optimizer::SPATIALIZE_GROUPS;
281 // opts |= osgUtil::Optimizer::COPY_SHARED_NODES;
282 opts |= osgUtil::Optimizer::TRISTRIP_GEOMETRY;
283 // opts |= osgUtil::Optimizer::TESSELATE_GEOMETRY;
284 // opts |= osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS;
285 optimizer.optimize(res.getNode(), opts);
288 registry->getSharedStateManager()->share(res.getNode());
290 // OSGFIXME: guard that with a flag
291 // OSGFIXME: in the long term it is unclear if we have an OpenGL context here...
292 osg::Texture::Extensions* e = osg::Texture::getExtensions(0, true);
293 if (e->isTextureCompressionARBSupported()) {
294 SGTexCompressionVisitor texComp(osg::Texture::USE_ARB_COMPRESSION);
295 res.getNode()->accept(texComp);
296 } else if (e->isTextureCompressionS3TCSupported()) {
297 SGTexCompressionVisitor texComp(osg::Texture::USE_S3TC_DXT5_COMPRESSION);
298 res.getNode()->accept(texComp);
302 // Add an extra reference to the model stored in the database.
303 // That it to avoid expiring the object from the cache even if it is still
304 // in use. Note that the object cache will think that a model is unused if the
305 // reference count is 1. If we clone all structural nodes here we need that extra
306 // reference to the original object
307 SGDatabaseReference* databaseReference = new SGDatabaseReference(res.getNode());
308 osg::CopyOp::CopyFlags flags = osg::CopyOp::DEEP_COPY_ALL;
309 flags &= ~osg::CopyOp::DEEP_COPY_TEXTURES;
310 flags &= ~osg::CopyOp::DEEP_COPY_IMAGES;
311 flags &= ~osg::CopyOp::DEEP_COPY_ARRAYS;
312 flags &= ~osg::CopyOp::DEEP_COPY_PRIMITIVES;
313 // This will safe display lists ...
314 flags &= ~osg::CopyOp::DEEP_COPY_DRAWABLES;
315 flags &= ~osg::CopyOp::DEEP_COPY_SHAPES;
316 res = osgDB::ReaderWriter::ReadResult(osg::CopyOp(flags)(res.getNode()));
317 res.getNode()->addObserver(databaseReference);
319 SGTextureUpdateVisitor liveryUpdate(osgDB::getDataFilePathList());
320 res.getNode()->accept(liveryUpdate);
322 // OSGFIXME: don't forget that mutex here
323 registry->getOrCreateSharedStateManager()->share(res.getNode(), 0);
329 class SGReadCallbackInstaller {
331 SGReadCallbackInstaller()
333 osg::Referenced::setThreadSafeReferenceCounting(true);
335 osgDB::Registry* registry = osgDB::Registry::instance();
336 osgDB::ReaderWriter::Options* options = new osgDB::ReaderWriter::Options;
337 options->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_ALL);
338 registry->setOptions(options);
339 registry->getOrCreateSharedStateManager()->setShareMode(osgDB::SharedStateManager::SHARE_TEXTURES);
340 registry->setReadFileCallback(new SGReadFileCallback);
344 static SGReadCallbackInstaller readCallbackInstaller;
347 SGLoadTexture2D(const std::string& path, bool wrapu, bool wrapv,
350 osg::Image* image = osgDB::readImageFile(path);
351 osg::Texture2D* texture = new osg::Texture2D;
352 texture->setImage(image);
354 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
356 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
358 texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
360 texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
366 if (mipmaplevels < 0) {
368 mipmaplevels = nMipMaps(s);
370 mipmaplevels = nMipMaps(t);
373 texture->setNumMipmapLevels(mipmaplevels);
375 // OSGFIXME: guard with a flag
376 if (osg::Texture::getExtensions(0, true)->isTextureCompressionARBSupported()) {
377 if (s <= t && 32 <= s) {
378 texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION);
379 } else if (t < s && 32 <= t) {
380 texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION);
382 } else if (osg::Texture::getExtensions(0, true)->isTextureCompressionS3TCSupported()) {
383 if (s <= t && 32 <= s) {
384 texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT5_COMPRESSION);
385 } else if (t < s && 32 <= t) {
386 texture->setInternalFormatMode(osg::Texture::USE_S3TC_DXT5_COMPRESSION);
393 class SGSwitchUpdateCallback : public osg::NodeCallback {
395 SGSwitchUpdateCallback(SGCondition* condition) :
396 mCondition(condition) {}
397 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
399 assert(dynamic_cast<osg::Switch*>(node));
400 osg::Switch* s = static_cast<osg::Switch*>(node);
402 if (mCondition && mCondition->test()) {
403 s->setAllChildrenOn();
404 // note, callback is responsible for scenegraph traversal so
405 // should always include call traverse(node,nv) to ensure
406 // that the rest of cullbacks and the scene graph are traversed.
409 s->setAllChildrenOff();
413 SGCondition* mCondition;
417 * Locate a named node in a branch.
419 class NodeFinder : public osg::NodeVisitor {
421 NodeFinder(const std::string& nameToFind) :
422 osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
423 osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
427 virtual void apply(osg::Node& node)
431 if (mName == node.getName()) {
438 osg::Node* getNode() const
446 * Splice a branch in between all child nodes and their parents.
449 splice_branch(osg::Group* group, osg::Node* child)
451 osg::Node::ParentList parents = child->getParents();
452 group->addChild(child);
453 osg::Node::ParentList::iterator i;
454 for (i = parents.begin(); i != parents.end(); ++i)
455 (*i)->replaceChild(child, group);
459 sgMakeAnimation( osg::Node * model,
461 vector<SGPropertyNode_ptr> &name_nodes,
462 SGPropertyNode *prop_root,
463 SGPropertyNode_ptr node,
465 SGPath &texture_path,
466 set<osg::Node*> &ignore_branches )
469 SGAnimation * animation = 0;
470 const char * type = node->getStringValue("type", "none");
471 if (!strcmp("none", type)) {
472 animation = new SGNullAnimation(node);
473 } else if (!strcmp("range", type)) {
474 animation = new SGRangeAnimation(prop_root, node);
475 } else if (!strcmp("billboard", type)) {
476 animation = new SGBillboardAnimation(node);
477 } else if (!strcmp("select", type)) {
478 animation = new SGSelectAnimation(prop_root, node);
479 } else if (!strcmp("spin", type)) {
480 animation = new SGSpinAnimation(prop_root, node, sim_time_sec );
481 } else if (!strcmp("timed", type)) {
482 animation = new SGTimedAnimation(node);
483 } else if (!strcmp("rotate", type)) {
484 animation = new SGRotateAnimation(prop_root, node);
485 } else if (!strcmp("translate", type)) {
486 animation = new SGTranslateAnimation(prop_root, node);
487 } else if (!strcmp("scale", type)) {
488 animation = new SGScaleAnimation(prop_root, node);
489 } else if (!strcmp("texrotate", type)) {
490 animation = new SGTexRotateAnimation(prop_root, node);
491 } else if (!strcmp("textranslate", type)) {
492 animation = new SGTexTranslateAnimation(prop_root, node);
493 } else if (!strcmp("texmultiple", type)) {
494 animation = new SGTexMultipleAnimation(prop_root, node);
495 } else if (!strcmp("blend", type)) {
496 animation = new SGBlendAnimation(prop_root, node);
498 } else if (!strcmp("alpha-test", type)) {
499 animation = new SGAlphaTestAnimation(node);
500 } else if (!strcmp("material", type)) {
501 animation = new SGMaterialAnimation(prop_root, node, texture_path);
502 } else if (!strcmp("flash", type)) {
503 animation = new SGFlashAnimation(node);
504 } else if (!strcmp("dist-scale", type)) {
505 animation = new SGDistScaleAnimation(node);
506 } else if (!strcmp("noshadow", type)) {
507 animation = new SGShadowAnimation(prop_root, node);
508 } else if (!strcmp("shader", type)) {
509 animation = new SGShaderAnimation(prop_root, node);
511 animation = new SGNullAnimation(node);
512 SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
516 animation->setName(name);
518 osg::Node * object = 0;
519 if (!name_nodes.empty()) {
520 const char * name = name_nodes[0]->getStringValue();
521 NodeFinder nodeFinder(name);
522 model->accept(nodeFinder);
523 object = nodeFinder.getNode();
525 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
533 if ( animation == 0 )
536 osg::Group* branch = animation->getBranch();
537 splice_branch(branch, object);
539 for (unsigned int i = 1; i < name_nodes.size(); i++) {
540 const char * name = name_nodes[i]->getStringValue();
541 NodeFinder nodeFinder(name);
542 model->accept(nodeFinder);
543 object = nodeFinder.getNode();
545 SG_LOG(SG_INPUT, SG_ALERT, "Object " << name << " not found");
549 osg::Group* oldParent = object->getParent(0);
550 branch->addChild(object);
551 oldParent->removeChild(object);
555 if ( animation != 0 ) {
557 branch->setUpdateCallback(animation);
559 ignore_branches.insert( branch );
565 ////////////////////////////////////////////////////////////////////////
567 ////////////////////////////////////////////////////////////////////////
570 sgLoad3DModel( const string &fg_root, const string &path,
571 SGPropertyNode *prop_root,
572 double sim_time_sec, osg::Node *(*load_panel)(SGPropertyNode *),
574 const SGPath& externalTexturePath )
576 osg::Switch* model = 0;
577 SGPropertyNode props;
579 // Load the 3D aircraft object itself
580 SGPath modelpath = path, texturepath = path;
581 if ( !ulIsAbsolutePathName( path.c_str() ) ) {
582 SGPath tmp = fg_root;
583 tmp.append(modelpath.str());
584 modelpath = texturepath = tmp;
587 // Check for an XML wrapper
588 if (modelpath.str().substr(modelpath.str().size() - 4, 4) == ".xml") {
589 readProperties(modelpath.str(), &props);
590 if (props.hasValue("/path")) {
591 modelpath = modelpath.dir();
592 modelpath.append(props.getStringValue("/path"));
593 if (props.hasValue("/texture-path")) {
594 texturepath = texturepath.dir();
595 texturepath.append(props.getStringValue("/texture-path"));
599 model = new osg::Switch;
603 osgDB::FilePathList pathList = osgDB::getDataFilePathList();
604 osgDB::Registry::instance()->initFilePathLists();
606 // Assume that textures are in
607 // the same location as the XML file.
609 if (texturepath.extension() != "")
610 texturepath = texturepath.dir();
612 osgDB::Registry::instance()->getDataFilePathList().push_front(texturepath.str());
614 osg::Node* node = osgDB::readNodeFile(modelpath.str());
616 throw sg_io_exception("Failed to load 3D model",
617 sg_location(modelpath.str()));
618 model = new osg::Switch;
619 model->addChild(node, true);
622 osgDB::Registry::instance()->getDataFilePathList().push_front(externalTexturePath.str());
624 // Set up the alignment node
625 osg::MatrixTransform* alignmainmodel = new osg::MatrixTransform;
626 alignmainmodel->addChild(model);
627 osg::Matrix res_matrix;
628 res_matrix.makeRotate(
629 props.getFloatValue("/offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
631 props.getFloatValue("/offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
633 props.getFloatValue("/offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
637 tmat.makeTranslate(props.getFloatValue("/offsets/x-m", 0.0),
638 props.getFloatValue("/offsets/y-m", 0.0),
639 props.getFloatValue("/offsets/z-m", 0.0));
640 alignmainmodel->setMatrix(res_matrix*tmat);
645 vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
646 for (i = 0; i < model_nodes.size(); i++) {
647 SGPropertyNode_ptr node = model_nodes[i];
648 osg::MatrixTransform* align = new osg::MatrixTransform;
649 res_matrix.makeIdentity();
650 res_matrix.makeRotate(
651 node->getFloatValue("offsets/heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
653 node->getFloatValue("offsets/roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
655 node->getFloatValue("offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
659 tmat.makeTranslate(node->getFloatValue("offsets/x-m", 0.0),
660 node->getFloatValue("offsets/y-m", 0.0),
661 node->getFloatValue("offsets/z-m", 0.0));
662 align->setMatrix(res_matrix*tmat);
665 const char * submodel = node->getStringValue("path");
667 kid = sgLoad3DModel( fg_root, submodel, prop_root, sim_time_sec, load_panel );
669 } catch (const sg_throwable &t) {
670 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
673 align->addChild(kid);
675 align->setName(node->getStringValue("name", ""));
676 model->addChild(align);
678 SGPropertyNode *cond = node->getNode("condition", false);
680 model->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
683 // restore old path list
684 osgDB::setDataFilePathList(pathList);
688 vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
689 for (i = 0; i < panel_nodes.size(); i++) {
690 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
691 osg::Node * panel = load_panel(panel_nodes[i]);
692 if (panel_nodes[i]->hasValue("name"))
693 panel->setName((char *)panel_nodes[i]->getStringValue("name"));
694 model->addChild(panel);
699 alignmainmodel->setUserData(data);
700 data->modelLoaded(path, &props, alignmainmodel);
703 set<osg::Node*> ignore_branches;
704 vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
705 for (i = 0; i < animation_nodes.size(); i++) {
706 const char * name = animation_nodes[i]->getStringValue("name", 0);
707 vector<SGPropertyNode_ptr> name_nodes =
708 animation_nodes[i]->getChildren("object-name");
709 sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
710 sim_time_sec, texturepath, ignore_branches);
713 return alignmainmodel;