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/WriteFile>
33 #include <osgDB/Registry>
35 #include <osgDB/FileNameUtils>
37 #include <simgear/compiler.h>
38 #include <simgear/structure/exception.hxx>
39 #include <simgear/props/props.hxx>
40 #include <simgear/props/props_io.hxx>
41 #include <simgear/props/condition.hxx>
42 #include <simgear/scene/util/SGNodeMasks.hxx>
44 #include "modellib.hxx"
45 #include "SGPagedLOD.hxx"
46 #include "SGReaderWriterXML.hxx"
47 #include "SGReaderWriterXMLOptions.hxx"
49 #include "animation.hxx"
50 #include "particles.hxx"
53 #include "SGMaterialAnimation.hxx"
56 using namespace simgear;
60 sgLoad3DModel_internal(const std::string& path,
61 const osgDB::ReaderWriter::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& fileName,
81 const osgDB::ReaderWriter::Options* options) const
85 result=sgLoad3DModel_internal(fileName, options);
86 } catch (const sg_throwable &t) {
87 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage());
93 return ReadResult::FILE_NOT_HANDLED;
96 class SGSwitchUpdateCallback : public osg::NodeCallback
99 SGSwitchUpdateCallback(SGCondition* condition) :
100 mCondition(condition) {}
101 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
102 assert(dynamic_cast<osg::Switch*>(node));
103 osg::Switch* s = static_cast<osg::Switch*>(node);
105 if (mCondition && mCondition->test()) {
106 s->setAllChildrenOn();
107 // note, callback is responsible for scenegraph traversal so
108 // should always include call traverse(node,nv) to ensure
109 // that the rest of cullbacks and the scene graph are traversed.
112 s->setAllChildrenOff();
116 SGSharedPtr<SGCondition> mCondition;
120 // Little helper class that holds an extra reference to a
122 // Since we clone all structural nodes from our 3d models,
123 // the database pager will only see one single reference to
124 // top node of the model and expire it relatively fast.
125 // We attach that extra reference to every model cloned from
126 // a base model in the pager. When that cloned model is deleted
127 // this extra reference is deleted too. So if there are no
128 // cloned models left the model will expire.
130 class SGDatabaseReference : public osg::Observer
133 SGDatabaseReference(osg::Referenced* referenced) :
134 mReferenced(referenced)
136 virtual void objectDeleted(void*)
141 osg::ref_ptr<osg::Referenced> mReferenced;
144 void makeEffectAnimations(PropertyList& animation_nodes,
145 PropertyList& effect_nodes)
147 for (PropertyList::iterator itr = animation_nodes.begin();
148 itr != animation_nodes.end();
150 SGPropertyNode_ptr effectProp;
151 SGPropertyNode* animProp = itr->ptr();
152 SGPropertyNode* typeProp = animProp->getChild("type");
155 const char* typeString = typeProp->getStringValue();
156 if (!strcmp(typeString, "material")) {
158 = SGMaterialAnimation::makeEffectProperties(animProp);
159 } else if (!strcmp(typeString, "shader")) {
161 SGPropertyNode* shaderProp = animProp->getChild("shader");
162 if (!shaderProp || strcmp(shaderProp->getStringValue(), "chrome"))
164 *itr = 0; // effect replaces animation
165 SGPropertyNode* textureProp = animProp->getChild("texture");
168 effectProp = new SGPropertyNode();
169 makeChild(effectProp.ptr(), "inherits-from")
170 ->setValue("Effects/chrome");
171 SGPropertyNode* paramsProp = makeChild(effectProp.get(), "parameters");
172 makeChild(paramsProp, "chrome-texture")
173 ->setValue(textureProp->getStringValue());
175 if (effectProp.valid()) {
176 PropertyList objectNameNodes = animProp->getChildren("object-name");
177 for (PropertyList::iterator objItr = objectNameNodes.begin(),
178 end = objectNameNodes.end();
181 effectProp->addChild("object-name")
182 ->setStringValue((*objItr)->getStringValue());
183 effect_nodes.push_back(effectProp);
187 animation_nodes.erase(remove_if(animation_nodes.begin(),
188 animation_nodes.end(),
189 !boost::bind(&SGPropertyNode_ptr::valid,
191 animation_nodes.end());
196 sgLoad3DModel_internal(const string &path,
197 const osgDB::ReaderWriter::Options* options_,
198 SGPropertyNode *overlay)
200 const SGReaderWriterXMLOptions* xmlOptions;
201 xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
203 SGSharedPtr<SGPropertyNode> prop_root;
204 osg::Node *(*load_panel)(SGPropertyNode *)=0;
205 osg::ref_ptr<SGModelData> data;
209 prop_root = xmlOptions->getPropRoot();
210 load_panel = xmlOptions->getLoadPanel();
211 data = xmlOptions->getModelData();
215 modelpath = SGModelLib::findDataFile(path);
218 prop_root = new SGPropertyNode;
221 if (modelpath.str().empty()) {
222 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
225 SGPath texturepath = modelpath;
227 osg::ref_ptr<osg::Node> model;
228 osg::ref_ptr<osg::Group> group;
229 SGPropertyNode_ptr props = new SGPropertyNode;
231 // Check for an XML wrapper
232 if (modelpath.extension() == "xml") {
234 readProperties(modelpath.str(), props);
235 } catch (const sg_throwable &t) {
236 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
237 << t.getFormattedMessage());
241 copyProperties(overlay, props);
243 if (props->hasValue("/path")) {
244 modelpath = modelpath.dir();
245 modelpath.append(props->getStringValue("/path"));
246 if (props->hasValue("/texture-path")) {
247 texturepath = texturepath.dir();
248 texturepath.append(props->getStringValue("/texture-path"));
251 model = new osg::Node;
254 SGPropertyNode *mp = props->getNode("multiplay");
255 if (mp && prop_root && prop_root->getParent())
256 copyProperties(mp, prop_root);
258 SG_LOG(SG_INPUT, SG_DEBUG, "model without wrapper: "
262 osg::ref_ptr<SGReaderWriterXMLOptions> options
263 = new SGReaderWriterXMLOptions(*options_);
264 options->setPropRoot(prop_root);
265 options->setLoadPanel(load_panel);
267 // Assume that textures are in
268 // the same location as the XML file.
270 if (!texturepath.extension().empty())
271 texturepath = texturepath.dir();
273 options->setDatabasePath(texturepath.str());
274 osgDB::ReaderWriter::ReadResult modelResult
275 = osgDB::Registry::instance()->readNode(modelpath.str(),
277 if (!modelResult.validNode())
278 throw sg_io_exception("Failed to load 3D model",
279 sg_location(modelpath.str()));
280 model = copyModel(modelResult.getNode());
281 // Add an extra reference to the model stored in the database.
282 // That is to avoid expiring the object from the cache even if
283 // it is still in use. Note that the object cache will think
284 // that a model is unused if the reference count is 1. If we
285 // clone all structural nodes here we need that extra
286 // reference to the original object
287 SGDatabaseReference* databaseReference;
288 databaseReference = new SGDatabaseReference(modelResult.getNode());
289 model->addObserver(databaseReference);
292 TextureUpdateVisitor liveryUpdate(options->getDatabasePathList());
293 model->accept(liveryUpdate);
295 // Copy the userdata fields, still sharing the boundingvolumes,
296 // but introducing new data for velocities.
297 UserDataCopyVisitor userDataCopyVisitor;
298 model->accept(userDataCopyVisitor);
300 model->setName(modelpath.str());
302 bool needTransform=false;
303 // Set up the alignment node if needed
304 SGPropertyNode *offsets = props->getNode("offsets", false);
307 osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
308 alignmainmodel->setDataVariance(osg::Object::STATIC);
309 osg::Matrix res_matrix;
310 res_matrix.makeRotate(
311 offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
313 offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
315 offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
319 tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
320 offsets->getFloatValue("y-m", 0.0),
321 offsets->getFloatValue("z-m", 0.0));
322 alignmainmodel->setMatrix(res_matrix*tmat);
323 group = alignmainmodel;
326 group = new osg::Group;
328 group->addChild(model.get());
331 vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
332 for (unsigned i = 0; i < model_nodes.size(); i++) {
333 SGPropertyNode_ptr sub_props = model_nodes[i];
336 osg::ref_ptr<osg::Node> submodel;
337 string submodelFileName = sub_props->getStringValue("path");
338 if (submodelFileName.size() > 2
339 && !submodelFileName.compare(0, 2, "./" )) {
340 submodelpath = modelpath.dir();
341 submodelpath.append( submodelFileName.substr( 2 ) );
343 submodelpath = submodelFileName;
345 osg::ref_ptr<SGReaderWriterXMLOptions> options;
346 options = new SGReaderWriterXMLOptions(*options_);
347 options->setPropRoot(prop_root);
348 options->setLoadPanel(load_panel);
351 submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(),
352 sub_props->getNode("overlay"));
353 } catch (const sg_throwable &t) {
354 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
358 osg::ref_ptr<osg::Node> submodel_final = submodel;
359 SGPropertyNode *offs = sub_props->getNode("offsets", false);
361 osg::Matrix res_matrix;
362 osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
363 align->setDataVariance(osg::Object::STATIC);
364 res_matrix.makeIdentity();
365 res_matrix.makeRotate(
366 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
368 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
370 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
375 tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
376 offs->getDoubleValue("y-m", 0),
377 offs->getDoubleValue("z-m", 0));
378 align->setMatrix(res_matrix*tmat);
379 align->addChild(submodel.get());
380 submodel_final = align;
382 submodel_final->setName(sub_props->getStringValue("name", ""));
384 SGPropertyNode *cond = sub_props->getNode("condition", false);
386 osg::ref_ptr<osg::Switch> sw = new osg::Switch;
387 sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
388 group->addChild(sw.get());
389 sw->addChild(submodel_final.get());
390 sw->setName("submodel condition switch");
392 group->addChild(submodel_final.get());
394 } // end of submodel loading
398 vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
399 for (unsigned i = 0; i < panel_nodes.size(); i++) {
400 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
401 osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
402 if (panel_nodes[i]->hasValue("name"))
403 panel->setName(panel_nodes[i]->getStringValue("name"));
404 group->addChild(panel.get());
408 std::vector<SGPropertyNode_ptr> particle_nodes;
409 particle_nodes = props->getChildren("particlesystem");
410 for (unsigned i = 0; i < particle_nodes.size(); ++i) {
412 if (!texturepath.extension().empty())
413 texturepath = texturepath.dir();
415 options->setDatabasePath(texturepath.str());
417 group->addChild(Particles::appendParticles(particle_nodes[i],
422 std::vector<SGPropertyNode_ptr> text_nodes;
423 text_nodes = props->getChildren("text");
424 for (unsigned i = 0; i < text_nodes.size(); ++i) {
425 group->addChild(SGText::appendText(text_nodes[i],
429 PropertyList effect_nodes = props->getChildren("effect");
430 PropertyList animation_nodes = props->getChildren("animation");
431 // Some material animations (eventually all) are actually effects.
432 makeEffectAnimations(animation_nodes, effect_nodes);
434 ref_ptr<Node> modelWithEffects
435 = instantiateEffects(group.get(), effect_nodes, options.get());
436 group = static_cast<Group*>(modelWithEffects.get());
438 for (unsigned i = 0; i < animation_nodes.size(); ++i)
439 /// OSGFIXME: duh, why not only model?????
440 SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
443 if (!needTransform && group->getNumChildren() < 2) {
444 model = group->getChild(0);
445 group->removeChild(model.get());
447 data->modelLoaded(modelpath.str(), props, model.get());
448 return model.release();
451 data->modelLoaded(modelpath.str(), props, group.get());
452 if (props->hasChild("debug-outfile")) {
453 std::string outputfile = props->getStringValue("debug-outfile",
455 osgDB::writeNodeFile(*group, outputfile);
458 return group.release();