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.
8 #include <Main/fg_props.hxx>
9 #include <Scenery/scenery.hxx>
12 #include "placement.hxx"
14 #include "modelmgr.hxx"
17 FGModelMgr::FGModelMgr ()
18 : _selector(new ssgSelector)
22 FGModelMgr::~FGModelMgr ()
24 for (unsigned int i = 0; i < _instances.size(); i++) {
25 globals->get_scenery()->get_models_branch()
26 ->removeKid(_instances[i]->model->getSceneGraph());
34 vector<SGPropertyNode_ptr> model_nodes =
35 fgGetNode("/models", true)->getChildren("model");
36 for (unsigned int i = 0; i < model_nodes.size(); i++) {
37 SGPropertyNode * node = model_nodes[i];
38 SG_LOG(SG_GENERAL, SG_INFO,
39 "Adding model " << node->getStringValue("name", "[unnamed]"));
40 Instance * instance = new Instance;
41 FGModelPlacement *model = new FGModelPlacement;
42 instance->model = model;
44 = fgLoad3DModel( globals->get_fg_root(),
45 node->getStringValue("path",
46 "Models/Geometry/glider.ac"),
48 globals->get_sim_time_sec() );
49 model->init( object );
51 // Set position and orientation either
52 // indirectly through property refs
53 // or directly with static values.
54 SGPropertyNode * child = node->getChild("longitude-deg-prop");
56 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
58 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
60 child = node->getChild("latitude-deg-prop");
62 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
64 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
66 child = node->getChild("elevation-ft-prop");
68 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
70 model->setElevationFt(node->getDoubleValue("elevation-ft"));
72 child = node->getChild("roll-deg-prop");
74 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
76 model->setRollDeg(node->getDoubleValue("roll-deg"));
78 child = node->getChild("pitch-deg-prop");
80 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
82 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
84 child = node->getChild("heading-deg-prop");
86 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
88 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
90 // Add this model to the global scene graph
91 globals->get_scenery()->get_scene_graph()->addKid(model->getSceneGraph());
93 // Save this instance for updating
94 add_instance(instance);
104 FGModelMgr::unbind ()
109 FGModelMgr::update (double dt)
111 for (unsigned int i = 0; i < _instances.size(); i++) {
112 Instance * instance = _instances[i];
113 FGModelPlacement * model = instance->model;
115 // Optionally set position from properties
116 if (instance->lon_deg_node != 0)
117 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
118 if (instance->lat_deg_node != 0)
119 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
120 if (instance->elev_ft_node != 0)
121 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
123 // Optionally set orientation from properties
124 if (instance->roll_deg_node != 0)
125 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
126 if (instance->pitch_deg_node != 0)
127 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
128 if (instance->heading_deg_node != 0)
129 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
131 instance->model->update( globals->get_scenery()->get_center() );
136 FGModelMgr::add_instance (Instance * instance)
138 _instances.push_back(instance);
142 FGModelMgr::remove_instance (Instance * instance)
144 vector<Instance *>::iterator it;
145 for (it = _instances.begin(); it != _instances.end(); it++) {
146 if (*it == instance) {
147 _instances.erase(it);
157 // ssgSetNearFar(_nearplane, _farplane);
158 // ssgCullAndDraw(_scene);
163 ////////////////////////////////////////////////////////////////////////
164 // Implementation of FGModelMgr::Instance
165 ////////////////////////////////////////////////////////////////////////
167 FGModelMgr::Instance::Instance ()
178 FGModelMgr::Instance::~Instance ()
183 // end of modelmgr.cxx