]> git.mxchange.org Git - flightgear.git/blob - src/Model/modelmgr.cxx
Support explicit aircraft interior models.
[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     double lon = node->getDoubleValue("longitude-deg"),
90         lat = node->getDoubleValue("latitude-deg"),
91         elevFt = node->getDoubleValue("elevation-ft");
92
93     model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt));
94 // Set position and orientation either
95 // indirectly through property refs
96 // or directly with static values.
97   SGPropertyNode * child = node->getChild("longitude-deg-prop");
98   if (child != 0)
99     instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
100
101   child = node->getChild("latitude-deg-prop");
102   if (child != 0)
103     instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
104
105   child = node->getChild("elevation-ft-prop");
106   if (child != 0)
107     instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
108
109   child = node->getChild("roll-deg-prop");
110   if (child != 0)
111     instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
112   else
113     model->setRollDeg(node->getDoubleValue("roll-deg"));
114
115   child = node->getChild("pitch-deg-prop");
116   if (child != 0)
117     instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
118   else
119     model->setPitchDeg(node->getDoubleValue("pitch-deg"));
120
121   child = node->getChild("heading-deg-prop");
122   if (child != 0)
123     instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
124   else
125     model->setHeadingDeg(node->getDoubleValue("heading-deg"));
126
127                         // Add this model to the global scene graph
128   globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
129
130
131                         // Save this instance for updating
132   add_instance(instance);
133 }
134
135 void
136 FGModelMgr::bind ()
137 {
138 }
139
140 void
141 FGModelMgr::unbind ()
142 {
143 }
144
145 namespace
146 {
147 double testNan(double val) throw (sg_range_exception)
148 {
149     if (SGMisc<double>::isNaN(val))
150         throw sg_range_exception("value is nan");
151     return val;
152 }
153
154 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
155 {
156     void operator()(FGModelMgr::Instance* instance) const
157     {
158         SGModelPlacement* model = instance->model;
159         double roll, pitch, heading;
160         roll = pitch = heading = 0.0;
161         SGGeod pos = model->getPosition();
162         
163         try {
164             // Optionally set position from properties
165             if (instance->lon_deg_node != 0)
166                 pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue()));
167             if (instance->lat_deg_node != 0)
168                 pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue()));
169             if (instance->elev_ft_node != 0)
170                 pos.setElevationFt(testNan(instance->elev_ft_node->getDoubleValue()));
171             
172             // Optionally set orientation from properties
173             if (instance->roll_deg_node != 0)
174                 roll = testNan(instance->roll_deg_node->getDoubleValue());
175             if (instance->pitch_deg_node != 0)
176                 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
177             if (instance->heading_deg_node != 0)
178                 heading = testNan(instance->heading_deg_node->getDoubleValue());
179         } catch (const sg_range_exception&) {
180             const char *path = instance->node->getStringValue("path",
181                                                               "unknown");
182             SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
183                    << " has invalid values");
184             return;
185         }
186
187         model->setPosition(pos);
188         // Optionally set orientation from properties
189         if (instance->roll_deg_node != 0)
190             model->setRollDeg(roll);
191         if (instance->pitch_deg_node != 0)
192             model->setPitchDeg(pitch);
193         if (instance->heading_deg_node != 0)
194             model->setHeadingDeg(heading);
195
196         instance->model->update();
197     }
198 };
199 }
200
201 void
202 FGModelMgr::update (double dt)
203 {
204     std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
205 }
206
207 void
208 FGModelMgr::add_instance (Instance * instance)
209 {
210     _instances.push_back(instance);
211 }
212
213 void
214 FGModelMgr::remove_instance (Instance * instance)
215 {
216     vector<Instance *>::iterator it;
217     for (it = _instances.begin(); it != _instances.end(); it++) {
218         if (*it == instance) {
219             _instances.erase(it);
220             delete instance;
221             return;
222         }
223     }
224 }
225
226 \f
227 ////////////////////////////////////////////////////////////////////////
228 // Implementation of FGModelMgr::Instance
229 ////////////////////////////////////////////////////////////////////////
230
231 FGModelMgr::Instance::Instance ()
232   : model(0),
233     node(0),
234     lon_deg_node(0),
235     lat_deg_node(0),
236     elev_ft_node(0),
237     roll_deg_node(0),
238     pitch_deg_node(0),
239     heading_deg_node(0),
240     shadow(false)
241 {
242 }
243
244 FGModelMgr::Instance::~Instance ()
245 {
246   delete model;
247 }
248
249
250 \f
251 ////////////////////////////////////////////////////////////////////////
252 // Implementation of FGModelMgr::Listener
253 ////////////////////////////////////////////////////////////////////////
254
255 void
256 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
257 {
258   if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
259     return;
260
261   _mgr->add_model(parent);
262 }
263
264 void
265 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
266 {
267   if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
268     return;
269
270   // search instance by node and remove it from scenegraph
271   vector<Instance *>::iterator it = _mgr->_instances.begin();
272   vector<Instance *>::iterator end = _mgr->_instances.end();
273
274   for (; it != end; ++it) {
275     Instance *instance = *it;
276     if (instance->node != child)
277       continue;
278
279     _mgr->_instances.erase(it);
280     osg::Node *branch = instance->model->getSceneGraph();
281     // OSGFIXME
282 //     if (shadows && instance->shadow)
283 //         shadows->deleteOccluder(branch);
284     globals->get_scenery()->get_scene_graph()->removeChild(branch);
285
286     delete instance;
287     break;
288   }
289 }
290
291 // end of modelmgr.cxx