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 *model_path = node->getStringValue("path", "Models/Geometry/glider.ac");
76 std::string fullPath = simgear::SGModelLib::findDataFile(model_path);
77 object = SGModelLib::loadDeferredModel(fullPath, globals->get_props());
78 } catch (const sg_throwable& t) {
79 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << model_path << ":\n "
80 << t.getFormattedMessage() << t.getOrigin());
84 Instance * instance = new Instance;
85 SGModelPlacement *model = new SGModelPlacement;
86 instance->model = model;
87 instance->node = node;
89 model->init( object );
90 double lon = node->getDoubleValue("longitude-deg"),
91 lat = node->getDoubleValue("latitude-deg"),
92 elevFt = node->getDoubleValue("elevation-ft");
94 model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt));
95 // Set position and orientation either
96 // indirectly through property refs
97 // or directly with static values.
98 SGPropertyNode * child = node->getChild("longitude-deg-prop");
100 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
102 child = node->getChild("latitude-deg-prop");
104 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
106 child = node->getChild("elevation-ft-prop");
108 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
110 child = node->getChild("roll-deg-prop");
112 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
114 model->setRollDeg(node->getDoubleValue("roll-deg"));
116 child = node->getChild("pitch-deg-prop");
118 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
120 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
122 child = node->getChild("heading-deg-prop");
124 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
126 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
128 // Add this model to the global scene graph
129 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
132 // Save this instance for updating
133 add_instance(instance);
142 FGModelMgr::unbind ()
148 double testNan(double val) throw (sg_range_exception)
150 if (SGMisc<double>::isNaN(val))
151 throw sg_range_exception("value is nan");
155 struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
157 void operator()(FGModelMgr::Instance* instance) const
159 SGModelPlacement* model = instance->model;
160 double roll, pitch, heading;
161 roll = pitch = heading = 0.0;
162 SGGeod pos = model->getPosition();
165 // Optionally set position from properties
166 if (instance->lon_deg_node != 0)
167 pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue()));
168 if (instance->lat_deg_node != 0)
169 pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue()));
170 if (instance->elev_ft_node != 0)
171 pos.setElevationFt(testNan(instance->elev_ft_node->getDoubleValue()));
173 // Optionally set orientation from properties
174 if (instance->roll_deg_node != 0)
175 roll = testNan(instance->roll_deg_node->getDoubleValue());
176 if (instance->pitch_deg_node != 0)
177 pitch = testNan(instance->pitch_deg_node->getDoubleValue());
178 if (instance->heading_deg_node != 0)
179 heading = testNan(instance->heading_deg_node->getDoubleValue());
180 } catch (const sg_range_exception&) {
181 const char *path = instance->node->getStringValue("path",
183 SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path
184 << " has invalid values");
188 model->setPosition(pos);
189 // Optionally set orientation from properties
190 if (instance->roll_deg_node != 0)
191 model->setRollDeg(roll);
192 if (instance->pitch_deg_node != 0)
193 model->setPitchDeg(pitch);
194 if (instance->heading_deg_node != 0)
195 model->setHeadingDeg(heading);
197 instance->model->update();
203 FGModelMgr::update (double dt)
205 std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
209 FGModelMgr::add_instance (Instance * instance)
211 _instances.push_back(instance);
215 FGModelMgr::remove_instance (Instance * instance)
217 vector<Instance *>::iterator it;
218 for (it = _instances.begin(); it != _instances.end(); it++) {
219 if (*it == instance) {
220 _instances.erase(it);
228 ////////////////////////////////////////////////////////////////////////
229 // Implementation of FGModelMgr::Instance
230 ////////////////////////////////////////////////////////////////////////
232 FGModelMgr::Instance::Instance ()
245 FGModelMgr::Instance::~Instance ()
252 ////////////////////////////////////////////////////////////////////////
253 // Implementation of FGModelMgr::Listener
254 ////////////////////////////////////////////////////////////////////////
257 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
259 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
262 _mgr->add_model(parent);
266 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
268 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
271 // search instance by node and remove it from scenegraph
272 vector<Instance *>::iterator it = _mgr->_instances.begin();
273 vector<Instance *>::iterator end = _mgr->_instances.end();
275 for (; it != end; ++it) {
276 Instance *instance = *it;
277 if (instance->node != child)
280 _mgr->_instances.erase(it);
281 osg::Node *branch = instance->model->getSceneGraph();
283 // if (shadows && instance->shadow)
284 // shadows->deleteOccluder(branch);
285 globals->get_scenery()->get_scene_graph()->removeChild(branch);
292 // end of modelmgr.cxx