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"
42 #include <simgear/math/SGMath.hxx>
45 using namespace simgear;
47 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
48 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
50 SGPropertyNode_ptr SGModelLib::static_propRoot;
51 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
53 ////////////////////////////////////////////////////////////////////////
54 // Implementation of SGModelLib.
55 ////////////////////////////////////////////////////////////////////////
56 void SGModelLib::init(const string &root_dir, SGPropertyNode* root)
58 osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
59 static_propRoot = root;
62 void SGModelLib::setPanelFunc(panel_func pf)
64 static_panelFunc = pf;
67 std::string SGModelLib::findDataFile(const std::string& file,
68 const osgDB::Options* opts,
73 SGPath p = ResourceManager::instance()->findPath(file, currentPath);
78 // finally hand on to standard OSG behaviour
79 return osgDB::findDataFile(file, opts);
82 SGModelLib::SGModelLib()
86 SGModelLib::~SGModelLib()
92 osg::Node* loadFile(const string& path, SGReaderWriterOptions* options)
95 using namespace osgDB;
96 if (boost::iends_with(path, ".ac"))
97 options->setInstantiateEffects(true);
98 ref_ptr<Node> model = readRefNodeFile(path, options);
102 return model.release();
107 SGModelLib::loadModel(const string &path,
108 SGPropertyNode *prop_root,
112 osg::ref_ptr<SGReaderWriterOptions> opt;
113 opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
114 opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
115 opt->setModelData(data);
118 opt->setLoadPanel(static_panelFunc);
121 osg::Node *n = loadFile(path, opt.get());
122 if (n && n->getName().empty())
123 n->setName("Direct loaded model \"" + path + "\"");
129 SGModelLib::loadDeferredModel(const string &path, SGPropertyNode *prop_root,
132 osg::ProxyNode* proxyNode = new osg::ProxyNode;
133 proxyNode->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER);
134 proxyNode->setFileName(0, path);
136 osg::ref_ptr<SGReaderWriterOptions> opt;
137 opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
138 opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
139 opt->setModelData(data);
140 opt->setLoadPanel(static_panelFunc);
141 if (SGPath(path).lower_extension() == "ac")
142 opt->setInstantiateEffects(true);
143 if (!prop_root || prop_root->getBoolValue("/sim/rendering/cache", true))
144 opt->setObjectCacheHint(osgDB::Options::CACHE_ALL);
146 opt->setObjectCacheHint(osgDB::Options::CACHE_NONE);
147 proxyNode->setDatabaseOptions(opt.get());
153 SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
156 osg::PagedLOD *plod = new osg::PagedLOD;
157 plod->setName("Paged LOD for \"" + path + "\"");
158 plod->setFileName(0, path);
159 plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
161 osg::ref_ptr<SGReaderWriterOptions> opt;
162 opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
163 opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
164 opt->setModelData(data);
165 opt->setLoadPanel(static_panelFunc);
166 if (SGPath(path).lower_extension() == "ac")
167 opt->setInstantiateEffects(true);
168 if (!prop_root || prop_root->getBoolValue("/sim/rendering/cache", true))
169 opt->setObjectCacheHint(osgDB::Options::CACHE_ALL);
171 opt->setObjectCacheHint(osgDB::Options::CACHE_NONE);
172 plod->setDatabaseOptions(opt.get());
176 // end of modellib.cxx