]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/modellib.cxx
Improved tile cache priority scheme.
[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 #include <simgear/misc/ResourceManager.hxx>
34
35 #include "SGPagedLOD.hxx"
36 #include "SGReaderWriterXML.hxx"
37 #include "SGReaderWriterXMLOptions.hxx"
38
39 #include "modellib.hxx"
40
41 #include <simgear/math/SGMath.hxx>
42
43 using std::string;
44 using namespace simgear;
45
46 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
47 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
48
49 \fSGPropertyNode_ptr SGModelLib::static_propRoot;
50 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
51
52 ////////////////////////////////////////////////////////////////////////
53 // Implementation of SGModelLib.
54 ////////////////////////////////////////////////////////////////////////
55 void SGModelLib::init(const string &root_dir)
56 {
57     osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
58 }
59
60 void SGModelLib::setPropRoot(SGPropertyNode* root)
61 {
62   static_propRoot = root;
63 }
64     
65 void SGModelLib::setPanelFunc(panel_func pf)
66 {
67   static_panelFunc = pf;
68 }
69
70 std::string SGModelLib::findDataFile(const std::string& file, 
71   const osgDB::ReaderWriter::Options* opts,
72   SGPath currentPath)
73 {
74   SGPath p = ResourceManager::instance()->findPath(file, currentPath);
75   if (p.exists()) {
76     return p.str();
77   }
78       
79   // finally hand on to standard OSG behaviour
80   return osgDB::findDataFile(file, opts);
81 }
82
83 SGModelLib::SGModelLib()
84 {
85 }
86
87 SGModelLib::~SGModelLib()
88 {
89 }
90
91 namespace
92 {
93 osg::Node* loadFile(const string& path, SGReaderWriterXMLOptions* options)
94 {
95     using namespace osg;
96     using namespace osgDB;
97     if (boost::iends_with(path, ".ac"))
98         options->setInstantiateEffects(true);
99     ref_ptr<Node> model = readRefNodeFile(path, options);
100     if (!model)
101         return 0;
102     else
103      return model.release();
104 }
105 }
106
107 osg::Node*
108 SGModelLib::loadModel(const string &path,
109                        SGPropertyNode *prop_root,
110                        SGModelData *data,
111                        bool load2DPanels)
112 {
113     osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
114     opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
115     opt->setModelData(data);
116     
117     if (load2DPanels) {
118        opt->setLoadPanel(static_panelFunc);
119     }
120     
121     osg::Node *n = loadFile(path, opt.get());
122     if (n && n->getName().empty())
123         n->setName("Direct loaded model \"" + path + "\"");
124     return n;
125
126 }
127
128 osg::Node*
129 SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
130                            SGModelData *data)
131 {
132     SGPagedLOD *plod = new SGPagedLOD;
133     plod->setName("Paged LOD for \"" + path + "\"");
134     plod->setFileName(0, path);
135     plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
136
137     osg::ref_ptr<SGReaderWriterXMLOptions> opt
138         = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()
139                                          ->getOptions()));
140     opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
141     opt->setModelData(data);
142     opt->setLoadPanel(static_panelFunc);
143     if (boost::iends_with(path, ".ac"))
144         opt->setInstantiateEffects(true);
145     plod->setReaderWriterOptions(opt.get());
146     return plod;
147 }
148
149 // end of modellib.cxx