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(*osgDB::Registry::instance()->getOptions());
174 // Assume that textures are in
175 // the same location as the XML file.
177 if (!texturepath.extension().empty())
178 texturepath = texturepath.dir();
180 options->setDatabasePath(texturepath.str());
181 model = osgDB::readNodeFile(modelpath.str(), options.get());
183 throw sg_io_exception("Failed to load 3D model",
184 sg_location(modelpath.str()));
186 model->setName(modelpath.str());
188 bool needTransform=false;
189 // Set up the alignment node if needed
190 SGPropertyNode *offsets = props->getNode("offsets", false);
193 osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
194 alignmainmodel->setDataVariance(osg::Object::STATIC);
195 osg::Matrix res_matrix;
196 res_matrix.makeRotate(
197 offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
199 offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
201 offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
205 tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
206 offsets->getFloatValue("y-m", 0.0),
207 offsets->getFloatValue("z-m", 0.0));
208 alignmainmodel->setMatrix(res_matrix*tmat);
209 group = alignmainmodel;
212 group = new osg::Group;
214 group->addChild(model.get());
217 vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
218 for (unsigned i = 0; i < model_nodes.size(); i++) {
219 SGPropertyNode_ptr sub_props = model_nodes[i];
222 osg::ref_ptr<osg::Node> submodel;
223 string submodelFileName = sub_props->getStringValue("path");
224 if ( submodelFileName.size() > 2 && submodelFileName.substr( 0, 2 ) == "./" ) {
225 submodelpath = modelpath.dir();
226 submodelpath.append( submodelFileName.substr( 2 ) );
228 submodelpath = submodelFileName;
230 osg::ref_ptr<SGReaderWriterXMLOptions> options;
231 options = new SGReaderWriterXMLOptions(*options_);
232 options->setPropRoot(prop_root);
234 submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(),
235 sub_props->getNode("overlay"));
236 } catch (const sg_throwable &t) {
237 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
241 osg::ref_ptr<osg::Node> submodel_final=submodel.get();
242 SGPropertyNode *offs = sub_props->getNode("offsets", false);
244 osg::Matrix res_matrix;
245 osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
246 align->setDataVariance(osg::Object::STATIC);
247 res_matrix.makeIdentity();
248 res_matrix.makeRotate(
249 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
251 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
253 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
258 tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
259 offs->getDoubleValue("y-m", 0),
260 offs->getDoubleValue("z-m", 0));
261 align->setMatrix(res_matrix*tmat);
262 align->addChild(submodel.get());
263 submodel_final=align.get();
265 submodel_final->setName(sub_props->getStringValue("name", ""));
267 SGPropertyNode *cond = sub_props->getNode("condition", false);
269 osg::ref_ptr<osg::Switch> sw = new osg::Switch;
270 sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
271 group->addChild(sw.get());
272 sw->addChild(submodel_final.get());
273 sw->setName("submodel condition switch");
275 group->addChild(submodel_final.get());
277 } // end of submodel loading
281 vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
282 for (unsigned i = 0; i < panel_nodes.size(); i++) {
283 SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
284 osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
285 if (panel_nodes[i]->hasValue("name"))
286 panel->setName(panel_nodes[i]->getStringValue("name"));
287 group->addChild(panel.get());
291 std::vector<SGPropertyNode_ptr> particle_nodes;
292 particle_nodes = props->getChildren("particlesystem");
293 for (unsigned i = 0; i < particle_nodes.size(); ++i) {
295 if (!texturepath.extension().empty())
296 texturepath = texturepath.dir();
298 options->setDatabasePath(texturepath.str());
300 group->addChild(Particles::appendParticles(particle_nodes[i],
305 std::vector<SGPropertyNode_ptr> animation_nodes;
306 animation_nodes = props->getChildren("animation");
307 for (unsigned i = 0; i < animation_nodes.size(); ++i)
308 /// OSGFIXME: duh, why not only model?????
309 SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
312 if (!needTransform && group->getNumChildren() < 2) {
313 model = group->getChild(0);
314 group->removeChild(model.get());
316 data->modelLoaded(modelpath.str(), props, model.get());
317 return model.release();
320 data->modelLoaded(modelpath.str(), props, group.get());
321 if (props->hasChild("debug-outfile")) {
322 std::string outputfile = props->getStringValue("debug-outfile",
324 osgDB::writeNodeFile(*group, outputfile);
327 return group.release();