]> git.mxchange.org Git - flightgear.git/blob - src/Model/modelmgr.cxx
142d7af4080d95b313853bdbb3c9757330f073e6
[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
165         try {
166             // Optionally set position from properties
167             if (instance->lon_deg_node != 0)
168                 lon = testNan(instance->lon_deg_node->getDoubleValue());
169             if (instance->lat_deg_node != 0)
170                 lat = testNan(instance->lat_deg_node->getDoubleValue());
171             if (instance->elev_ft_node != 0)
172                 elev = testNan(instance->elev_ft_node->getDoubleValue());
173
174             // Optionally set orientation from properties
175             if (instance->roll_deg_node != 0)
176                 roll = testNan(instance->roll_deg_node->getDoubleValue());
177             if (instance->pitch_deg_node != 0)
178                 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
179             if (instance->heading_deg_node != 0)
180                 heading = testNan(instance->heading_deg_node->getDoubleValue());
181         } catch (const sg_range_exception&) {
182             const char *path = instance->node->getStringValue("path",
183                                                               "unknown");
184             SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
185                    << " has invalid values");
186             return;
187         }
188         // Optionally set position from properties
189         if (instance->lon_deg_node != 0)
190             model->setLongitudeDeg(lon);
191         if (instance->lat_deg_node != 0)
192             model->setLatitudeDeg(lat);
193         if (instance->elev_ft_node != 0)
194             model->setElevationFt(elev);
195
196         // Optionally set orientation from properties
197         if (instance->roll_deg_node != 0)
198             model->setRollDeg(roll);
199         if (instance->pitch_deg_node != 0)
200             model->setPitchDeg(pitch);
201         if (instance->heading_deg_node != 0)
202             model->setHeadingDeg(heading);
203
204         instance->model->update();
205     }
206 };
207 }
208
209 void
210 FGModelMgr::update (double dt)
211 {
212     std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
213 }
214
215 void
216 FGModelMgr::add_instance (Instance * instance)
217 {
218     _instances.push_back(instance);
219 }
220
221 void
222 FGModelMgr::remove_instance (Instance * instance)
223 {
224     vector<Instance *>::iterator it;
225     for (it = _instances.begin(); it != _instances.end(); it++) {
226         if (*it == instance) {
227             _instances.erase(it);
228             delete instance;
229             return;
230         }
231     }
232 }
233
234 \f
235 ////////////////////////////////////////////////////////////////////////
236 // Implementation of FGModelMgr::Instance
237 ////////////////////////////////////////////////////////////////////////
238
239 FGModelMgr::Instance::Instance ()
240   : model(0),
241     node(0),
242     lon_deg_node(0),
243     lat_deg_node(0),
244     elev_ft_node(0),
245     roll_deg_node(0),
246     pitch_deg_node(0),
247     heading_deg_node(0),
248     shadow(false)
249 {
250 }
251
252 FGModelMgr::Instance::~Instance ()
253 {
254   delete model;
255 }
256
257
258 \f
259 ////////////////////////////////////////////////////////////////////////
260 // Implementation of FGModelMgr::Listener
261 ////////////////////////////////////////////////////////////////////////
262
263 void
264 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
265 {
266   if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
267     return;
268
269   _mgr->add_model(parent);
270 }
271
272 void
273 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
274 {
275   if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
276     return;
277
278   // search instance by node and remove it from scenegraph
279   vector<Instance *>::iterator it = _mgr->_instances.begin();
280   vector<Instance *>::iterator end = _mgr->_instances.end();
281
282   for (; it != end; ++it) {
283     Instance *instance = *it;
284     if (instance->node != child)
285       continue;
286
287     _mgr->_instances.erase(it);
288     osg::Node *branch = instance->model->getSceneGraph();
289     // OSGFIXME
290 //     if (shadows && instance->shadow)
291 //         shadows->deleteOccluder(branch);
292     globals->get_scenery()->get_scene_graph()->removeChild(branch);
293
294     delete instance;
295     break;
296   }
297 }
298
299 // end of modelmgr.cxx