]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/modellib.cxx
Make return type from loadPagedModel explicit.
[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 <osg/PagedLOD>
25 #include <osg/ProxyNode>
26 #include <osgDB/ReadFile>
27 #include <osgDB/WriteFile>
28 #include <osgDB/Registry>
29
30 #include <simgear/constants.h>
31 #include <simgear/props/props.hxx>
32 #include <simgear/props/props_io.hxx>
33 #include <simgear/scene/model/model.hxx>
34 #include <simgear/scene/model/ModelRegistry.hxx>
35 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
36 #include <simgear/misc/ResourceManager.hxx>
37
38 #include "SGReaderWriterXML.hxx"
39
40 #include "modellib.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 SGPropertyNode_ptr SGModelLib::static_propRoot;
49 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
50
51 ////////////////////////////////////////////////////////////////////////
52 // Implementation of SGModelLib.
53 ////////////////////////////////////////////////////////////////////////
54 void SGModelLib::init(const string &root_dir, SGPropertyNode* root)
55 {
56     osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
57     static_propRoot = root;
58 }
59
60 void SGModelLib::resetPropertyRoot()
61 {
62     static_propRoot.clear();
63 }
64
65 void SGModelLib::setPanelFunc(panel_func pf)
66 {
67   static_panelFunc = pf;
68 }
69
70 std::string SGModelLib::findDataFile(const std::string& file, 
71   const osgDB::Options* opts,
72   SGPath currentPath)
73 {
74   if (file.empty())
75     return file;
76   SGPath p = ResourceManager::instance()->findPath(file, currentPath);
77   if (p.exists()) {
78     return p.str();
79   }
80
81   // finally hand on to standard OSG behaviour
82   return osgDB::findDataFile(file, opts);
83 }
84
85 SGModelLib::SGModelLib()
86 {
87 }
88
89 SGModelLib::~SGModelLib()
90 {
91 }
92
93 namespace
94 {
95 osg::Node* loadFile(const string& path, SGReaderWriterOptions* options)
96 {
97     using namespace osg;
98     using namespace osgDB;
99     if (boost::iends_with(path, ".ac"))
100         options->setInstantiateEffects(true);
101     ref_ptr<Node> model = readRefNodeFile(path, options);
102     if (!model)
103         return 0;
104     else
105      return model.release();
106 }
107 }
108
109 osg::Node*
110 SGModelLib::loadModel(const string &path,
111                        SGPropertyNode *prop_root,
112                        SGModelData *data,
113                        bool load2DPanels)
114 {
115     osg::ref_ptr<SGReaderWriterOptions> opt;
116     opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
117     opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
118     opt->setModelData(data);
119     
120     if (load2DPanels) {
121        opt->setLoadPanel(static_panelFunc);
122     }
123     
124     osg::Node *n = loadFile(path, opt.get());
125     if (n && n->getName().empty())
126         n->setName("Direct loaded model \"" + path + "\"");
127     return n;
128
129 }
130
131 osg::Node*
132 SGModelLib::loadDeferredModel(const string &path, SGPropertyNode *prop_root,
133                              SGModelData *data)
134 {
135     osg::ProxyNode* proxyNode = new osg::ProxyNode;
136     proxyNode->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER);
137     proxyNode->setFileName(0, path);
138
139     osg::ref_ptr<SGReaderWriterOptions> opt;
140     opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
141     opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
142     opt->setModelData(data);
143     opt->setLoadPanel(static_panelFunc);
144     if (SGPath(path).lower_extension() == "ac")
145         opt->setInstantiateEffects(true);
146     if (!prop_root || prop_root->getBoolValue("/sim/rendering/cache", true))
147         opt->setObjectCacheHint(osgDB::Options::CACHE_ALL);
148     else
149         opt->setObjectCacheHint(osgDB::Options::CACHE_NONE);
150     proxyNode->setDatabaseOptions(opt.get());
151
152     return proxyNode;
153 }
154
155 osg::PagedLOD*
156 SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
157                            SGModelData *data)
158 {
159     osg::PagedLOD *plod = new osg::PagedLOD;
160     plod->setName("Paged LOD for \"" + path + "\"");
161     plod->setFileName(0, path);
162     plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
163
164     osg::ref_ptr<SGReaderWriterOptions> opt;
165     opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
166     opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
167     opt->setModelData(data);
168     opt->setLoadPanel(static_panelFunc);
169     if (SGPath(path).lower_extension() == "ac")
170         opt->setInstantiateEffects(true);
171     if (!prop_root || prop_root->getBoolValue("/sim/rendering/cache", true))
172         opt->setObjectCacheHint(osgDB::Options::CACHE_ALL);
173     else
174         opt->setObjectCacheHint(osgDB::Options::CACHE_NONE);
175     plod->setDatabaseOptions(opt.get());
176     return plod;
177 }
178
179 // end of modellib.cxx