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");
49 ////////////////////////////////////////////////////////////////////////
50 // Implementation of SGModelLib.
51 ////////////////////////////////////////////////////////////////////////
52 void SGModelLib::init(const string &root_dir)
54 osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
57 SGModelLib::SGModelLib()
61 SGModelLib::~SGModelLib()
67 osg::Node* loadFile(const string& path, osgDB::ReaderWriter::Options* options)
70 using namespace osgDB;
71 ref_ptr<Node> model = readRefNodeFile(path, options);
74 if (boost::iends_with(path, ".ac")) {
75 ref_ptr<SGReaderWriterXMLOptions> sgOptions;
77 sgOptions = new SGReaderWriterXMLOptions(*options);
78 model = instantiateEffects(model.get(), sgOptions.get());
80 return model.release();
85 SGModelLib::loadModel(const string &path,
86 SGPropertyNode *prop_root,
89 osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
90 opt->setPropRoot(prop_root);
91 opt->setModelData(data);
92 osg::Node *n = loadFile(path, opt.get());
93 if (n && n->getName().empty())
94 n->setName("Direct loaded model \"" + path + "\"");
100 SGModelLib::loadModel(const string &path,
101 SGPropertyNode *prop_root,
104 osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
105 opt->setPropRoot(prop_root);
106 opt->setLoadPanel(pf);
107 return loadFile(path, opt.get());
111 SGModelLib::loadPagedModel(const string &path,
112 SGPropertyNode *prop_root,
115 SGPagedLOD *plod = new SGPagedLOD;
116 plod->setName("Paged LOD for \"" + path + "\"");
117 plod->setFileName(0, path);
118 plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
120 osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
121 opt->setPropRoot(prop_root);
122 opt->setModelData(data);
123 plod->setReaderWriterOptions(opt.get());
127 // end of modellib.cxx