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