]> git.mxchange.org Git - flightgear.git/commitdiff
Make the view-manager and sound-manager independent.
authorJames Turner <zakalawe@mac.com>
Tue, 25 Sep 2012 08:17:00 +0000 (09:17 +0100)
committerJames Turner <zakalawe@mac.com>
Tue, 25 Sep 2012 08:17:00 +0000 (09:17 +0100)
Use properties to pass current-view information to the sound-manager, so there is no hard-coded dependency between the subsystems.

src/Sound/soundmanager.cxx
src/Sound/soundmanager.hxx
src/Viewer/viewer.cxx
src/Viewer/viewmgr.cxx
src/Viewer/viewmgr.hxx

index 16db632894620a25639eaf75f02ba8f5e6941d72..2a41ade13b61570f2a171514a04b9459fafc8877 100644 (file)
@@ -64,6 +64,15 @@ void FGSoundManager::init()
     _volume        = fgGetNode("/sim/sound/volume");
     _device_name   = fgGetNode("/sim/sound/device-name");
 
+    _currentView = fgGetNode("sim/current-view");
+    _viewPosLon    = fgGetNode("sim/current-view/viewer-lon-deg");
+    _viewPosLat    = fgGetNode("sim/current-view/viewer-lat-deg");
+    _viewPosElev   = fgGetNode("sim/current-view/viewer-elev-ft");
+  
+    _velocityNorthFPS = fgGetNode("velocities/speed-north-fps", true);
+    _velocityEastFPS = fgGetNode("velocities/speed-east-fps", true);
+    _velocityDownFPS = fgGetNode("velocities/speed-down-fps", true);
+  
     reinit();
 }
 
@@ -113,6 +122,15 @@ void FGSoundManager::update_device_list()
     devices.clear();
 }
 
+bool FGSoundManager::stationary() const
+{
+  // this is an ugly hack to decide if the *viewer* is stationary,
+  // since we don't model the viewer velocity directly.
+  return (_currentView->getDoubleValue("x-offset-m") == 0.0) &&
+         (_currentView->getDoubleValue("y-offset-m") == 0.0) &&
+         (_currentView->getDoubleValue("z-offset-m") == 0.0);
+}
+
 // Update sound manager and propagate property values,
 // since the sound manager doesn't read any properties itself.
 // Actual sound update is triggered by the subsystem manager.
@@ -120,6 +138,29 @@ void FGSoundManager::update(double dt)
 {
     if (_is_initialized && _sound_working->getBoolValue() && _sound_enabled->getBoolValue())
     {
+        SGGeod viewPosGeod(SGGeod::fromDegFt(_viewPosLon->getDoubleValue(),
+                                             _viewPosLat->getDoubleValue(),
+                                             _viewPosElev->getDoubleValue()));
+        SGVec3d cartPos = SGVec3d::fromGeod(viewPosGeod);
+        
+        set_position(cartPos, viewPosGeod);
+        
+        SGQuatd viewOrientation;
+        for (int i=0; i<4; ++i) {
+          viewOrientation[i] = _currentView->getChild("raw-orientation", i, true)->getDoubleValue();
+        }
+        
+        set_orientation( viewOrientation );
+        
+        SGVec3d velocity(SGVec3d::zeros());
+        if (!stationary()) {
+          velocity = SGVec3d(_velocityNorthFPS->getDoubleValue(),
+                             _velocityEastFPS->getDoubleValue(),
+                             _velocityDownFPS->getDoubleValue() );
+        }
+        
+        set_velocity( velocity );
+      
         set_volume(_volume->getFloatValue());
         SGSoundMgr::update(dt);
     }
index d43e49478169993fa58e7a29da65312bdcf07a6e..e9edbd7d3d4410c579bb4021dcf89ea43ae34921 100644 (file)
@@ -46,8 +46,13 @@ public:
     void update_device_list();
 
 private:
+    bool stationary() const;
+  
     bool _is_initialized;
     SGPropertyNode_ptr _sound_working, _sound_enabled, _volume, _device_name;
+    SGPropertyNode_ptr _currentView;
+    SGPropertyNode_ptr _viewPosLon, _viewPosLat, _viewPosElev;
+    SGPropertyNode_ptr _velocityNorthFPS, _velocityEastFPS, _velocityDownFPS;
     Listener* _listener;
 };
 #else
index 98e5fe24f6eb635cde9b7b126c544adbb60e877f..2d652182207e9fa432094a2cba12f7f97a2d6116 100644 (file)
@@ -27,6 +27,8 @@
 #  include "config.h"
 #endif
 
