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 <osgDB/ReadFile>
25 #include <osgDB/WriteFile>
26 #include <osgDB/Registry>
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>
34 #include "SGPagedLOD.hxx"
35 #include "SGReaderWriterXML.hxx"
36 #include "SGReaderWriterXMLOptions.hxx"
38 #include "modellib.hxx"
40 #include <simgear/math/SGMath.hxx>
43 using namespace simgear;
45 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
46 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
48 \fSGPropertyNode_ptr SGModelLib::static_propRoot;
49 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
50 SGModelLib::resolve_func SGModelLib::static_resolver = NULL;
52 ////////////////////////////////////////////////////////////////////////
53 // Implementation of SGModelLib.
54 ////////////////////////////////////////////////////////////////////////
55 void SGModelLib::init(const string &root_dir)
57 osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
60 void SGModelLib::setPropRoot(SGPropertyNode* root)
62 static_propRoot = root;
65 void SGModelLib::setPanelFunc(panel_func pf)
67 static_panelFunc = pf;
70 void SGModelLib::setResolveFunc(resolve_func rf)
75 std::string SGModelLib::findDataFile(const std::string& file,
76 const osgDB::ReaderWriter::Options* opts)
78 if (static_resolver) {
79 SGPath p = static_resolver(file);
85 return osgDB::findDataFile(file, opts);
88 SGModelLib::SGModelLib()
92 SGModelLib::~SGModelLib()
98 osg::Node* loadFile(const string& path, SGReaderWriterXMLOptions* options)
101 using namespace osgDB;
102 if (boost::iends_with(path, ".ac"))
103 options->setInstantiateEffects(true);
104 ref_ptr<Node> model = readRefNodeFile(path, options);
108 return model.release();
113 SGModelLib::loadModel(const string &path,
114 SGPropertyNode *prop_root,
117 osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
118 opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
119 opt->setModelData(data);
120 opt->setLoadPanel(static_panelFunc);
122 osg::Node *n = loadFile(path, opt.get());
123 if (n && n->getName().empty())
124 n->setName("Direct loaded model \"" + path + "\"");
130 SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
133 SGPagedLOD *plod = new SGPagedLOD;
134 plod->setName("Paged LOD for \"" + path + "\"");
135 plod->setFileName(0, path);
136 plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
138 osg::ref_ptr<SGReaderWriterXMLOptions> opt
139 = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()
141 opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
142 opt->setModelData(data);
143 opt->setLoadPanel(static_panelFunc);
144 if (boost::iends_with(path, ".ac"))
145 opt->setInstantiateEffects(true);
146 plod->setReaderWriterOptions(opt.get());
150 // end of modellib.cxx