1 // model.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 ()
52 FGAircraftModel::~FGAircraftModel ()
54 osg::Node* node = _aircraft->getSceneGraph();
55 globals->get_scenery()->get_aircraft_branch()->removeChild(node);
61 FGAircraftModel::init ()
63 _aircraft = new SGModelPlacement;
64 string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
66 osg::Node *model = fgLoad3DModelPanel( path, globals->get_props());
67 _aircraft->init( model );
68 } catch (const sg_exception &ex) {
69 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
70 SG_LOG(SG_GENERAL, SG_ALERT, " " << ex.getFormattedMessage());
71 SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
72 osg::Node *model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
73 globals->get_props());
74 _aircraft->init( model );
76 osg::Node* node = _aircraft->getSceneGraph();
77 // Do not do altitude computations with that model
78 node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
79 globals->get_scenery()->get_aircraft_branch()->addChild(node);
83 FGAircraftModel::bind ()
85 _lon = fgGetNode("position/longitude-deg", true);
86 _lat = fgGetNode("position/latitude-deg", true);
87 _alt = fgGetNode("position/altitude-ft", true);
88 _pitch = fgGetNode("orientation/pitch-deg", true);
89 _roll = fgGetNode("orientation/roll-deg", true);
90 _heading = fgGetNode("orientation/heading-deg", true);
91 _speed_north = fgGetNode("/velocities/speed-north-fps", true);
92 _speed_east = fgGetNode("/velocities/speed-east-fps", true);
93 _speed_up = fgGetNode("/velocities/vertical-speed-fps", true);
97 FGAircraftModel::unbind ()
103 FGAircraftModel::update (double dt)
105 int view_number = globals->get_viewmgr()->get_current();
106 int is_internal = fgGetBool("/sim/current-view/internal");
108 if (view_number == 0 && !is_internal) {
109 _aircraft->setVisible(false);
111 _aircraft->setVisible(true);
114 _aircraft->setPosition(_lon->getDoubleValue(),
115 _lat->getDoubleValue(),
116 _alt->getDoubleValue());
117 _aircraft->setOrientation(_roll->getDoubleValue(),
118 _pitch->getDoubleValue(),
119 _heading->getDoubleValue());
123 SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
125 _fx = new FGFX(smgr, "fx");
131 // Get the Cartesian coordinates in meters
132 SGVec3d pos = SGVec3d::fromGeod(_aircraft->getPosition());
133 _fx->set_position( pos );
135 SGQuatd orient_m = SGQuatd::fromLonLat(_aircraft->getPosition());
136 orient_m *= SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
137 _pitch->getDoubleValue(),
138 _roll->getDoubleValue());
139 SGVec3d orient = orient_m.rotateBack(SGVec3d::e1());
140 _fx->set_orientation( toVec3f(orient) );
142 SGVec3f vel = SGVec3f( _speed_north->getFloatValue(),
143 _speed_east->getFloatValue(),
144 _speed_up->getFloatValue());
145 // TODO: rotate to properly align with the model orientation
147 _fx->set_velocity( vel*SG_FEET_TO_METER );