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