]> git.mxchange.org Git - flightgear.git/blob - src/Model/modelmgr.cxx
Replace a program segmentation fault by an annoying message
[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 std::vector;
32
33 using namespace simgear;
34
35 // OSGFIXME
36 // extern SGShadowVolume *shadows;
37
38 FGModelMgr::FGModelMgr ()
39   : _models(fgGetNode("/models", true)),
40     _listener(new Listener(this))
41 {
42   _models->addChangeListener(_listener);
43 }
44
45 #include <stdio.h>
46 FGModelMgr::~FGModelMgr ()
47 {
48   _models->removeChangeListener(_listener);
49   delete _listener;
50
51   osg::Group *scene_graph = globals->get_scenery()->get_scene_graph();
52   if (!scene_graph)
53     SG_LOG(SG_AIRCRAFT, SG_ALERT, "Warning: The scenegraph wass deleted before the models could be removed");
54
55   for (unsigned int i = 0; i < _instances.size(); i++) {
56     if (scene_graph)
57       scene_graph->removeChild(_instances[i]->model->getSceneGraph());
58     delete _instances[i];
59   }
60 }
61
62 void
63 FGModelMgr::init ()
64 {
65   vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
66
67   for (unsigned int i = 0; i < model_nodes.size(); i++)
68       add_model(model_nodes[i]);
69 }
70
71 void
72 FGModelMgr::add_model (SGPropertyNode * node)
73 {
74   SG_LOG(SG_AIRCRAFT, SG_INFO,
75          "Adding model " << node->getStringValue("name", "[unnamed]"));
76
77   const char *model_path = node->getStringValue("path", "Models/Geometry/glider.ac");
78   osg::Node *object;
79
80   try {
81       std::string fullPath = simgear::SGModelLib::findDataFile(model_path);
82       object = SGModelLib::loadDeferredModel(fullPath, globals->get_props());
83   } catch (const sg_throwable& t) {
84     SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << model_path << ":\n  "
85         << t.getFormattedMessage() << t.getOrigin());
86     return;
87   }
88
89   Instance * instance = new Instance;
90   SGModelPlacement *model = new SGModelPlacement;
91   instance->model = model;
92   instance->node = node;
93
94   model->init( object );
95     double lon = node->getDoubleValue("longitude-deg"),
96         lat = node->getDoubleValue("latitude-deg"),
97         elevFt = node->getDoubleValue("elevation-ft");
98
99     model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt));
100 // Set position and orientation either
101 // indirectly through property refs
102 // or directly with static values.
103   SGPropertyNode * child = node->getChild("longitude-deg-prop");
104   if (child != 0)
105     instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
106
107   child = node->getChild("latitude-deg-prop");
108   if (child != 0)
109     instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
110
111   child = node->getChild("elevation-ft-prop");
112   if (child != 0)
113     instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
114
115   child = node->getChild("roll-deg-prop");
116   if (child != 0)
117     instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
118   else
119     model->setRollDeg(node->getDoubleValue("roll-deg"));
120
121   child = node->getChild("pitch-deg-prop");
122   if (child != 0)
123     instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
124   else
125     model->setPitchDeg(node->getDoubleValue("pitch-deg"));
126
127   child = node->getChild("heading-deg-prop");
128   if (child != 0)
129     instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
130   else
131     model->setHeadingDeg(node->getDoubleValue("heading-deg"));
132
133                         // Add this model to the global scene graph
134   globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
135
136
137                         // Save this instance for updating
138   add_instance(instance);
139 }
140
141 void
142 FGModelMgr::bind ()
143 {
144 }
145
146 void
147 FGModelMgr::unbind ()
148 {
149 }
150
151 namespace
152 {
153 double testNan(double val) throw (sg_range_exception)
154 {
155     if (SGMisc<double>::isNaN(val))
156         throw sg_range_exception("value is nan");
157     return val;
158 }
159
160 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
161 {
162     void operator()(FGModelMgr::Instance* instance) const
163     {
164         SGModelPlacement* model = instance->model;
165         double roll, pitch, heading;
166         roll = pitch = heading = 0.0;
167         SGGeod pos = model->getPosition();
168         
169         try {
170             // Optionally set position from properties
171             if (instance->lon_deg_node != 0)
172                 pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue()));
173             if (instance->lat_deg_node != 0)
174                 pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue()));
175             if (instance->elev_ft_node != 0)
176                 pos.setElevationFt(testNan(instance->elev_ft_node->getDoubleValue()));
177             
178             // Optionally set orientation from properties
179             if (instance->roll_deg_node != 0)
180                 roll = testNan(instance->roll_deg_node->getDoubleValue());
181             if (instance->pitch_deg_node != 0)
182                 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
183             if (instance->heading_deg_node != 0)
184                 heading = testNan(instance->heading_deg_node->getDoubleValue());
185         } catch (const sg_range_exception&) {
186             const char *path = instance->node->getStringValue("path",
187                                                               "unknown");
188             SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
189                    << " has invalid values");
190             return;
191         }
192
193         model->setPosition(pos);
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