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_GENERAL, SG_INFO,
72 "Adding model " << node->getStringValue("name", "[unnamed]"));
74 const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
78 object = SGModelLib::loadPagedModel(path, globals->get_props());
79 } catch (const sg_throwable& t) {
80 SG_LOG(SG_GENERAL, 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;
166 // Optionally set position from properties
167 if (instance->lon_deg_node != 0)
168 lon = testNan(instance->lon_deg_node->getDoubleValue());
169 if (instance->lat_deg_node != 0)
170 lat = testNan(instance->lat_deg_node->getDoubleValue());
171 if (instance->elev_ft_node != 0)
172 elev = testNan(instance->elev_ft_node->getDoubleValue());
174 // Optionally set orientation from properties
175 if (instance->roll_deg_node != 0)
176 roll = testNan(instance->roll_deg_node->getDoubleValue());
177 if (instance->pitch_deg_node != 0)
178 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
179 if (instance->heading_deg_node != 0)
180 heading = testNan(instance->heading_deg_node->getDoubleValue());
181 } catch (const sg_range_exception&) {
182 const char *path = instance->node->getStringValue("path",
184 SG_LOG(SG_GENERAL, SG_INFO, "Instance of model " << path
185 << " has invalid values");
188 // Optionally set position from properties
189 if (instance->lon_deg_node != 0)
190 model->setLongitudeDeg(lon);
191 if (instance->lat_deg_node != 0)
192 model->setLatitudeDeg(lat);
193 if (instance->elev_ft_node != 0)
194 model->setElevationFt(elev);
196 // Optionally set orientation from properties
197 if (instance->roll_deg_node != 0)
198 model->setRollDeg(roll);
199 if (instance->pitch_deg_node != 0)
200 model->setPitchDeg(pitch);
201 if (instance->heading_deg_node != 0)
202 model->setHeadingDeg(heading);
204 instance->model->update();
210 FGModelMgr::update (double dt)
212 std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
216 FGModelMgr::add_instance (Instance * instance)
218 _instances.push_back(instance);
222 FGModelMgr::remove_instance (Instance * instance)
224 vector<Instance *>::iterator it;
225 for (it = _instances.begin(); it != _instances.end(); it++) {
226 if (*it == instance) {
227 _instances.erase(it);
235 ////////////////////////////////////////////////////////////////////////
236 // Implementation of FGModelMgr::Instance
237 ////////////////////////////////////////////////////////////////////////
239 FGModelMgr::Instance::Instance ()
252 FGModelMgr::Instance::~Instance ()
259 ////////////////////////////////////////////////////////////////////////
260 // Implementation of FGModelMgr::Listener
261 ////////////////////////////////////////////////////////////////////////
264 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
266 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
269 _mgr->add_model(parent);
273 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
275 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
278 // search instance by node and remove it from scenegraph
279 vector<Instance *>::iterator it = _mgr->_instances.begin();
280 vector<Instance *>::iterator end = _mgr->_instances.end();
282 for (; it != end; ++it) {
283 Instance *instance = *it;
284 if (instance->node != child)
287 _mgr->_instances.erase(it);
288 osg::Node *branch = instance->model->getSceneGraph();
290 // if (shadows && instance->shadow)
291 // shadows->deleteOccluder(branch);
292 globals->get_scenery()->get_scene_graph()->removeChild(branch);
299 // end of modelmgr.cxx