Skip to content

Commit

Permalink
🔧 Handle checks for obstructions in a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiradur committed Dec 21, 2024
1 parent 055d20e commit 7c7ae43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
46 changes: 21 additions & 25 deletions source/main/audio/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,10 @@ void SoundManager::recomputeAllSources()

void SoundManager::UpdateSourceFilters(const int hardware_index) const
{
bool source_is_obstructed = this->UpdateObstructionFilter(hardware_index);
bool source_is_obstructed = this->IsHardwareSourceObstructed(hardware_index);

this->UpdateObstructionFilter(hardware_index, source_is_obstructed);

/*
* If an obstruction was detected, also check for occlusions.
* The current implementation ignores the case of exclusion since
Expand All @@ -946,30 +949,21 @@ void SoundManager::UpdateSourceFilters(const int hardware_index) const
}
}

bool SoundManager::UpdateObstructionFilter(const int hardware_index) const
bool SoundManager::IsHardwareSourceObstructed(const int hardware_index) const
{
if (hardware_sources_map[hardware_index] == -1) { return false; } // no sound assigned to hardware source

if (!App::audio_enable_obstruction->getBool())
{
// detach the obstruction filter in case it was attached when the feature was previously enabled
alSourcei(hardware_sources[hardware_index], AL_DIRECT_FILTER, AL_FILTER_NULL);
return false;
}

bool obstruction_detected = false;
const SoundPtr& corresponding_sound = audio_sources[hardware_sources_map[hardware_index]];

// TODO: Simulate diffraction path.
bool obstruction_detected = false;

/*
* Perform various line of sight checks until either a collision was detected
* and the filter has to be applied or no obstruction was detected.
*/
* Perform various line of sight checks until either a collision was detected
* and the filter has to be applied or no obstruction was detected.
*/

std::pair<bool, Ogre::Real> intersection;
const Ogre::Vector3 direction_to_sound = corresponding_sound->getPosition() - m_listener_position;
const Ogre::Ray direct_path_to_sound = Ray(m_listener_position, direction_to_sound);
const Ogre::Vector3 direction_to_sound = corresponding_sound->getPosition() - m_listener_position;
const Ogre::Ray direct_path_to_sound = Ray(m_listener_position, direction_to_sound);

// perform line of sight check against collision meshes
// for this to work correctly, the direction vector of the ray must have
Expand Down Expand Up @@ -1046,18 +1040,20 @@ bool SoundManager::UpdateObstructionFilter(const int hardware_index) const
obstruction_detected = intersection.first;
}

if (obstruction_detected)
return obstruction_detected;
}

void SoundManager::UpdateObstructionFilter(const int hardware_index, const bool enable_obstruction_filter) const
{
if (!App::audio_enable_obstruction->getBool() || !enable_obstruction_filter)
{
// detach the obstruction filter in case it was attached when the feature was previously enabled
alSourcei(hardware_sources[hardware_index], AL_DIRECT_FILTER, AL_FILTER_NULL);
}
else {
// Apply obstruction filter to the source
alSourcei(hardware_sources[hardware_index], AL_DIRECT_FILTER, m_efx_outdoor_obstruction_lowpass_filter_id);
}
else
{
// reset direct filter for the source in case it has been set previously
alSourcei(hardware_sources[hardware_index], AL_DIRECT_FILTER, AL_FILTER_NULL);
}

return obstruction_detected;
}

bool SoundManager::UpdateOcclusionFilter(const int hardware_index, const ALuint effect_slot_id, const EFXEAXREVERBPROPERTIES* reference_efx_reverb_properties) const
Expand Down
16 changes: 11 additions & 5 deletions source/main/audio/SoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,19 @@ class SoundManager
void UpdateSourceFilters(const int hardware_index) const;

/**
* Applies an obstruction filter to the provided source if certain conditions apply.
* To decide whether the filter should be applied or not, the function performs
* various checks against the environment of the listener.
* Performs various checks against the environment of the listener to determine
* whether the sound belonging to a hardware source is obstructed or not.
* @param hardware_index The index of the hardware source.
* @return True if an obstruction was detected, false otherwise.
* @return True if the sound is obstructed from the listener's point of view, false otherwise.
*/
bool UpdateObstructionFilter(const int hardware_index) const;
bool IsHardwareSourceObstructed(const int hardware_index) const;

/**
* Applies an obstruction filter to the provided hardware source.
* @param hardware_index The index of the hardware source.
* @param enable_obstruction_filter Whether the obstruction filter should be enabled for the hardware source or not.
*/
void UpdateObstructionFilter(const int hardware_index, const bool enable_obstruction_filter) const;

/**
* Applies an occlusion filter to the provided source if certain conditions apply.
Expand Down

0 comments on commit 7c7ae43

Please sign in to comment.