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>
27 #include <boost/bind.hpp>
30 #include <osg/MatrixTransform>
31 #include <osgDB/WriteFile>
32 #include <osgDB/Registry>
34 #include <osgDB/FileNameUtils>
36 #include <simgear/compiler.h>
37 #include <simgear/structure/exception.hxx>
38 #include <simgear/props/props.hxx>
39 #include <simgear/props/props_io.hxx>
40 #include <simgear/props/condition.hxx>
41 #include <simgear/scene/util/SGNodeMasks.hxx>
43 #include "modellib.hxx"
44 #include "SGPagedLOD.hxx"
45 #include "SGReaderWriterXML.hxx"
46 #include "SGReaderWriterXMLOptions.hxx"
48 #include "animation.hxx"
49 #include "particles.hxx"
54 using namespace simgear;
58 sgLoad3DModel_internal(const std::string& path,
59 const osgDB::ReaderWriter::Options* options,
60 SGPropertyNode *overlay = 0);
63 SGReaderWriterXML::SGReaderWriterXML()
65 supportsExtension("xml", "SimGear xml database format");
68 SGReaderWriterXML::~SGReaderWriterXML()
72 const char* SGReaderWriterXML::className() const
74 return "XML database reader";
77 osgDB::ReaderWriter::ReadResult
78 SGReaderWriterXML::readNode(const std::string& fileName,
79 const osgDB::ReaderWriter::Options* options) const
83 result=sgLoad3DModel_internal(fileName, options);
84 } catch (const sg_throwable &t) {
85 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage());
91 return ReadResult::FILE_NOT_HANDLED;
94 class SGSwitchUpdateCallback : public osg::NodeCallback
97 SGSwitchUpdateCallback(SGCondition* condition) :
98 mCondition(condition) {}
99 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
100 assert(dynamic_cast<osg::Switch*>(node));
101 osg::Switch* s = static_cast<osg::Switch*>(node);
103 if (mCondition && mCondition->test()) {
104 s->setAllChildrenOn();
105 // note, callback is responsible for scenegraph traversal so
106 // should always include call traverse(node,nv) to ensure
107 // that the rest of cullbacks and the scene graph are traversed.
110 s->setAllChildrenOff();
114 SGSharedPtr<SGCondition> mCondition;
118 // Little helper class that holds an extra reference to a
120 // Since we clone all structural nodes from our 3d models,
121 // the database pager will only see one single reference to
122 // top node of the model and expire it relatively fast.
123 // We attach that extra reference to every model cloned from
124 // a base model in the pager. When that cloned model is deleted
125 // this extra reference is deleted too. So if there are no
126 // cloned models left the model will expire.
128 class SGDatabaseReference : public osg::Observer
131 SGDatabaseReference(osg::Referenced* referenced) :
132 mReferenced(referenced)
134 virtual void objectDeleted(void*)
139 osg::ref_ptr<osg::Referenced> mReferenced;
142 void makeEffectAnimations(PropertyList& animation_nodes,
143 PropertyList& effect_nodes)
145 for (PropertyList::iterator itr = animation_nodes.begin();
146 itr != animation_nodes.end();
148 SGPropertyNode* animProp = itr->ptr();
149 SGPropertyNode* typeProp = animProp->getChild("type");
150 if (!typeProp || strcmp(typeProp->getStringValue(), "shader"))
152 SGPropertyNode* shaderProp = animProp->getChild("shader");
153 if (!shaderProp || strcmp(shaderProp->getStringValue(), "chrome"))
156 SGPropertyNode* textureProp = animProp->getChild("texture");
159 SGPropertyNode_ptr effectProp = new SGPropertyNode();
160 makeChild(effectProp.ptr(), "inherits-from")
161 ->setValue("Effects/chrome");
162 SGPropertyNode* paramsProp = makeChild(effectProp.get(), "parameters");
163 makeChild(paramsProp, "chrome-texture")
164 ->setValue(textureProp->getStringValue());
165 PropertyList objectNameNodes = animProp->getChildren("object-name");
166 for (PropertyList::iterator objItr = objectNameNodes.begin(),
167 end = objectNameNodes.end();
170 effectProp->addChild("object-name")
171 ->setStringValue((*objItr)->getStringValue());
172 effect_nodes.push_back(effectProp);
174 animation_nodes.erase(remove_if(animation_nodes.begin(),
175 animation_nodes.end(),
176 !boost::bind(&SGPropertyNode_ptr::valid,
178 animation_nodes.end());
183 sgLoad3DModel_internal(const string &path,
184 const osgDB::ReaderWriter::Options* options_,
185 SGPropertyNode *overlay)
187 const SGReaderWriterXMLOptions* xmlOptions;
188 xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
190 SGSharedPtr<SGPropertyNode> prop_root;
191 osg::Node *(*load_panel)(SGPropertyNode *)=0;
192 osg::ref_ptr<SGModelData> data;
195 prop_root = xmlOptions->getPropRoot();
196 load_panel = xmlOptions->getLoadPanel();
197 data = xmlOptions->getModelData();
200 prop_root = new SGPropertyNode;
203 osgDB::FilePathList filePathList;
204 filePathList = osgDB::Registry::instance()->getDataFilePathList();
205 filePathList.push_front(std::string());
207 SGPath modelpath = osgDB::findFileInPath(path, filePathList);
208 if (modelpath.str().empty()) {
209 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
212 SGPath texturepath = modelpath;
214 osg::ref_ptr<osg::Node> model;
215 osg::ref_ptr<osg::Group> group;
216 SGPropertyNode_ptr props = new SGPropertyNode;
218 // Check for an XML wrapper
219 if (modelpath.extension() == "xml") {
221 readProperties(modelpath.str(), props);
222 } catch (const sg_throwable &t) {
223 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
224 << t.getFormattedMessage());
228 copyProperties(overlay, props);
230 if (props->hasValue("/path")) {
231 modelpath = modelpath.dir();
232 modelpath.append(props->getStringValue("/path"));
233 if (props->hasValue("/texture-path")) {
234 texturepath = texturepath.dir();
235 texturepath.append(props->getStringValue("/texture-path"));
238 model = new osg::Node;
241 SGPropertyNode *mp = props->getNode("multiplay");
242 if (mp && prop_root && prop_root->getParent())
243 copyProperties(mp, prop_root);
245 SG_LOG(SG_INPUT, SG_DEBUG, "model without wrapper: "
249 osg::ref_ptr<SGReaderWriterXMLOptions> options
250 = new SGReaderWriterXMLOptions(*options_);
251 options->setPropRoot(prop_root);
252 options->setLoadPanel(load_panel);
254 // Assume that textures are in
255 // the same location as the XML file.
257 if (!texturepath.extension().empty())
258 texturepath = texturepath.dir();
260 options->setDatabasePath(texturepath.str());
261 osgDB::ReaderWriter::ReadResult modelResult
262 = osgDB::Registry::instance()->readNode(modelpath.str(),
264 if (!modelResult.validNode())
265 throw sg_io_exception("Failed to load 3D model",
266 sg_location(modelpath.str()));
267 model = copyModel(modelResult.getNode());
268 // Add an extra reference to the model stored in the database.
269 // That is to avoid expiring the object from the cache even if
270 // it is still in use. Note that the object cache will think
271 // that a model is unused if the reference count is 1. If we
272 // clone all structural nodes here we need that extra
273 // reference to the original object
274 SGDatabaseReference* databaseReference;
275 databaseReference = new SGDatabaseReference(modelResult.getNode());
276 model->addObserver(databaseReference);
279 TextureUpdateVisitor liveryUpdate(options->getDatabasePathList());
280 model->accept(liveryUpdate);
282 // Copy the userdata fields, still sharing the boundingvolumes,
283 // but introducing new data for velocities.
284 UserDataCopyVisitor userDataCopyVisitor;
285 model->accept(userDataCopyVisitor);
287 model->setName(modelpath.str());
289 bool needTransform=false;
290 // Set up the alignment node if needed
291 SGPropertyNode *offsets = props->getNode("offsets", false);
294 osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
295 alignmainmodel->setDataVariance(osg::Object::STATIC);
296 osg::Matrix res_matrix;
297 res_matrix.makeRotate(
298 offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
300 offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
302 offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
306 tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
307 offsets->getFloatValue("y-m", 0.0),
308 offsets->getFloatValue("z-m", 0.0));
309 alignmainmodel->setMatrix(res_matrix*tmat);
310 group = alignmainmodel;
313 group = new osg::Group;
315 group->addChild(model.get());
318 vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
319 for (unsigned i = 0; i < model_nodes.size(); i++) {
320 SGPropertyNode_ptr sub_props = model_nodes[i];
323 osg::ref_ptr<osg::Node> submodel;
324 string submodelFileName = sub_props->getStringValue("path");
325 if (submodelFileName.size() > 2
326 && !submodelFileName.compare(0, 2, "./" )) {
327 submodelpath = modelpath.dir();
328 submodelpath.append( submodelFileName.substr( 2 ) );
330 submodelpath = submodelFileName;
332 osg::ref_ptr<SGReaderWriterXMLOptions> options;
333 options = new SGReaderWriterXMLOptions(*options_);
334 options->setPropRoot(prop_root);
335 options->setLoadPanel(load_panel);
337 submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(),
338 sub_props->getNode("overlay"));
339 } catch (const sg_throwable &t) {
340 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
344 osg::ref_ptr<osg::Node> submodel_final = submodel;
345 SGPropertyNode *offs = sub_props->getNode("offsets", false);
347 osg::Matrix res_matrix;
348 osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
349 align->setDataVariance(osg::Object::STATIC);
350 res_matrix.makeIdentity();
351 res_matrix.makeRotate(
352 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
354 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
356 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
361 tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
362 offs->getDoubleValue("y-m", 0),
363 offs->getDoubleValue("z-m", 0));
364 align->setMatrix(res_matrix*tmat);
365 align->addChild(submodel.get());
366 submodel_final = align;
368 submodel_final->setName(sub_props->getStringValue("name", ""));
370 SGPropertyNode *cond = sub_props->getNode("condition", false);
372 osg::ref_ptr<osg::Switch> sw = new osg::Switch;
373 sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
374 group->addChild(sw.get());
375 sw->addChild(submodel_final.get());
376 sw->setName("submodel condition switch");
378 group->addChild(submodel_final.get());
380 } // end of submodel loading
384 vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
385 for (unsigned i = 0; i < panel_nodes.size(); i++) {
386 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
387 osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
388 if (panel_nodes[i]->hasValue("name"))
389 panel->setName(panel_nodes[i]->getStringValue("name"));
390 group->addChild(panel.get());
394 std::vector<SGPropertyNode_ptr> particle_nodes;
395 particle_nodes = props->getChildren("particlesystem");
396 for (unsigned i = 0; i < particle_nodes.size(); ++i) {
398 if (!texturepath.extension().empty())
399 texturepath = texturepath.dir();
401 options->setDatabasePath(texturepath.str());
403 group->addChild(Particles::appendParticles(particle_nodes[i],
408 std::vector<SGPropertyNode_ptr> text_nodes;
409 text_nodes = props->getChildren("text");
410 for (unsigned i = 0; i < text_nodes.size(); ++i) {
411 group->addChild(SGText::appendText(text_nodes[i],
415 PropertyList effect_nodes = props->getChildren("effect");
416 PropertyList animation_nodes = props->getChildren("animation");
417 // Some material animations (eventually all) are actually effects.
418 makeEffectAnimations(animation_nodes, effect_nodes);
420 ref_ptr<Node> modelWithEffects
421 = instantiateEffects(group.get(), effect_nodes, options.get());
422 group = static_cast<Group*>(modelWithEffects.get());
424 for (unsigned i = 0; i < animation_nodes.size(); ++i)
425 /// OSGFIXME: duh, why not only model?????
426 SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
429 if (!needTransform && group->getNumChildren() < 2) {
430 model = group->getChild(0);
431 group->removeChild(model.get());
433 data->modelLoaded(modelpath.str(), props, model.get());
434 return model.release();
437 data->modelLoaded(modelpath.str(), props, group.get());
438 if (props->hasChild("debug-outfile")) {
439 std::string outputfile = props->getStringValue("debug-outfile",
441 osgDB::writeNodeFile(*group, outputfile);
444 return group.release();