]> git.mxchange.org Git - flightgear.git/blob - src/Model/modelmgr.cxx
Suppress warnings
[flightgear.git] / src / Model / modelmgr.cxx
1 // modelmgr.cxx - manage a collection of 3D models.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef _MSC_VER
7 #  pragma warning( disable: 4355 )
8 #endif
9
10 #ifdef HAVE_CONFIG_H
11 #  include <config.h>
12 #endif
13
14 #include <simgear/compiler.h>
15
16 #include <algorithm>
17 #include <functional>
18 #include <vector>
19
20 #include <osg/Math>
21
22 #include <simgear/scene/model/placement.hxx>
23 #include <simgear/scene/model/modellib.hxx>
24 #include <simgear/structure/exception.hxx>
25
26 #include <Main/fg_props.hxx>
27 #include <Scenery/scenery.hxx>
28
29
30 #include "modelmgr.hxx"
31
32 using std::vector;
33
34 using namespace simgear;
35
36 // OSGFIXME
37 // extern SGShadowVolume *shadows;
38
39 FGModelMgr::FGModelMgr ()
40   : _models(fgGetNode("/models", true)),
41     _listener(new Listener(this))
42 {
43   _models->addChangeListener(_listener);
44 }
45
46 FGModelMgr::~FGModelMgr ()
47 {
48   _models->removeChangeListener(_listener);
49   delete _listener;
50
51   for (unsigned int i = 0; i < _instances.size(); i++) {
52     globals->get_scenery()->get_scene_graph()
53       ->removeChild(_instances[i]->model->getSceneGraph());
54     delete _instances[i];
55   }
56 }
57
58 void
59 FGModelMgr::init ()
60 {
61   vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
62
63   for (unsigned int i = 0; i < model_nodes.size(); i++)
64       add_model(model_nodes[i]);
65 }
66
67 void
68 FGModelMgr::add_model (SGPropertyNode * node)
69 {
70   SG_LOG(SG_GENERAL, SG_INFO,
71          "Adding model " << node->getStringValue("name", "[unnamed]"));
72   Instance * instance = new Instance;
73   SGModelPlacement *model = new SGModelPlacement;
74   instance->model = model;
75   instance->node = node;
76
77   const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
78   osg::Node *object;
79
80   try {
81     object = SGModelLib::loadPagedModel(path, globals->get_props());
82   } catch (const sg_throwable& t) {
83     SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n  "
84         << t.getFormattedMessage() << t.getOrigin());
85     return;
86   }
87
88   model->init( object );
89
90                                 // Set position and orientation either
91                                 // indirectly through property refs
92                                 // or directly with static values.
93   SGPropertyNode * child = node->getChild("longitude-deg-prop");
94   if (child != 0)
95     instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
96   else
97     model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
98
99   child = node->getChild("latitude-deg-prop");
100   if (child != 0)
101     instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
102   else
103     model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
104
105   child = node->getChild("elevation-ft-prop");
106   if (child != 0)
107     instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
108   else
109     model->setElevationFt(node->getDoubleValue("elevation-ft"));
110
111   child = node->getChild("roll-deg-prop");
112   if (child != 0)
113     instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
114   else
115     model->setRollDeg(node->getDoubleValue("roll-deg"));
116
117   child = node->getChild("pitch-deg-prop");
118   if (child != 0)
119     instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
120   else
121     model->setPitchDeg(node->getDoubleValue("pitch-deg"));
122
123   child = node->getChild("heading-deg-prop");
124   if (child != 0)
125     instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
126   else
127     model->setHeadingDeg(node->getDoubleValue("heading-deg"));
128
129                         // Add this model to the global scene graph
130   globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
131
132
133                         // Save this instance for updating
134   add_instance(instance);
135 }
136
137 void
138 FGModelMgr::bind ()
139 {
140 }
141
142 void
143 FGModelMgr::unbind ()
144 {
145 }
146
147 namespace
148 {
149 double testNan(double val) throw (sg_range_exception)
150 {
151     if (osg::isNaN(val))
152         throw sg_range_exception("value is nan");
153     return val;
154 }
155
156 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
157 {
158     void operator()(FGModelMgr::Instance* instance) const
159     {
160         SGModelPlacement* model = instance->model;
161         double lon, lat, elev, roll, pitch, heading;
162
163         try {
164             // Optionally set position from properties
165             if (instance->lon_deg_node != 0)
166                 lon = testNan(instance->lon_deg_node->getDoubleValue());
167             if (instance->lat_deg_node != 0)
168                 lat = testNan(instance->lat_deg_node->getDoubleValue());
169             if (instance->elev_ft_node != 0)
170                 elev = testNan(instance->elev_ft_node->getDoubleValue());
171
172             // Optionally set orientation from properties
173             if (instance->roll_deg_node != 0)
174                 roll = testNan(instance->roll_deg_node->getDoubleValue());
175             if (instance->pitch_deg_node != 0)
176                 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
177             if (instance->heading_deg_node != 0)
178                 heading = testNan(instance->heading_deg_node->getDoubleValue());
179         } catch (const sg_range_exception&) {
180             const char *path = instance->node->getStringValue("path",
181                                                               "unknown");
182             SG_LOG(SG_GENERAL, SG_INFO, "Instance of model " << path
183                    << " has invalid values");
184             return;
185         }
186         // Optionally set position from properties
187         if (instance->lon_deg_node != 0)
188             model->setLongitudeDeg(lon);
189         if (instance->lat_deg_node != 0)
190             model->setLatitudeDeg(lat);
191         if (instance->elev_ft_node != 0)
192             model->setElevationFt(elev);
193
194         // Optionally set orientation from properties
195         if (instance->roll_deg_node != 0)
196             model->setRollDeg(roll);
197         if (instance->pitch_deg_node != 0)
198             model->setPitchDeg(pitch);
199         if (instance->heading_deg_node != 0)
200             model->setHeadingDeg(heading);
201
202         instance->model->update();
203     }
204 };
205 }
206
207 void
208 FGModelMgr::update (double dt)
209 {
210     std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
211 }
212
213 void
214 FGModelMgr::add_instance (Instance * instance)
215 {
216     _instances.push_back(instance);
217 }
218
219 void
220 FGModelMgr::remove_instance (Instance * instance)
221 {
222     vector<Instance *>::iterator it;
223     for (it = _instances.begin(); it != _instances.end(); it++) {
224         if (*it == instance) {
225             _instances.erase(it);
226             delete instance;
227             return;
228         }
229     }
230 }
231
232 \f
233 ////////////////////////////////////////////////////////////////////////
234 // Implementation of FGModelMgr::Instance
235 ////////////////////////////////////////////////////////////////////////
236
237 FGModelMgr::Instance::Instance ()
238   : model(0),
239     node(0),
240     lon_deg_node(0),
241     lat_deg_node(0),
242     elev_ft_node(0),
243     roll_deg_node(0),
244     pitch_deg_node(0),
245     heading_deg_node(0),
246     shadow(false)
247 {
248 }
249
250 FGModelMgr::Instance::~Instance ()
251 {
252   delete model;
253 }
254
255
256 \f
257 ////////////////////////////////////////////////////////////////////////
258 // Implementation of FGModelMgr::Listener
259 ////////////////////////////////////////////////////////////////////////
260
261 void
262 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
263 {
264   if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
265     return;
266
267   _mgr->add_model(parent);
268 }
269
270 void
271 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
272 {
273   if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
274     return;
275
276   // search instance by node and remove it from scenegraph
277   vector<Instance *>::iterator it = _mgr->_instances.begin();
278   vector<Instance *>::iterator end = _mgr->_instances.end();
279
280   for (; it != end; ++it) {
281     Instance *instance = *it;
282     if (instance->node != child)
283       continue;
284
285     _mgr->_instances.erase(it);
286     osg::Node *branch = instance->model->getSceneGraph();
287     // OSGFIXME
288 //     if (shadows && instance->shadow)
289 //         shadows->deleteOccluder(branch);
290     globals->get_scenery()->get_scene_graph()->removeChild(branch);
291
292     delete instance;
293     break;
294   }
295 }
296
297 // end of modelmgr.cxx