]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGReaderWriterXML.cxx
Merge branch 'ehofman/sound'
[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
27 #include <boost/bind.hpp>
28
29 #include <osg/Geode>
30 #include <osg/MatrixTransform>
31 #include <osgDB/WriteFile>
32 #include <osgDB/Registry>
33 #include <osg/Switch>
34 #include <osgDB/FileNameUtils>
35
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>
42
43 #include "modellib.hxx"
44 #include "SGPagedLOD.hxx"
45 #include "SGReaderWriterXML.hxx"
46 #include "SGReaderWriterXMLOptions.hxx"
47
48 #include "animation.hxx"
49 #include "particles.hxx"
50 #include "model.hxx"
51 #include "SGText.hxx"
52
53 using namespace std;
54 using namespace simgear;
55 using namespace osg;
56
57 static osg::Node *
58 sgLoad3DModel_internal(const std::string& path,
59                        const osgDB::ReaderWriter::Options* options,
60                        SGPropertyNode *overlay = 0);
61
62
63 SGReaderWriterXML::SGReaderWriterXML()
64 {
65     supportsExtension("xml", "SimGear xml database format");
66 }
67
68 SGReaderWriterXML::~SGReaderWriterXML()
69 {
70 }
71
72 const char* SGReaderWriterXML::className() const
73 {
74     return "XML database reader";
75 }
76
77 osgDB::ReaderWriter::ReadResult
78 SGReaderWriterXML::readNode(const std::string& fileName,
79                             const osgDB::ReaderWriter::Options* options) const
80 {
81     osg::Node *result=0;
82     try {
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());
86         result=new osg::Node;
87     }
88     if (result)
89         return result;
90     else
91         return ReadResult::FILE_NOT_HANDLED;
92 }
93
94 class SGSwitchUpdateCallback : public osg::NodeCallback
95 {
96 public:
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);
102
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.
108             traverse(node, nv);
109         } else
110             s->setAllChildrenOff();
111     }
112
113 private:
114     SGSharedPtr<SGCondition> mCondition;
115 };
116
117
118 // Little helper class that holds an extra reference to a
119 // loaded 3d model.
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.
127 namespace {
128 class SGDatabaseReference : public osg::Observer
129 {
130 public:
131     SGDatabaseReference(osg::Referenced* referenced) :
132         mReferenced(referenced)
133     { }
134     virtual void objectDeleted(void*)
135     {
136         mReferenced = 0;
137     }
138 private:
139     osg::ref_ptr<osg::Referenced> mReferenced;
140 };
141
142 void makeEffectAnimations(PropertyList& animation_nodes,
143                           PropertyList& effect_nodes)
144 {
145     for (PropertyList::iterator itr = animation_nodes.begin();
146          itr != animation_nodes.end();
147          ++itr) {
148         SGPropertyNode* animProp = itr->ptr();
149         SGPropertyNode* typeProp = animProp->getChild("type");
150         if (!typeProp || strcmp(typeProp->getStringValue(), "shader"))
151             continue;
152         SGPropertyNode* shaderProp = animProp->getChild("shader");
153         if (!shaderProp || strcmp(shaderProp->getStringValue(), "chrome"))
154             continue;
155         *itr = 0;
156         SGPropertyNode* textureProp = animProp->getChild("texture");
157         if (!textureProp)
158             continue;
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();
168              objItr != end;
169             ++objItr)
170             effectProp->addChild("object-name")
171                 ->setStringValue((*objItr)->getStringValue());
172         effect_nodes.push_back(effectProp);
173     }
174     animation_nodes.erase(remove_if(animation_nodes.begin(),
175                                     animation_nodes.end(),
176                                     !boost::bind(&SGPropertyNode_ptr::valid,
177                                                  _1)),
178                           animation_nodes.end());
179 }
180 }
181
182 static osg::Node *
183 sgLoad3DModel_internal(const string &path,
184                        const osgDB::ReaderWriter::Options* options_,
185                        SGPropertyNode *overlay)
186 {
187     const SGReaderWriterXMLOptions* xmlOptions;
188     xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
189
190     SGSharedPtr<SGPropertyNode> prop_root;
191     osg::Node *(*load_panel)(SGPropertyNode *)=0;
192     osg::ref_ptr<SGModelData> data;
193
194     if (xmlOptions) {
195         prop_root = xmlOptions->getPropRoot();
196         load_panel = xmlOptions->getLoadPanel();
197         data = xmlOptions->getModelData();
198     }
199     if (!prop_root) {
200         prop_root = new SGPropertyNode;
201     }
202
203     osgDB::FilePathList filePathList;
204     filePathList = osgDB::Registry::instance()->getDataFilePathList();
205     filePathList.push_front(std::string());
206
207     SGPath modelpath = osgDB::findFileInPath(path, filePathList);
208     if (modelpath.str().empty()) {
209         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
210         return 0;
211     }
212     SGPath texturepath = modelpath;
213
214     osg::ref_ptr<osg::Node> model;
215     osg::ref_ptr<osg::Group> group;
216     SGPropertyNode_ptr props = new SGPropertyNode;
217
218     // Check for an XML wrapper
219     if (modelpath.extension() == "xml") {
220        try {
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());
225             throw;
226         }
227         if (overlay)
228             copyProperties(overlay, props);
229
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"));
236             }
237         } else {
238             model = new osg::Node;
239         }
240
241         SGPropertyNode *mp = props->getNode("multiplay");
242         if (mp && prop_root && prop_root->getParent())
243             copyProperties(mp, prop_root);
244     } else {
245         SG_LOG(SG_INPUT, SG_DEBUG, "model without wrapper: "
246                << modelpath.str());
247     }
248
249     osg::ref_ptr<SGReaderWriterXMLOptions> options
250     = new SGReaderWriterXMLOptions(*options_);
251     options->setPropRoot(prop_root);
252     options->setLoadPanel(load_panel);
253
254     // Assume that textures are in
255     // the same location as the XML file.
256     if (!model) {
257         if (!texturepath.extension().empty())
258             texturepath = texturepath.dir();
259
260         options->setDatabasePath(texturepath.str());
261         osgDB::ReaderWriter::ReadResult modelResult
262             = osgDB::Registry::instance()->readNode(modelpath.str(),
263                                                     options.get());
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);
277
278         // Update liveries
279         TextureUpdateVisitor liveryUpdate(options->getDatabasePathList());
280         model->accept(liveryUpdate);
281
282         // Copy the userdata fields, still sharing the boundingvolumes,
283         // but introducing new data for velocities.
284         UserDataCopyVisitor userDataCopyVisitor;
285         model->accept(userDataCopyVisitor);
286     }
287     model->setName(modelpath.str());
288
289     bool needTransform=false;
290     // Set up the alignment node if needed
291     SGPropertyNode *offsets = props->getNode("offsets", false);
292     if (offsets) {
293         needTransform=true;
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,
299             osg::Vec3(0, 1, 0),
300             offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
301             osg::Vec3(1, 0, 0),
302             offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
303             osg::Vec3(0, 0, 1));
304
305         osg::Matrix tmat;
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;
311     }
312     if (!group) {
313         group = new osg::Group;
314     }
315     group->addChild(model.get());
316
317     // Load sub-models
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];
321
322         SGPath submodelpath;
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 ) );
329         } else {
330             submodelpath = submodelFileName;
331         }
332         osg::ref_ptr<SGReaderWriterXMLOptions> options;
333         options = new SGReaderWriterXMLOptions(*options_);
334         options->setPropRoot(prop_root);
335         options->setLoadPanel(load_panel);
336         try {
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());
341             throw;
342         }
343
344         osg::ref_ptr<osg::Node> submodel_final = submodel;
345         SGPropertyNode *offs = sub_props->getNode("offsets", false);
346         if (offs) {
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,
353                 osg::Vec3(0, 1, 0),
354                 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
355                 osg::Vec3(1, 0, 0),
356                 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
357                 osg::Vec3(0, 0, 1));
358
359             osg::Matrix tmat;
360             tmat.makeIdentity();
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;
367         }
368         submodel_final->setName(sub_props->getStringValue("name", ""));
369
370         SGPropertyNode *cond = sub_props->getNode("condition", false);
371         if (cond) {
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");
377         } else {
378             group->addChild(submodel_final.get());
379         }
380     } // end of submodel loading
381
382     if ( load_panel ) {
383         // Load panels
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());
391         }
392     }
393
394     std::vector<SGPropertyNode_ptr> particle_nodes;
395     particle_nodes = props->getChildren("particlesystem");
396     for (unsigned i = 0; i < particle_nodes.size(); ++i) {
397         if (i==0) {
398             if (!texturepath.extension().empty())
399                 texturepath = texturepath.dir();
400
401             options->setDatabasePath(texturepath.str());
402         }
403         group->addChild(Particles::appendParticles(particle_nodes[i],
404                         prop_root,
405                         options.get()));
406     }
407
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],
412                         prop_root,
413                         options.get()));
414     }
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);
419     {
420         ref_ptr<Node> modelWithEffects
421             = instantiateEffects(group.get(), effect_nodes, options.get());
422         group = static_cast<Group*>(modelWithEffects.get());
423     }
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,
427                              options.get());
428
429     if (!needTransform && group->getNumChildren() < 2) {
430         model = group->getChild(0);
431         group->removeChild(model.get());
432         if (data.valid())
433             data->modelLoaded(modelpath.str(), props, model.get());
434         return model.release();
435     }
436     if (data.valid())
437         data->modelLoaded(modelpath.str(), props, group.get());
438     if (props->hasChild("debug-outfile")) {
439         std::string outputfile = props->getStringValue("debug-outfile",
440                                  "debug-model.osg");
441         osgDB::writeNodeFile(*group, outputfile);
442     }
443
444     return group.release();
445 }
446