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