From 5ac2abe3abe399858e7a8c7288ce71338daf20bf Mon Sep 17 00:00:00 2001
From: ehofman <ehofman>
Date: Mon, 2 Nov 2009 21:38:39 +0000
Subject: [PATCH] Remove more unneeded code and properly set relative position
 and sound direction

---
 simgear/sound/sample_group.cxx    | 48 +++++++++++--------------------
 simgear/sound/sample_group.hxx    |  4 +--
 simgear/sound/sample_openal.cxx   |  8 ++----
 simgear/sound/sample_openal.hxx   |  9 ------
 simgear/sound/soundmgr_openal.cxx |  4 ---
 simgear/sound/soundmgr_openal.hxx |  4 +--
 simgear/sound/xmlsound.cxx        |  8 +++---
 7 files changed, 26 insertions(+), 59 deletions(-)

diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx
index 4ba82f56..6cccf634 100644
--- a/simgear/sound/sample_group.cxx
+++ b/simgear/sound/sample_group.cxx
@@ -41,8 +41,8 @@ SGSampleGroup::SGSampleGroup () :
     _pause(false),
     _tied_to_listener(false),
     _velocity(SGVec3d::zeros()),
-    _orientation(SGQuatd::zeros()),
-    _position_geod(SGGeod())
+    _base_pos(SGVec3d::zeros()),
+    _orientation(SGQuatd::zeros())
 {
     _samples.clear();
 }
@@ -55,8 +55,8 @@ SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
     _pause(false),
     _tied_to_listener(false),
     _velocity(SGVec3d::zeros()),
-    _orientation(SGQuatd::zeros()),
-    _position_geod(SGGeod())
+    _base_pos(SGVec3d::zeros()),
+    _orientation(SGQuatd::zeros())
 {
     _smgr->add(this, refname);
     _samples.clear();
@@ -334,45 +334,31 @@ void SGSampleGroup::set_velocity( const SGVec3f &vel ) {
     }
 }
 
