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