+#include "viewer.hxx"
+
 #include <simgear/compiler.h>
 #include <cassert>
 
 
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
-#include <Scenery/scenery.hxx>
-#include <Model/acmodel.hxx>
-
-#include "viewer.hxx"
-
 #include "CameraGroup.hxx"
 
 using namespace flightgear;
index 76a1eb3df6fe9096523eba916694616ba9257719..91657fa1c045ffa8839ecd385d135fd7ed52d97b 100644 (file)
@@ -30,7 +30,6 @@
 #include <string.h>            // strcmp
 
 #include <simgear/compiler.h>
-#include <simgear/sound/soundmgr_openal.hxx>
 #include <Main/fg_props.hxx>
 #include "viewer.hxx"
 
@@ -44,8 +43,7 @@ FGViewMgr::FGViewMgr( void ) :
   abs_viewer_position(SGVec3d::zeros()),
   current(0),
   current_view_orientation(SGQuatd::zeros()),
-  current_view_or_offset(SGQuatd::zeros()),
-  smgr(globals->get_soundmgr())
+  current_view_or_offset(SGQuatd::zeros())
 {
 }
 
@@ -179,11 +177,7 @@ FGViewMgr::bind()
 
 void
 FGViewMgr::do_bind()
-{
-  velocityNorthFPS = fgGetNode("velocities/speed-north-fps", true);
-  velocityEastFPS = fgGetNode("velocities/speed-east-fps", true);
-  velocityDownFPS = fgGetNode("velocities/speed-down-fps", true);
-  
+{  
   // these are bound to the current view properties
   _tiedProperties.setRoot(fgGetNode("/sim/current-view", true));
   _tiedProperties.Tie("heading-offset-deg", this,
@@ -240,6 +234,11 @@ FGViewMgr::do_bind()
   _tiedProperties.Tie(n->getNode("viewer-y-m", true),SGRawValuePointer<double>(&abs_viewer_position[1]));
   _tiedProperties.Tie(n->getNode("viewer-z-m", true),SGRawValuePointer<double>(&abs_viewer_position[2]));
 
+  _tiedProperties.Tie("viewer-lon-deg", this, &FGViewMgr::getViewLon_deg);
+  _tiedProperties.Tie("viewer-lat-deg", this, &FGViewMgr::getViewLat_deg);
+  _tiedProperties.Tie("viewer-elev-ft", this, &FGViewMgr::getViewElev_ft);
+  
+  
   _tiedProperties.Tie("debug/orientation-w", this,
                       &FGViewMgr::getCurrentViewOrientation_w);
   _tiedProperties.Tie("debug/orientation-x", this,
@@ -277,8 +276,8 @@ FGViewMgr::unbind ()
 void
 FGViewMgr::update (double dt)
 {
-  FGViewer *loop_view = (FGViewer *)get_current_view();
-  if (loop_view == 0) return;
+  FGViewer* currentView = (FGViewer *)get_current_view();
+  if (!currentView) return;
 
   SGPropertyNode *n = config_list[current];
   double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
@@ -293,15 +292,15 @@ FGViewMgr::update (double dt)
     pitch_deg = fgGetDouble(n->getStringValue("config/eye-pitch-deg-path"));
     heading_deg = fgGetDouble(n->getStringValue("config/eye-heading-deg-path"));
 
-    loop_view->setPosition(lon_deg, lat_deg, alt_ft);
-    loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
+    currentView->setPosition(lon_deg, lat_deg, alt_ft);
+    currentView->setOrientation(roll_deg, pitch_deg, heading_deg);
   } else {
     // force recalc in viewer
-    loop_view->set_dirty();
+    currentView->set_dirty();
   }
 
   // if lookat (type 1) then get target data...
-  if (loop_view->getType() == FG_LOOKAT) {
+  if (currentView->getType() == FG_LOOKAT) {
     if (!n->getBoolValue("config/from-model")) {
       lon_deg = fgGetDouble(n->getStringValue("config/target-lon-deg-path"));
       lat_deg = fgGetDouble(n->getStringValue("config/target-lat-deg-path"));
@@ -310,10 +309,10 @@ FGViewMgr::update (double dt)
       pitch_deg = fgGetDouble(n->getStringValue("config/target-pitch-deg-path"));
       heading_deg = fgGetDouble(n->getStringValue("config/target-heading-deg-path"));
 
-      loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
-      loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
+      currentView->setTargetPosition(lon_deg, lat_deg, alt_ft);
+      currentView->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
     } else {
-      loop_view->set_dirty();
+      currentView->set_dirty();
     }
   }
 
@@ -325,27 +324,19 @@ FGViewMgr::update (double dt)
   setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
   setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
 
-  current_view_orientation = loop_view->getViewOrientation();
-  current_view_or_offset = loop_view->getViewOrientationOffset();
+  current_view_orientation = currentView->getViewOrientation();
+  current_view_or_offset = currentView->getViewOrientationOffset();
 
   // Update the current view
   do_axes();
-  loop_view->update(dt);
-  abs_viewer_position = loop_view->getViewPosition();
-
-  // update audio listener values
-  // set the viewer position in Cartesian coordinates in meters
-  smgr->set_position( abs_viewer_position, loop_view->getPosition() );
-  smgr->set_orientation( current_view_orientation );
-
-  // get the model velocity
-  SGVec3d velocity = SGVec3d::zeros();
-  if ( !stationary() ) {
-    velocity = SGVec3d( velocityNorthFPS->getDoubleValue(),
-                        velocityEastFPS->getDoubleValue(),
-                        velocityDownFPS->getDoubleValue() );
+  currentView->update(dt);
+  abs_viewer_position = currentView->getViewPosition();
+  
+  // expose the raw (OpenGL) orientation to the proeprty tree,
+  // for the sound-manager
+  for (int i=0; i<4; ++i) {
+    _tiedProperties.getRoot()->getChild("raw-orientation", i, true)->setDoubleValue(current_view_orientation[i]);
   }
-  smgr->set_velocity( velocity );
 }
 
 void
@@ -619,20 +610,6 @@ FGViewMgr::setViewZOffset_m (double z)
   }
 }
 
-bool
-FGViewMgr::stationary () const
-{
-  const FGViewer * view = get_current_view();
-  if (view != 0) {
-    if (((FGViewer *)view)->getXOffset_m() == 0.0 &&
-        ((FGViewer *)view)->getYOffset_m() == 0.0 &&
-        ((FGViewer *)view)->getZOffset_m() == 0.0)
-      return true;
-  }
-
-  return false;
-}
-
 double
 FGViewMgr::getViewTargetXOffset_m () const
 {
@@ -785,6 +762,28 @@ FGViewMgr::setViewAxisLat (double axis)
   axis_lat = axis;
 }
 
+double
+FGViewMgr::getViewLon_deg() const
+{
+  const FGViewer* view = get_current_view();
+  return (view != NULL) ? view->getPosition().getLongitudeDeg() : 0.0;
+}
+
+double
+FGViewMgr::getViewLat_deg() const
+{
+  const FGViewer* view = get_current_view();
+  return (view != NULL) ? view->getPosition().getLatitudeDeg() : 0.0;
+}
+
+double
+FGViewMgr::getViewElev_ft() const
+{
+  const FGViewer* view = get_current_view();
+  return (view != NULL) ? view->getPosition().getElevationFt() : 0.0;
+}
+
+
 // reference frame orientation.
 // This is the view orientation you get when you have no
 // view offset, i.e. the offset operator is the identity.
index a53b880e516252b83853c25b0437a2d799f0325e..587ac9f281374f5c3b68c3a198361092b60be8e2 100644 (file)
@@ -34,7 +34,6 @@
 
 // forward decls
 class FGViewer;
-class SGSoundMgr;
 typedef SGSharedPtr<FGViewer> FGViewerPtr;
 
 // Define a structure containing view information
@@ -119,6 +118,10 @@ private:
     int getView () const;
     void setView (int newview);
 
+    double getViewLon_deg() const;
+    double getViewLat_deg() const;
+    double getViewElev_ft() const;
+  
 // quaternion accessors, for debugging:
     double getCurrentViewOrientation_w() const;
     double getCurrentViewOrientation_x() const;
@@ -133,8 +136,6 @@ private:
     double getCurrentViewFrame_y() const;
     double getCurrentViewFrame_z() const;
 
-    bool stationary () const;
-
     // copies current offset settings to current-view path...
     void copyToCurrent ();
     
@@ -147,10 +148,6 @@ private:
 
     int current;
     SGQuatd current_view_orientation, current_view_or_offset;
-
-    SGSoundMgr *smgr;
-  
-    SGPropertyNode_ptr velocityNorthFPS, velocityEastFPS, velocityDownFPS;
 };
 
 // This takes the conventional aviation XYZ body system