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>
19 #include <Main/fg_props.hxx>
20 #include <Scenery/scenery.hxx>
23 #include "modelmgr.hxx"
28 FGModelMgr::FGModelMgr ()
29 : _selector(new ssgSelector)
33 FGModelMgr::~FGModelMgr ()
35 for (unsigned int i = 0; i < _instances.size(); i++) {
36 globals->get_scenery()->get_models_branch()
37 ->removeKid(_instances[i]->model->getSceneGraph());
45 vector<SGPropertyNode_ptr> model_nodes =
46 fgGetNode("/models", true)->getChildren("model");
47 for (unsigned int i = 0; i < model_nodes.size(); i++) {
48 SGPropertyNode * node = model_nodes[i];
49 SG_LOG(SG_GENERAL, SG_INFO,
50 "Adding model " << node->getStringValue("name", "[unnamed]"));
51 Instance * instance = new Instance;
52 SGModelPlacement *model = new SGModelPlacement;
53 instance->model = model;
55 = sgLoad3DModel( globals->get_fg_root(),
56 node->getStringValue("path",
57 "Models/Geometry/glider.ac"),
59 globals->get_sim_time_sec() );
60 model->init( object );
62 // Set position and orientation either
63 // indirectly through property refs
64 // or directly with static values.
65 SGPropertyNode * child = node->getChild("longitude-deg-prop");
67 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
69 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
71 child = node->getChild("latitude-deg-prop");
73 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
75 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
77 child = node->getChild("elevation-ft-prop");
79 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
81 model->setElevationFt(node->getDoubleValue("elevation-ft"));
83 child = node->getChild("roll-deg-prop");
85 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
87 model->setRollDeg(node->getDoubleValue("roll-deg"));
89 child = node->getChild("pitch-deg-prop");
91 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
93 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
95 child = node->getChild("heading-deg-prop");
97 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
99 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
101 // Add this model to the global scene graph
102 globals->get_scenery()->get_scene_graph()->addKid(model->getSceneGraph());
104 // Register that one at the scenery manager
105 globals->get_scenery()->register_placement_transform(model->getTransform());
108 // Save this instance for updating
109 add_instance(instance);
119 FGModelMgr::unbind ()
124 FGModelMgr::update (double dt)
126 for (unsigned int i = 0; i < _instances.size(); i++) {
127 Instance * instance = _instances[i];
128 SGModelPlacement * model = instance->model;
130 // Optionally set position from properties
131 if (instance->lon_deg_node != 0)
132 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
133 if (instance->lat_deg_node != 0)
134 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
135 if (instance->elev_ft_node != 0)
136 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
138 // Optionally set orientation from properties
139 if (instance->roll_deg_node != 0)
140 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
141 if (instance->pitch_deg_node != 0)
142 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
143 if (instance->heading_deg_node != 0)
144 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
146 instance->model->update( globals->get_scenery()->get_center() );
151 FGModelMgr::add_instance (Instance * instance)
153 _instances.push_back(instance);
157 FGModelMgr::remove_instance (Instance * instance)
159 vector<Instance *>::iterator it;
160 for (it = _instances.begin(); it != _instances.end(); it++) {
161 if (*it == instance) {
162 _instances.erase(it);
172 // ssgSetNearFar(_nearplane, _farplane);
173 // ssgCullAndDraw(_scene);
178 ////////////////////////////////////////////////////////////////////////
179 // Implementation of FGModelMgr::Instance
180 ////////////////////////////////////////////////////////////////////////
182 FGModelMgr::Instance::Instance ()
193 FGModelMgr::Instance::~Instance ()
195 // Unregister that one at the scenery manager
196 globals->get_scenery()->unregister_placement_transform(model->getTransform());
201 // end of modelmgr.cxx