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