1 // acmodel.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
10 #include <cstring> // for strcmp()
11 #include <boost/foreach.hpp>
13 #include <simgear/compiler.h>
14 #include <simgear/debug/logstream.hxx>
15 #include <simgear/structure/exception.hxx>
16 #include <simgear/misc/sg_path.hxx>
17 #include <simgear/scene/model/placement.hxx>
18 #include <simgear/scene/util/SGNodeMasks.hxx>
19 #include <simgear/scene/model/modellib.hxx>
21 #include <Main/globals.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Viewer/renderer.hxx>
24 #include <Viewer/viewmgr.hxx>
25 #include <Viewer/viewer.hxx>
26 #include <Scenery/scenery.hxx>
27 #include <Sound/fg_fx.hxx>
29 #include "acmodel.hxx"
33 fgLoad3DModelPanel(const std::string &path, SGPropertyNode *prop_root)
35 bool loadPanels = true;
36 osg::Node* node = simgear::SGModelLib::loadModel(path, prop_root, NULL, loadPanels);
38 node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
42 ////////////////////////////////////////////////////////////////////////
43 // Implementation of FGAircraftModel
44 ////////////////////////////////////////////////////////////////////////
46 FGAircraftModel::FGAircraftModel ()
47 : _velocity(SGVec3d::zeros()),
57 FGAircraftModel::~FGAircraftModel ()
65 FGAircraftModel::init ()
67 if (_aircraft.get()) {
68 SG_LOG(SG_AIRCRAFT, SG_ALERT, "FGAircraftModel::init: already inited");
72 SGPropertyNode_ptr sim = fgGetNode("/sim", true);
73 BOOST_FOREACH(SGPropertyNode_ptr model, sim->getChildren("model")) {
74 std::string path = model->getStringValue("path", "Models/Geometry/glider.ac");
75 std::string usage = model->getStringValue("usage", "external");
77 SGPath resolvedPath = globals->resolve_aircraft_path(path);
78 if (resolvedPath.isNull()) {
79 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to find aircraft model: " << path);
83 osg::Node* node = NULL;
85 node = fgLoad3DModelPanel( resolvedPath.str(), globals->get_props());
86 } catch (const sg_exception &ex) {
87 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':');
88 SG_LOG(SG_AIRCRAFT, SG_ALERT, " " << ex.getFormattedMessage());
91 if (usage == "interior") {
93 if (!_interior.get()) {
94 _interior.reset(new SGModelPlacement);
95 _interior->init(node);
100 // normal / exterior model
101 if (!_aircraft.get()) {
102 _aircraft.reset(new SGModelPlacement);
103 _aircraft->init(node);
105 _aircraft->add(node);
107 } // of model usage switch
108 } // of models iteration
110 // no models loaded, load the glider instead
111 if (!_aircraft.get()) {
112 SG_LOG(SG_AIRCRAFT, SG_ALERT, "(Falling back to glider.ac.)");
113 osg::Node* model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
114 globals->get_props());
115 _aircraft.reset(new SGModelPlacement);
116 _aircraft->init(model);
120 osg::Node* node = _aircraft->getSceneGraph();
121 globals->get_scenery()->get_aircraft_branch()->addChild(node);
123 if (_interior.get()) {
124 node = _interior->getSceneGraph();
125 globals->get_scenery()->get_interior_branch()->addChild(node);
130 FGAircraftModel::reinit()
135 // TODO globally create signals for all subsystems (re)initialized
136 fgSetBool("/sim/signals/model-reinit", true);
140 FGAircraftModel::shutdown()
142 if (!_aircraft.get()) {
146 osg::Node* node = _aircraft->getSceneGraph();
147 globals->get_scenery()->get_aircraft_branch()->removeChild(node);
149 if (_interior.get()) {
150 globals->get_scenery()->get_interior_branch()->removeChild(_interior->getSceneGraph());
158 FGAircraftModel::bind ()
160 _speed_n = fgGetNode("velocities/speed-north-fps", true);
161 _speed_e = fgGetNode("velocities/speed-east-fps", true);
162 _speed_d = fgGetNode("velocities/speed-down-fps", true);
166 FGAircraftModel::unbind ()
172 FGAircraftModel::update (double dt)
174 int view_number = globals->get_viewmgr()->get_current();
175 int is_internal = fgGetBool("/sim/current-view/internal");
177 if (view_number == 0 && !is_internal) {
178 _aircraft->setVisible(false);
180 _aircraft->setVisible(true);
183 double heading, pitch, roll;
184 globals->get_aircraft_orientation(heading, pitch, roll);
185 SGQuatd orient = SGQuatd::fromYawPitchRollDeg(heading, pitch, roll);
187 SGGeod pos = globals->get_aircraft_position();
189 _aircraft->setPosition(pos);
190 _aircraft->setOrientation(orient);
193 if (_interior.get()) {
194 _interior->setPosition(pos);
195 _interior->setOrientation(orient);
199 // update model's audio sample values
200 _fx->set_position_geod( pos );
201 _fx->set_orientation( orient );
203 _velocity = SGVec3d( _speed_n->getDoubleValue(),
204 _speed_e->getDoubleValue(),
205 _speed_d->getDoubleValue() );
206 _fx->set_velocity( _velocity );