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);
46 FGModelMgr::~FGModelMgr ()
48 _models->removeChangeListener(_listener);
51 osg::Group *scene_graph = globals->get_scenery()->get_scene_graph();
53 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Warning: The scenegraph wass deleted before the models could be removed");
55 for (unsigned int i = 0; i < _instances.size(); i++) {
57 scene_graph->removeChild(_instances[i]->model->getSceneGraph());
65 vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
67 for (unsigned int i = 0; i < model_nodes.size(); i++)
68 add_model(model_nodes[i]);
72 FGModelMgr::add_model (SGPropertyNode * node)
74 SG_LOG(SG_AIRCRAFT, SG_INFO,
75 "Adding model " << node->getStringValue("name", "[unnamed]"));
77 const char *model_path = node->getStringValue("path", "Models/Geometry/glider.ac");
81 std::string fullPath = simgear::SGModelLib::findDataFile(model_path);
82 object = SGModelLib::loadDeferredModel(fullPath, globals->get_props());
83 } catch (const sg_throwable& t) {
84 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << model_path << ":\n "
85 << t.getFormattedMessage() << t.getOrigin());
89 Instance * instance = new Instance;
90 SGModelPlacement *model = new SGModelPlacement;
91 instance->model = model;
92 instance->node = node;
94 model->init( object );
95 double lon = node->getDoubleValue("longitude-deg"),
96 lat = node->getDoubleValue("latitude-deg"),
97 elevFt = node->getDoubleValue("elevation-ft");
99 model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt));
100 // Set position and orientation either
101 // indirectly through property refs
102 // or directly with static values.
103 SGPropertyNode * child = node->getChild("longitude-deg-prop");
105 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
107 child = node->getChild("latitude-deg-prop");
109 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
111 child = node->getChild("elevation-ft-prop");
113 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
115 child = node->getChild("roll-deg-prop");
117 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
119 model->setRollDeg(node->getDoubleValue("roll-deg"));
121 child = node->getChild("pitch-deg-prop");
123 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
125 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
127 child = node->getChild("heading-deg-prop");
129 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
131 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
133 // Add this model to the global scene graph
134 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
137 // Save this instance for updating
138 add_instance(instance);
147 FGModelMgr::unbind ()
153 double testNan(double val) throw (sg_range_exception)
155 if (SGMisc<double>::isNaN(val))
156 throw sg_range_exception("value is nan");
160 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
162 void operator()(FGModelMgr::Instance* instance) const
164 SGModelPlacement* model = instance->model;
165 double roll, pitch, heading;
166 roll = pitch = heading = 0.0;
167 SGGeod pos = model->getPosition();
170 // Optionally set position from properties
171 if (instance->lon_deg_node != 0)
172 pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue()));
173 if (instance->lat_deg_node != 0)
174 pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue()));
175 if (instance->elev_ft_node != 0)
176 pos.setElevationFt(testNan(instance->elev_ft_node->getDoubleValue()));
178 // Optionally set orientation from properties
179 if (instance->roll_deg_node != 0)
180 roll = testNan(instance->roll_deg_node->getDoubleValue());
181 if (instance->pitch_deg_node != 0)
182 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
183 if (instance->heading_deg_node != 0)
184 heading = testNan(instance->heading_deg_node->getDoubleValue());
185 } catch (const sg_range_exception&) {
186 const char *path = instance->node->getStringValue("path",
188 SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
189 << " has invalid values");
193 model->setPosition(pos);
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