]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
Initial commit of the new sound system, expect more updates to follow
[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     _fx(0),
40     _lon(0),
41     _lat(0),
42     _alt(0),
43     _pitch(0),
44     _roll(0),
45     _heading(0),
46     _speed_north(0),
47     _speed_east(0),
48     _speed_up(0)
49 {
50 }
51
52 FGAircraftModel::~FGAircraftModel ()
53 {
54   osg::Node* node = _aircraft->getSceneGraph();
55   globals->get_scenery()->get_aircraft_branch()->removeChild(node);
56
57   delete _aircraft;
58 }
59
60 void 
61 FGAircraftModel::init ()
62 {
63   _aircraft = new SGModelPlacement;
64   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
65   try {
66     osg::Node *model = fgLoad3DModelPanel( path, globals->get_props());
67     _aircraft->init( model );
68   } catch (const sg_exception &ex) {
69     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
70     SG_LOG(SG_GENERAL, SG_ALERT, "  " << ex.getFormattedMessage());
71     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
72     osg::Node *model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
73                                            globals->get_props());
74     _aircraft->init( model );
75   }
76   osg::Node* node = _aircraft->getSceneGraph();
77   // Do not do altitude computations with that model
78   node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
79   globals->get_scenery()->get_aircraft_branch()->addChild(node);
80 }
81
82 void
83 FGAircraftModel::bind ()
84 {
85    _lon = fgGetNode("position/longitude-deg", true);
86    _lat = fgGetNode("position/latitude-deg", true);
87    _alt = fgGetNode("position/altitude-ft", true);
88    _pitch = fgGetNode("orientation/pitch-deg", true);
89    _roll = fgGetNode("orientation/roll-deg", true);
90    _heading = fgGetNode("orientation/heading-deg", true);
91    _speed_north = fgGetNode("/velocities/speed-north-fps", true);
92    _speed_east = fgGetNode("/velocities/speed-east-fps", true);
93    _speed_up = fgGetNode("/velocities/vertical-speed-fps", true);
94 }
95
96 void
97 FGAircraftModel::unbind ()
98 {
99   // No-op
100 }
101
102 void
103 FGAircraftModel::update (double dt)
104 {
105   int view_number = globals->get_viewmgr()->get_current();
106   int is_internal = fgGetBool("/sim/current-view/internal");
107
108   if (view_number == 0 && !is_internal) {
109     _aircraft->setVisible(false);
110   } else {
111     _aircraft->setVisible(true);
112   }
113
114   _aircraft->setPosition(_lon->getDoubleValue(),
115                          _lat->getDoubleValue(),
116                          _alt->getDoubleValue());
117   _aircraft->setOrientation(_roll->getDoubleValue(),
118                             _pitch->getDoubleValue(),
119                             _heading->getDoubleValue());
120   _aircraft->update();
121
122   if ( !_fx) {
123     SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
124     if (smgr) {
125         _fx = new FGFX(smgr, "fx");
126         _fx->init();
127     }
128   }
129
130   if (_fx) {
131     // Get the Cartesian coordinates in meters
132     SGVec3d pos = SGVec3d::fromGeod(_aircraft->getPosition());
133     _fx->set_position( pos );
134
135     SGQuatd orient_m = SGQuatd::fromLonLat(_aircraft->getPosition());
136     orient_m *= SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
137                                              _pitch->getDoubleValue(),
138                                              _roll->getDoubleValue());
139     SGVec3d orient = orient_m.rotateBack(SGVec3d::e1());
140     _fx->set_orientation( toVec3f(orient) );
141  
142     SGVec3f vel = SGVec3f( _speed_north->getFloatValue(),
143                            _speed_east->getFloatValue(),
144                            _speed_up->getFloatValue());
145 // TODO: rotate to properly align with the model orientation
146
147     _fx->set_velocity( vel*SG_FEET_TO_METER );
148   }
149 }
150
151
152 // end of model.cxx