]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/modellib.cxx
Merge branch 'next' of git.gitorious.org:fg/simgear into next
[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 SGPropertyNode_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, SGPropertyNode* root)
56 {
57     osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
58     static_propRoot = root;
59     SGPagedLOD::setRenderingCache(root->getBoolValue("/sim/rendering/cache",true));
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, SGReaderWriterXMLOptions* 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<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
111     opt->setPropRoot(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::loadPagedModel(const string &path, SGPropertyNode *prop_root,
127                            SGModelData *data)
128 {
129     SGPagedLOD *plod = new SGPagedLOD;
130     plod->setName("Paged LOD for \"" + path + "\"");
131     plod->setFileName(0, path);
132     plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
133
134     osg::ref_ptr<SGReaderWriterXMLOptions> opt
135         = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()
136                                          ->getOptions()));
137     opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
138     opt->setModelData(data);
139     opt->setLoadPanel(static_panelFunc);
140     if (boost::iends_with(path, ".ac"))
141         opt->setInstantiateEffects(true);
142     plod->setReaderWriterOptions(opt.get());
143     return plod;
144 }
145
146 // end of modellib.cxx