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