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/model.hxx>
18 #include <simgear/structure/exception.hxx>
20 #include <Main/fg_props.hxx>
21 #include <Scenery/scenery.hxx>
24 #include "modelmgr.hxx"
29 FGModelMgr::FGModelMgr ()
30 : _selector(new ssgSelector)
34 FGModelMgr::~FGModelMgr ()
36 for (unsigned int i = 0; i < _instances.size(); i++) {
37 globals->get_scenery()->get_models_branch()
38 ->removeKid(_instances[i]->model->getSceneGraph());
46 vector<SGPropertyNode_ptr> model_nodes =
47 fgGetNode("/models", true)->getChildren("model");
49 for (unsigned int i = 0; i < model_nodes.size(); i++) {
51 add_model(model_nodes[i]);
52 } catch (const sg_throwable& t) {
53 SG_LOG(SG_GENERAL, SG_ALERT, t.getFormattedMessage() << t.getOrigin());
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;
67 = sgLoad3DModel( globals->get_fg_root(),
68 node->getStringValue("path",
69 "Models/Geometry/glider.ac"),
71 globals->get_sim_time_sec() );
72 model->init( object );
74 // Set position and orientation either
75 // indirectly through property refs
76 // or directly with static values.
77 SGPropertyNode * child = node->getChild("longitude-deg-prop");
79 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
81 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
83 child = node->getChild("latitude-deg-prop");
85 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
87 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
89 child = node->getChild("elevation-ft-prop");
91 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
93 model->setElevationFt(node->getDoubleValue("elevation-ft"));
95 child = node->getChild("roll-deg-prop");
97 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
99 model->setRollDeg(node->getDoubleValue("roll-deg"));
101 child = node->getChild("pitch-deg-prop");
103 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
105 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
107 child = node->getChild("heading-deg-prop");
109 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
111 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
113 // Add this model to the global scene graph
114 globals->get_scenery()->get_scene_graph()->addKid(model->getSceneGraph());
116 // Register that one at the scenery manager
117 globals->get_scenery()->register_placement_transform(model->getTransform());
120 // Save this instance for updating
121 add_instance(instance);
130 FGModelMgr::unbind ()
135 FGModelMgr::update (double dt)
137 for (unsigned int i = 0; i < _instances.size(); i++) {
138 Instance * instance = _instances[i];
139 SGModelPlacement * model = instance->model;
141 // Optionally set position from properties
142 if (instance->lon_deg_node != 0)
143 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
144 if (instance->lat_deg_node != 0)
145 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
146 if (instance->elev_ft_node != 0)
147 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
149 // Optionally set orientation from properties
150 if (instance->roll_deg_node != 0)
151 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
152 if (instance->pitch_deg_node != 0)
153 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
154 if (instance->heading_deg_node != 0)
155 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
157 instance->model->update();
162 FGModelMgr::add_instance (Instance * instance)
164 _instances.push_back(instance);
168 FGModelMgr::remove_instance (Instance * instance)
170 vector<Instance *>::iterator it;
171 for (it = _instances.begin(); it != _instances.end(); it++) {
172 if (*it == instance) {
173 _instances.erase(it);
183 // ssgSetNearFar(_nearplane, _farplane);
184 // ssgCullAndDraw(_scene);
189 ////////////////////////////////////////////////////////////////////////
190 // Implementation of FGModelMgr::Instance
191 ////////////////////////////////////////////////////////////////////////
193 FGModelMgr::Instance::Instance ()
204 FGModelMgr::Instance::~Instance ()
206 // Unregister that one at the scenery manager
207 globals->get_scenery()->unregister_placement_transform(model->getTransform());
212 // end of modelmgr.cxx