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.
10 #include <simgear/compiler.h>
14 #include <simgear/scene/model/placement.hxx>
15 #include <simgear/scene/model/modellib.hxx>
16 #include <simgear/structure/exception.hxx>
18 #include <Main/fg_props.hxx>
19 #include <Scenery/scenery.hxx>
22 #include "modelmgr.hxx"
27 // extern SGShadowVolume *shadows;
30 FGModelMgr::FGModelMgr ()
31 : _models(fgGetNode("/models", true)),
32 _listener(new Listener(this))
34 _models->addChangeListener(_listener);
37 FGModelMgr::~FGModelMgr ()
39 _models->removeChangeListener(_listener);
42 for (unsigned int i = 0; i < _instances.size(); i++) {
43 globals->get_scenery()->get_scene_graph()
44 ->removeChild(_instances[i]->model->getSceneGraph());
52 vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
54 for (unsigned int i = 0; i < model_nodes.size(); i++) {
56 add_model(model_nodes[i]);
57 } catch (const sg_throwable& t) {
58 SG_LOG(SG_GENERAL, SG_ALERT, t.getFormattedMessage() << t.getOrigin());
64 FGModelMgr::add_model (SGPropertyNode * node)
66 SG_LOG(SG_GENERAL, SG_INFO,
67 "Adding model " << node->getStringValue("name", "[unnamed]"));
68 Instance * instance = new Instance;
69 SGModelPlacement *model = new SGModelPlacement;
70 instance->model = model;
71 instance->node = node;
72 SGModelLib *model_lib = globals->get_model_lib();
73 osg::Node *object = model_lib->load_model(
74 globals->get_fg_root(),
75 node->getStringValue("path",
76 "Models/Geometry/glider.ac"),
78 globals->get_sim_time_sec(), /*cache_object=*/false);
80 model->init( object );
82 // Set position and orientation either
83 // indirectly through property refs
84 // or directly with static values.
85 SGPropertyNode * child = node->getChild("longitude-deg-prop");
87 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
89 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
91 child = node->getChild("latitude-deg-prop");
93 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
95 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
97 child = node->getChild("elevation-ft-prop");
99 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
101 model->setElevationFt(node->getDoubleValue("elevation-ft"));
103 child = node->getChild("roll-deg-prop");
105 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
107 model->setRollDeg(node->getDoubleValue("roll-deg"));
109 child = node->getChild("pitch-deg-prop");
111 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
113 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
115 child = node->getChild("heading-deg-prop");
117 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
119 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
121 // Add this model to the global scene graph
122 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
124 // Register that one at the scenery manager
125 globals->get_scenery()->register_placement_transform(model->getTransform());
128 // Save this instance for updating
129 add_instance(instance);
138 FGModelMgr::unbind ()
143 FGModelMgr::update (double dt)
145 for (unsigned int i = 0; i < _instances.size(); i++) {
146 Instance * instance = _instances[i];
147 SGModelPlacement * model = instance->model;
149 // Optionally set position from properties
150 if (instance->lon_deg_node != 0)
151 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
152 if (instance->lat_deg_node != 0)
153 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
154 if (instance->elev_ft_node != 0)
155 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
157 // Optionally set orientation from properties
158 if (instance->roll_deg_node != 0)
159 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
160 if (instance->pitch_deg_node != 0)
161 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
162 if (instance->heading_deg_node != 0)
163 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
165 instance->model->update();
168 // if (shadows && !instance->shadow) {
169 // osg::Node *branch = instance->model->getSceneGraph();
170 // shadows->addOccluder(branch, SGShadowVolume::occluderTypeTileObject);
171 // instance->shadow = true;
177 FGModelMgr::add_instance (Instance * instance)
179 _instances.push_back(instance);
183 FGModelMgr::remove_instance (Instance * instance)
185 vector<Instance *>::iterator it;
186 for (it = _instances.begin(); it != _instances.end(); it++) {
187 if (*it == instance) {
188 _instances.erase(it);
196 ////////////////////////////////////////////////////////////////////////
197 // Implementation of FGModelMgr::Instance
198 ////////////////////////////////////////////////////////////////////////
200 FGModelMgr::Instance::Instance ()
213 FGModelMgr::Instance::~Instance ()
215 // Unregister that one at the scenery manager
216 globals->get_scenery()->unregister_placement_transform(model->getTransform());
223 ////////////////////////////////////////////////////////////////////////
224 // Implementation of FGModelMgr::Listener
225 ////////////////////////////////////////////////////////////////////////
228 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
230 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
234 _mgr->add_model(parent);
235 } catch (const sg_throwable& t) {
236 SG_LOG(SG_GENERAL, SG_ALERT, t.getFormattedMessage() << t.getOrigin());
241 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
243 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
246 // search instance by node and remove it from scenegraph
247 vector<Instance *>::iterator it = _mgr->_instances.begin();
248 vector<Instance *>::iterator end = _mgr->_instances.end();
250 for (; it != end; ++it) {
251 Instance *instance = *it;
252 if (instance->node != child)
255 _mgr->_instances.erase(it);
256 osg::Node *branch = instance->model->getSceneGraph();
258 // if (shadows && instance->shadow)
259 // shadows->deleteOccluder(branch);
260 globals->get_scenery()->get_scene_graph()->removeChild(branch);
267 // end of modelmgr.cxx