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 <Main/renderer.hxx>
22 #include <Main/viewmgr.hxx>
23 #include <Main/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()),
51 SGSoundMgr *smgr = globals->get_soundmgr();
52 _fx = new FGFX(smgr, "fx");
56 FGAircraftModel::~FGAircraftModel ()
62 FGAircraftModel::init ()
64 _aircraft = new SGModelPlacement;
65 string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
67 osg::Node *model = fgLoad3DModelPanel( path, globals->get_props());
68 _aircraft->init( model );
69 } catch (const sg_exception &ex) {
70 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
71 SG_LOG(SG_GENERAL, SG_ALERT, " " << ex.getFormattedMessage());
72 SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
73 osg::Node *model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
74 globals->get_props());
75 _aircraft->init( model );
77 osg::Node* node = _aircraft->getSceneGraph();
78 // Do not do altitude computations with that model
79 node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
80 globals->get_scenery()->get_aircraft_branch()->addChild(node);
84 FGAircraftModel::reinit()
92 FGAircraftModel::deinit()
98 osg::Node* node = _aircraft->getSceneGraph();
99 globals->get_scenery()->get_aircraft_branch()->removeChild(node);
106 FGAircraftModel::bind ()
108 _lon = fgGetNode("position/longitude-deg", true);
109 _lat = fgGetNode("position/latitude-deg", true);
110 _alt = fgGetNode("position/altitude-ft", true);
111 _pitch = fgGetNode("orientation/pitch-deg", true);
112 _roll = fgGetNode("orientation/roll-deg", true);
113 _heading = fgGetNode("orientation/heading-deg", true);
114 _speed_n = fgGetNode("velocities/speed-north-fps", true);
115 _speed_e = fgGetNode("velocities/speed-east-fps", true);
116 _speed_d = fgGetNode("velocities/speed-down-fps", true);
120 FGAircraftModel::unbind ()
126 FGAircraftModel::update (double dt)
128 int view_number = globals->get_viewmgr()->get_current();
129 int is_internal = fgGetBool("/sim/current-view/internal");
131 if (view_number == 0 && !is_internal) {
132 _aircraft->setVisible(false);
134 _aircraft->setVisible(true);
137 _aircraft->setPosition(_lon->getDoubleValue(),
138 _lat->getDoubleValue(),
139 _alt->getDoubleValue());
140 _aircraft->setOrientation(_roll->getDoubleValue(),
141 _pitch->getDoubleValue(),
142 _heading->getDoubleValue());
145 // update model's audio sample values
146 SGGeod position = _aircraft->getPosition();
147 _fx->set_position_geod( position );
149 SGQuatd orient = SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
150 _pitch->getDoubleValue(),
151 _roll->getDoubleValue());
152 _fx->set_orientation( orient );
154 _velocity = SGVec3d( _speed_n->getDoubleValue(),
155 _speed_e->getDoubleValue(),
156 _speed_d->getDoubleValue() );
157 _fx->set_velocity( _velocity );