]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/modellib.cxx
6a13c516fdfbe5a10a15f870665d823a6be70535
[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 <osgDB/ReadFile>
25 #include <osgDB/WriteFile>
26 #include <osgDB/Registry>
27
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>
33
34 #include "SGPagedLOD.hxx"
35 #include "SGReaderWriterXML.hxx"
36 #include "SGReaderWriterXMLOptions.hxx"
37
38 #include "modellib.hxx"
39
40 #include <simgear/math/SGMath.hxx>
41
42
43 using namespace simgear;
44
45 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
46 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
47
48 \f
49 ////////////////////////////////////////////////////////////////////////
50 // Implementation of SGModelLib.
51 ////////////////////////////////////////////////////////////////////////
52 void SGModelLib::init(const string &root_dir)
53 {
54     osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
55 }
56
57 SGModelLib::SGModelLib()
58 {
59 }
60
61 SGModelLib::~SGModelLib()
62 {
63 }
64
65 namespace
66 {
67 osg::Node* loadFile(const string& path, osgDB::ReaderWriter::Options* options)
68 {
69     using namespace osg;
70     using namespace osgDB;
71     ref_ptr<Node> model = readRefNodeFile(path, options);
72     if (!model)
73         return 0;
74     if (boost::iends_with(path, ".ac"))
75         model = instantiateEffects(model.get(), options);
76      return model.release();
77 }
78 }
79
80 osg::Node*
81 SGModelLib::loadModel(const string &path,
82                        SGPropertyNode *prop_root,
83                        SGModelData *data)
84 {
85     osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
86     opt->setPropRoot(prop_root);
87     opt->setModelData(data);
88     osg::Node *n = loadFile(path, opt.get());
89     if (n && n->getName().empty())
90         n->setName("Direct loaded model \"" + path + "\"");
91     return n;
92
93 }
94
95 osg::Node*
96 SGModelLib::loadModel(const string &path,
97                                 SGPropertyNode *prop_root,
98                                 panel_func pf)
99 {
100     osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
101     opt->setPropRoot(prop_root);
102     opt->setLoadPanel(pf);
103     return loadFile(path, opt.get());
104 }
105
106 osg::Node*
107 SGModelLib::loadPagedModel(const string &path,
108                            SGPropertyNode *prop_root,
109                            SGModelData *data)
110 {
111     SGPagedLOD *plod = new SGPagedLOD;
112     plod->setName("Paged LOD for \"" + path + "\"");
113     plod->setFileName(0, path);
114     plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
115
116     osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
117     opt->setPropRoot(prop_root);
118     opt->setModelData(data);
119     plod->setReaderWriterOptions(opt.get());
120     return plod;
121 }
122
123 // end of modellib.cxx