]> git.mxchange.org Git - flightgear.git/blob - src/Model/modelmgr.cxx
Remove conditional compilation of ATCDCL
[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_GENERAL, SG_INFO,
72          "Adding model " << node->getStringValue("name", "[unnamed]"));
73   Instance * instance = new Instance;
74   SGModelPlacement *model = new SGModelPlacement;
75   instance->model = model;
76   instance->node = node;
77
78   const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
79   osg::Node *object;
80
81   try {
82     object = SGModelLib::loadPagedModel(path, globals->get_props());
83   } catch (const sg_throwable& t) {
84     SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n  "
85         << t.getFormattedMessage() << t.getOrigin());
86     return;
87   }
88
89   model->init( object );
90
91                                 // Set position and orientation either
92                                 // indirectly through property refs
93                                 // or directly with static values.
94   SGPropertyNode * child = node->getChild("longitude-deg-prop");
95   if (child != 0)
96     instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
97   else
98     model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
99
100   child = node->getChild("latitude-deg-prop");
101   if (child != 0)
102     instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
103   else
104     model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
105
106   child = node->getChild("elevation-ft-prop");
107   if (child != 0)
108     instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
109   else
110     model->setElevationFt(node->getDoubleValue("elevation-ft"));
111
112   child = node->getChild("roll-deg-prop");
113   if (child != 0)
114     instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
115   else
116     model->setRollDeg(node->getDoubleValue("roll-deg"));
117
118   child = node->getChild("pitch-deg-prop");
119   if (child != 0)
120     instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
121   else
122     model->setPitchDeg(node->getDoubleValue("pitch-deg"));
123
124   child = node->getChild("heading-deg-prop");
125   if (child != 0)
126     instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
127   else
128     model->setHeadingDeg(node->getDoubleValue("heading-deg"));
129
130                         // Add this model to the global scene graph
131   globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
132
133
134                         // Save this instance for updating
135   add_instance(instance);
136 }
137
138 void
139 FGModelMgr::bind ()
140 {
141 }
142
143 void
144 FGModelMgr::unbind ()
145 {
146 }
147
148 namespace
149 {
150 double testNan(double val) throw (sg_range_exception)
151 {
152     if (osg::isNaN(val))
153         throw sg_range_exception("value is nan");
154     return val;
155 }
156
157 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
158 {
159     void operator()(FGModelMgr::Instance* instance) const
160     {
161         SGModelPlacement* model = instance->model;
162         double lon, lat, elev, roll, pitch, heading;
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_GENERAL, 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