]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
0541a1159b273446b9f584d12fa9a9dcb7e2acdf
[flightgear.git] / src / Model / acmodel.cxx
1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <string.h>             // for strcmp()
11
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>
18
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>
26
27 #include "model_panel.hxx"
28
29 #include "acmodel.hxx"
30
31
32 \f
33 ////////////////////////////////////////////////////////////////////////
34 // Implementation of FGAircraftModel
35 ////////////////////////////////////////////////////////////////////////
36
37 FGAircraftModel::FGAircraftModel ()
38   : _aircraft(0),
39     _velocity(SGVec3f::zeros()),
40     _fx(0),
41     _lon(0),
42     _lat(0),
43     _alt(0),
44     _pitch(0),
45     _roll(0),
46     _heading(0),
47     _speed(0),
48     _speed_up(0)
49 {
50     SGSoundMgr *smgr;
51     smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
52     _fx = new FGFX(smgr, "fx");
53     _fx->init();
54 }
55
56 FGAircraftModel::~FGAircraftModel ()
57 {
58   osg::Node* node = _aircraft->getSceneGraph();
59   globals->get_scenery()->get_aircraft_branch()->removeChild(node);
60
61   delete _aircraft;
62 }
63
64 void 
65 FGAircraftModel::init ()
66 {
67   _aircraft = new SGModelPlacement;
68   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
69   try {
70     osg::Node *model = fgLoad3DModelPanel( path, globals->get_props());
71     _aircraft->init( model );
72   } catch (const sg_exception &ex) {
73     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
74     SG_LOG(SG_GENERAL, SG_ALERT, "  " << ex.getFormattedMessage());
75     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
76     osg::Node *model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
77                                            globals->get_props());
78     _aircraft->init( model );
79   }
80   osg::Node* node = _aircraft->getSceneGraph();
81   // Do not do altitude computations with that model
82   node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
83   globals->get_scenery()->get_aircraft_branch()->addChild(node);
84 }
85
86 void
87 FGAircraftModel::bind ()
88 {
89    _lon = fgGetNode("position/longitude-deg", true);
90    _lat = fgGetNode("position/latitude-deg", true);
91    _alt = fgGetNode("position/altitude-ft", true);
92    _pitch = fgGetNode("orientation/pitch-deg", true);
93    _roll = fgGetNode("orientation/roll-deg", true);
94    _heading = fgGetNode("orientation/heading-deg", true);
95    _speed = fgGetNode("velocities/true-airspeed-kt", true);
96    _speed_up = fgGetNode("velocities/vertical-speed-fps", true);
97 }
98
99 void
100 FGAircraftModel::unbind ()
101 {
102   // No-op
103 }
104
105 void
106 FGAircraftModel::update (double dt)
107 {
108   int view_number = globals->get_viewmgr()->get_current();
109   int is_internal = fgGetBool("/sim/current-view/internal");
110
111   if (view_number == 0 && !is_internal) {
112     _aircraft->setVisible(false);
113   } else {
114     _aircraft->setVisible(true);
115   }
116
117   _aircraft->setPosition(_lon->getDoubleValue(),
118                          _lat->getDoubleValue(),
119                          _alt->getDoubleValue());
120   _aircraft->setOrientation(_roll->getDoubleValue(),
121                             _pitch->getDoubleValue(),
122                             _heading->getDoubleValue());
123   _aircraft->update();
124
125   // update model's audio sample values
126   // Get the Cartesian coordinates in meters
127   SGVec3d pos = SGVec3d::fromGeod(_aircraft->getPosition());
128   _fx->set_position( pos );
129
130   SGQuatd orient_m = SGQuatd::fromLonLat(_aircraft->getPosition());
131   orient_m *= SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
132                                            _pitch->getDoubleValue(),
133                                            _roll->getDoubleValue());
134   SGVec3d orient = -orient_m.rotate(SGVec3d::e1());
135   _fx->set_orientation( toVec3f(orient) );
136  
137   // For now assume the aircraft speed is always along the longitudinal
138   // axis, so sideslipping is not taken into account. This should be fine
139   // for audio.
140   _velocity = toVec3f(orient * _speed->getDoubleValue() * SG_KT_TO_FPS);
141   // _velocity[2] = _speed_up->getFloatValue();
142   _velocity *= SG_FEET_TO_METER;
143   _fx->set_velocity( _velocity );
144 }
145
146
147 // end of model.cxx