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"
33 using namespace simgear;
36 // extern SGShadowVolume *shadows;
38 FGModelMgr::FGModelMgr ()
39 : _models(fgGetNode("/models", true)),
40 _listener(new Listener(this))
42 _models->addChangeListener(_listener);
45 FGModelMgr::~FGModelMgr ()
47 _models->removeChangeListener(_listener);
50 for (unsigned int i = 0; i < _instances.size(); i++) {
51 globals->get_scenery()->get_scene_graph()
52 ->removeChild(_instances[i]->model->getSceneGraph());
60 vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
62 for (unsigned int i = 0; i < model_nodes.size(); i++)
63 add_model(model_nodes[i]);
67 FGModelMgr::add_model (SGPropertyNode * node)
69 SG_LOG(SG_AIRCRAFT, SG_INFO,
70 "Adding model " << node->getStringValue("name", "[unnamed]"));
72 const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
76 object = SGModelLib::loadDeferredModel(path, globals->get_props());
77 } catch (const sg_throwable& t) {
78 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << path << ":\n "
79 << t.getFormattedMessage() << t.getOrigin());
83 Instance * instance = new Instance;
84 SGModelPlacement *model = new SGModelPlacement;
85 instance->model = model;
86 instance->node = node;
88 model->init( object );
89 double lon = node->getDoubleValue("longitude-deg"),
90 lat = node->getDoubleValue("latitude-deg"),
91 elevFt = node->getDoubleValue("elevation-ft");
93 model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt));
94 // Set position and orientation either
95 // indirectly through property refs
96 // or directly with static values.
97 SGPropertyNode * child = node->getChild("longitude-deg-prop");
99 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
101 child = node->getChild("latitude-deg-prop");
103 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
105 child = node->getChild("elevation-ft-prop");
107 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
109 child = node->getChild("roll-deg-prop");
111 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
113 model->setRollDeg(node->getDoubleValue("roll-deg"));
115 child = node->getChild("pitch-deg-prop");
117 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
119 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
121 child = node->getChild("heading-deg-prop");
123 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
125 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
127 // Add this model to the global scene graph
128 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
131 // Save this instance for updating
132 add_instance(instance);
141 FGModelMgr::unbind ()
147 double testNan(double val) throw (sg_range_exception)
149 if (SGMisc<double>::isNaN(val))
150 throw sg_range_exception("value is nan");
154 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
156 void operator()(FGModelMgr::Instance* instance) const
158 SGModelPlacement* model = instance->model;
159 double roll, pitch, heading;
160 roll = pitch = heading = 0.0;
161 SGGeod pos = model->getPosition();
164 // Optionally set position from properties
165 if (instance->lon_deg_node != 0)
166 pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue()));
167 if (instance->lat_deg_node != 0)
168 pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue()));
169 if (instance->elev_ft_node != 0)
170 pos.setElevationFt(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_AIRCRAFT, SG_INFO, "Instance of model " << path
183 << " has invalid values");
187 model->setPosition(pos);
188 // Optionally set orientation from properties
189 if (instance->roll_deg_node != 0)
190 model->setRollDeg(roll);
191 if (instance->pitch_deg_node != 0)
192 model->setPitchDeg(pitch);
193 if (instance->heading_deg_node != 0)
194 model->setHeadingDeg(heading);
196 instance->model->update();
202 FGModelMgr::update (double dt)
204 std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
208 FGModelMgr::add_instance (Instance * instance)
210 _instances.push_back(instance);
214 FGModelMgr::remove_instance (Instance * instance)
216 vector<Instance *>::iterator it;
217 for (it = _instances.begin(); it != _instances.end(); it++) {
218 if (*it == instance) {
219 _instances.erase(it);
227 ////////////////////////////////////////////////////////////////////////
228 // Implementation of FGModelMgr::Instance
229 ////////////////////////////////////////////////////////////////////////
231 FGModelMgr::Instance::Instance ()
244 FGModelMgr::Instance::~Instance ()
251 ////////////////////////////////////////////////////////////////////////
252 // Implementation of FGModelMgr::Listener
253 ////////////////////////////////////////////////////////////////////////
256 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
258 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
261 _mgr->add_model(parent);
265 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
267 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
270 // search instance by node and remove it from scenegraph
271 vector<Instance *>::iterator it = _mgr->_instances.begin();
272 vector<Instance *>::iterator end = _mgr->_instances.end();
274 for (; it != end; ++it) {
275 Instance *instance = *it;
276 if (instance->node != child)
279 _mgr->_instances.erase(it);
280 osg::Node *branch = instance->model->getSceneGraph();
282 // if (shadows && instance->shadow)
283 // shadows->deleteOccluder(branch);
284 globals->get_scenery()->get_scene_graph()->removeChild(branch);
291 // end of modelmgr.cxx