]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
#858: Fix loading issue with aircraft models in "--fg-aircraft" directories
[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 <Viewer/renderer.hxx>
22 #include <Viewer/viewmgr.hxx>
23 #include <Viewer/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   // drop reference
59   _fx = 0;
60   deinit();
61 }
62
63 void 
64 FGAircraftModel::init ()
65 {
66   osg::Node *model = NULL;
67
68   _aircraft = new SGModelPlacement;
69   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
70
71   SGPath resolvedPath = globals->resolve_aircraft_path(path);
72   if (resolvedPath.isNull())
73   {
74       SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':');
75   }
76   else
77   {
78       try {
79         model = fgLoad3DModelPanel( resolvedPath.str(), globals->get_props());
80       } catch (const sg_exception &ex) {
81         SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':');
82         SG_LOG(SG_AIRCRAFT, SG_ALERT, "  " << ex.getFormattedMessage());
83         SG_LOG(SG_AIRCRAFT, SG_ALERT, "(Falling back to glider.ac.)");
84       }
85   }
86
87   if (!model)
88   {
89       model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
90                                   globals->get_props());
91   }
92   _aircraft->init( model );
93
94   osg::Node* node = _aircraft->getSceneGraph();
95   // Do not do altitude computations with that model
96   node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
97   globals->get_scenery()->get_aircraft_branch()->addChild(node);
98 }
99
100 void
101 FGAircraftModel::reinit()
102 {
103   deinit();
104   _fx->reinit();
105   init();
106 }
107
108 void
109 FGAircraftModel::deinit()
110 {
111   if (!_aircraft) {
112     return;
113   }
114   
115   osg::Node* node = _aircraft->getSceneGraph();
116   globals->get_scenery()->get_aircraft_branch()->removeChild(node);
117
118   delete _aircraft;
119   _aircraft = NULL;
120 }
121
122 void
123 FGAircraftModel::bind ()
124 {
125    _lon = fgGetNode("position/longitude-deg", true);
126    _lat = fgGetNode("position/latitude-deg", true);
127    _alt = fgGetNode("position/altitude-ft", true);
128    _pitch = fgGetNode("orientation/pitch-deg", true);
129    _roll = fgGetNode("orientation/roll-deg", true);
130    _heading = fgGetNode("orientation/heading-deg", true);
131    _speed_n = fgGetNode("velocities/speed-north-fps", true);
132    _speed_e = fgGetNode("velocities/speed-east-fps", true);
133    _speed_d = fgGetNode("velocities/speed-down-fps", true);
134 }
135
136 void
137 FGAircraftModel::unbind ()
138 {
139   // No-op
140 }
141
142 void
143 FGAircraftModel::update (double dt)
144 {
145   int view_number = globals->get_viewmgr()->get_current();
146   int is_internal = fgGetBool("/sim/current-view/internal");
147
148   if (view_number == 0 && !is_internal) {
149     _aircraft->setVisible(false);
150   } else {
151     _aircraft->setVisible(true);
152   }
153
154   _aircraft->setPosition(_lon->getDoubleValue(),
155                          _lat->getDoubleValue(),
156                          _alt->getDoubleValue());
157   _aircraft->setOrientation(_roll->getDoubleValue(),
158                             _pitch->getDoubleValue(),
159                             _heading->getDoubleValue());
160   _aircraft->update();
161
162   // update model's audio sample values
163   SGGeod position = _aircraft->getPosition();
164   _fx->set_position_geod( position );
165
166   SGQuatd orient = SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
167                                                 _pitch->getDoubleValue(),
168                                                 _roll->getDoubleValue());
169   _fx->set_orientation( orient );
170  
171   _velocity = SGVec3d( _speed_n->getDoubleValue(),
172                        _speed_e->getDoubleValue(),
173                        _speed_d->getDoubleValue() );
174   _fx->set_velocity( _velocity );
175 }
176
177
178 // end of model.cxx