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>
23 #include <simgear/scene/model/placement.hxx>
24 #include <simgear/scene/model/modellib.hxx>
25 #include <simgear/structure/exception.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Scenery/scenery.hxx>
31 #include "modelmgr.hxx"
35 using namespace simgear;
38 // extern SGShadowVolume *shadows;
40 FGModelMgr::FGModelMgr ()
41 : _models(fgGetNode("/models", true)),
42 _listener(new Listener(this))
44 _models->addChangeListener(_listener);
47 FGModelMgr::~FGModelMgr ()
49 _models->removeChangeListener(_listener);
52 for (unsigned int i = 0; i < _instances.size(); i++) {
53 globals->get_scenery()->get_scene_graph()
54 ->removeChild(_instances[i]->model->getSceneGraph());
62 vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
64 for (unsigned int i = 0; i < model_nodes.size(); i++)
65 add_model(model_nodes[i]);
69 FGModelMgr::add_model (SGPropertyNode * node)
71 SG_LOG(SG_AIRCRAFT, SG_INFO,
72 "Adding model " << node->getStringValue("name", "[unnamed]"));
74 const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
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());
85 Instance * instance = new Instance;
86 SGModelPlacement *model = new SGModelPlacement;
87 instance->model = model;
88 instance->node = node;
90 model->init( object );
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");
97 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
99 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
101 child = node->getChild("latitude-deg-prop");
103 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
105 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
107 child = node->getChild("elevation-ft-prop");
109 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
111 model->setElevationFt(node->getDoubleValue("elevation-ft"));
113 child = node->getChild("roll-deg-prop");
115 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
117 model->setRollDeg(node->getDoubleValue("roll-deg"));
119 child = node->getChild("pitch-deg-prop");
121 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
123 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
125 child = node->getChild("heading-deg-prop");
127 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
129 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
131 // Add this model to the global scene graph
132 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
135 // Save this instance for updating
136 add_instance(instance);
145 FGModelMgr::unbind ()
151 double testNan(double val) throw (sg_range_exception)
154 throw sg_range_exception("value is nan");
158 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
160 void operator()(FGModelMgr::Instance* instance) const
162 SGModelPlacement* model = instance->model;
163 double lon, lat, elev, roll, pitch, heading;
164 lon = lat = elev = roll = pitch = heading = 0.0;
167 // Optionally set position from properties
168 if (instance->lon_deg_node != 0)
169 lon = testNan(instance->lon_deg_node->getDoubleValue());
170 if (instance->lat_deg_node != 0)
171 lat = testNan(instance->lat_deg_node->getDoubleValue());
172 if (instance->elev_ft_node != 0)
173 elev = testNan(instance->elev_ft_node->getDoubleValue());
175 // Optionally set orientation from properties
176 if (instance->roll_deg_node != 0)
177 roll = testNan(instance->roll_deg_node->getDoubleValue());
178 if (instance->pitch_deg_node != 0)
179 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
180 if (instance->heading_deg_node != 0)
181 heading = testNan(instance->heading_deg_node->getDoubleValue());
182 } catch (const sg_range_exception&) {
183 const char *path = instance->node->getStringValue("path",
185 SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
186 << " has invalid values");
189 // Optionally set position from properties
190 if (instance->lon_deg_node != 0)
191 model->setLongitudeDeg(lon);
192 if (instance->lat_deg_node != 0)
193 model->setLatitudeDeg(lat);
194 if (instance->elev_ft_node != 0)
195 model->setElevationFt(elev);
197 // Optionally set orientation from properties
198 if (instance->roll_deg_node != 0)
199 model->setRollDeg(roll);
200 if (instance->pitch_deg_node != 0)
201 model->setPitchDeg(pitch);
202 if (instance->heading_deg_node != 0)
203 model->setHeadingDeg(heading);
205 instance->model->update();
211 FGModelMgr::update (double dt)
213 std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
217 FGModelMgr::add_instance (Instance * instance)
219 _instances.push_back(instance);
223 FGModelMgr::remove_instance (Instance * instance)
225 vector<Instance *>::iterator it;
226 for (it = _instances.begin(); it != _instances.end(); it++) {
227 if (*it == instance) {
228 _instances.erase(it);
236 ////////////////////////////////////////////////////////////////////////
237 // Implementation of FGModelMgr::Instance
238 ////////////////////////////////////////////////////////////////////////
240 FGModelMgr::Instance::Instance ()
253 FGModelMgr::Instance::~Instance ()
260 ////////////////////////////////////////////////////////////////////////
261 // Implementation of FGModelMgr::Listener
262 ////////////////////////////////////////////////////////////////////////
265 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
267 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
270 _mgr->add_model(parent);
274 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
276 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
279 // search instance by node and remove it from scenegraph
280 vector<Instance *>::iterator it = _mgr->_instances.begin();
281 vector<Instance *>::iterator end = _mgr->_instances.end();
283 for (; it != end; ++it) {
284 Instance *instance = *it;
285 if (instance->node != child)
288 _mgr->_instances.erase(it);
289 osg::Node *branch = instance->model->getSceneGraph();
291 // if (shadows && instance->shadow)
292 // shadows->deleteOccluder(branch);
293 globals->get_scenery()->get_scene_graph()->removeChild(branch);
300 // end of modelmgr.cxx