]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGReaderWriterXML.cxx
remove CopyPolicy from ModelRegistry
[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     }
197
198     osg::ref_ptr<SGReaderWriterXMLOptions> options
199     = new SGReaderWriterXMLOptions(*options_);
200     options->setPropRoot(prop_root);
201     options->setLoadPanel(load_panel);
202
203     // Assume that textures are in
204     // the same location as the XML file.
205     if (!model) {
206         if (!texturepath.extension().empty())
207             texturepath = texturepath.dir();
208
209         options->setDatabasePath(texturepath.str());
210         osg::Node* origModel
211             = osgDB::readNodeFile(modelpath.str(), options.get());
212
213         if (!origModel)
214             throw sg_io_exception("Failed to load 3D model",
215                                   sg_location(modelpath.str()));
216         model = copyModel(origModel);
217         // Add an extra reference to the model stored in the database.
218         // That is to avoid expiring the object from the cache even if
219         // it is still in use. Note that the object cache will think
220         // that a model is unused if the reference count is 1. If we
221         // clone all structural nodes here we need that extra
222         // reference to the original object
223         SGDatabaseReference* databaseReference;
224         databaseReference = new SGDatabaseReference(origModel);
225         model->addObserver(databaseReference);
226
227         // Update liveries
228         TextureUpdateVisitor liveryUpdate(options->getDatabasePathList());
229         model->accept(liveryUpdate);
230
231         // Copy the userdata fields, still sharing the boundingvolumes,
232         // but introducing new data for velocities.
233         UserDataCopyVisitor userDataCopyVisitor;
234         model->accept(userDataCopyVisitor);
235     }
236     model->setName(modelpath.str());
237
238     bool needTransform=false;
239     // Set up the alignment node if needed
240     SGPropertyNode *offsets = props->getNode("offsets", false);
241     if (offsets) {
242         needTransform=true;
243         osg::MatrixTransform *alignmainmodel = new osg::MatrixTransform;
244         alignmainmodel->setDataVariance(osg::Object::STATIC);
245         osg::Matrix res_matrix;
246         res_matrix.makeRotate(
247             offsets->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
248             osg::Vec3(0, 1, 0),
249             offsets->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
250             osg::Vec3(1, 0, 0),
251             offsets->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
252             osg::Vec3(0, 0, 1));
253
254         osg::Matrix tmat;
255         tmat.makeTranslate(offsets->getFloatValue("x-m", 0.0),
256                            offsets->getFloatValue("y-m", 0.0),
257                            offsets->getFloatValue("z-m", 0.0));
258         alignmainmodel->setMatrix(res_matrix*tmat);
259         group = alignmainmodel;
260     }
261     if (!group) {
262         group = new osg::Group;
263     }
264     group->addChild(model.get());
265
266     // Load sub-models
267     vector<SGPropertyNode_ptr> model_nodes = props->getChildren("model");
268     for (unsigned i = 0; i < model_nodes.size(); i++) {
269         SGPropertyNode_ptr sub_props = model_nodes[i];
270
271         SGPath submodelpath;
272         osg::ref_ptr<osg::Node> submodel;
273         string submodelFileName = sub_props->getStringValue("path");
274         if ( submodelFileName.size() > 2 && submodelFileName.substr( 0, 2 ) == "./" ) {
275             submodelpath = modelpath.dir();
276             submodelpath.append( submodelFileName.substr( 2 ) );
277         } else {
278             submodelpath = submodelFileName;
279         }
280         osg::ref_ptr<SGReaderWriterXMLOptions> options;
281         options = new SGReaderWriterXMLOptions(*options_);
282         options->setPropRoot(prop_root);
283         options->setLoadPanel(load_panel);
284         try {
285             submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(),
286                                               sub_props->getNode("overlay"));
287         } catch (const sg_throwable &t) {
288             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
289             throw;
290         }
291
292         osg::ref_ptr<osg::Node> submodel_final=submodel.get();
293         SGPropertyNode *offs = sub_props->getNode("offsets", false);
294         if (offs) {
295             osg::Matrix res_matrix;
296             osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
297             align->setDataVariance(osg::Object::STATIC);
298             res_matrix.makeIdentity();
299             res_matrix.makeRotate(
300                 offs->getDoubleValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
301                 osg::Vec3(0, 1, 0),
302                 offs->getDoubleValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
303                 osg::Vec3(1, 0, 0),
304                 offs->getDoubleValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
305                 osg::Vec3(0, 0, 1));
306
307             osg::Matrix tmat;
308             tmat.makeIdentity();
309             tmat.makeTranslate(offs->getDoubleValue("x-m", 0),
310                                offs->getDoubleValue("y-m", 0),
311                                offs->getDoubleValue("z-m", 0));
312             align->setMatrix(res_matrix*tmat);
313             align->addChild(submodel.get());
314             submodel_final=align.get();
315         }
316         submodel_final->setName(sub_props->getStringValue("name", ""));
317
318         SGPropertyNode *cond = sub_props->getNode("condition", false);
319         if (cond) {
320             osg::ref_ptr<osg::Switch> sw = new osg::Switch;
321             sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
322             group->addChild(sw.get());
323             sw->addChild(submodel_final.get());
324             sw->setName("submodel condition switch");
325         } else {
326             group->addChild(submodel_final.get());
327         }
328     } // end of submodel loading
329
330     if ( load_panel ) {
331         // Load panels
332         vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
333         for (unsigned i = 0; i < panel_nodes.size(); i++) {
334             SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
335             osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
336             if (panel_nodes[i]->hasValue("name"))
337                 panel->setName(panel_nodes[i]->getStringValue("name"));
338             group->addChild(panel.get());
339         }
340     }
341
342     std::vector<SGPropertyNode_ptr> particle_nodes;
343     particle_nodes = props->getChildren("particlesystem");
344     for (unsigned i = 0; i < particle_nodes.size(); ++i) {
345         if (i==0) {
346             if (!texturepath.extension().empty())
347                 texturepath = texturepath.dir();
348
349             options->setDatabasePath(texturepath.str());
350         }
351         group->addChild(Particles::appendParticles(particle_nodes[i],
352                         prop_root,
353                         options.get()));
354     }
355
356     std::vector<SGPropertyNode_ptr> text_nodes;
357     text_nodes = props->getChildren("text");
358     for (unsigned i = 0; i < text_nodes.size(); ++i) {
359         group->addChild(SGText::appendText(text_nodes[i],
360                         prop_root,
361                         options.get()));
362     }
363
364     std::vector<SGPropertyNode_ptr> animation_nodes;
365     animation_nodes = props->getChildren("animation");
366     for (unsigned i = 0; i < animation_nodes.size(); ++i)
367         /// OSGFIXME: duh, why not only model?????
368         SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
369                              options.get());
370
371     if (!needTransform && group->getNumChildren() < 2) {
372         model = group->getChild(0);
373         group->removeChild(model.get());
374         if (data.valid())
375             data->modelLoaded(modelpath.str(), props, model.get());
376         return model.release();
377     }
378     if (data.valid())
379         data->modelLoaded(modelpath.str(), props, group.get());
380     if (props->hasChild("debug-outfile")) {
381         std::string outputfile = props->getStringValue("debug-outfile",
382                                  "debug-model.osg");
383         osgDB::writeNodeFile(*group, outputfile);
384     }
385
386     return group.release();
387 }
388