mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
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.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include "Cubed/audio/audio_engine.hpp"
|
#include "Cubed/audio/audio_engine.hpp"
|
||||||
|
|
||||||
|
#include "Cubed/audio/audio_error.hpp"
|
||||||
#include "Cubed/config.hpp"
|
#include "Cubed/config.hpp"
|
||||||
#include "Cubed/tools/cubed_assert.hpp"
|
#include "Cubed/tools/cubed_assert.hpp"
|
||||||
#include "Cubed/tools/log.hpp"
|
#include "Cubed/tools/log.hpp"
|
||||||
@@ -34,6 +35,9 @@ void AudioEngine::init() {
|
|||||||
}
|
}
|
||||||
alcMakeContextCurrent(context);
|
alcMakeContextCurrent(context);
|
||||||
|
|
||||||
|
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
|
||||||
|
check_al_error();
|
||||||
|
|
||||||
auto& config = Config::get();
|
auto& config = Config::get();
|
||||||
|
|
||||||
m_music_volume = static_cast<float>(config.get<double>("volume.music"));
|
m_music_volume = static_cast<float>(config.get<double>("volume.music"));
|
||||||
|
|||||||
@@ -39,20 +39,22 @@ void AudioSource::set_buffer_3d(const AudioBuffer& buffer,
|
|||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
m_duration = buffer.duration();
|
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());
|
alSourcei(m_source, AL_BUFFER, buffer.buffer());
|
||||||
check_al_error();
|
check_al_error();
|
||||||
alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z);
|
alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z);
|
||||||
check_al_error();
|
check_al_error();
|
||||||
|
|
||||||
alSourcef(m_source, AL_REFERENCE_DISTANCE, 4.0f);
|
alSourcef(m_source, AL_REFERENCE_DISTANCE, 4.0f);
|
||||||
|
|
||||||
alSourcef(m_source, AL_ROLLOFF_FACTOR, 1.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) {
|
void AudioSource::set_loop(bool on) {
|
||||||
|
|||||||
@@ -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);
|
auto [chunk_x, chunk_z] = get_chunk_pos(world_x, world_z);
|
||||||
ChunkPos pos{chunk_x, chunk_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;
|
BlockType origin_id = 0;
|
||||||
{
|
{
|
||||||
chunk_acc acc;
|
chunk_acc acc;
|
||||||
@@ -161,7 +160,8 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
|
|||||||
if (!m_chunks.find(acc, pos)) {
|
if (!m_chunks.find(acc, pos)) {
|
||||||
return;
|
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 ||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUNK_SIZE) {
|
z >= CHUNK_SIZE) {
|
||||||
return;
|
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->set_chunk_block(idx, id);
|
||||||
acc->second->mark_dirty();
|
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) {
|
if (id == 0) {
|
||||||
std::string name = BlockManager::name_form_id(origin_id);
|
std::string name = BlockManager::name_form_id(origin_id);
|
||||||
|
|||||||
Reference in New Issue
Block a user