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.
10 #include <simgear/compiler.h>
18 #include <simgear/scene/model/placement.hxx>
19 #include <simgear/scene/model/modellib.hxx>
20 #include <simgear/structure/exception.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Scenery/scenery.hxx>
26 #include "modelmgr.hxx"
30 using namespace simgear;
33 // extern SGShadowVolume *shadows;
35 FGModelMgr::FGModelMgr ()
36 : _models(fgGetNode("/models", true)),
37 _listener(new Listener(this))
39 _models->addChangeListener(_listener);
42 FGModelMgr::~FGModelMgr ()
44 _models->removeChangeListener(_listener);
47 for (unsigned int i = 0; i < _instances.size(); i++) {
48 globals->get_scenery()->get_scene_graph()
49 ->removeChild(_instances[i]->model->getSceneGraph());
57 vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
59 for (unsigned int i = 0; i < model_nodes.size(); i++)
60 add_model(model_nodes[i]);
64 FGModelMgr::add_model (SGPropertyNode * node)
66 SG_LOG(SG_GENERAL, SG_INFO,
67 "Adding model " << node->getStringValue("name", "[unnamed]"));
68 Instance * instance = new Instance;
69 SGModelPlacement *model = new SGModelPlacement;
70 instance->model = model;
71 instance->node = node;
73 const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
77 object = SGModelLib::loadPagedModel(path, globals->get_props());
78 } catch (const sg_throwable& t) {
79 SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n "
80 << t.getFormattedMessage() << t.getOrigin());
84 model->init( object );
86 // Set position and orientation either
87 // indirectly through property refs
88 // or directly with static values.
89 SGPropertyNode * child = node->getChild("longitude-deg-prop");
91 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
93 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
95 child = node->getChild("latitude-deg-prop");
97 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
99 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
101 child = node->getChild("elevation-ft-prop");
103 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
105 model->setElevationFt(node->getDoubleValue("elevation-ft"));
107 child = node->getChild("roll-deg-prop");
109 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
111 model->setRollDeg(node->getDoubleValue("roll-deg"));
113 child = node->getChild("pitch-deg-prop");
115 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
117 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
119 child = node->getChild("heading-deg-prop");
121 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
123 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
125 // Add this model to the global scene graph
126 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
129 // Save this instance for updating
130 add_instance(instance);
139 FGModelMgr::unbind ()
145 double testNan(double val) throw (sg_range_exception)
148 throw sg_range_exception("value is nan");
152 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
154 void operator()(FGModelMgr::Instance* instance) const
156 SGModelPlacement* model = instance->model;
157 double lon, lat, elev, roll, pitch, heading;
160 // Optionally set position from properties
161 if (instance->lon_deg_node != 0)
162 lon = testNan(instance->lon_deg_node->getDoubleValue());
163 if (instance->lat_deg_node != 0)
164 lat = testNan(instance->lat_deg_node->getDoubleValue());
165 if (instance->elev_ft_node != 0)
166 elev = testNan(instance->elev_ft_node->getDoubleValue());
168 // Optionally set orientation from properties
169 if (instance->roll_deg_node != 0)
170 roll = testNan(instance->roll_deg_node->getDoubleValue());
171 if (instance->pitch_deg_node != 0)
172 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
173 if (instance->heading_deg_node != 0)
174 heading = testNan(instance->heading_deg_node->getDoubleValue());
175 } catch (const sg_range_exception& e) {
176 const char *path = instance->node->getStringValue("path",
178 SG_LOG(SG_GENERAL, SG_INFO, "Instance of model " << path
179 << " has invalid values");
182 // Optionally set position from properties
183 if (instance->lon_deg_node != 0)
184 model->setLongitudeDeg(lon);
185 if (instance->lat_deg_node != 0)
186 model->setLatitudeDeg(lat);
187 if (instance->elev_ft_node != 0)
188 model->setElevationFt(elev);
190 // Optionally set orientation from properties
191 if (instance->roll_deg_node != 0)
192 model->setRollDeg(roll);
193 if (instance->pitch_deg_node != 0)
194 model->setPitchDeg(pitch);
195 if (instance->heading_deg_node != 0)
196 model->setHeadingDeg(heading);
198 instance->model->update();
204 FGModelMgr::update (double dt)
206 std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
210 FGModelMgr::add_instance (Instance * instance)
212 _instances.push_back(instance);
216 FGModelMgr::remove_instance (Instance * instance)
218 vector<Instance *>::iterator it;
219 for (it = _instances.begin(); it != _instances.end(); it++) {
220 if (*it == instance) {
221 _instances.erase(it);
229 ////////////////////////////////////////////////////////////////////////
230 // Implementation of FGModelMgr::Instance
231 ////////////////////////////////////////////////////////////////////////
233 FGModelMgr::Instance::Instance ()
246 FGModelMgr::Instance::~Instance ()
253 ////////////////////////////////////////////////////////////////////////
254 // Implementation of FGModelMgr::Listener
255 ////////////////////////////////////////////////////////////////////////
258 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
260 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
263 _mgr->add_model(parent);
267 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
269 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
272 // search instance by node and remove it from scenegraph
273 vector<Instance *>::iterator it = _mgr->_instances.begin();
274 vector<Instance *>::iterator end = _mgr->_instances.end();
276 for (; it != end; ++it) {
277 Instance *instance = *it;
278 if (instance->node != child)
281 _mgr->_instances.erase(it);
282 osg::Node *branch = instance->model->getSceneGraph();
284 // if (shadows && instance->shadow)
285 // shadows->deleteOccluder(branch);
286 globals->get_scenery()->get_scene_graph()->removeChild(branch);
293 // end of modelmgr.cxx