1 // Copyright (C) 2007 Tim Moore timoore@redhat.com
2 // Copyright (C) 2008 Till Busch buti@bux.at
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.
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.
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.
20 # include <simgear_config.h>
23 #include <osg/MatrixTransform>
24 #include <osgDB/WriteFile>
25 #include <osgDB/Registry>
27 #include <osgDB/FileNameUtils>
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>
36 #include "modellib.hxx"
37 #include "SGPagedLOD.hxx"
38 #include "SGReaderWriterXML.hxx"
39 #include "SGReaderWriterXMLOptions.hxx"
41 #include "animation.hxx"
42 #include "particles.hxx"
45 using namespace simgear;
48 sgLoad3DModel_internal(const std::string& path,
49 const osgDB::ReaderWriter::Options* options,
50 SGPropertyNode *overlay = 0);
53 SGReaderWriterXML::SGReaderWriterXML()
55 supportsExtension("xml", "SimGear xml database format");
58 SGReaderWriterXML::~SGReaderWriterXML()
62 const char* SGReaderWriterXML::className() const
64 return "XML database reader";
67 osgDB::ReaderWriter::ReadResult
68 SGReaderWriterXML::readNode(const std::string& fileName,
69 const osgDB::ReaderWriter::Options* options) const
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());
81 return ReadResult::FILE_NOT_HANDLED;
84 class SGSwitchUpdateCallback : public osg::NodeCallback
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);
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.
100 s->setAllChildrenOff();
104 SGSharedPtr<SGCondition> mCondition;
108 sgLoad3DModel_internal(const string &path,
109 const osgDB::ReaderWriter::Options* options_,
110 SGPropertyNode *overlay)
112 const SGReaderWriterXMLOptions* xmlOptions;
113 xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
115 SGSharedPtr<SGPropertyNode> prop_root;
116 osg::Node *(*load_panel)(SGPropertyNode *)=0;
117 osg::ref_ptr<SGModelData> data;
120 prop_root = xmlOptions->getPropRoot();
121 load_panel = xmlOptions->getLoadPanel();
122 data = xmlOptions->getModelData();
125 prop_root = new SGPropertyNode;
128 osgDB::FilePathList filePathList;
129 filePathList = osgDB::Registry::instance()->getDataFilePathList();
130 filePathList.push_front(std::string());
132 SGPath modelpath = osgDB::findFileInPath(path, filePathList);
133 if (modelpath.str().empty()) {
134 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
137 SGPath texturepath = modelpath;
139 osg::ref_ptr<osg::Node> model;
140 osg::ref_ptr<osg::Group> group;
141 SGPropertyNode_ptr props = new SGPropertyNode;
143 // Check for an XML wrapper
144 if (modelpath.extension() == "xml") {
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());
153 copyProperties(overlay, props);
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"));
163 model = new osg::Node;
166 SGPropertyNode *mp = props->getNode("multiplay");
167 if (mp && prop_root && prop_root->getParent())
168 copyProperties(mp, prop_root);
171 osg::ref_ptr<SGReaderWriterXMLOptions> options
172 = new SGReaderWriterXMLOptions(*options_);
173 options->setPropRoot(prop_root);
174 options->setLoadPanel(load_panel);
176 // Assume that textures are in
177 // the same location as the XML file.
179 if (!texturepath.extension().empty())
180 texturepath = texturepath.dir();
182 options->setDatabasePath(texturepath.str());
183 model = osgDB::readNodeFile(modelpath.str(), options.get());
185 throw sg_io_exception("Failed to load 3D model",
186 sg_location(modelpath.str()));
188 model->setName(modelpath.str());
190 bool needTransform=false;
191 // Set up the alignment node if needed
192 SGPropertyNode *offsets = props->getNode("offsets", false);
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,
201 offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
203 offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
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;
214 group = new osg::Group;
216 group->addChild(model.get());
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];
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 ) );
230 submodelpath = submodelFileName;
232 osg::ref_ptr<SGReaderWriterXMLOptions> options;
233 options = new SGReaderWriterXMLOptions(*options_);
234 options->setPropRoot(prop_root);
235 options->setLoadPanel(load_panel);
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());
244 osg::ref_ptr<osg::Node> submodel_final=submodel.get();
245 SGPropertyNode *offs = sub_props->getNode("offsets", false);
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,
254 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
256 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
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();
268 submodel_final->setName(sub_props->getStringValue("name", ""));
270 SGPropertyNode *cond = sub_props->getNode("condition", false);
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");
278 group->addChild(submodel_final.get());
280 } // end of submodel loading
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());
294 std::vector<SGPropertyNode_ptr> particle_nodes;
295 particle_nodes = props->getChildren("particlesystem");
296 for (unsigned i = 0; i < particle_nodes.size(); ++i) {
298 if (!texturepath.extension().empty())
299 texturepath = texturepath.dir();
301 options->setDatabasePath(texturepath.str());
303 group->addChild(Particles::appendParticles(particle_nodes[i],
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,
315 if (!needTransform && group->getNumChildren() < 2) {
316 model = group->getChild(0);
317 group->removeChild(model.get());
319 data->modelLoaded(modelpath.str(), props, model.get());
320 return model.release();
323 data->modelLoaded(modelpath.str(), props, group.get());
324 if (props->hasChild("debug-outfile")) {
325 std::string outputfile = props->getStringValue("debug-outfile",
327 osgDB::writeNodeFile(*group, outputfile);
330 return group.release();