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 : _scene(new ssgRoot),
18 FGModelMgr::~FGModelMgr ()
20 for (int i = 0; i < _instances.size(); i++) {
29 vector<SGPropertyNode *> model_nodes =
30 fgGetNode("/models", true)->getChildren("model");
31 for (int i = 0; i < model_nodes.size(); i++) {
32 SGPropertyNode * node = model_nodes[i];
33 SG_LOG(SG_GENERAL, SG_INFO,
34 "Adding model " << node->getStringValue("name", "[unnamed]"));
35 Instance * instance = new Instance;
36 FG3DModel * model = new FG3DModel;
37 instance->model = model;
38 model->init(node->getStringValue("path", "Models/Geometry/glider.ac"));
40 // Set position and orientation either
41 // indirectly through property refs
42 // or directly with static values.
43 SGPropertyNode * child = node->getChild("longitude-deg-prop");
45 instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
47 model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
49 child = node->getChild("latitude-deg-prop");
51 instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
53 model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
55 child = node->getChild("elevation-ft-prop");
57 instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
59 model->setElevationFt(node->getDoubleValue("elevation-ft"));
61 child = node->getChild("roll-deg-prop");
63 instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
65 model->setRollDeg(node->getDoubleValue("roll-deg"));
67 child = node->getChild("pitch-deg-prop");
69 instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
71 model->setPitchDeg(node->getDoubleValue("pitch-deg"));
73 child = node->getChild("heading-deg-prop");
75 instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
77 model->setHeadingDeg(node->getDoubleValue("heading-deg"));
79 // Add this model to the scene graph
80 _scene->addKid(model->getSceneGraph());
82 // Save this instance for updating
83 _instances.push_back(instance);
98 FGModelMgr::update (int dt)
100 for (int i = 0; i < _instances.size(); i++) {
101 Instance * instance = _instances[i];
102 FG3DModel * model = instance->model;
104 // Optionally set position from properties
105 if (instance->lon_deg_node != 0)
106 model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
107 if (instance->lat_deg_node != 0)
108 model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
109 if (instance->elev_ft_node != 0)
110 model->setElevationFt(instance->elev_ft_node->getDoubleValue());
112 // Optionally set orientation from properties
113 if (instance->roll_deg_node != 0)
114 model->setRollDeg(instance->roll_deg_node->getDoubleValue());
115 if (instance->pitch_deg_node != 0)
116 model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
117 if (instance->heading_deg_node != 0)
118 model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
120 instance->model->update(dt);
127 ssgSetNearFar(_nearplane, _farplane);
128 ssgCullAndDraw(_scene);
133 ////////////////////////////////////////////////////////////////////////
134 // Implementation of FGModelMgr::Instance
135 ////////////////////////////////////////////////////////////////////////
137 FGModelMgr::Instance::Instance ()
148 FGModelMgr::Instance::~Instance ()
153 // end of modelmgr.cxx