From 191ee3a0edfb52444714c7a8a1fae64016bde1f5 Mon Sep 17 00:00:00 2001 From: ehofman Date: Thu, 29 Oct 2009 13:33:52 +0000 Subject: [PATCH] Commit the current state of affairs to see if it fixes the position code for others --- simgear/sound/openal_test3.cxx | 7 +-- simgear/sound/sample_group.cxx | 83 +++++++++++++++---------------- simgear/sound/sample_group.hxx | 4 +- simgear/sound/sample_openal.cxx | 6 ++- simgear/sound/sample_openal.hxx | 16 +++--- simgear/sound/soundmgr_openal.cxx | 8 +-- simgear/sound/soundmgr_openal.hxx | 8 +-- simgear/sound/xmlsound.cxx | 14 +++--- 8 files changed, 74 insertions(+), 72 deletions(-) diff --git a/simgear/sound/openal_test3.cxx b/simgear/sound/openal_test3.cxx index c5f124f7..79269f10 100644 --- a/simgear/sound/openal_test3.cxx +++ b/simgear/sound/openal_test3.cxx @@ -34,21 +34,22 @@ int main( int argc, char *argv[] ) { smgr->update(1.0); printf("playing sample\n"); sleep(3); - sgr->stop("sound1"); + sample1->stop(); smgr->update(3.0); sleep(1); - printf("source at lat,lon = (10,-10), listener at (0.999,-0.999)\n"); + printf("source at lat,lon = (10,-10), listener at (9.99,-9.99)\n"); sample1->set_position( SGGeod::fromDeg(10,-10) ); smgr->set_position( SGVec3d::fromGeod(SGGeod::fromDeg(9.99,-9.99)) ); sample1->play_looped(); smgr->update(1.0); printf("playing sample\n"); sleep(3); - sgr->stop("sound1"); + sample1->stop(); smgr->update(3.0); sleep(1); + sgr->remove("sound1"); smgr->unbind(); sleep(2); delete smgr; diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx index 90278dc5..eed424c1 100644 --- a/simgear/sound/sample_group.cxx +++ b/simgear/sound/sample_group.cxx @@ -156,7 +156,7 @@ void SGSampleGroup::update( double dt ) { sample->stop(); sample->no_valid_source(); _smgr->release_source( sample->get_source() ); - } else { + } else if ( _smgr->has_changed() ) { update_sample_config( sample ); } @@ -308,7 +308,7 @@ bool SGSampleGroup::stop( const string& refname ) { } // set source velocity of all managed sounds -void SGSampleGroup::set_velocity( const SGVec3d &vel ) { +void SGSampleGroup::set_velocity( const SGVec3f &vel ) { if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) ) { SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity"); @@ -368,47 +368,46 @@ void SGSampleGroup::set_volume( float vol ) } void SGSampleGroup::update_sample_config( SGSoundSample *sample ) { - if ( sample->is_valid_source() ) { - unsigned int source = sample->get_source(); - - float *position, *orientation, *velocity; - if ( _tied_to_listener ) { - position = _smgr->get_position().data(); - orientation = _smgr->get_direction().data(); - velocity = _smgr->get_velocity().data(); - } else { - sample->update_absolute_position(); - position = sample->get_position(); - orientation = sample->get_orientation(); - velocity = sample->get_velocity(); - } + SGVec3f orientation, velocity; + SGVec3d position; + + if ( _tied_to_listener ) { + orientation = _smgr->get_direction(); + position = _smgr->get_position(); + velocity = _smgr->get_velocity(); + } else { + sample->update_absolute_position(); + orientation = sample->get_orientation(); + position = sample->get_position(); + velocity = sample->get_velocity(); + } - if (dist(_smgr->get_position(), sample->get_position_vec()) > 50000) - printf("source and listener distance greater than 50km!\n"); - if (isNaN(position)) printf("NaN in source position\n"); - if (isNaN(orientation)) printf("NaN in source orientation\n"); - if (isNaN(velocity)) printf("NaN in source velocity\n"); - - alSourcefv( source, AL_POSITION, position ); - alSourcefv( source, AL_VELOCITY, velocity ); - alSourcefv( source, AL_DIRECTION, orientation ); - testForALError("position and orientation"); - - alSourcef( source, AL_PITCH, sample->get_pitch() ); - alSourcef( source, AL_GAIN, sample->get_volume() ); - testForALError("pitch and gain"); - - if ( sample->has_static_data_changed() ) { - alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() ); - alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() ); - alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() ); - testForALError("audio cone"); - - alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() ); - alSourcef( source, AL_REFERENCE_DISTANCE, - sample->get_reference_dist() ); - testForALError("distance rolloff"); - } + if (dist(position, _smgr->get_position()) > 10000) + printf("source and listener distance greater than 20km!\n"); + if (isNaN(position)) printf("NaN in source position\n"); + if (isNaN(orientation)) printf("NaN in source orientation\n"); + if (isNaN(velocity)) printf("NaN in source velocity\n"); + + unsigned int source = sample->get_source(); + alSourcefv( source, AL_POSITION, toVec3f(position).data() ); + alSourcefv( source, AL_VELOCITY, velocity.data() ); + alSourcefv( source, AL_DIRECTION, orientation.data() ); + testForALError("position and orientation"); + + alSourcef( source, AL_PITCH, sample->get_pitch() ); + alSourcef( source, AL_GAIN, sample->get_volume() ); + testForALError("pitch and gain"); + + if ( sample->has_static_data_changed() ) { + alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() ); + alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() ); + alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() ); + testForALError("audio cone"); + + alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() ); + alSourcef( source, AL_REFERENCE_DISTANCE, + sample->get_reference_dist() ); + testForALError("distance rolloff"); } } diff --git a/simgear/sound/sample_group.hxx b/simgear/sound/sample_group.hxx index af85e312..d74866e7 100644 --- a/simgear/sound/sample_group.hxx +++ b/simgear/sound/sample_group.hxx @@ -189,7 +189,7 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. * @param vel Velocity vector */ - void set_velocity( const SGVec3d& vel ); + void set_velocity( const SGVec3f& vel ); /** * Set the position of this sample group. @@ -219,7 +219,7 @@ private: float _volume; bool _tied_to_listener; - SGVec3d _velocity; + SGVec3f _velocity; SGQuatd _orientation; SGGeod _position; diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx index 05fc1ed8..75d663a5 100644 --- a/simgear/sound/sample_openal.cxx +++ b/simgear/sound/sample_openal.cxx @@ -207,8 +207,10 @@ void SGSoundSample::update_absolute_position() { // The cartesian position of the base sound coordinate SGVec3d position = SGVec3d::fromGeod(_base_pos); - _absolute_pos = position + (sc2body*q).backTransform(_relative_pos); - _orivec = toVec3f( (sc2body*q).backTransform(_direction) ); + _absolute_pos = position; // + (sc2body*q).backTransform(_relative_pos); + if ( !(_direction[0] == 0 && _direction[1] == 0 && _direction[2] == 0) ) { + _orivec = toVec3f((sc2body*q).backTransform(toVec3d(_direction))); + } } string SGSoundSample::random_string() { diff --git a/simgear/sound/sample_openal.hxx b/simgear/sound/sample_openal.hxx index 972a57c4..0d924246 100644 --- a/simgear/sound/sample_openal.hxx +++ b/simgear/sound/sample_openal.hxx @@ -320,8 +320,7 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. * @return Absolute position */ - float *get_position() const { return toVec3f(_absolute_pos).data(); } - SGVec3f get_position_vec() const { return toVec3f(_absolute_pos); } + SGVec3d& get_position() { return _absolute_pos; } /** * Set the orientation of this sound. @@ -336,7 +335,7 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right * @param dir Sound emission direction */ - inline void set_direction( const SGVec3d& dir ) { + inline void set_direction( const SGVec3f& dir ) { _direction = dir; _changed = true; } @@ -359,7 +358,8 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right * @return Orientaton vector */ - float *get_orientation() { return _orivec.data(); } + SGVec3f& get_orientation() { return _orivec; } + SGVec3f& get_direction() { return _direction; } /** * Get the inner angle of the audio cone. @@ -384,7 +384,7 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right * @param Velocity vector */ - inline void set_velocity( const SGVec3d& vel ) { + inline void set_velocity( const SGVec3f& vel ) { _velocity = vel; _changed = true; } @@ -393,7 +393,7 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right * @return Velocity vector */ - float *get_velocity() { return toVec3f(_velocity).data(); } + SGVec3f& get_velocity() { return _velocity; } /** @@ -442,8 +442,8 @@ private: // Position of the source sound. SGVec3d _absolute_pos; // absolute position SGVec3d _relative_pos; // position relative to the base position - SGVec3d _direction; // orientation offset - SGVec3d _velocity; // Velocity of the source sound. + SGVec3f _direction; // orientation offset + SGVec3f _velocity; // Velocity of the source sound. // The position and orientation of this sound SGQuatd _orientation; // base orientation diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index dcb06b5a..6b530e53 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -255,11 +255,11 @@ void SGSoundMgr::update( double dt ) { if (_changed) { if (isNaN(_at_up_vec)) printf("NaN in listener orientation\n"); if (isNaN(toVec3f(_position).data())) printf("NaN in listener position\n"); -if (isNaN(toVec3f(_velocity).data())) printf("NaN in listener velocity\n"); +if (isNaN(_velocity.data())) printf("NaN in listener velocity\n"); alListenerf( AL_GAIN, _volume ); alListenerfv( AL_ORIENTATION, _at_up_vec ); alListenerfv( AL_POSITION, toVec3f(_position).data() ); - alListenerfv( AL_VELOCITY, toVec3f(_velocity).data() ); + alListenerfv( AL_VELOCITY, _velocity.data() ); // alDopplerVelocity(340.3); // TODO: altitude dependent testForALError("update"); _changed = false; @@ -352,8 +352,8 @@ void SGSoundMgr::set_orientation( const SGQuatd& ori, const SGQuatd& offs ) _orientation = ori; _orient_offs = offs; - SGVec3d sgv_up = _orient_offs.rotate(SGVec3d::e2()); - SGVec3d sgv_at = _orient_offs.rotate(SGVec3d::e3()); + SGVec3d sgv_up = offs.rotate(SGVec3d::e2()); + SGVec3d sgv_at = offs.rotate(SGVec3d::e3()); _at_up_vec[0] = sgv_at[0]; _at_up_vec[1] = sgv_at[1]; _at_up_vec[2] = sgv_at[2]; diff --git a/simgear/sound/soundmgr_openal.hxx b/simgear/sound/soundmgr_openal.hxx index bc3851f9..74e6b89f 100644 --- a/simgear/sound/soundmgr_openal.hxx +++ b/simgear/sound/soundmgr_openal.hxx @@ -167,14 +167,14 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right * @return OpenAL listener position */ - SGVec3f get_position() { return toVec3f(_position); } + SGVec3d& get_position() { return _position; } /** * Set the velocity vector of the sound manager * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. * @param vel Velocity vector of the OpenAL listener */ - void set_velocity( SGVec3d& vel ) { + void set_velocity( SGVec3f& vel ) { _velocity = vel; _changed = true; } @@ -183,7 +183,7 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. * @return Velocity vector of the OpenAL listener */ - inline SGVec3f get_velocity() { return toVec3f(_velocity); } + inline SGVec3f& get_velocity() { return _velocity; } /** * Set the orientation of the sound manager @@ -284,7 +284,7 @@ private: SGVec3d _position; // Velocity of the listener. - SGVec3d _velocity; + SGVec3f _velocity; // Orientation of the listener. // first 3 elements are "at" vector, second 3 are "up" vector diff --git a/simgear/sound/xmlsound.cxx b/simgear/sound/xmlsound.cxx index 45b303a1..04b8e060 100644 --- a/simgear/sound/xmlsound.cxx +++ b/simgear/sound/xmlsound.cxx @@ -237,18 +237,18 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, // // Orientation // - SGVec3d dir = SGVec3d::zeros(); + SGVec3f dir = SGVec3f::zeros(); float inner = 360.0; float outer = 360.0; float outer_gain = 0.0; prop = node->getChild("orientation"); if ( prop != NULL ) { - dir = SGVec3d(prop->getDoubleValue("y", 0.0), - prop->getDoubleValue("z", 0.0), - prop->getDoubleValue("x", 0.0)); - inner = prop->getDoubleValue("inner-angle", 360.0); - outer = prop->getDoubleValue("outer-angle", 360.0); - outer_gain = prop->getDoubleValue("outer-gain", 0.0); + dir = SGVec3f(prop->getFloatValue("y", 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