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