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.
6 #include <simgear/compiler.h>
12 #include <simgear/scene/model/placement.hxx>
13 #include <simgear/scene/model/model.hxx>
15 #include <Main/fg_props.hxx>
16 #include <Scenery/scenery.hxx>
19 #include "modelmgr.hxx"
24 FGModelMgr::FGModelMgr ()
25 : _selector(new ssgSelector)
29 FGModelMgr::~FGModelMgr ()
31 for (unsigned int i = 0; i < _instances.size(); i++) {
32 globals->get_scenery()->get_models_branch()
33 ->removeKid(_instances[i]->model->getSceneGraph());
41 vector<SGPropertyNode_ptr> model_nodes =
42 fgGetNode("/models", true)->getChildren("model");
43 for (unsigned int i = 0; i < model_nodes.size(); i++) {
44 SGPropertyNode * node = model_nodes[i];
45 SG_LOG(SG_GENERAL, SG_INFO,
46 "Adding model " << node->getStringValue("name", "[unnamed]"));
47 Instance * instance = new Instance;
48 SGModelPlacement *model = new SGModelPlacement;
49 instance->model = model;
51 = sgLoad3DModel( globals->get_fg_root(),
52 node->getStringValue("path",
53 "Models/Geometry/glider.ac"),
55 globals->get_sim_time_sec() );
56 model->init( object );
58 // Set position and orientation either
59 // indirectly through property refs
60 // or directly with static values.
61 SGPropertyNode * child = node->getChild("longitude-deg-prop");
63 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
65 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
67 child = node->getChild("latitude-deg-prop");
69 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
71 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
73 child = node->getChild("elevation-ft-prop");
75 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
77 model->setElevationFt(node->getDoubleValue("elevation-ft"));
79 child = node->getChild("roll-deg-prop");
81 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
83 model->setRollDeg(node->getDoubleValue("roll-deg"));
85 child = node->getChild("pitch-deg-prop");
87 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
89 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
91 child = node->getChild("heading-deg-prop");
93 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
95 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
97 // Add this model to the global scene graph
98 globals->get_scenery()->get_scene_graph()->addKid(model->getSceneGraph());
100 // Save this instance for updating
101 add_instance(instance);
111 FGModelMgr::unbind ()
116 FGModelMgr::update (double dt)
118 for (unsigned int i = 0; i < _instances.size(); i++) {
119 Instance * instance = _instances[i];
120 SGModelPlacement * model = instance->model;
122 // Optionally set position from properties
123 if (instance->lon_deg_node != 0)
124 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
125 if (instance->lat_deg_node != 0)
126 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
127 if (instance->elev_ft_node != 0)
128 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
130 // Optionally set orientation from properties
131 if (instance->roll_deg_node != 0)
132 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
133 if (instance->pitch_deg_node != 0)
134 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
135 if (instance->heading_deg_node != 0)
136 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
138 instance->model->update( globals->get_scenery()->get_center() );
143 FGModelMgr::add_instance (Instance * instance)
145 _instances.push_back(instance);
149 FGModelMgr::remove_instance (Instance * instance)
151 vector<Instance *>::iterator it;
152 for (it = _instances.begin(); it != _instances.end(); it++) {
153 if (*it == instance) {
154 _instances.erase(it);
164 // ssgSetNearFar(_nearplane, _farplane);
165 // ssgCullAndDraw(_scene);
170 ////////////////////////////////////////////////////////////////////////
171 // Implementation of FGModelMgr::Instance
172 ////////////////////////////////////////////////////////////////////////
174 FGModelMgr::Instance::Instance ()
185 FGModelMgr::Instance::~Instance ()
190 // end of modelmgr.cxx