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>
22 #include <simgear/scene/model/placement.hxx>
23 #include <simgear/scene/model/modellib.hxx>
24 #include <simgear/structure/exception.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Scenery/scenery.hxx>
30 #include "modelmgr.hxx"
34 using namespace simgear;
37 // extern SGShadowVolume *shadows;
39 FGModelMgr::FGModelMgr ()
40 : _models(fgGetNode("/models", true)),
41 _listener(new Listener(this))
43 _models->addChangeListener(_listener);
46 FGModelMgr::~FGModelMgr ()
48 _models->removeChangeListener(_listener);
51 for (unsigned int i = 0; i < _instances.size(); i++) {
52 globals->get_scenery()->get_scene_graph()
53 ->removeChild(_instances[i]->model->getSceneGraph());
61 vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
63 for (unsigned int i = 0; i < model_nodes.size(); i++)
64 add_model(model_nodes[i]);
68 FGModelMgr::add_model (SGPropertyNode * node)
70 SG_LOG(SG_GENERAL, SG_INFO,
71 "Adding model " << node->getStringValue("name", "[unnamed]"));
72 Instance * instance = new Instance;
73 SGModelPlacement *model = new SGModelPlacement;
74 instance->model = model;
75 instance->node = node;
77 const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
81 object = SGModelLib::loadPagedModel(path, globals->get_props());
82 } catch (const sg_throwable& t) {
83 SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n "
84 << t.getFormattedMessage() << t.getOrigin());
88 model->init( object );
90 // Set position and orientation either
91 // indirectly through property refs
92 // or directly with static values.
93 SGPropertyNode * child = node->getChild("longitude-deg-prop");
95 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
97 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
99 child = node->getChild("latitude-deg-prop");
101 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
103 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
105 child = node->getChild("elevation-ft-prop");
107 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
109 model->setElevationFt(node->getDoubleValue("elevation-ft"));
111 child = node->getChild("roll-deg-prop");
113 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
115 model->setRollDeg(node->getDoubleValue("roll-deg"));
117 child = node->getChild("pitch-deg-prop");
119 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
121 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
123 child = node->getChild("heading-deg-prop");
125 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
127 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
129 // Add this model to the global scene graph
130 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
133 // Save this instance for updating
134 add_instance(instance);
143 FGModelMgr::unbind ()
149 double testNan(double val) throw (sg_range_exception)
152 throw sg_range_exception("value is nan");
156 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
158 void operator()(FGModelMgr::Instance* instance) const
160 SGModelPlacement* model = instance->model;
161 double lon, lat, elev, roll, pitch, heading;
164 // Optionally set position from properties
165 if (instance->lon_deg_node != 0)
166 lon = testNan(instance->lon_deg_node->getDoubleValue());
167 if (instance->lat_deg_node != 0)
168 lat = testNan(instance->lat_deg_node->getDoubleValue());
169 if (instance->elev_ft_node != 0)
170 elev = testNan(instance->elev_ft_node->getDoubleValue());
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",
182 SG_LOG(SG_GENERAL, SG_INFO, "Instance of model " << path
183 << " has invalid values");
186 // Optionally set position from properties
187 if (instance->lon_deg_node != 0)
188 model->setLongitudeDeg(lon);
189 if (instance->lat_deg_node != 0)
190 model->setLatitudeDeg(lat);
191 if (instance->elev_ft_node != 0)
192 model->setElevationFt(elev);
194 // Optionally set orientation from properties
195 if (instance->roll_deg_node != 0)
196 model->setRollDeg(roll);
197 if (instance->pitch_deg_node != 0)
198 model->setPitchDeg(pitch);
199 if (instance->heading_deg_node != 0)
200 model->setHeadingDeg(heading);
202 instance->model->update();
208 FGModelMgr::update (double dt)
210 std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
214 FGModelMgr::add_instance (Instance * instance)
216 _instances.push_back(instance);
220 FGModelMgr::remove_instance (Instance * instance)
222 vector<Instance *>::iterator it;
223 for (it = _instances.begin(); it != _instances.end(); it++) {
224 if (*it == instance) {
225 _instances.erase(it);
233 ////////////////////////////////////////////////////////////////////////
234 // Implementation of FGModelMgr::Instance
235 ////////////////////////////////////////////////////////////////////////
237 FGModelMgr::Instance::Instance ()
250 FGModelMgr::Instance::~Instance ()
257 ////////////////////////////////////////////////////////////////////////
258 // Implementation of FGModelMgr::Listener
259 ////////////////////////////////////////////////////////////////////////
262 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
264 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
267 _mgr->add_model(parent);
271 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
273 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
276 // search instance by node and remove it from scenegraph
277 vector<Instance *>::iterator it = _mgr->_instances.begin();
278 vector<Instance *>::iterator end = _mgr->_instances.end();
280 for (; it != end; ++it) {
281 Instance *instance = *it;
282 if (instance->node != child)
285 _mgr->_instances.erase(it);
286 osg::Node *branch = instance->model->getSceneGraph();
288 // if (shadows && instance->shadow)
289 // shadows->deleteOccluder(branch);
290 globals->get_scenery()->get_scene_graph()->removeChild(branch);
297 // end of modelmgr.cxx