From 7eebed40e095dcf0232e74407ab7928d542c2da5 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Tue, 7 Jul 2026 14:52:55 +0800 Subject: [PATCH] fix(audio): set distance model, reduce max distance, fix sound pos - Set OpenAL distance model to AL_INVERSE_DISTANCE_CLAMPED in engine. - Reduced AL_MAX_DISTANCE from 100 to 48 for better falloff. - Fixed block sound position to use world coordinates instead of local block coordinates. --- src/audio/audio_engine.cpp | 4 ++++ src/audio/audio_source.cpp | 14 ++++++++------ src/gameplay/client_world.cpp | 9 +++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/audio/audio_engine.cpp b/src/audio/audio_engine.cpp index 1204e3d..c475055 100644 --- a/src/audio/audio_engine.cpp +++ b/src/audio/audio_engine.cpp @@ -1,5 +1,6 @@ #include "Cubed/audio/audio_engine.hpp" +#include "Cubed/audio/audio_error.hpp" #include "Cubed/config.hpp" #include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/log.hpp" @@ -34,6 +35,9 @@ void AudioEngine::init() { } alcMakeContextCurrent(context); + alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); + check_al_error(); + auto& config = Config::get(); m_music_volume = static_cast(config.get("volume.music")); diff --git a/src/audio/audio_source.cpp b/src/audio/audio_source.cpp index 5cfc709..2873303 100644 --- a/src/audio/audio_source.cpp +++ b/src/audio/audio_source.cpp @@ -39,20 +39,22 @@ void AudioSource::set_buffer_3d(const AudioBuffer& buffer, stop(); } m_duration = buffer.duration(); - Logger::info("buffer={}", buffer.buffer()); - Logger::info("source={} isSource={}", m_source, alIsSource(m_source)); - Logger::info("buffer={} isBuffer={}", buffer.buffer(), - alIsBuffer(buffer.buffer())); alSourcei(m_source, AL_BUFFER, buffer.buffer()); check_al_error(); alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z); check_al_error(); alSourcef(m_source, AL_REFERENCE_DISTANCE, 4.0f); - alSourcef(m_source, AL_ROLLOFF_FACTOR, 1.0f); + alSourcef(m_source, AL_MAX_DISTANCE, 48.0f); + ALfloat l[3]; + alGetListenerfv(AL_POSITION, l); - alSourcef(m_source, AL_MAX_DISTANCE, 100.0f); + Logger::info("Listener Pos = ({}, {}, {})", l[0], l[1], l[2]); + ALfloat p[3]; + alGetSourcefv(m_source, AL_POSITION, p); + + Logger::info("Source Pos = ({}, {}, {})", p[0], p[1], p[2]); } void AudioSource::set_loop(bool on) { diff --git a/src/gameplay/client_world.cpp b/src/gameplay/client_world.cpp index d4ab031..04d9bd7 100644 --- a/src/gameplay/client_world.cpp +++ b/src/gameplay/client_world.cpp @@ -152,8 +152,7 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) { auto [chunk_x, chunk_z] = get_chunk_pos(world_x, world_z); ChunkPos pos{chunk_x, chunk_z}; - auto [x, y, z] = ClientChunk::world_to_block(world_x, world_y, world_z, - chunk_x, chunk_z); + BlockType origin_id = 0; { chunk_acc acc; @@ -161,7 +160,8 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) { if (!m_chunks.find(acc, pos)) { return; } - + auto [x, y, z] = ClientChunk::world_to_block(world_x, world_y, world_z, + chunk_x, chunk_z); if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y || z >= CHUNK_SIZE) { return; @@ -171,7 +171,8 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) { acc->second->set_chunk_block(idx, id); acc->second->mark_dirty(); } - glm::vec3 sound_pos{x + 0.5, y, z + 0.5}; + + glm::vec3 sound_pos{world_x + 0.5f, world_y + 0.5f, world_z + 0.5f}; if (id == 0) { std::string name = BlockManager::name_form_id(origin_id);