]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGReaderWriterXML.cxx
2a4d612e6f2f485939dc79499b6a438993a430d4
[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 <osg/MatrixTransform>
24 #include <osgDB/WriteFile>
25 #include <osgDB/Registry>
26 #include <osg/Switch>
27 #include <osgDB/FileNameUtils>
28
29 #include <simgear/compiler.h>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/props/props.hxx>
32 #include <simgear/props/props_io.hxx>
33 #include <simgear/props/condition.hxx>
34 #include <simgear/scene/util/SGNodeMasks.hxx>
35
36 #include "modellib.hxx"
37 #include "SGPagedLOD.hxx"
38 #include "SGReaderWriterXML.hxx"
39 #include "SGReaderWriterXMLOptions.hxx"
40
41 #include "animation.hxx"
42 #include "particles.hxx"
43 #include "model.hxx"
44 #include "SGText.hxx"
45
46 using namespace simgear;
47
48 static osg::Node *
49 sgLoad3DModel_internal(const std::string& path,
50                        const osgDB::ReaderWriter::Options* options,
51                        SGPropertyNode *overlay = 0);
52
53
54 SGReaderWriterXML::SGReaderWriterXML()
55 {
56     supportsExtension("xml", "SimGear xml database format");
57 }
58
59 SGReaderWriterXML::~SGReaderWriterXML()
60 {
61 }
62
63 const char* SGReaderWriterXML::className() const
64 {
65     return "XML database reader";
66 }
67
68 osgDB::ReaderWriter::ReadResult
69 SGReaderWriterXML::readNode(const std::string& fileName,
70                             const osgDB::ReaderWriter::Options* options) const
71 {
72     osg::Node *result=0;
73     try {
74         result=sgLoad3DModel_internal(fileName, options);
75     } catch (const sg_throwable &t) {
76         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage());
77         result=new osg::Node;
78     }
79     if (result)
80         return result;
81     else
82         return ReadResult::FILE_NOT_HANDLED;
83 }
84
85 class SGSwitchUpdateCallback : public osg::NodeCallback
86 {
87 public:
88     SGSwitchUpdateCallback(SGCondition* condition) :
89             mCondition(condition) {}
90     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
91         assert(dynamic_cast<osg::Switch*>(node));
92         osg::Switch* s = static_cast<osg::Switch*>(node);
93
94         if (mCondition && mCondition->test()) {
95             s->setAllChildrenOn();
96             // note, callback is responsible for scenegraph traversal so
97             // should always include call traverse(node,nv) to ensure
98             // that the rest of cullbacks and the scene graph are traversed.
99             traverse(node, nv);
100         } else
101             s->setAllChildrenOff();
102     }
103
104 private:
105     SGSharedPtr<SGCondition> mCondition;
106 };
107
108
109 // Little helper class that holds an extra reference to a
110 // loaded 3d model.
111 // Since we clone all structural nodes from our 3d models,
112 // the database pager will only see one single reference to
113 // top node of the model and expire it relatively fast.
114 // We attach that extra reference to every model cloned from
115 // a base model in the pager. When that cloned model is deleted
116 // this extra reference is deleted too. So if there are no
117 // cloned models left the model will expire.
118 namespace {
119 class SGDatabaseReference : public osg::Observer
120 {
121 public:
122     SGDatabaseReference(osg::Referenced* referenced) :
123         mReferenced(referenced)
124     { }
125     virtual void objectDeleted(void*)
126     {
127         mReferenced = 0;
128     }
129 private:
130     osg::ref_ptr<osg::Referenced> mReferenced;
131 };
132 }
133
134 static osg::Node *
135 sgLoad3DModel_internal(const string &path,
136                        const osgDB::ReaderWriter::Options* options_,
137                        SGPropertyNode *overlay)
138 {
139     const SGReaderWriterXMLOptions* xmlOptions;
140     xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
141
142     SGSharedPtr<SGPropertyNode> prop_root;
143     osg::Node *(*load_panel)(SGPropertyNode *)=0;
144     osg::ref_ptr<SGModelData> data;
145
146     if (xmlOptions) {
147         prop_root = xmlOptions->getPropRoot();
148         load_panel = xmlOptions->getLoadPanel();
149         data = xmlOptions->getModelData();
150     }
151     if (!prop_root) {
152         prop_root = new SGPropertyNode;
153     }
154
155     osgDB::FilePathList filePathList;
156     filePathList = osgDB::Registry::instance()->getDataFilePathList();
157     filePathList.push_front(std::string());
158
159     SGPath modelpath = osgDB::findFileInPath(path, filePathList);
160     if (modelpath.str().empty()) {
161         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
162         return 0;
163     }
164     SGPath texturepath = modelpath;
165
166     osg::ref_ptr<osg::Node> model;
167     osg::ref_ptr<osg::Group> group;
168     SGPropertyNode_ptr props = new SGPropertyNode;
169
170     // Check for an XML wrapper
171     if (modelpath.extension() == "xml") {
172        try {
173             readProperties(modelpath.str(), props);
174         } catch (const sg_throwable &t) {
175             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
176                    << t.getFormattedMessage());
177             throw;
178         }
179         if (overlay)
180             copyProperties(overlay, props);
181
182         if (props->hasValue("/path")) {
183             modelpath = modelpath.dir();
184             modelpath.append(props->getStringValue("/path"));
185             if (props->hasValue("/texture-path")) {
186                 texturepath = texturepath.dir();
187                 texturepath.append(props->getStringValue("/texture-path"));
188             }
189         } else {
190             model = new osg::Node;
191         }
192
193         SGPropertyNode *mp = props->getNode("multiplay");
194         if (mp && prop_root && prop_root->getParent())
195             copyProperties(mp, prop_root);
196     } else {
197         SG_LOG(SG_INPUT, SG_DEBUG, "model without wrapper: "
198                << modelpath.str());
199     }
200
201     osg::ref_ptr<SGReaderWriterXMLOptions> options
202     = new SGReaderWriterXMLOptions(*options_);
203     options->setPropRoot(prop_root);
204     options->setLoadPanel(load_panel);
205
206     // Assume that textures are in
207     // the same location as the XML file.
208     if (!model) {
209         if (!texturepath.extension().empty())
210             texturepath = texturepath.dir();
211
212         options->setDatabasePath(texturepath.str());
213         osg::Node* origModel
214             = osgDB::readNodeFile(modelpath.str(), options.get());
215
216         if (!origModel)
217             throw sg_io_exception("Failed to load 3D model",
218                                   sg_location(modelpath.str()));
219         model = copyModel(origModel);
220         // Add an extra reference to the model stored in the database.
221         // That is to avoid expiring the object from the cache even if
222         // it is still in use. Note that the object cache will think
223         // that a model is unused if the reference count is 1. If we
224         // clone all structural nodes here we need that extra
225         // reference to the original object
226         SGDatabaseReference* databaseReference;
227         databaseReference = new SGDatabaseReference(origModel);
228         model->addObserver(databaseReference);
229
230         // Update liveries
231         TextureUpdateVisitor liveryUpdate(options->getDatabasePathList());
232         model->accept(liveryUpdate);
233
234         // Copy the userdata fields, still sharing the boundingvolumes,
235         // but introducing new data for velocities.
236         UserDataCopyVisitor userDataCopyVisitor;
237         model->accept(userDataCopyVisitor);
238     }
239     model->setName(modelpath.str());
240
241     bool needTransform=false;
242     // Set up the alignment node if needed
243     SGPropertyNode *offsets = props->getNode("offsets", false);
244     if (offsets) {
245         needTransform=true;
246         osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
247         alignmainmodel->setDataVariance(osg::Object::STATIC);
248         osg::Matrix res_matrix;
249         res_matrix.makeRotate(
250             offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
251             osg::Vec3(0, 1, 0),
252             offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
253             osg::Vec3(1, 0, 0),
254             offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
255             osg::Vec3(0, 0, 1));
256
257         osg::Matrix tmat;
258         tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
259                            offsets->getFloatValue("y-m", 0.0),
260                            offsets->getFloatValue("z-m", 0.0));
261         alignmainmodel->setMatrix(res_matrix*tmat);
262         group = alignmainmodel;
263     }
264     if (!group) {
265         group = new osg::Group;
266     }
267     group->addChild(model.get());
268
269     // Load sub-models
270     vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
271     for (unsigned i = 0; i < model_nodes.size(); i++) {
272         SGPropertyNode_ptr sub_props = model_nodes[i];
273
274         SGPath submodelpath;
275         osg::ref_ptr<osg::Node> submodel;
276         string submodelFileName = sub_props->getStringValue("path");
277         if ( submodelFileName.size() > 2 && submodelFileName.substr( 0, 2 ) == "./" ) {
278             submodelpath = modelpath.dir();
279             submodelpath.append( submodelFileName.substr( 2 ) );
280         } else {
281             submodelpath = submodelFileName;
282         }
283         osg::ref_ptr<SGReaderWriterXMLOptions> options;
284         options = new SGReaderWriterXMLOptions(*options_);
285         options->setPropRoot(prop_root);
286         options->setLoadPanel(load_panel);
287         try {
288             submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(),
289                                               sub_props->getNode("overlay"));
290         } catch (const sg_throwable &t) {
291             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
292             throw;
293         }
294
295         osg::ref_ptr<osg::Node> submodel_final=submodel.get();
296         SGPropertyNode *offs = sub_props->getNode("offsets", false);
297         if (offs) {
298             osg::Matrix res_matrix;
299             osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
300             align->setDataVariance(osg::Object::STATIC);
301             res_matrix.makeIdentity();
302             res_matrix.makeRotate(
303                 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
304                 osg::Vec3(0, 1, 0),
305                 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
306                 osg::Vec3(1, 0, 0),
307                 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
308                 osg::Vec3(0, 0, 1));
309
310             osg::Matrix tmat;
311             tmat.makeIdentity();
312             tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
313                                offs->getDoubleValue("y-m", 0),
314                                offs->getDoubleValue("z-m", 0));
315             align->setMatrix(res_matrix*tmat);
316             align->addChild(submodel.get());
317             submodel_final=align.get();
318         }
319         submodel_final->setName(sub_props->getStringValue("name", ""));
320
321         SGPropertyNode *cond = sub_props->getNode("condition", false);
322         if (cond) {
323             osg::ref_ptr<osg::Switch> sw = new osg::Switch;
324             sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
325             group->addChild(sw.get());
326             sw->addChild(submodel_final.get());
327             sw->setName("submodel condition switch");
328         } else {
329             group->addChild(submodel_final.get());
330         }
331     } // end of submodel loading
332
333     if ( load_panel ) {
334         // Load panels
335         vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
336         for (unsigned i = 0; i < panel_nodes.size(); i++) {
337             SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
338             osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
339             if (panel_nodes[i]->hasValue("name"))
340                 panel->setName(panel_nodes[i]->getStringValue("name"));
341             group->addChild(panel.get());
342         }
343     }
344
345     std::vector<SGPropertyNode_ptr> particle_nodes;
346     particle_nodes = props->getChildren("particlesystem");
347     for (unsigned i = 0; i < particle_nodes.size(); ++i) {
348         if (i==0) {
349             if (!texturepath.extension().empty())
350                 texturepath = texturepath.dir();
351
352             options->setDatabasePath(texturepath.str());
353         }
354         group->addChild(Particles::appendParticles(particle_nodes[i],
355                         prop_root,
356                         options.get()));
357     }
358
359     std::vector<SGPropertyNode_ptr> text_nodes;
360     text_nodes = props->getChildren("text");
361     for (unsigned i = 0; i < text_nodes.size(); ++i) {
362         group->addChild(SGText::appendText(text_nodes[i],
363                         prop_root,
364                         options.get()));
365     }
366
367     std::vector<SGPropertyNode_ptr> animation_nodes;
368     animation_nodes = props->getChildren("animation");
369     for (unsigned i = 0; i < animation_nodes.size(); ++i)
370         /// OSGFIXME: duh, why not only model?????
371         SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
372                              options.get());
373
374     if (!needTransform && group->getNumChildren() < 2) {
375         model = group->getChild(0);
376         group->removeChild(model.get());
377         if (data.valid())
378             data->modelLoaded(modelpath.str(), props, model.get());
379         return model.release();
380     }
381     if (data.valid())
382         data->modelLoaded(modelpath.str(), props, group.get());
383     if (props->hasChild("debug-outfile")) {
384         std::string outputfile = props->getStringValue("debug-outfile",
385                                  "debug-model.osg");
386         osgDB::writeNodeFile(*group, outputfile);
387     }
388
389     return group.release();
390 }
391