]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
Fix unused private vars.
[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   std::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   // TODO globally create signals for all subsystems (re)initialized
106   fgSetBool("/sim/signals/model-reinit", true);
107 }
108
109 void
110 FGAircraftModel::deinit()
111 {
112   if (!_aircraft) {
113     return;
114   }
115   
116   osg::Node* node = _aircraft->getSceneGraph();
117   globals->get_scenery()->get_aircraft_branch()->removeChild(node);
118
119   delete _aircraft;
120   _aircraft = NULL;
121 }
122
123 void
124 FGAircraftModel::bind ()
125 {
126    _lon = fgGetNode("position/longitude-deg", true);
127    _lat = fgGetNode("position/latitude-deg", true);
128    _alt = fgGetNode("position/altitude-ft", true);
129    _pitch = fgGetNode("orientation/pitch-deg", true);
130    _roll = fgGetNode("orientation/roll-deg", true);
131    _heading = fgGetNode("orientation/heading-deg", true);
132    _speed_n = fgGetNode("velocities/speed-north-fps", true);
133    _speed_e = fgGetNode("velocities/speed-east-fps", true);
134    _speed_d = fgGetNode("velocities/speed-down-fps", true);
135 }
136
137 void
138 FGAircraftModel::unbind ()
139 {
140   _fx->unbind();
141 }
142
143 void
144 FGAircraftModel::update (double dt)
145 {
146   int view_number = globals->get_viewmgr()->get_current();
147   int is_internal = fgGetBool("/sim/current-view/internal");
148
149   if (view_number == 0 && !is_internal) {
150     _aircraft->setVisible(false);
151   } else {
152     _aircraft->setVisible(true);
153   }
154
155   _aircraft->setPosition(_lon->getDoubleValue(),
156                          _lat->getDoubleValue(),
157                          _alt->getDoubleValue());
158   _aircraft->setOrientation(_roll->getDoubleValue(),
159                             _pitch->getDoubleValue(),
160                             _heading->getDoubleValue());
161   _aircraft->update();
162
163   // update model's audio sample values
164   SGGeod position = _aircraft->getPosition();
165   _fx->set_position_geod( position );
166
167   SGQuatd orient = SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
168                                                 _pitch->getDoubleValue(),
169                                                 _roll->getDoubleValue());
170   _fx->set_orientation( orient );
171  
172   _velocity = SGVec3d( _speed_n->getDoubleValue(),
173                        _speed_e->getDoubleValue(),
174                        _speed_d->getDoubleValue() );
175   _fx->set_velocity( _velocity );
176 }
177
178
179 // end of model.cxx