Fixed: Relative positioning mode was not implemented in OpenAL library driver with manual rolloff enabled

This commit is contained in:
kaetemi 2012-04-12 17:48:56 +02:00
parent a87afeb44b
commit dec156f728
2 changed files with 36 additions and 10 deletions

View file

@ -22,6 +22,8 @@
#include "source_al.h" #include "source_al.h"
#include "ext_al.h" #include "ext_al.h"
// #define NLSOUND_DEBUG_GAIN
using namespace std; using namespace std;
using namespace NLMISC; using namespace NLMISC;
@ -30,7 +32,7 @@ namespace NLSOUND {
CSourceAL::CSourceAL(CSoundDriverAL *soundDriver) : CSourceAL::CSourceAL(CSoundDriverAL *soundDriver) :
_SoundDriver(NULL), _Buffer(NULL), _Source(AL_NONE), _SoundDriver(NULL), _Buffer(NULL), _Source(AL_NONE),
_DirectFilter(AL_FILTER_NULL), _EffectFilter(AL_FILTER_NULL), _DirectFilter(AL_FILTER_NULL), _EffectFilter(AL_FILTER_NULL),
_IsPlaying(false), _IsPaused(false), _StartTime(0), _IsStreaming(false), _IsPlaying(false), _IsPaused(false), _StartTime(0), _IsStreaming(false), _RelativeMode(false),
_Pos(0.0f, 0.0f, 0.0f), _Gain(NLSOUND_DEFAULT_GAIN), _Alpha(1.0), _Pos(0.0f, 0.0f, 0.0f), _Gain(NLSOUND_DEFAULT_GAIN), _Alpha(1.0),
_MinDistance(1.0f), _MaxDistance(numeric_limits<float>::max()), _MinDistance(1.0f), _MaxDistance(numeric_limits<float>::max()),
_Effect(NULL), _Direct(true), _Effect(NULL), _Direct(true),
@ -92,11 +94,17 @@ void CSourceAL::release()
/// (Internal) Update the 3d changes. /// (Internal) Update the 3d changes.
void CSourceAL::updateManualRolloff() void CSourceAL::updateManualRolloff()
{ {
CVector distanceVector = _Pos - CListenerAL::getInstance()->getPos(); CVector distanceVector = _RelativeMode ? _Pos : (_Pos - CListenerAL::getInstance()->getPos());
float distanceSquare = distanceVector.sqrnorm(); float distanceSquare = distanceVector.sqrnorm();
float rolloff = ISource::computeManualRolloff(_Alpha, distanceSquare, _MinDistance, _MaxDistance); float rolloff = ISource::computeManualRolloff(_Alpha, distanceSquare, _MinDistance, _MaxDistance);
alSourcef(_Source, AL_GAIN, _Gain * rolloff); alSourcef(_Source, AL_GAIN, _Gain * rolloff);
alTestError(); alTestError();
#ifdef NLSOUND_DEBUG_GAIN
ALfloat gain;
alGetSourcef(_Source, AL_GAIN, &gain);
nlwarning("Called updateManualRolloff(), physical gain is %f, configured gain is %f, distanceSquare is %f, rolloff is %f", gain, _Gain, distanceSquare, rolloff);
alTestError();
#endif
} }
/// Enable or disable streaming mode. Source must be stopped to call this. /// Enable or disable streaming mode. Source must be stopped to call this.
@ -210,6 +218,13 @@ bool CSourceAL::getLooping() const
/// Play the static buffer (or stream in and play) /// Play the static buffer (or stream in and play)
bool CSourceAL::play() bool CSourceAL::play()
{ {
#ifdef NLSOUND_DEBUG_GAIN
if (_IsStreaming)
{
nlwarning("Called play on a streaming source");
}
#endif
// Commit 3D changes before starting play // Commit 3D changes before starting play
if (_SoundDriver->getOption(ISoundDriver::OptionManualRolloff)) if (_SoundDriver->getOption(ISoundDriver::OptionManualRolloff))
updateManualRolloff(); updateManualRolloff();
@ -422,15 +437,23 @@ void CSourceAL::setGain(float gain)
alSourcef(_Source, AL_GAIN, _Gain); alSourcef(_Source, AL_GAIN, _Gain);
alTestError(); alTestError();
} }
#ifdef NLSOUND_DEBUG_GAIN
else
{
nlwarning("Called setGain(), manual rolloff, commit deferred to play or update, physical gain is %f, configured gain is %f", gain, _Gain);
}
#endif
} }
/// Get the gain /// Get the gain
float CSourceAL::getGain() const float CSourceAL::getGain() const
{ {
//ALfloat gain; #ifdef NLSOUND_DEBUG_GAIN
//alGetSourcef(_Source, AL_GAIN, &gain); ALfloat gain;
//alTestError(); alGetSourcef(_Source, AL_GAIN, &gain);
//return gain; nlwarning("Called getGain(), physical gain is %f, configured gain is %f", gain, _Gain);
alTestError();
#endif
return _Gain; return _Gain;
} }
@ -453,6 +476,7 @@ float CSourceAL::getPitch() const
/// Set the source relative mode. If true, positions are interpreted relative to the listener position. /// Set the source relative mode. If true, positions are interpreted relative to the listener position.
void CSourceAL::setSourceRelativeMode( bool mode ) void CSourceAL::setSourceRelativeMode( bool mode )
{ {
_RelativeMode = mode;
alSourcei(_Source, AL_SOURCE_RELATIVE, mode?AL_TRUE:AL_FALSE ); alSourcei(_Source, AL_SOURCE_RELATIVE, mode?AL_TRUE:AL_FALSE );
alTestError(); alTestError();
} }
@ -460,10 +484,11 @@ void CSourceAL::setSourceRelativeMode( bool mode )
/// Get the source relative mode (3D mode only) /// Get the source relative mode (3D mode only)
bool CSourceAL::getSourceRelativeMode() const bool CSourceAL::getSourceRelativeMode() const
{ {
ALint b; //ALint b;
alGetSourcei(_Source, AL_SOURCE_RELATIVE, &b ); //alGetSourcei(_Source, AL_SOURCE_RELATIVE, &b );
alTestError(); //alTestError();
return (b==AL_TRUE); //return (b==AL_TRUE);
return _RelativeMode;
} }
/// Set the min and max distances (3D mode only) /// Set the min and max distances (3D mode only)

View file

@ -61,6 +61,7 @@ private:
NLMISC::TTime _StartTime; NLMISC::TTime _StartTime;
bool _IsStreaming; bool _IsStreaming;
bool _RelativeMode;
NLMISC::CVector _Pos; NLMISC::CVector _Pos;
float _Gain; float _Gain;