]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/modellib.cxx
Standardise path-handling in XML mode files
[simgear.git] / simgear / scene / model / modellib.cxx
1 // Copyright (C) 2008 Till Busch buti@bux.at
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include <boost/algorithm/string.hpp>
23
24 #include <osgDB/ReadFile>
25 #include <osgDB/WriteFile>
26 #include <osgDB/Registry>
27
28 #include <simgear/constants.h>
29 #include <simgear/props/props.hxx>
30 #include <simgear/props/props_io.hxx>
31 #include <simgear/scene/model/model.hxx>
32 #include <simgear/scene/model/ModelRegistry.hxx>
33
34 #include "SGPagedLOD.hxx"
35 #include "SGReaderWriterXML.hxx"
36 #include "SGReaderWriterXMLOptions.hxx"
37
38 #include "modellib.hxx"
39
40 #include <simgear/math/SGMath.hxx>
41
42 using std::string;
43 using namespace simgear;
44
45 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
46 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
47
48 \fSGPropertyNode_ptr SGModelLib::static_propRoot;
49 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
50 SGModelLib::resolve_func SGModelLib::static_resolver = NULL;
51
52 ////////////////////////////////////////////////////////////////////////
53 // Implementation of SGModelLib.
54 ////////////////////////////////////////////////////////////////////////
55 void SGModelLib::init(const string &root_dir)
56 {
57     osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
58 }
59
60 void SGModelLib::setPropRoot(SGPropertyNode* root)
61 {
62   static_propRoot = root;
63 }
64     
65 void SGModelLib::setPanelFunc(panel_func pf)
66 {
67   static_panelFunc = pf;
68 }
69
70 void SGModelLib::setResolveFunc(resolve_func rf)
71 {
72   static_resolver = rf;
73 }
74
75 std::string SGModelLib::findDataFile(const std::string& file, 
76   const osgDB::ReaderWriter::Options* opts,
77   SGPath currentPath)
78 {
79   // if we have a valid current path, first attempt to resolve relative
80   // to that path
81   if (currentPath.exists()) {
82     SGPath p = currentPath;
83     p.append(file);
84     if (p.exists()) {
85       return p.str();
86     }
87   }
88   
89   // next try the resolve function if one has been defined
90   if (static_resolver) {
91     SGPath p = static_resolver(file);
92     if (p.exists()) {
93       return p.str();
94     }
95   }
96   
97   // finally hand on to standard OSG behaviour
98   return osgDB::findDataFile(file, opts);
99 }
100
101 SGModelLib::SGModelLib()
102 {
103 }
104
105 SGModelLib::~SGModelLib()
106 {
107 }
108
109 namespace
110 {
111 osg::Node* loadFile(const string& path, SGReaderWriterXMLOptions* options)
112 {
113     using namespace osg;
114     using namespace osgDB;
115     if (boost::iends_with(path, ".ac"))
116         options->setInstantiateEffects(true);
117     ref_ptr<Node> model = readRefNodeFile(path, options);
118     if (!model)
119         return 0;
120     else
121      return model.release();
122 }
123 }
124
125 osg::Node*
126 SGModelLib::loadModel(const string &path,
127                        SGPropertyNode *prop_root,
128                        SGModelData *data,
129                        bool load2DPanels)
130 {
131     osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
132     opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
133     opt->setModelData(data);
134     
135     if (load2DPanels) {
136        opt->setLoadPanel(static_panelFunc);
137     }
138     
139     osg::Node *n = loadFile(path, opt.get());
140     if (n && n->getName().empty())
141         n->setName("Direct loaded model \"" + path + "\"");
142     return n;
143
144 }
145
146 osg::Node*
147 SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
148                            SGModelData *data)
149 {
150     SGPagedLOD *plod = new SGPagedLOD;
151     plod->setName("Paged LOD for \"" + path + "\"");
152     plod->setFileName(0, path);
153     plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
154
155     osg::ref_ptr<SGReaderWriterXMLOptions> opt
156         = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()
157                                          ->getOptions()));
158     opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
159     opt->setModelData(data);
160     opt->setLoadPanel(static_panelFunc);
161     if (boost::iends_with(path, ".ac"))
162         opt->setInstantiateEffects(true);
163     plod->setReaderWriterOptions(opt.get());
164     return plod;
165 }
166
167 // end of modellib.cxx