]> git.mxchange.org Git - flightgear.git/blob - src/Model/modelmgr.cxx
Improve transponder instrumentation: new version
[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 FGModelMgr::~FGModelMgr ()
46 {
47   _models->removeChangeListener(_listener);
48   delete _listener;
49
50   for (unsigned int i = 0; i < _instances.size(); i++) {
51     globals->get_scenery()->get_scene_graph()
52       ->removeChild(_instances[i]->model->getSceneGraph());
53     delete _instances[i];
54   }
55 }
56
57 void
58 FGModelMgr::init ()
59 {
60   vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
61
62   for (unsigned int i = 0; i < model_nodes.size(); i++)
63       add_model(model_nodes[i]);
64 }
65
66 void
67 FGModelMgr::add_model (SGPropertyNode * node)
68 {
69   SG_LOG(SG_AIRCRAFT, SG_INFO,
70          "Adding model " << node->getStringValue("name", "[unnamed]"));
71
72   const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
73   osg::Node *object;
74
75   try {
76       object = SGModelLib::loadDeferredModel(path, globals->get_props());
77   } catch (const sg_throwable& t) {
78     SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << path << ":\n  "
79         << t.getFormattedMessage() << t.getOrigin());
80     return;
81   }
82
83   Instance * instance = new Instance;
84   SGModelPlacement *model = new SGModelPlacement;
85   instance->model = model;
86   instance->node = node;
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 (SGMisc<double>::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         lon = lat = elev = roll = pitch = heading = 0.0;
163
164         try {
165             // Optionally set position from properties
166             if (instance->lon_deg_node != 0)
167                 lon = testNan(instance->lon_deg_node->getDoubleValue());
168             if (instance->lat_deg_node != 0)
169                 lat = testNan(instance->lat_deg_node->getDoubleValue());
170             if (instance->elev_ft_node != 0)
171                 elev = testNan(instance->elev_ft_node->getDoubleValue());
172
173             // Optionally set orientation from properties
174             if (instance->roll_deg_node != 0)
175                 roll = testNan(instance->roll_deg_node->getDoubleValue());
176             if (instance->pitch_deg_node != 0)
177                 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
178             if (instance->heading_deg_node != 0)
179                 heading = testNan(instance->heading_deg_node->getDoubleValue());
180         } catch (const sg_range_exception&) {
181             const char *path = instance->node->getStringValue("path",
182                                                               "unknown");
183             SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
184                    << " has invalid values");
185             return;
186         }
187         // Optionally set position from properties
188         if (instance->lon_deg_node != 0)
189             model->setLongitudeDeg(lon);
190         if (instance->lat_deg_node != 0)
191             model->setLatitudeDeg(lat);
192         if (instance->elev_ft_node != 0)
193             model->setElevationFt(elev);
194
195         // Optionally set orientation from properties
196         if (instance->roll_deg_node != 0)
197             model->setRollDeg(roll);
198         if (instance->pitch_deg_node != 0)
199             model->setPitchDeg(pitch);
200         if (instance->heading_deg_node != 0)
201             model->setHeadingDeg(heading);
202
203         instance->model->update();
204     }
205 };
206 }
207
208 void
209 FGModelMgr::update (double dt)
210 {
211     std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
212 }
213
214 void
215 FGModelMgr::add_instance (Instance * instance)
216 {
217     _instances.push_back(instance);
218 }
219
220 void
221 FGModelMgr::remove_instance (Instance * instance)
222 {
223     vector<Instance *>::iterator it;
224     for (it = _instances.begin(); it != _instances.end(); it++) {
225         if (*it == instance) {
226             _instances.erase(it);
227             delete instance;
228             return;
229         }
230     }
231 }
232
233 \f
234 ////////////////////////////////////////////////////////////////////////
235 // Implementation of FGModelMgr::Instance
236 ////////////////////////////////////////////////////////////////////////
237
238 FGModelMgr::Instance::Instance ()
239   : model(0),
240     node(0),
241     lon_deg_node(0),
242     lat_deg_node(0),
243     elev_ft_node(0),
244     roll_deg_node(0),
245     pitch_deg_node(0),
246     heading_deg_node(0),
247     shadow(false)
248 {
249 }
250
251 FGModelMgr::Instance::~Instance ()
252 {
253   delete model;
254 }
255
256
257 \f
258 ////////////////////////////////////////////////////////////////////////
259 // Implementation of FGModelMgr::Listener
260 ////////////////////////////////////////////////////////////////////////
261
262 void
263 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
264 {
265   if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
266     return;
267
268   _mgr->add_model(parent);
269 }
270
271 void
272 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
273 {
274   if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
275     return;
276
277   // search instance by node and remove it from scenegraph
278   vector<Instance *>::iterator it = _mgr->_instances.begin();
279   vector<Instance *>::iterator end = _mgr->_instances.end();
280
281   for (; it != end; ++it) {
282     Instance *instance = *it;
283     if (instance->node != child)
284       continue;
285
286     _mgr->_instances.erase(it);
287     osg::Node *branch = instance->model->getSceneGraph();
288     // OSGFIXME
289 //     if (shadows && instance->shadow)
290 //         shadows->deleteOccluder(branch);
291     globals->get_scenery()->get_scene_graph()->removeChild(branch);
292
293     delete instance;
294     break;
295   }
296 }
297
298 // end of modelmgr.cxx