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