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 "modelmgr.hxx"
8 #include <Main/fg_props.hxx>
11 FGModelMgr::FGModelMgr ()
12 : _selector(new ssgSelector)
16 FGModelMgr::~FGModelMgr ()
18 for (int i = 0; i < _instances.size(); i++) {
19 globals->get_models_branch()
20 ->removeKid(_instances[i]->model->getSceneGraph());
28 vector<SGPropertyNode *> model_nodes =
29 fgGetNode("/models", true)->getChildren("model");
30 for (int i = 0; i < model_nodes.size(); i++) {
31 SGPropertyNode * node = model_nodes[i];
32 SG_LOG(SG_GENERAL, SG_INFO,
33 "Adding model " << node->getStringValue("name", "[unnamed]"));
34 Instance * instance = new Instance;
35 FG3DModel * model = new FG3DModel;
36 instance->model = model;
37 model->init(node->getStringValue("path", "Models/Geometry/glider.ac"));
39 // Set position and orientation either
40 // indirectly through property refs
41 // or directly with static values.
42 SGPropertyNode * child = node->getChild("longitude-deg-prop");
44 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
46 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
48 child = node->getChild("latitude-deg-prop");
50 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
52 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
54 child = node->getChild("elevation-ft-prop");
56 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
58 model->setElevationFt(node->getDoubleValue("elevation-ft"));
60 child = node->getChild("roll-deg-prop");
62 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
64 model->setRollDeg(node->getDoubleValue("roll-deg"));
66 child = node->getChild("pitch-deg-prop");
68 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
70 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
72 child = node->getChild("heading-deg-prop");
74 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
76 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
78 // Add this model to the global scene graph
79 globals->get_scene_graph()->addKid(model->getSceneGraph());
81 // Save this instance for updating
82 _instances.push_back(instance);
97 FGModelMgr::update (int dt)
99 for (int i = 0; i < _instances.size(); i++) {
100 Instance * instance = _instances[i];
101 FG3DModel * model = instance->model;
103 // Optionally set position from properties
104 if (instance->lon_deg_node != 0)
105 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
106 if (instance->lat_deg_node != 0)
107 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
108 if (instance->elev_ft_node != 0)
109 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
111 // Optionally set orientation from properties
112 if (instance->roll_deg_node != 0)
113 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
114 if (instance->pitch_deg_node != 0)
115 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
116 if (instance->heading_deg_node != 0)
117 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
119 instance->model->update(dt);
126 // ssgSetNearFar(_nearplane, _farplane);
127 // ssgCullAndDraw(_scene);
132 ////////////////////////////////////////////////////////////////////////
133 // Implementation of FGModelMgr::Instance
134 ////////////////////////////////////////////////////////////////////////
136 FGModelMgr::Instance::Instance ()
147 FGModelMgr::Instance::~Instance ()
152 // end of modelmgr.cxx