]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGReaderWriterXML.cxx
Standardise path-handling in XML mode files
[simgear.git] / simgear / scene / model / SGReaderWriterXML.cxx
1 // Copyright (C) 2007 Tim Moore timoore@redhat.com
2 // Copyright (C) 2008 Till Busch buti@bux.at
3 //
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.
8 //
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.
13 //
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.
17 //
18
19 #ifdef HAVE_CONFIG_H
20 #  include <simgear_config.h>
21 #endif
22
23 #include <algorithm>
24 //yuck
25 #include <cstring>
26 #include <cassert>
27
28 #include <boost/bind.hpp>
29
30 #include <osg/Geode>
31 #include <osg/MatrixTransform>
32 #include <osgDB/WriteFile>
33 #include <osgDB/Registry>
34 #include <osg/Switch>
35 #include <osgDB/FileNameUtils>
36
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>
43
44 #include "modellib.hxx"
45 #include "SGPagedLOD.hxx"
46 #include "SGReaderWriterXML.hxx"
47 #include "SGReaderWriterXMLOptions.hxx"
48
49 #include "animation.hxx"
50 #include "particles.hxx"
51 #include "model.hxx"
52 #include "SGText.hxx"
53 #include "SGMaterialAnimation.hxx"
54
55 using namespace std;
56 using namespace simgear;
57 using namespace osg;
58
59 static osg::Node *
60 sgLoad3DModel_internal(const SGPath& path,
61                        const osgDB::ReaderWriter::Options* options,
62                        SGPropertyNode *overlay = 0);
63
64
65 SGReaderWriterXML::SGReaderWriterXML()
66 {
67     supportsExtension("xml", "SimGear xml database format");
68 }
69
70 SGReaderWriterXML::~SGReaderWriterXML()
71 {
72 }
73
74 const char* SGReaderWriterXML::className() const
75 {
76     return "XML database reader";
77 }
78
79 osgDB::ReaderWriter::ReadResult
80 SGReaderWriterXML::readNode(const std::string& fileName,
81                             const osgDB::ReaderWriter::Options* options) const
82 {
83     osg::Node *result=0;
84     try {
85         SGPath p = SGModelLib::findDataFile(fileName);
86         if (!p.exists()) {
87           return ReadResult::FILE_NOT_FOUND;
88         }
89         
90         result=sgLoad3DModel_internal(p, options);
91     } catch (const sg_exception &t) {
92         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage()
93           << "\n\tfrom:" << fileName);
94         result=new osg::Node;
95     }
96     if (result)
97         return result;
98     else
99         return ReadResult::FILE_NOT_HANDLED;
100 }
101
102 class SGSwitchUpdateCallback : public osg::NodeCallback
103 {
104 public:
105     SGSwitchUpdateCallback(SGCondition* condition) :
106             mCondition(condition) {}
107     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
108         assert(dynamic_cast<osg::Switch*>(node));
109         osg::Switch* s = static_cast<osg::Switch*>(node);
110
111         if (mCondition && mCondition->test()) {
112             s->setAllChildrenOn();
113             // note, callback is responsible for scenegraph traversal so
114             // should always include call traverse(node,nv) to ensure
115             // that the rest of cullbacks and the scene graph are traversed.
116             traverse(node, nv);
117         } else
118             s->setAllChildrenOff();
119     }
120
121 private:
122     SGSharedPtr<SGCondition> mCondition;
123 };
124
125
126 // Little helper class that holds an extra reference to a
127 // loaded 3d model.
128 // Since we clone all structural nodes from our 3d models,
129 // the database pager will only see one single reference to
130 // top node of the model and expire it relatively fast.
131 // We attach that extra reference to every model cloned from
132 // a base model in the pager. When that cloned model is deleted
133 // this extra reference is deleted too. So if there are no
134 // cloned models left the model will expire.
135 namespace {
136 class SGDatabaseReference : public osg::Observer
137 {
138 public:
139     SGDatabaseReference(osg::Referenced* referenced) :
140         mReferenced(referenced)
141     { }
142     virtual void objectDeleted(void*)
143     {
144         mReferenced = 0;
145     }
146 private:
147     osg::ref_ptr<osg::Referenced> mReferenced;
148 };
149
150 void makeEffectAnimations(PropertyList& animation_nodes,
151                           PropertyList& effect_nodes)
152 {
153     for (PropertyList::iterator itr = animation_nodes.begin();
154          itr != animation_nodes.end();
155          ++itr) {
156         SGPropertyNode_ptr effectProp;
157         SGPropertyNode* animProp = itr->ptr();
158         SGPropertyNode* typeProp = animProp->getChild("type");
159         if (!typeProp)
160             continue;
161         const char* typeString = typeProp->getStringValue();
162         if (!strcmp(typeString, "material")) {
163             effectProp
164                 = SGMaterialAnimation::makeEffectProperties(animProp);
165         } else if (!strcmp(typeString, "shader")) {
166             
167             SGPropertyNode* shaderProp = animProp->getChild("shader");
168             if (!shaderProp || strcmp(shaderProp->getStringValue(), "chrome"))
169                 continue;
170             *itr = 0;           // effect replaces animation
171             SGPropertyNode* textureProp = animProp->getChild("texture");
172             if (!textureProp)
173                 continue;
174             effectProp = new SGPropertyNode();
175             makeChild(effectProp.ptr(), "inherits-from")
176                 ->setValue("Effects/chrome");
177             SGPropertyNode* paramsProp = makeChild(effectProp.get(), "parameters");
178             makeChild(paramsProp, "chrome-texture")
179                 ->setValue(textureProp->getStringValue());
180         }
181         if (effectProp.valid()) {
182             PropertyList objectNameNodes = animProp->getChildren("object-name");
183             for (PropertyList::iterator objItr = objectNameNodes.begin(),
184                      end = objectNameNodes.end();
185                  objItr != end;
186                  ++objItr)
187                 effectProp->addChild("object-name")
188                     ->setStringValue((*objItr)->getStringValue());
189             effect_nodes.push_back(effectProp);
190
191         }
192     }
193     animation_nodes.erase(remove_if(animation_nodes.begin(),
194                                     animation_nodes.end(),
195                                     !boost::bind(&SGPropertyNode_ptr::valid,
196                                                  _1)),
197                           animation_nodes.end());
198 }
199 }
200
201 static osg::Node *
202 sgLoad3DModel_internal(const SGPath& path,
203                        const osgDB::ReaderWriter::Options* options_,
204                        SGPropertyNode *overlay)
205 {
206     if (!path.exists()) {
207       SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path.str() << "\"");
208       return NULL;
209     }
210     
211     const SGReaderWriterXMLOptions* xmlOptions;
212     xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
213
214     SGSharedPtr<SGPropertyNode> prop_root;
215     osg::Node *(*load_panel)(SGPropertyNode *)=0;
216     osg::ref_ptr<SGModelData> data;
217     SGPath modelpath(path);
218     SGPath texturepath(path);
219     SGPath modelDir(modelpath.dir());
220     
221     if (xmlOptions) {
222         prop_root = xmlOptions->getPropRoot();
223         load_panel = xmlOptions->getLoadPanel();
224         data = xmlOptions->getModelData();
225     }
226     
227     if (!prop_root) {
228         prop_root = new SGPropertyNode;
229     }
230
231     osg::ref_ptr<osg::Node> model;
232     osg::ref_ptr<osg::Group> group;
233     SGPropertyNode_ptr props = new SGPropertyNode;
234
235     // Check for an XML wrapper
236     if (modelpath.extension() == "xml") {
237        try {
238             readProperties(modelpath.str(), props);
239         } catch (const sg_exception &t) {
240             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
241                    << t.getFormattedMessage());
242             throw;
243         }
244         if (overlay)
245             copyProperties(overlay, props);
246
247         if (props->hasValue("/path")) {
248             modelpath = SGModelLib::findDataFile(props->getStringValue("/path"), NULL, modelDir);
249             if (props->hasValue("/texture-path")) {
250                 texturepath = SGModelLib::findDataFile(props->getStringValue("/texture-path"), NULL, modelDir);
251             }
252         } else {
253             model = new osg::Node;
254         }
255
256         SGPropertyNode *mp = props->getNode("multiplay");
257         if (mp && prop_root && prop_root->getParent())
258             copyProperties(mp, prop_root);
259     } else {
260         // model without wrapper
261     }
262
263     osg::ref_ptr<SGReaderWriterXMLOptions> options
264     = new SGReaderWriterXMLOptions(*options_);
265     options->setPropRoot(prop_root);
266     options->setLoadPanel(load_panel);
267     
268     // Assume that textures are in
269     // the same location as the XML file.
270     if (!model) {
271         if (!texturepath.extension().empty())
272             texturepath = texturepath.dir();
273
274         options->setDatabasePath(texturepath.str());
275         osgDB::ReaderWriter::ReadResult modelResult
276             = osgDB::Registry::instance()->readNode(modelpath.str(),
277                                                     options.get());
278         if (!modelResult.validNode())
279             throw sg_io_exception("Failed to load 3D model:" + modelResult.message(),
280                                   modelpath.str());
281         model = copyModel(modelResult.getNode());
282         // Add an extra reference to the model stored in the database.
283         // That is to avoid expiring the object from the cache even if
284         // it is still in use. Note that the object cache will think
285         // that a model is unused if the reference count is 1. If we
286         // clone all structural nodes here we need that extra
287         // reference to the original object
288         SGDatabaseReference* databaseReference;
289         databaseReference = new SGDatabaseReference(modelResult.getNode());
290         model->addObserver(databaseReference);
291
292         // Update liveries
293         TextureUpdateVisitor liveryUpdate(options->getDatabasePathList());
294         model->accept(liveryUpdate);
295
296         // Copy the userdata fields, still sharing the boundingvolumes,
297         // but introducing new data for velocities.
298         UserDataCopyVisitor userDataCopyVisitor;
299         model->accept(userDataCopyVisitor);
300     }
301     model->setName(modelpath.str());
302
303     bool needTransform=false;
304     // Set up the alignment node if needed
305     SGPropertyNode *offsets = props->getNode("offsets", false);
306     if (offsets) {
307         needTransform=true;
308         osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
309         alignmainmodel->setDataVariance(osg::Object::STATIC);
310         osg::Matrix res_matrix;
311         res_matrix.makeRotate(
312             offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
313             osg::Vec3(0, 1, 0),
314             offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
315             osg::Vec3(1, 0, 0),
316             offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
317             osg::Vec3(0, 0, 1));
318
319         osg::Matrix tmat;
320         tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
321                            offsets->getFloatValue("y-m", 0.0),
322                            offsets->getFloatValue("z-m", 0.0));
323         alignmainmodel->setMatrix(res_matrix*tmat);
324         group = alignmainmodel;
325     }
326     if (!group) {
327         group = new osg::Group;
328     }
329     group->addChild(model.get());
330
331     // Load sub-models
332     vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
333     for (unsigned i = 0; i < model_nodes.size(); i++) {
334         SGPropertyNode_ptr sub_props = model_nodes[i];
335
336         SGPath submodelpath;
337         osg::ref_ptr<osg::Node> submodel;
338         
339         SGPath submodelPath = SGModelLib::findDataFile(sub_props->getStringValue("path"), 
340           NULL, modelDir);
341
342         osg::ref_ptr<SGReaderWriterXMLOptions> options;
343         options = new SGReaderWriterXMLOptions(*options_);
344         options->setPropRoot(prop_root);
345         options->setLoadPanel(load_panel);
346         
347         try {
348             submodel = sgLoad3DModel_internal(submodelPath, options.get(),
349                                               sub_props->getNode("overlay"));
350         } catch (const sg_exception &t) {
351             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage()
352               << "\n\tfrom:" << t.getOrigin());
353         }
354
355         osg::ref_ptr<osg::Node> submodel_final = submodel;
356         SGPropertyNode *offs = sub_props->getNode("offsets", false);
357         if (offs) {
358             osg::Matrix res_matrix;
359             osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
360             align->setDataVariance(osg::Object::STATIC);
361             res_matrix.makeIdentity();
362             res_matrix.makeRotate(
363                 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
364                 osg::Vec3(0, 1, 0),
365                 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
366                 osg::Vec3(1, 0, 0),
367                 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
368                 osg::Vec3(0, 0, 1));
369
370             osg::Matrix tmat;
371             tmat.makeIdentity();
372             tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
373                                offs->getDoubleValue("y-m", 0),
374                                offs->getDoubleValue("z-m", 0));
375             align->setMatrix(res_matrix*tmat);
376             align->addChild(submodel.get());
377             submodel_final = align;
378         }
379         submodel_final->setName(sub_props->getStringValue("name", ""));
380
381         SGPropertyNode *cond = sub_props->getNode("condition", false);
382         if (cond) {
383             osg::ref_ptr<osg::Switch> sw = new osg::Switch;
384             sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
385             group->addChild(sw.get());
386             sw->addChild(submodel_final.get());
387             sw->setName("submodel condition switch");
388         } else {
389             group->addChild(submodel_final.get());
390         }
391     } // end of submodel loading
392
393     if ( load_panel ) {
394         // Load panels
395         vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
396         for (unsigned i = 0; i < panel_nodes.size(); i++) {
397             SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
398             osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
399             if (panel_nodes[i]->hasValue("name"))
400                 panel->setName(panel_nodes[i]->getStringValue("name"));
401             group->addChild(panel.get());
402         }
403     }
404
405     std::vector<SGPropertyNode_ptr> particle_nodes;
406     particle_nodes = props->getChildren("particlesystem");
407     for (unsigned i = 0; i < particle_nodes.size(); ++i) {
408         if (i==0) {
409             if (!texturepath.extension().empty())
410                 texturepath = texturepath.dir();
411
412             options->setDatabasePath(texturepath.str());
413         }
414         group->addChild(Particles::appendParticles(particle_nodes[i],
415                         prop_root,
416                         options.get()));
417     }
418
419     std::vector<SGPropertyNode_ptr> text_nodes;
420     text_nodes = props->getChildren("text");
421     for (unsigned i = 0; i < text_nodes.size(); ++i) {
422         group->addChild(SGText::appendText(text_nodes[i],
423                         prop_root,
424                         options.get()));
425     }
426     PropertyList effect_nodes = props->getChildren("effect");
427     PropertyList animation_nodes = props->getChildren("animation");
428     // Some material animations (eventually all) are actually effects.
429     makeEffectAnimations(animation_nodes, effect_nodes);
430     {
431         ref_ptr<Node> modelWithEffects
432             = instantiateEffects(group.get(), effect_nodes, options.get());
433         group = static_cast<Group*>(modelWithEffects.get());
434     }
435     for (unsigned i = 0; i < animation_nodes.size(); ++i)
436         /// OSGFIXME: duh, why not only model?????
437         SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
438                              options.get());
439
440     if (!needTransform && group->getNumChildren() < 2) {
441         model = group->getChild(0);
442         group->removeChild(model.get());
443         if (data.valid())
444             data->modelLoaded(modelpath.str(), props, model.get());
445         return model.release();
446     }
447     if (data.valid())
448         data->modelLoaded(modelpath.str(), props, group.get());
449     if (props->hasChild("debug-outfile")) {
450         std::string outputfile = props->getStringValue("debug-outfile",
451                                  "debug-model.osg");
452         osgDB::writeNodeFile(*group, outputfile);
453     }
454
455     return group.release();
456 }
457