1 // Copyright (C) 2008 Till Busch buti@bux.at
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.
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.
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.
19 # include <simgear_config.h>
22 #include <boost/algorithm/string.hpp>
24 #include <osg/PagedLOD>
25 #include <osg/ProxyNode>
26 #include <osgDB/ReadFile>
27 #include <osgDB/WriteFile>
28 #include <osgDB/Registry>
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>
38 #include "SGReaderWriterXML.hxx"
40 #include "modellib.hxx"
43 using namespace simgear;
45 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
46 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
48 SGPropertyNode_ptr SGModelLib::static_propRoot;
49 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
51 ////////////////////////////////////////////////////////////////////////
52 // Implementation of SGModelLib.
53 ////////////////////////////////////////////////////////////////////////
54 void SGModelLib::init(const string &root_dir, SGPropertyNode* root)
56 osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
57 static_propRoot = root;
60 void SGModelLib::setPanelFunc(panel_func pf)
62 static_panelFunc = pf;
65 std::string SGModelLib::findDataFile(const std::string& file,
66 const osgDB::Options* opts,
71 SGPath p = ResourceManager::instance()->findPath(file, currentPath);
76 // finally hand on to standard OSG behaviour
77 return osgDB::findDataFile(file, opts);
80 SGModelLib::SGModelLib()
84 SGModelLib::~SGModelLib()
90 osg::Node* loadFile(const string& path, SGReaderWriterOptions* options)
93 using namespace osgDB;
94 if (boost::iends_with(path, ".ac"))
95 options->setInstantiateEffects(true);
96 ref_ptr<Node> model = readRefNodeFile(path, options);
100 return model.release();
105 SGModelLib::loadModel(const string &path,
106 SGPropertyNode *prop_root,
110 osg::ref_ptr<SGReaderWriterOptions> opt;
111 opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
112 opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
113 opt->setModelData(data);
116 opt->setLoadPanel(static_panelFunc);
119 osg::Node *n = loadFile(path, opt.get());
120 if (n && n->getName().empty())
121 n->setName("Direct loaded model \"" + path + "\"");
127 SGModelLib::loadDeferredModel(const string &path, SGPropertyNode *prop_root,
130 osg::ProxyNode* proxyNode = new osg::ProxyNode;
131 proxyNode->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER);
132 proxyNode->setFileName(0, path);
134 osg::ref_ptr<SGReaderWriterOptions> opt;
135 opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
136 opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
137 opt->setModelData(data);
138 opt->setLoadPanel(static_panelFunc);
139 if (SGPath(path).lower_extension() == "ac")
140 opt->setInstantiateEffects(true);
141 if (!prop_root || prop_root->getBoolValue("/sim/rendering/cache", true))
142 opt->setObjectCacheHint(osgDB::Options::CACHE_ALL);
144 opt->setObjectCacheHint(osgDB::Options::CACHE_NONE);
145 proxyNode->setDatabaseOptions(opt.get());
151 SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
154 osg::PagedLOD *plod = new osg::PagedLOD;
155 plod->setName("Paged LOD for \"" + path + "\"");
156 plod->setFileName(0, path);
157 plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
159 osg::ref_ptr<SGReaderWriterOptions> opt;
160 opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
161 opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
162 opt->setModelData(data);
163 opt->setLoadPanel(static_panelFunc);
164 if (SGPath(path).lower_extension() == "ac")
165 opt->setInstantiateEffects(true);
166 if (!prop_root || prop_root->getBoolValue("/sim/rendering/cache", true))
167 opt->setObjectCacheHint(osgDB::Options::CACHE_ALL);
169 opt->setObjectCacheHint(osgDB::Options::CACHE_NONE);
170 plod->setDatabaseOptions(opt.get());
174 // end of modellib.cxx