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++)
55 add_model(model_nodes[i]);
59 FGModelMgr::add_model (SGPropertyNode * node)
61 SG_LOG(SG_GENERAL, SG_INFO,
62 "Adding model " << node->getStringValue("name", "[unnamed]"));
63 Instance * instance = new Instance;
64 SGModelPlacement *model = new SGModelPlacement;
65 instance->model = model;
66 instance->node = node;
67 SGModelLib *model_lib = globals->get_model_lib();
69 const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
73 object = model_lib->load_model(
74 globals->get_fg_root(),
77 globals->get_sim_time_sec(), /*cache_object=*/false);
78 } catch (const sg_throwable& t) {
79 SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n "
80 << t.getFormattedMessage() << t.getOrigin());
84 model->init( object );
86 // Set position and orientation either
87 // indirectly through property refs
88 // or directly with static values.
89 SGPropertyNode * child = node->getChild("longitude-deg-prop");
91 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
93 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
95 child = node->getChild("latitude-deg-prop");
97 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
99 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
101 child = node->getChild("elevation-ft-prop");
103 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
105 model->setElevationFt(node->getDoubleValue("elevation-ft"));
107 child = node->getChild("roll-deg-prop");
109 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
111 model->setRollDeg(node->getDoubleValue("roll-deg"));
113 child = node->getChild("pitch-deg-prop");
115 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
117 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
119 child = node->getChild("heading-deg-prop");
121 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
123 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
125 // Add this model to the global scene graph
126 globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
129 // Save this instance for updating
130 add_instance(instance);
139 FGModelMgr::unbind ()
144 FGModelMgr::update (double dt)
146 for (unsigned int i = 0; i < _instances.size(); i++) {
147 Instance * instance = _instances[i];
148 SGModelPlacement * model = instance->model;
150 // Optionally set position from properties
151 if (instance->lon_deg_node != 0)
152 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
153 if (instance->lat_deg_node != 0)
154 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
155 if (instance->elev_ft_node != 0)
156 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
158 // Optionally set orientation from properties
159 if (instance->roll_deg_node != 0)
160 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
161 if (instance->pitch_deg_node != 0)
162 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
163 if (instance->heading_deg_node != 0)
164 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
166 instance->model->update();
169 // if (shadows && !instance->shadow) {
170 // osg::Node *branch = instance->model->getSceneGraph();
171 // shadows->addOccluder(branch, SGShadowVolume::occluderTypeTileObject);
172 // instance->shadow = true;
178 FGModelMgr::add_instance (Instance * instance)
180 _instances.push_back(instance);
184 FGModelMgr::remove_instance (Instance * instance)
186 vector<Instance *>::iterator it;
187 for (it = _instances.begin(); it != _instances.end(); it++) {
188 if (*it == instance) {
189 _instances.erase(it);
197 ////////////////////////////////////////////////////////////////////////
198 // Implementation of FGModelMgr::Instance
199 ////////////////////////////////////////////////////////////////////////
201 FGModelMgr::Instance::Instance ()
214 FGModelMgr::Instance::~Instance ()
221 ////////////////////////////////////////////////////////////////////////
222 // Implementation of FGModelMgr::Listener
223 ////////////////////////////////////////////////////////////////////////
226 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
228 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
231 _mgr->add_model(parent);
235 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
237 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
240 // search instance by node and remove it from scenegraph
241 vector<Instance *>::iterator it = _mgr->_instances.begin();
242 vector<Instance *>::iterator end = _mgr->_instances.end();
244 for (; it != end; ++it) {
245 Instance *instance = *it;
246 if (instance->node != child)
249 _mgr->_instances.erase(it);
250 osg::Node *branch = instance->model->getSceneGraph();
252 // if (shadows && instance->shadow)
253 // shadows->deleteOccluder(branch);
254 globals->get_scenery()->get_scene_graph()->removeChild(branch);
261 // end of modelmgr.cxx