]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/modellib.cxx
5fa40fca2e5b296f24ec315face10c32949033e0
[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 <osg/PagedLOD>
25 #include <osg/ProxyNode>
26 #include <osgDB/ReadFile>
27 #include <osgDB/WriteFile>
28 #include <osgDB/Registry>
29
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>
37
38 #include "SGReaderWriterXML.hxx"
39
40 #include "modellib.hxx"
41
42 #include <simgear/math/SGMath.hxx>
43
44 using std::string;
45 using namespace simgear;
46
47 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
48 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
49
50 SGPropertyNode_ptr SGModelLib::static_propRoot;
51 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
52
53 ////////////////////////////////////////////////////////////////////////
54 // Implementation of SGModelLib.
55 ////////////////////////////////////////////////////////////////////////
56 void SGModelLib::init(const string &root_dir, SGPropertyNode* root)
57 {
58     osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
59     static_propRoot = root;
60 }
61
62 void SGModelLib::setPanelFunc(panel_func pf)
63 {
64   static_panelFunc = pf;
65 }
66
67 std::string SGModelLib::findDataFile(const std::string& file, 
68   const osgDB::ReaderWriter::Options* opts,
69   SGPath currentPath)
70 {
71   SGPath p = ResourceManager::instance()->findPath(file, currentPath);
72   if (p.exists()) {
73     return p.str();
74   }
75       
76   // finally hand on to standard OSG behaviour
77   return osgDB::findDataFile(file, opts);
78 }
79
80 SGModelLib::SGModelLib()
81 {
82 }
83
84 SGModelLib::~SGModelLib()
85 {
86 }
87
88 namespace
89 {
90 osg::Node* loadFile(const string& path, SGReaderWriterOptions* options)
91 {
92     using namespace osg;
93     using namespace osgDB;
94     if (boost::iends_with(path, ".ac"))
95         options->setInstantiateEffects(true);
96     ref_ptr<Node> model = readRefNodeFile(path, options);
97     if (!model)
98         return 0;
99     else
100      return model.release();
101 }
102 }
103
104 osg::Node*
105 SGModelLib::loadModel(const string &path,
106                        SGPropertyNode *prop_root,
107                        SGModelData *data,
108                        bool load2DPanels)
109 {
110     osg::ref_ptr<SGReaderWriterOptions> opt = new SGReaderWriterOptions(*(osgDB::Registry::instance()->getOptions()));
111     opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
112     opt->setModelData(data);
113     
114     if (load2DPanels) {
115        opt->setLoadPanel(static_panelFunc);
116     }
117     
118     osg::Node *n = loadFile(path, opt.get());
119     if (n && n->getName().empty())
120         n->setName("Direct loaded model \"" + path + "\"");
121     return n;
122
123 }
124
125 osg::Node*
126 SGModelLib::loadDeferredModel(const string &path, SGPropertyNode *prop_root,
127                              SGModelData *data)
128 {
129     osg::ProxyNode* proxyNode = new osg::ProxyNode;
130     proxyNode->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER);
131     proxyNode->setFileName(0, path);
132
133     osg::ref_ptr<SGReaderWriterOptions> opt
134         = new SGReaderWriterOptions(*(osgDB::Registry::instance()
135                                          ->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::ReaderWriter::Options::CACHE_ALL);
143     else
144         opt->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_NONE);
145     proxyNode->setDatabaseOptions(opt.get());
146
147     return proxyNode;
148 }
149
150 osg::Node*
151 SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
152                            SGModelData *data)
153 {
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);
158
159     osg::ref_ptr<SGReaderWriterOptions> opt
160         = new SGReaderWriterOptions(*(osgDB::Registry::instance()
161                                          ->getOptions()));
162     opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get());
163     opt->setModelData(data);
164     opt->setLoadPanel(static_panelFunc);
165     if (SGPath(path).lower_extension() == "ac")
166         opt->setInstantiateEffects(true);
167     if (!prop_root || prop_root->getBoolValue("/sim/rendering/cache", true))
168         opt->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_ALL);
169     else
170         opt->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_NONE);
171     plod->setDatabaseOptions(opt.get());
172     return plod;
173 }
174
175 // end of modellib.cxx