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>
16 #include <simgear/scene/model/placement.hxx>
17 #include <simgear/scene/model/modellib.hxx>
18 #include <simgear/scene/model/shadowvolume.hxx>
19 #include <simgear/structure/exception.hxx>
21 #include <Main/fg_props.hxx>
22 #include <Scenery/scenery.hxx>
25 #include "modelmgr.hxx"
29 extern SGShadowVolume *shadows;
32 FGModelMgr::FGModelMgr ()
33 : _models(fgGetNode("/models", true)),
34 _listener(new Listener(this))
37 _models->addChangeListener(_listener);
40 FGModelMgr::~FGModelMgr ()
42 _models->removeChangeListener(_listener);
45 for (unsigned int i = 0; i < _instances.size(); i++) {
46 globals->get_scenery()->get_scene_graph()
47 ->removeKid(_instances[i]->model->getSceneGraph());
55 vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
57 for (unsigned int i = 0; i < model_nodes.size(); i++) {
59 add_model(model_nodes[i]);
60 } catch (const sg_throwable& t) {
61 SG_LOG(SG_GENERAL, SG_ALERT, t.getFormattedMessage() << t.getOrigin());
67 FGModelMgr::add_model (SGPropertyNode * node)
69 SG_LOG(SG_GENERAL, SG_INFO,
70 "Adding model " << node->getStringValue("name", "[unnamed]"));
71 Instance * instance = new Instance;
72 SGModelPlacement *model = new SGModelPlacement;
73 instance->model = model;
74 instance->node = node;
75 SGModelLib *model_lib = globals->get_model_lib();
76 ssgBranch *object = (ssgBranch *)model_lib->load_model(
77 globals->get_fg_root(),
78 node->getStringValue("path",
79 "Models/Geometry/glider.ac"),
81 globals->get_sim_time_sec(), /*cache_object=*/false);
83 model->init( object );
85 shadows->addOccluder((ssgBranch *)object, SGShadowVolume::occluderTypeTileObject);
87 // Set position and orientation either
88 // indirectly through property refs
89 // or directly with static values.
90 SGPropertyNode * child = node->getChild("longitude-deg-prop");
92 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
94 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
96 child = node->getChild("latitude-deg-prop");
98 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
100 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
102 child = node->getChild("elevation-ft-prop");
104 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
106 model->setElevationFt(node->getDoubleValue("elevation-ft"));
108 child = node->getChild("roll-deg-prop");
110 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
112 model->setRollDeg(node->getDoubleValue("roll-deg"));
114 child = node->getChild("pitch-deg-prop");
116 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
118 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
120 child = node->getChild("heading-deg-prop");
122 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
124 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
126 // Add this model to the global scene graph
127 globals->get_scenery()->get_scene_graph()->addKid(model->getSceneGraph());
129 // Register that one at the scenery manager
130 globals->get_scenery()->register_placement_transform(model->getTransform());
133 // Save this instance for updating
134 add_instance(instance);
143 FGModelMgr::unbind ()
148 FGModelMgr::update (double dt)
150 for (unsigned int i = 0; i < _instances.size(); i++) {
151 Instance * instance = _instances[i];
152 SGModelPlacement * model = instance->model;
154 // Optionally set position from properties
155 if (instance->lon_deg_node != 0)
156 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
157 if (instance->lat_deg_node != 0)
158 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
159 if (instance->elev_ft_node != 0)
160 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
162 // Optionally set orientation from properties
163 if (instance->roll_deg_node != 0)
164 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
165 if (instance->pitch_deg_node != 0)
166 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
167 if (instance->heading_deg_node != 0)
168 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
170 instance->model->update();
175 FGModelMgr::add_instance (Instance * instance)
177 _instances.push_back(instance);
181 FGModelMgr::remove_instance (Instance * instance)
183 vector<Instance *>::iterator it;
184 for (it = _instances.begin(); it != _instances.end(); it++) {
185 if (*it == instance) {
186 _instances.erase(it);
196 // ssgSetNearFar(_nearplane, _farplane);
197 // ssgCullAndDraw(_scene);
202 ////////////////////////////////////////////////////////////////////////
203 // Implementation of FGModelMgr::Instance
204 ////////////////////////////////////////////////////////////////////////
206 FGModelMgr::Instance::Instance ()
218 FGModelMgr::Instance::~Instance ()
220 // Unregister that one at the scenery manager
221 globals->get_scenery()->unregister_placement_transform(model->getTransform());
228 ////////////////////////////////////////////////////////////////////////
229 // Implementation of FGModelMgr::Listener
230 ////////////////////////////////////////////////////////////////////////
233 FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
235 if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
239 _mgr->add_model(parent);
240 } catch (const sg_throwable& t) {
241 SG_LOG(SG_GENERAL, SG_ALERT, t.getFormattedMessage() << t.getOrigin());
246 FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
248 if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
251 // search instance by node and remove it from scenegraph
252 vector<Instance *>::iterator it = _mgr->_instances.begin();
253 vector<Instance *>::iterator end = _mgr->_instances.end();
255 for (; it != end; ++it) {
256 Instance *instance = *it;
257 if (instance->node != child)
260 _mgr->_instances.erase(it);
261 ssgBranch *branch = (ssgBranch *)instance->model->getSceneGraph();
263 shadows->deleteOccluder(branch);
264 globals->get_scenery()->get_scene_graph()->removeKid(branch);
271 // end of modelmgr.cxx