1 // modelmgr.cxx - manage a collection of 3D models.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
7 # pragma warning( disable: 4355 )
14 #include <simgear/compiler.h>
21 #include <simgear/scene/model/placement.hxx>
22 #include <simgear/scene/model/modellib.hxx>
23 #include <simgear/structure/exception.hxx>
25 #include <Main/fg_props.hxx>
26 #include <Scenery/scenery.hxx>
29 #include "modelmgr.hxx"
31 using namespace simgear;
34 // extern SGShadowVolume *shadows;
36 FGModelMgr::FGModelMgr ()
40 FGModelMgr::~FGModelMgr ()
47 std::vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
49 for (unsigned int i = 0; i < model_nodes.size(); i++)
50 add_model(model_nodes[i]);
53 void FGModelMgr::shutdown()
55 osg::Group *scene_graph = NULL;
56 if (globals->get_scenery()) {
57 scene_graph = globals->get_scenery()->get_scene_graph();
60 // always delete instances, even if the scene-graph is gone
61 for (unsigned int i = 0; i < _instances.size(); i++) {
63 scene_graph->removeChild(_instances[i]->model->getSceneGraph());
71 FGModelMgr::add_model (SGPropertyNode * node)
73 SG_LOG(SG_AIRCRAFT, SG_INFO,
74 "Adding model " << node->getStringValue("name", "[unnamed]"));
76 const char *model_path = node->getStringValue("path", "Models/Geometry/glider.ac");
80 std::string fullPath = simgear::SGModelLib::findDataFile(model_path);
81 object = SGModelLib::loadDeferredModel(fullPath, globals->get_props());
82 } catch (const sg_throwable& t) {
83 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << model_path << ":\n "
84 << t.getFormattedMessage() << t.getOrigin());
88 Instance * instance = new Instance;
89 SGModelPlacement *model = new SGModelPlacement;
90 instance->model = model;
91 instance->node = node;
93 model->init( object );
94 double lon = node->getDoubleValue("longitude-deg"),
95 lat = node->getDoubleValue("latitude-deg"),
96 elevFt = node->getDoubleValue("elevation-ft");
98 model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt));
99 // Set position and orientation either
100 // indirectly through property refs
101 // or directly with static values.
102 SGPropertyNode * child = node->getChild("longitude-deg-prop");
104 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
106 child = node->getChild("latitude-deg-prop");
108 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
110 child = node->getChild("elevation-ft-prop");
112 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
114 child = node->getChild("roll-deg-prop");
116 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
118 model->setRollDeg(node->getDoubleValue("roll-deg"));
120 child = node->getChild("pitch-deg-prop");
122 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
124 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
126 child = node->getChild("heading-deg-prop");
128 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
130 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
132 // Add this model to the global scene graph
133 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
136 // Save this instance for updating
137 add_instance(instance);
143 _models = fgGetNode("/models", true);
145 _listener.reset(new Listener(this));
146 _models->addChangeListener(_listener.get());
150 FGModelMgr::unbind ()
152 _models->removeChangeListener(_listener.get());
159 double testNan(double val) throw (sg_range_exception)
161 if (SGMisc<double>::isNaN(val))
162 throw sg_range_exception("value is nan");
166 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
168 void operator()(FGModelMgr::Instance* instance) const
170 SGModelPlacement* model = instance->model;
171 double roll, pitch, heading;
172 roll = pitch = heading = 0.0;
173 SGGeod pos = model->getPosition();
176 // Optionally set position from properties
177 if (instance->lon_deg_node != 0)
178 pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue()));
179 if (instance->lat_deg_node != 0)
180 pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue()));
181 if (instance->elev_ft_node != 0)
182 pos.setElevationFt(testNan(instance->elev_ft_node->getDoubleValue()));
184 // Optionally set orientation from properties
185 if (instance->roll_deg_node != 0)
186 roll = testNan(instance->roll_deg_node->getDoubleValue());
187 if (instance->pitch_deg_node != 0)
188 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
189 if (instance->heading_deg_node != 0)
190 heading = testNan(instance->heading_deg_node->getDoubleValue());
191 } catch (const sg_range_exception&) {
192 const char *path = instance->node->getStringValue("path",
194 SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
195 << " has invalid values");
199 model->setPosition(pos);
200 // Optionally set orientation from properties
201 if (instance->roll_deg_node != 0)
202 model->setRollDeg(roll);
203 if (instance->pitch_deg_node != 0)
204 model->setPitchDeg(pitch);
205 if (instance->heading_deg_node != 0)
206 model->setHeadingDeg(heading);
208 instance->model->update();
214 FGModelMgr::update (double dt)
216 std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
220 FGModelMgr::add_instance (Instance * instance)
222 _instances.push_back(instance);
226 FGModelMgr::remove_instance (Instance * instance)
228 std::vector<Instance *>::iterator it;
229 for (it = _instances.begin(); it != _instances.end(); it++) {
230 if (*it == instance) {
231 _instances.erase(it);
239 ////////////////////////////////////////////////////////////////////////
240 // Implementation of FGModelMgr::Instance
241 ////////////////////////////////////////////////////////////////////////
243 FGModelMgr::Instance::Instance ()
256 FGModelMgr::Instance::~Instance ()
263 ////////////////////////////////////////////////////////////////////////
264 // Implementation of FGModelMgr::Listener
265 ////////////////////////////////////////////////////////////////////////
268 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
270 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
273 _mgr->add_model(parent);
277 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
279 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
282 // search instance by node and remove it from scenegraph
283 std::vector<Instance *>::iterator it = _mgr->_instances.begin();
284 std::vector<Instance *>::iterator end = _mgr->_instances.end();
286 for (; it != end; ++it) {
287 Instance *instance = *it;
288 if (instance->node != child)
291 _mgr->_instances.erase(it);
292 osg::Node *branch = instance->model->getSceneGraph();
294 // if (shadows && instance->shadow)
295 // shadows->deleteOccluder(branch);
297 if (globals->get_scenery() && globals->get_scenery()->get_scene_graph()) {
298 globals->get_scenery()->get_scene_graph()->removeChild(branch);
306 // end of modelmgr.cxx