-// set the source position of all managed sounds
-void SGSampleGroup::update_pos_and_orientation() {
-
-    SGVec3d position = SGVec3d::fromGeod( _position_geod );
-    SGVec3d pos_offs = SGVec3d::fromGeod( _smgr->get_position_geod() );
-
-    // The rotation rotating from the earth centerd frame to
-    // the horizontal local frame
-    SGQuatd hlOr = SGQuatd::fromLonLat(_position_geod);
-
-    // Rotate the x-forward, y-right, z-down coordinate system
-    // into the OpenGL camera system with x-right, y-up, z-back.
-    SGQuatd q(-0.5, -0.5, 0.5, 0.5);
-
-    // Compute the sounds orientation and position
-    // wrt the earth centered frame - that is global coorinates
-    SGQuatd sc2body = hlOr*_orientation*q;
+void SGSampleGroup::set_volume( float vol )
+{
+    _volume = vol;
+    if (_volume < 0.0) _volume = 0.0;
+    if (_volume > 1.0) _volume = 1.0;
 
     sample_map_iterator sample_current = _samples.begin();
     sample_map_iterator sample_end = _samples.end();
     for ( ; sample_current != sample_end; ++sample_current ) {
         SGSoundSample *sample = sample_current->second;
-        sample->set_position( position - pos_offs );
-        sample->set_orientation( _orientation );
-        sample->set_rotation( sc2body );
+        sample->set_master_volume( _volume );
     }
 }
 
-void SGSampleGroup::set_volume( float vol )
-{
-    _volume = vol;
-    if (_volume < 0.0) _volume = 0.0;
-    if (_volume > 1.0) _volume = 1.0;
+// set the source position and orientation of all managed sounds
+void SGSampleGroup::update_pos_and_orientation() {
+ 
+    SGVec3d position = _base_pos - _smgr->get_position();
 
     sample_map_iterator sample_current = _samples.begin();
     sample_map_iterator sample_end = _samples.end();
     for ( ; sample_current != sample_end; ++sample_current ) {
         SGSoundSample *sample = sample_current->second;
-        sample->set_master_volume( _volume );
+        sample->set_position( position );
+        sample->set_orientation( _orientation );
     }
 }
 
@@ -382,7 +368,7 @@ void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
 
     if ( _tied_to_listener ) {
         orientation = _smgr->get_direction();
-        position = _smgr->get_position();
+        position = SGVec3d::zeros();
         velocity = _smgr->get_velocity();
     } else {
         sample->update_pos_and_orientation();
diff --git a/simgear/sound/sample_group.hxx b/simgear/sound/sample_group.hxx
index c13883b3..e6bb10e2 100644
--- a/simgear/sound/sample_group.hxx
+++ b/simgear/sound/sample_group.hxx
@@ -197,7 +197,7 @@ public:
      * @param pos Base position
      */
     void set_position_geod( const SGGeod& pos ) {
-        _position_geod = pos; _changed = true;
+        _base_pos = SGVec3d::fromGeod( pos ); _changed = true;
     }
 
     /**
@@ -225,8 +225,8 @@ private:
     bool _tied_to_listener;
 
     SGVec3f _velocity;
+    SGVec3d _base_pos;
     SGQuatd _orientation;
-    SGGeod _position_geod;
 
     sample_map _samples;
     std::vector< SGSharedPtr<SGSoundSample> > _removed_samples;
diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx
index 0d00083c..4abbb7cb 100644
--- a/simgear/sound/sample_openal.cxx
+++ b/simgear/sound/sample_openal.cxx
@@ -48,7 +48,6 @@ SGSoundSample::SGSoundSample() :
     _direction(SGVec3d::zeros()),
     _velocity(SGVec3f::zeros()),
     _orientation(SGQuatd::zeros()),
-    _rotation(SGQuatd::zeros()),
     _orivec(SGVec3f::zeros()),
     _base_pos(SGVec3d::zeros()),
     _refname(random_string()),
@@ -83,7 +82,6 @@ SGSoundSample::SGSoundSample( const char *path, const char *file ) :
     _direction(SGVec3d::zeros()),
     _velocity(SGVec3f::zeros()),
     _orientation(SGQuatd::zeros()),
-    _rotation(SGQuatd::zeros()),
     _orivec(SGVec3f::zeros()),
     _base_pos(SGVec3d::zeros()),
     _refname(file),
@@ -124,7 +122,6 @@ SGSoundSample::SGSoundSample( const unsigned char** data,
     _direction(SGVec3d::zeros()),
     _velocity(SGVec3f::zeros()),
     _orientation(SGQuatd::zeros()),
-    _rotation(SGQuatd::zeros()),
     _orivec(SGVec3f::zeros()),
     _base_pos(SGVec3d::zeros()),
     _refname(random_string()),
@@ -160,7 +157,6 @@ SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
     _direction(SGVec3d::zeros()),
     _velocity(SGVec3f::zeros()),
     _orientation(SGQuatd::zeros()),
-    _rotation(SGQuatd::zeros()),
     _orivec(SGVec3f::zeros()),
     _base_pos(SGVec3d::zeros()),
     _refname(random_string()),
@@ -199,11 +195,11 @@ void SGSoundSample::update_pos_and_orientation() {
 
     _absolute_pos = _base_pos;
     if ( _relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
-        _absolute_pos += _rotation.backTransform(_relative_pos);
+        _absolute_pos += _orientation.backTransform(_relative_pos);
     }
 
     if ( _direction[0] || _direction[1] || _direction[2] ) {
-        _orivec = toVec3f( _rotation.rotate(_direction) );
+        _orivec = toVec3f( _orientation.rotate(_direction) );
     }
     else
         _orivec = SGVec3f::zeros();
diff --git a/simgear/sound/sample_openal.hxx b/simgear/sound/sample_openal.hxx
index cef5889d..9a4cb06a 100644
--- a/simgear/sound/sample_openal.hxx
+++ b/simgear/sound/sample_openal.hxx
@@ -337,14 +337,6 @@ public:
         _orientation = ori; _changed = true;
     }
 
-    /**
-     * Set the rotation quatgernion of this sound.
-     * @param rotation Quaternion containing the rotation information
-     */
-    inline void set_rotation( const SGQuatd& rotation ) {
-        _rotation = rotation; _changed = true;
-    }
-
     /**
      * Set direction of this sound relative to the orientation.
      * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
@@ -461,7 +453,6 @@ private:
 
     // The position and orientation of this sound
     SGQuatd _orientation;       // base orientation
-    SGQuatd _rotation;		// rotation vector for relative offsets
     SGVec3f _orivec;		// orientation vector for OpenAL
     SGVec3d _base_pos;		// base position
 
diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx
index 76b3f138..1cb6c409 100644
--- a/simgear/sound/soundmgr_openal.cxx
+++ b/simgear/sound/soundmgr_openal.cxx
@@ -63,7 +63,6 @@ SGSoundMgr::SGSoundMgr() :
     _volume(0.0),
     _device(NULL),
     _context(NULL),
-    _position_geod(SGGeod::fromDeg(0,0)),
     _absolute_pos(SGVec3d::zeros()),
     _velocity(SGVec3d::zeros()),
     _orientation(SGQuatd::zeros()),
@@ -462,9 +461,6 @@ void SGSoundMgr::release_buffer(SGSoundSample *sample)
 }
 
 void SGSoundMgr::update_pos_and_orientation() {
-    // cartesian position of the listener
-    _absolute_pos = SGVec3d::fromGeod( _position_geod );
-
     /**
      * Description: ORIENTATION is a pair of 3-tuples representing the
      * 'at' direction vector and 'up' direction of the Object in
diff --git a/simgear/sound/soundmgr_openal.hxx b/simgear/sound/soundmgr_openal.hxx
index 1be7eebd..676d6d53 100644
--- a/simgear/sound/soundmgr_openal.hxx
+++ b/simgear/sound/soundmgr_openal.hxx
@@ -157,7 +157,7 @@ public:
      * @param pos OpenAL listener position
      */
     void set_position_geod( const SGGeod& pos ) {
-        _position_geod = pos; _changed = true;
+        _absolute_pos = SGVec3d::fromGeod( pos ); _changed = true;
     }
 
     /**
@@ -166,7 +166,6 @@ public:
      * @return OpenAL listener position
      */
     SGVec3d& get_position() { return _absolute_pos; }
-    SGGeod& get_position_geod() { return _position_geod; }
 
     /**
      * Set the velocity vector (in meters per second) of the sound manager
@@ -281,7 +280,6 @@ private:
     ALCcontext *_context;
 
     // Position of the listener.
-    SGGeod _position_geod;
     SGVec3d _absolute_pos;
 
     // Velocity of the listener.
diff --git a/simgear/sound/xmlsound.cxx b/simgear/sound/xmlsound.cxx
index 29bf06b5..fcfe41e5 100644
--- a/simgear/sound/xmlsound.cxx
+++ b/simgear/sound/xmlsound.cxx
@@ -230,8 +230,8 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
    SGPropertyNode_ptr prop = node->getChild("position");
    if ( prop != NULL ) {
        offset_pos[0] = prop->getDoubleValue("y", 0.0);
-       offset_pos[1] = prop->getDoubleValue("z", 0.0);
-       offset_pos[2] = prop->getDoubleValue("x", 0.0);
+       offset_pos[1] = -prop->getDoubleValue("z", 0.0);
+       offset_pos[2] = -prop->getDoubleValue("x", 0.0);
    }
 
    //
@@ -244,8 +244,8 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
    prop = node->getChild("orientation");
    if ( prop != NULL ) {
       dir = SGVec3f(prop->getFloatValue("y", 0.0),
-                    prop->getFloatValue("z", 0.0),
-                    prop->getFloatValue("x", 0.0));
+                    -prop->getFloatValue("z", 0.0),
+                    -prop->getFloatValue("x", 0.0));
       inner = prop->getFloatValue("inner-angle", 360.0);
       outer = prop->getFloatValue("outer-angle", 360.0);
       outer_gain = prop->getFloatValue("outer-gain", 0.0);
-- 
2.39.5