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