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 <string.h> // for strcmp()
12 #include <simgear/compiler.h>
13 #include <simgear/debug/logstream.hxx>
14 #include <simgear/structure/exception.hxx>
15 #include <simgear/misc/sg_path.hxx>
16 #include <simgear/scene/model/placement.hxx>
17 #include <simgear/scene/util/SGNodeMasks.hxx>
19 #include <Main/globals.hxx>
20 #include <Main/fg_props.hxx>
21 #include <Viewer/renderer.hxx>
22 #include <Viewer/viewmgr.hxx>
23 #include <Viewer/viewer.hxx>
24 #include <Scenery/scenery.hxx>
25 #include <Sound/fg_fx.hxx>
27 #include "model_panel.hxx"
29 #include "acmodel.hxx"
33 ////////////////////////////////////////////////////////////////////////
34 // Implementation of FGAircraftModel
35 ////////////////////////////////////////////////////////////////////////
37 FGAircraftModel::FGAircraftModel ()
39 _velocity(SGVec3d::zeros()),
55 FGAircraftModel::~FGAircraftModel ()
63 FGAircraftModel::init ()
65 osg::Node *model = NULL;
67 _aircraft = new SGModelPlacement;
68 string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
70 SGPath resolvedPath = globals->resolve_aircraft_path(path);
71 if (resolvedPath.isNull())
73 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':');
78 model = fgLoad3DModelPanel( resolvedPath.str(), globals->get_props());
79 } catch (const sg_exception &ex) {
80 SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':');
81 SG_LOG(SG_AIRCRAFT, SG_ALERT, " " << ex.getFormattedMessage());
87 SG_LOG(SG_AIRCRAFT, SG_ALERT, "(Falling back to glider.ac.)");
88 model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
89 globals->get_props());
91 _aircraft->init( model );
93 osg::Node* node = _aircraft->getSceneGraph();
94 // Do not do altitude computations with that model
95 node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
96 globals->get_scenery()->get_aircraft_branch()->addChild(node);
100 FGAircraftModel::reinit()
108 FGAircraftModel::deinit()
114 osg::Node* node = _aircraft->getSceneGraph();
115 globals->get_scenery()->get_aircraft_branch()->removeChild(node);
122 FGAircraftModel::bind ()
124 _lon = fgGetNode("position/longitude-deg", true);
125 _lat = fgGetNode("position/latitude-deg", true);
126 _alt = fgGetNode("position/altitude-ft", true);
127 _pitch = fgGetNode("orientation/pitch-deg", true);
128 _roll = fgGetNode("orientation/roll-deg", true);
129 _heading = fgGetNode("orientation/heading-deg", true);
130 _speed_n = fgGetNode("velocities/speed-north-fps", true);
131 _speed_e = fgGetNode("velocities/speed-east-fps", true);
132 _speed_d = fgGetNode("velocities/speed-down-fps", true);
136 FGAircraftModel::unbind ()
142 FGAircraftModel::update (double dt)
144 int view_number = globals->get_viewmgr()->get_current();
145 int is_internal = fgGetBool("/sim/current-view/internal");
147 if (view_number == 0 && !is_internal) {
148 _aircraft->setVisible(false);
150 _aircraft->setVisible(true);
153 _aircraft->setPosition(_lon->getDoubleValue(),
154 _lat->getDoubleValue(),
155 _alt->getDoubleValue());
156 _aircraft->setOrientation(_roll->getDoubleValue(),
157 _pitch->getDoubleValue(),
158 _heading->getDoubleValue());
161 // update model's audio sample values
162 SGGeod position = _aircraft->getPosition();
163 _fx->set_position_geod( position );
165 SGQuatd orient = SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
166 _pitch->getDoubleValue(),
167 _roll->getDoubleValue());
168 _fx->set_orientation( orient );
170 _velocity = SGVec3d( _speed_n->getDoubleValue(),
171 _speed_e->getDoubleValue(),
172 _speed_d->getDoubleValue() );
173 _fx->set_velocity( _velocity );