1 // Copyright (C) 2007 Tim Moore timoore@redhat.com
2 // Copyright (C) 2008 Till Busch buti@bux.at
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # include <simgear_config.h>
28 #include <boost/bind.hpp>
31 #include <osg/MatrixTransform>
32 #include <osgDB/ReadFile>
33 #include <osgDB/WriteFile>
34 #include <osgDB/Registry>
36 #include <osgDB/FileNameUtils>
38 #include <simgear/compiler.h>
39 #include <simgear/structure/exception.hxx>
40 #include <simgear/props/props.hxx>
41 #include <simgear/props/props_io.hxx>
42 #include <simgear/props/condition.hxx>
43 #include <simgear/scene/util/SGNodeMasks.hxx>
44 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
46 #include "modellib.hxx"
47 #include "SGReaderWriterXML.hxx"
49 #include "animation.hxx"
50 #include "particles.hxx"
53 #include "SGMaterialAnimation.hxx"
56 using namespace simgear;
60 sgLoad3DModel_internal(const SGPath& path,
61 const osgDB::Options* options,
62 SGPropertyNode *overlay = 0);
65 SGReaderWriterXML::SGReaderWriterXML()
67 supportsExtension("xml", "SimGear xml database format");
70 SGReaderWriterXML::~SGReaderWriterXML()
74 const char* SGReaderWriterXML::className() const
76 return "XML database reader";
79 osgDB::ReaderWriter::ReadResult
80 SGReaderWriterXML::readNode(const std::string& name,
81 const osgDB::Options* options) const
83 std::string fileName = osgDB::findDataFile(name, options);
87 SGPath p = SGModelLib::findDataFile(fileName);
89 return ReadResult::FILE_NOT_FOUND;
92 result=sgLoad3DModel_internal(p, options);
93 } catch (const sg_exception &t) {
94 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage()
95 << "\n\tfrom:" << fileName);
101 return ReadResult::FILE_NOT_HANDLED;
104 class SGSwitchUpdateCallback : public osg::NodeCallback
107 SGSwitchUpdateCallback(SGCondition* condition) :
108 mCondition(condition) {}
109 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
110 assert(dynamic_cast<osg::Switch*>(node));
111 osg::Switch* s = static_cast<osg::Switch*>(node);
113 if (mCondition && mCondition->test()) {
114 s->setAllChildrenOn();
115 // note, callback is responsible for scenegraph traversal so
116 // should always include call traverse(node,nv) to ensure
117 // that the rest of cullbacks and the scene graph are traversed.
120 s->setAllChildrenOff();
124 SGSharedPtr<SGCondition> mCondition;
128 // Little helper class that holds an extra reference to a
130 // Since we clone all structural nodes from our 3d models,
131 // the database pager will only see one single reference to
132 // top node of the model and expire it relatively fast.
133 // We attach that extra reference to every model cloned from
134 // a base model in the pager. When that cloned model is deleted
135 // this extra reference is deleted too. So if there are no
136 // cloned models left the model will expire.
138 class SGDatabaseReference : public osg::Observer
141 SGDatabaseReference(osg::Referenced* referenced) :
142 mReferenced(referenced)
144 virtual void objectDeleted(void*)
149 osg::ref_ptr<osg::Referenced> mReferenced;
152 void makeEffectAnimations(PropertyList& animation_nodes,
153 PropertyList& effect_nodes)
155 for (PropertyList::iterator itr = animation_nodes.begin();
156 itr != animation_nodes.end();
158 SGPropertyNode_ptr effectProp;
159 SGPropertyNode* animProp = itr->ptr();
160 SGPropertyNode* typeProp = animProp->getChild("type");
164 const char* typeString = typeProp->getStringValue();
165 if (!strcmp(typeString, "material")) {
167 = SGMaterialAnimation::makeEffectProperties(animProp);
168 } else if (!strcmp(typeString, "shader")) {
170 SGPropertyNode* shaderProp = animProp->getChild("shader");
171 if (!shaderProp || strcmp(shaderProp->getStringValue(), "chrome"))
173 *itr = 0; // effect replaces animation
174 SGPropertyNode* textureProp = animProp->getChild("texture");
177 effectProp = new SGPropertyNode();
178 makeChild(effectProp.ptr(), "inherits-from")
179 ->setValue("Effects/chrome");
180 SGPropertyNode* paramsProp = makeChild(effectProp.get(), "parameters");
181 makeChild(paramsProp, "chrome-texture")
182 ->setValue(textureProp->getStringValue());
184 if (effectProp.valid()) {
185 PropertyList objectNameNodes = animProp->getChildren("object-name");
186 for (PropertyList::iterator objItr = objectNameNodes.begin(),
187 end = objectNameNodes.end();
190 effectProp->addChild("object-name")
191 ->setStringValue((*objItr)->getStringValue());
192 effect_nodes.push_back(effectProp);
196 animation_nodes.erase(remove_if(animation_nodes.begin(),
197 animation_nodes.end(),
198 !boost::bind(&SGPropertyNode_ptr::valid,
200 animation_nodes.end());
205 class SetNodeMaskVisitor : public osg::NodeVisitor {
207 SetNodeMaskVisitor(osg::Node::NodeMask nms, osg::Node::NodeMask nmc) :
208 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), nodeMaskSet(nms), nodeMaskClear(nmc)
210 virtual void apply(osg::Geode& node) {
211 node.setNodeMask((node.getNodeMask() | nodeMaskSet) & ~nodeMaskClear);
215 osg::Node::NodeMask nodeMaskSet;
216 osg::Node::NodeMask nodeMaskClear;
222 class ExcludeInPreview
225 bool operator()(SGPropertyNode* aNode) const
227 string typeString(aNode->getStringValue("type"));
228 // exclude these so we don't show yellow outlines in preview mode
229 return (typeString == "pick") || (typeString == "knob");
233 bool removeNamedNode(osg::Group* aGroup, const std::string& aName)
235 int nKids = aGroup->getNumChildren();
236 for (int i = 0; i < nKids; i++) {
237 osg::Node* child = aGroup->getChild(i);
238 if (child->getName() == aName) {
239 aGroup->removeChild(child);
244 for (int i = 0; i < nKids; i++) {
245 osg::Group* childGroup = aGroup->getChild(i)->asGroup();
249 if (removeNamedNode(childGroup, aName))
251 } // of child groups traversal
258 sgLoad3DModel_internal(const SGPath& path,
259 const osgDB::Options* dbOptions,
260 SGPropertyNode *overlay)
262 if (!path.exists()) {
263 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path.str() << "\"");
267 osg::ref_ptr<SGReaderWriterOptions> options;
268 options = SGReaderWriterOptions::copyOrCreate(dbOptions);
270 SGPath modelpath(path);
271 SGPath texturepath(path);
272 SGPath modelDir(modelpath.dir());
274 SGSharedPtr<SGPropertyNode> prop_root = options->getPropertyNode();
275 if (!prop_root.valid())
276 prop_root = new SGPropertyNode;
277 // The model data appear to be only used in the topmost model
278 osg::ref_ptr<SGModelData> data = options->getModelData();
279 options->setModelData(0);
281 osg::ref_ptr<osg::Node> model;
282 osg::ref_ptr<osg::Group> group;
283 SGPropertyNode_ptr props = new SGPropertyNode;
284 bool previewMode = (dbOptions->getPluginStringData("SimGear::PREVIEW") == "ON");
286 // Check for an XML wrapper
287 if (modelpath.extension() == "xml") {
289 readProperties(modelpath.str(), props);
290 } catch (const sg_exception &t) {
291 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
292 << t.getFormattedMessage());
296 copyProperties(overlay, props);
298 if (previewMode && props->hasChild("nopreview")) {
302 if (props->hasValue("/path")) {
303 string modelPathStr = props->getStringValue("/path");
304 modelpath = SGModelLib::findDataFile(modelPathStr, NULL, modelDir);
305 if (modelpath.isNull())
306 throw sg_io_exception("Model file not found: '" + modelPathStr + "'",
309 if (props->hasValue("/texture-path")) {
310 string texturePathStr = props->getStringValue("/texture-path");
311 if (!texturePathStr.empty())
313 texturepath = SGModelLib::findDataFile(texturePathStr, NULL, modelDir);
314 if (texturepath.isNull())
315 throw sg_io_exception("Texture file not found: '" + texturePathStr + "'",
320 model = new osg::Node;
323 SGPropertyNode *mp = props->getNode("multiplay");
324 if (mp && prop_root && prop_root->getParent())
325 copyProperties(mp, prop_root);
327 // model without wrapper
330 // Assume that textures are in
331 // the same location as the XML file.
333 if (!texturepath.extension().empty())
334 texturepath = texturepath.dir();
336 options->setDatabasePath(texturepath.str());
337 osgDB::ReaderWriter::ReadResult modelResult;
338 modelResult = osgDB::readNodeFile(modelpath.str(), options.get());
339 if (!modelResult.validNode())
340 throw sg_io_exception("Failed to load 3D model:" + modelResult.message(),
342 model = copyModel(modelResult.getNode());
343 // Add an extra reference to the model stored in the database.
344 // That is to avoid expiring the object from the cache even if
345 // it is still in use. Note that the object cache will think
346 // that a model is unused if the reference count is 1. If we
347 // clone all structural nodes here we need that extra
348 // reference to the original object
349 SGDatabaseReference* databaseReference;
350 databaseReference = new SGDatabaseReference(modelResult.getNode());
351 model->addObserver(databaseReference);
354 TextureUpdateVisitor liveryUpdate(options->getDatabasePathList());
355 model->accept(liveryUpdate);
357 // Copy the userdata fields, still sharing the boundingvolumes,
358 // but introducing new data for velocities.
359 UserDataCopyVisitor userDataCopyVisitor;
360 model->accept(userDataCopyVisitor);
362 SetNodeMaskVisitor setNodeMaskVisitor(0, simgear::MODELLIGHT_BIT);
363 model->accept(setNodeMaskVisitor);
365 model->setName(modelpath.str());
367 bool needTransform=false;
368 // Set up the alignment node if needed
369 SGPropertyNode *offsets = props->getNode("offsets", false);
372 osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
373 alignmainmodel->setDataVariance(osg::Object::STATIC);
374 osg::Matrix res_matrix;
375 res_matrix.makeRotate(
376 offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
378 offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
380 offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
384 tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
385 offsets->getFloatValue("y-m", 0.0),
386 offsets->getFloatValue("z-m", 0.0));
387 alignmainmodel->setMatrix(res_matrix*tmat);
388 group = alignmainmodel;
391 group = new osg::Group;
393 group->addChild(model.get());
396 vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
397 for (unsigned i = 0; i < model_nodes.size(); i++) {
398 SGPropertyNode_ptr sub_props = model_nodes[i];
401 osg::ref_ptr<osg::Node> submodel;
403 string subPathStr = sub_props->getStringValue("path");
404 SGPath submodelPath = SGModelLib::findDataFile(subPathStr,
407 if (submodelPath.isNull()) {
408 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << subPathStr << "\"");
413 submodel = sgLoad3DModel_internal(submodelPath, options.get(),
414 sub_props->getNode("overlay"));
415 } catch (const sg_exception &t) {
416 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage()
417 << "\n\tfrom:" << t.getOrigin());
424 osg::ref_ptr<osg::Node> submodel_final = submodel;
425 SGPropertyNode *offs = sub_props->getNode("offsets", false);
427 osg::Matrix res_matrix;
428 osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
429 align->setDataVariance(osg::Object::STATIC);
430 res_matrix.makeIdentity();
431 res_matrix.makeRotate(
432 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
434 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
436 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
441 tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
442 offs->getDoubleValue("y-m", 0),
443 offs->getDoubleValue("z-m", 0));
444 align->setMatrix(res_matrix*tmat);
445 align->addChild(submodel.get());
446 submodel_final = align;
448 submodel_final->setName(sub_props->getStringValue("name", ""));
450 SGPropertyNode *cond = sub_props->getNode("condition", false);
452 osg::ref_ptr<osg::Switch> sw = new osg::Switch;
453 sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
454 group->addChild(sw.get());
455 sw->addChild(submodel_final.get());
456 sw->setName("submodel condition switch");
458 group->addChild(submodel_final.get());
460 } // end of submodel loading
462 osg::Node *(*load_panel)(SGPropertyNode *) = options->getLoadPanel();
465 vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
466 for (unsigned i = 0; i < panel_nodes.size(); i++) {
467 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
468 osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
469 if (panel_nodes[i]->hasValue("name"))
470 panel->setName(panel_nodes[i]->getStringValue("name"));
471 group->addChild(panel.get());
475 if (dbOptions->getPluginStringData("SimGear::PARTICLESYSTEM") != "OFF") {
476 std::vector<SGPropertyNode_ptr> particle_nodes;
477 particle_nodes = props->getChildren("particlesystem");
478 for (unsigned i = 0; i < particle_nodes.size(); ++i) {
479 osg::ref_ptr<SGReaderWriterOptions> options2;
480 options2 = new SGReaderWriterOptions(*options);
482 if (!texturepath.extension().empty())
483 texturepath = texturepath.dir();
485 options2->setDatabasePath(texturepath.str());
487 group->addChild(Particles::appendParticles(particle_nodes[i],
493 std::vector<SGPropertyNode_ptr> text_nodes;
494 text_nodes = props->getChildren("text");
495 for (unsigned i = 0; i < text_nodes.size(); ++i) {
496 group->addChild(SGText::appendText(text_nodes[i],
501 PropertyList effect_nodes = props->getChildren("effect");
502 PropertyList animation_nodes = props->getChildren("animation");
505 PropertyList::iterator it;
506 it = std::remove_if(animation_nodes.begin(), animation_nodes.end(), ExcludeInPreview());
507 animation_nodes.erase(it, animation_nodes.end());
510 // Some material animations (eventually all) are actually effects.
511 makeEffectAnimations(animation_nodes, effect_nodes);
513 ref_ptr<Node> modelWithEffects
514 = instantiateEffects(group.get(), effect_nodes, options.get());
515 group = static_cast<Group*>(modelWithEffects.get());
518 for (unsigned i = 0; i < animation_nodes.size(); ++i) {
519 if (previewMode && animation_nodes[i]->hasChild("nopreview")) {
520 PropertyList names(animation_nodes[i]->getChildren("object-name"));
521 for (unsigned int n=0; n<names.size(); ++n) {
522 removeNamedNode(group, names[n]->getStringValue());
523 } // of object-names in the animation
527 /// OSGFIXME: duh, why not only model?????
528 SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
529 options.get(), path.str(), i);
532 if (!needTransform && group->getNumChildren() < 2) {
533 model = group->getChild(0);
534 group->removeChild(model.get());
536 data->modelLoaded(modelpath.str(), props, model.get());
537 return model.release();
540 data->modelLoaded(modelpath.str(), props, group.get());
541 if (props->hasChild("debug-outfile")) {
542 std::string outputfile = props->getStringValue("debug-outfile",
544 osgDB::writeNodeFile(*group, outputfile);
547 return group.release();