]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGReaderWriterXML.cxx
cleanup
[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
45 using namespace simgear;
46
47 static osg::Node *
48 sgLoad3DModel_internal(const std::string& path,
49                        const osgDB::ReaderWriter::Options* options,
50                        SGPropertyNode *overlay = 0);
51
52
53 SGReaderWriterXML::SGReaderWriterXML()
54 {
55     supportsExtension("xml", "SimGear xml database format");
56 }
57
58 SGReaderWriterXML::~SGReaderWriterXML()
59 {
60 }
61
62 const char* SGReaderWriterXML::className() const
63 {
64     return "XML database reader";
65 }
66
67 osgDB::ReaderWriter::ReadResult
68 SGReaderWriterXML::readNode(const std::string& fileName,
69                             const osgDB::ReaderWriter::Options* options) const
70 {
71     osg::Node *result=0;
72     try {
73         result=sgLoad3DModel_internal(fileName, options);
74     } catch (const sg_throwable &t) {
75         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage());
76         result=new osg::Node;
77     }
78     if (result)
79         return result;
80     else
81         return ReadResult::FILE_NOT_HANDLED;
82 }
83
84 class SGSwitchUpdateCallback : public osg::NodeCallback
85 {
86 public:
87     SGSwitchUpdateCallback(SGCondition* condition) :
88             mCondition(condition) {}
89     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
90         assert(dynamic_cast<osg::Switch*>(node));
91         osg::Switch* s = static_cast<osg::Switch*>(node);
92
93         if (mCondition && mCondition->test()) {
94             s->setAllChildrenOn();
95             // note, callback is responsible for scenegraph traversal so
96             // should always include call traverse(node,nv) to ensure
97             // that the rest of cullbacks and the scene graph are traversed.
98             traverse(node, nv);
99         } else
100             s->setAllChildrenOff();
101     }
102
103 private:
104     SGSharedPtr<SGCondition> mCondition;
105 };
106
107 static osg::Node *
108 sgLoad3DModel_internal(const string &path,
109                        const osgDB::ReaderWriter::Options* options_,
110                        SGPropertyNode *overlay)
111 {
112     const SGReaderWriterXMLOptions* xmlOptions;
113     xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
114
115     SGSharedPtr<SGPropertyNode> prop_root;
116     osg::Node *(*load_panel)(SGPropertyNode *)=0;
117     osg::ref_ptr<SGModelData> data;
118
119     if (xmlOptions) {
120         prop_root = xmlOptions->getPropRoot();
121         load_panel = xmlOptions->getLoadPanel();
122         data = xmlOptions->getModelData();
123     }
124     if (!prop_root) {
125         prop_root = new SGPropertyNode;
126     }
127
128     osgDB::FilePathList filePathList;
129     filePathList = osgDB::Registry::instance()->getDataFilePathList();
130     filePathList.push_front(std::string());
131
132     SGPath modelpath = osgDB::findFileInPath(path, filePathList);
133     if (modelpath.str().empty()) {
134         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
135         return 0;
136     }
137     SGPath texturepath = modelpath;
138
139     osg::ref_ptr<osg::Node> model;
140     osg::ref_ptr<osg::Group> group;
141     SGPropertyNode_ptr props = new SGPropertyNode;
142
143     // Check for an XML wrapper
144     if (modelpath.extension() == "xml") {
145        try {
146             readProperties(modelpath.str(), props);
147         } catch (const sg_throwable &t) {
148             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
149                    << t.getFormattedMessage());
150             throw;
151         }
152         if (overlay)
153             copyProperties(overlay, props);
154
155         if (props->hasValue("/path")) {
156             modelpath = modelpath.dir();
157             modelpath.append(props->getStringValue("/path"));
158             if (props->hasValue("/texture-path")) {
159                 texturepath = texturepath.dir();
160                 texturepath.append(props->getStringValue("/texture-path"));
161             }
162         } else {
163             model = new osg::Node;
164         }
165
166         SGPropertyNode *mp = props->getNode("multiplay");
167         if (mp && prop_root && prop_root->getParent())
168             copyProperties(mp, prop_root);
169     }
170
171     osg::ref_ptr<SGReaderWriterXMLOptions> options
172     = new SGReaderWriterXMLOptions(*options_);
173     options->setPropRoot(prop_root);
174     options->setLoadPanel(load_panel);
175
176     // Assume that textures are in
177     // the same location as the XML file.
178     if (!model) {
179         if (!texturepath.extension().empty())
180             texturepath = texturepath.dir();
181
182         options->setDatabasePath(texturepath.str());
183         model = osgDB::readNodeFile(modelpath.str(), options.get());
184         if (model == 0)
185             throw sg_io_exception("Failed to load 3D model",
186                                   sg_location(modelpath.str()));
187     }
188     model->setName(modelpath.str());
189
190     bool needTransform=false;
191     // Set up the alignment node if needed
192     SGPropertyNode *offsets = props->getNode("offsets", false);
193     if (offsets) {
194         needTransform=true;
195         osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
196         alignmainmodel->setDataVariance(osg::Object::STATIC);
197         osg::Matrix res_matrix;
198         res_matrix.makeRotate(
199             offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
200             osg::Vec3(0, 1, 0),
201             offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
202             osg::Vec3(1, 0, 0),
203             offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
204             osg::Vec3(0, 0, 1));
205
206         osg::Matrix tmat;
207         tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
208                            offsets->getFloatValue("y-m", 0.0),
209                            offsets->getFloatValue("z-m", 0.0));
210         alignmainmodel->setMatrix(res_matrix*tmat);
211         group = alignmainmodel;
212     }
213     if (!group) {
214         group = new osg::Group;
215     }
216     group->addChild(model.get());
217
218     // Load sub-models
219     vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
220     for (unsigned i = 0; i < model_nodes.size(); i++) {
221         SGPropertyNode_ptr sub_props = model_nodes[i];
222
223         SGPath submodelpath;
224         osg::ref_ptr<osg::Node> submodel;
225         string submodelFileName = sub_props->getStringValue("path");
226         if ( submodelFileName.size() > 2 && submodelFileName.substr( 0, 2 ) == "./" ) {
227             submodelpath = modelpath.dir();
228             submodelpath.append( submodelFileName.substr( 2 ) );
229         } else {
230             submodelpath = submodelFileName;
231         }
232         osg::ref_ptr<SGReaderWriterXMLOptions> options;
233         options = new SGReaderWriterXMLOptions(*options_);
234         options->setPropRoot(prop_root);
235         options->setLoadPanel(load_panel);
236         try {
237             submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(),
238                                               sub_props->getNode("overlay"));
239         } catch (const sg_throwable &t) {
240             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
241             throw;
242         }
243
244         osg::ref_ptr<osg::Node> submodel_final=submodel.get();
245         SGPropertyNode *offs = sub_props->getNode("offsets", false);
246         if (offs) {
247             osg::Matrix res_matrix;
248             osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
249             align->setDataVariance(osg::Object::STATIC);
250             res_matrix.makeIdentity();
251             res_matrix.makeRotate(
252                 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
253                 osg::Vec3(0, 1, 0),
254                 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
255                 osg::Vec3(1, 0, 0),
256                 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
257                 osg::Vec3(0, 0, 1));
258
259             osg::Matrix tmat;
260             tmat.makeIdentity();
261             tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
262                                offs->getDoubleValue("y-m", 0),
263                                offs->getDoubleValue("z-m", 0));
264             align->setMatrix(res_matrix*tmat);
265             align->addChild(submodel.get());
266             submodel_final=align.get();
267         }
268         submodel_final->setName(sub_props->getStringValue("name", ""));
269
270         SGPropertyNode *cond = sub_props->getNode("condition", false);
271         if (cond) {
272             osg::ref_ptr<osg::Switch> sw = new osg::Switch;
273             sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
274             group->addChild(sw.get());
275             sw->addChild(submodel_final.get());
276             sw->setName("submodel condition switch");
277         } else {
278             group->addChild(submodel_final.get());
279         }
280     } // end of submodel loading
281
282     if ( load_panel ) {
283         // Load panels
284         vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
285         for (unsigned i = 0; i < panel_nodes.size(); i++) {
286             SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
287             osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
288             if (panel_nodes[i]->hasValue("name"))
289                 panel->setName(panel_nodes[i]->getStringValue("name"));
290             group->addChild(panel.get());
291         }
292     }
293
294     std::vector<SGPropertyNode_ptr> particle_nodes;
295     particle_nodes = props->getChildren("particlesystem");
296     for (unsigned i = 0; i < particle_nodes.size(); ++i) {
297         if (i==0) {
298             if (!texturepath.extension().empty())
299                 texturepath = texturepath.dir();
300
301             options->setDatabasePath(texturepath.str());
302         }
303         group->addChild(Particles::appendParticles(particle_nodes[i],
304                         prop_root,
305                         options.get()));
306     }
307
308     std::vector<SGPropertyNode_ptr> animation_nodes;
309     animation_nodes = props->getChildren("animation");
310     for (unsigned i = 0; i < animation_nodes.size(); ++i)
311         /// OSGFIXME: duh, why not only model?????
312         SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
313                              options.get());
314
315     if (!needTransform && group->getNumChildren() < 2) {
316         model = group->getChild(0);
317         group->removeChild(model.get());
318         if (data.valid())
319             data->modelLoaded(modelpath.str(), props, model.get());
320         return model.release();
321     }
322     if (data.valid())
323         data->modelLoaded(modelpath.str(), props, group.get());
324     if (props->hasChild("debug-outfile")) {
325         std::string outputfile = props->getStringValue("debug-outfile",
326                                  "debug-model.osg");
327         osgDB::writeNodeFile(*group, outputfile);
328     }
329
330     return group.release();
331 }
332