]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
Expose a radio function (receiveBeacon) to the Nasal subsystem
[flightgear.git] / src / Model / acmodel.cxx
1 // acmodel.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(SGVec3d::zeros()),
40     _fx(0),
41     _lon(0),
42     _lat(0),
43     _alt(0),
44     _pitch(0),
45     _roll(0),
46     _heading(0),
47     _speed_n(0),
48     _speed_e(0),
49     _speed_d(0)
50 {
51     SGSoundMgr *smgr = globals->get_soundmgr();
52     _fx = new FGFX(smgr, "fx");
53     _fx->init();
54 }
55
56 FGAircraftModel::~FGAircraftModel ()
57 {
58   deinit();
59 }
60
61 void 
62 FGAircraftModel::init ()
63 {
64   _aircraft = new SGModelPlacement;
65   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
66   try {
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 );
76   }
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);
81 }
82
83 void
84 FGAircraftModel::reinit()
85 {
86   deinit();
87   _fx->reinit();
88   init();
89 }
90
91 void
92 FGAircraftModel::deinit()
93 {
94   // drop reference
95   _fx = 0;
96
97   if (!_aircraft) {
98     return;
99   }
100   
101   osg::Node* node = _aircraft->getSceneGraph();
102   globals->get_scenery()->get_aircraft_branch()->removeChild(node);
103
104   delete _aircraft;
105   _aircraft = NULL;
106 }
107
108 void
109 FGAircraftModel::bind ()
110 {
111    _lon = fgGetNode("position/longitude-deg", true);
112    _lat = fgGetNode("position/latitude-deg", true);
113    _alt = fgGetNode("position/altitude-ft", true);
114    _pitch = fgGetNode("orientation/pitch-deg", true);
115    _roll = fgGetNode("orientation/roll-deg", true);
116    _heading = fgGetNode("orientation/heading-deg", true);
117    _speed_n = fgGetNode("velocities/speed-north-fps", true);
118    _speed_e = fgGetNode("velocities/speed-east-fps", true);
119    _speed_d = fgGetNode("velocities/speed-down-fps", true);
120 }
121
122 void
123 FGAircraftModel::unbind ()
124 {
125   // No-op
126 }
127
128 void
129 FGAircraftModel::update (double dt)
130 {
131   int view_number = globals->get_viewmgr()->get_current();
132   int is_internal = fgGetBool("/sim/current-view/internal");
133
134   if (view_number == 0 && !is_internal) {
135     _aircraft->setVisible(false);
136   } else {
137     _aircraft->setVisible(true);
138   }
139
140   _aircraft->setPosition(_lon->getDoubleValue(),
141                          _lat->getDoubleValue(),
142                          _alt->getDoubleValue());
143   _aircraft->setOrientation(_roll->getDoubleValue(),
144                             _pitch->getDoubleValue(),
145                             _heading->getDoubleValue());
146   _aircraft->update();
147
148   // update model's audio sample values
149   SGGeod position = _aircraft->getPosition();
150   _fx->set_position_geod( position );
151
152   SGQuatd orient = SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
153                                                 _pitch->getDoubleValue(),
154                                                 _roll->getDoubleValue());
155   _fx->set_orientation( orient );
156  
157   _velocity = SGVec3d( _speed_n->getDoubleValue(),
158                        _speed_e->getDoubleValue(),
159                        _speed_d->getDoubleValue() );
160   _fx->set_velocity( _velocity );
161 }
162
163
164 // end of model.cxx