]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
Assorted small soundsystem related fixes.
[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     _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;
52     smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
53     _fx = new FGFX(smgr, "fx");
54     _fx->init();
55 }
56
57 FGAircraftModel::~FGAircraftModel ()
58 {
59   osg::Node* node = _aircraft->getSceneGraph();
60   globals->get_scenery()->get_aircraft_branch()->removeChild(node);
61
62   delete _aircraft;
63 }
64
65 void 
66 FGAircraftModel::init ()
67 {
68   _aircraft = new SGModelPlacement;
69   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
70   try {
71     osg::Node *model = fgLoad3DModelPanel( path, globals->get_props());
72     _aircraft->init( model );
73   } catch (const sg_exception &ex) {
74     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
75     SG_LOG(SG_GENERAL, SG_ALERT, "  " << ex.getFormattedMessage());
76     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
77     osg::Node *model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
78                                            globals->get_props());
79     _aircraft->init( model );
80   }
81   osg::Node* node = _aircraft->getSceneGraph();
82   // Do not do altitude computations with that model
83   node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
84   globals->get_scenery()->get_aircraft_branch()->addChild(node);
85 }
86
87 void
88 FGAircraftModel::bind ()
89 {
90    _lon = fgGetNode("position/longitude-deg", true);
91    _lat = fgGetNode("position/latitude-deg", true);
92    _alt = fgGetNode("position/altitude-ft", true);
93    _pitch = fgGetNode("orientation/pitch-deg", true);
94    _roll = fgGetNode("orientation/roll-deg", true);
95    _heading = fgGetNode("orientation/heading-deg", true);
96    _speed_n = fgGetNode("velocities/speed-north-fps", true);
97    _speed_e = fgGetNode("velocities/speed-east-fps", true);
98    _speed_d = fgGetNode("velocities/speed-down-fps", true);
99 }
100
101 void
102 FGAircraftModel::unbind ()
103 {
104   // No-op
105 }
106
107 void
108 FGAircraftModel::update (double dt)
109 {
110   int view_number = globals->get_viewmgr()->get_current();
111   int is_internal = fgGetBool("/sim/current-view/internal");
112
113   if (view_number == 0 && !is_internal) {
114     _aircraft->setVisible(false);
115   } else {
116     _aircraft->setVisible(true);
117   }
118
119   _aircraft->setPosition(_lon->getDoubleValue(),
120                          _lat->getDoubleValue(),
121                          _alt->getDoubleValue());
122   _aircraft->setOrientation(_roll->getDoubleValue(),
123                             _pitch->getDoubleValue(),
124                             _heading->getDoubleValue());
125   _aircraft->update();
126
127   // update model's audio sample values
128   _fx->set_position( _aircraft->getPosition() );
129
130   SGQuatd orient = SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(),
131                                                 _pitch->getDoubleValue(),
132                                                 _roll->getDoubleValue());
133   _fx->set_orientation( orient );
134  
135   _velocity = SGVec3d( -_speed_n->getDoubleValue(),
136                        -_speed_e->getDoubleValue(),
137                        -_speed_d->getDoubleValue());
138   _fx->set_velocity( _velocity );
139 }
140
141
142 // end of model.cxx