From 7c7ae4313dbba2c9b1e78deb8f8f58a8bd5b076f Mon Sep 17 00:00:00 2001 From: Niklas Kersten Date: Sat, 21 Dec 2024 16:12:11 +0100 Subject: [PATCH] :wrench: Handle checks for obstructions in a separate function --- source/main/audio/SoundManager.cpp | 46 ++++++++++++++---------------- source/main/audio/SoundManager.h | 16 +++++++---- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/source/main/audio/SoundManager.cpp b/source/main/audio/SoundManager.cpp index 1ae0666d8b..071771130f 100644 --- a/source/main/audio/SoundManager.cpp +++ b/source/main/audio/SoundManager.cpp @@ -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 @@ -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 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 @@ -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 diff --git a/source/main/audio/SoundManager.h b/source/main/audio/SoundManager.h index 0f7f9d054e..30899712a0 100644 --- a/source/main/audio/SoundManager.h +++ b/source/main/audio/SoundManager.h @@ -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.