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>
11 #include "modelmgr.hxx"
15 FGModelMgr::FGModelMgr ()
16 : _selector(new ssgSelector)
20 FGModelMgr::~FGModelMgr ()
22 for (unsigned int i = 0; i < _instances.size(); i++) {
23 globals->get_scenery()->get_models_branch()
24 ->removeKid(_instances[i]->model->getSceneGraph());
32 vector<SGPropertyNode_ptr> model_nodes =
33 fgGetNode("/models", true)->getChildren("model");
34 for (unsigned int i = 0; i < model_nodes.size(); i++) {
35 SGPropertyNode * node = model_nodes[i];
36 SG_LOG(SG_GENERAL, SG_INFO,
37 "Adding model " << node->getStringValue("name", "[unnamed]"));
38 Instance * instance = new Instance;
39 FGModelPlacement * model = new FGModelPlacement;
40 instance->model = model;
41 model->init( globals->get_fg_root(),
42 node->getStringValue("path", "Models/Geometry/glider.ac"),
44 globals->get_sim_time_sec() );
46 // Set position and orientation either
47 // indirectly through property refs
48 // or directly with static values.
49 SGPropertyNode * child = node->getChild("longitude-deg-prop");
51 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
53 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
55 child = node->getChild("latitude-deg-prop");
57 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
59 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
61 child = node->getChild("elevation-ft-prop");
63 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
65 model->setElevationFt(node->getDoubleValue("elevation-ft"));
67 child = node->getChild("roll-deg-prop");
69 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
71 model->setRollDeg(node->getDoubleValue("roll-deg"));
73 child = node->getChild("pitch-deg-prop");
75 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
77 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
79 child = node->getChild("heading-deg-prop");
81 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
83 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
85 // Add this model to the global scene graph
86 globals->get_scenery()->get_scene_graph()->addKid(model->getSceneGraph());
88 // Save this instance for updating
89 add_instance(instance);
104 FGModelMgr::update (double dt)
106 for (unsigned int i = 0; i < _instances.size(); i++) {
107 Instance * instance = _instances[i];
108 FGModelPlacement * model = instance->model;
110 // Optionally set position from properties
111 if (instance->lon_deg_node != 0)
112 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
113 if (instance->lat_deg_node != 0)
114 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
115 if (instance->elev_ft_node != 0)
116 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
118 // Optionally set orientation from properties
119 if (instance->roll_deg_node != 0)
120 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
121 if (instance->pitch_deg_node != 0)
122 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
123 if (instance->heading_deg_node != 0)
124 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
126 instance->model->update( globals->get_scenery()->get_center() );
131 FGModelMgr::add_instance (Instance * instance)
133 _instances.push_back(instance);
137 FGModelMgr::remove_instance (Instance * instance)
139 vector<Instance *>::iterator it;
140 for (it = _instances.begin(); it != _instances.end(); it++) {
141 if (*it == instance) {
142 _instances.erase(it);
152 // ssgSetNearFar(_nearplane, _farplane);
153 // ssgCullAndDraw(_scene);
158 ////////////////////////////////////////////////////////////////////////
159 // Implementation of FGModelMgr::Instance
160 ////////////////////////////////////////////////////////////////////////
162 FGModelMgr::Instance::Instance ()
173 FGModelMgr::Instance::~Instance ()
178 // end of modelmgr.cxx