mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 17:39:46 +00:00
make audio positions relative to the listener
OpenAL takes positions as floats. Positions of the listener and of the speakers were passed in the world space, so far from the world origin both were rounded to the float step (2 m at 30M blocks): a sound next to the player came from a wrong side and with a wrong volume. A speaker keeps its position as worldvec3, the listener stays in the OpenAL origin, and a source is placed at "speaker - listener". Sources are refreshed in the speaker update, once per frame.
This commit is contained in:
parent
d012f0530b
commit
56ab3de514
7 changed files with 41 additions and 42 deletions
|
|
@ -342,6 +342,7 @@ void ALSpeaker::update(const Channel* channel) {
|
|||
if (source == 0) return;
|
||||
float gain = this->volume * channel->getVolume();
|
||||
AL_CHECK(alSourcef(source, AL_GAIN, gain));
|
||||
refreshPosition();
|
||||
|
||||
if (!paused) {
|
||||
if (isPaused() && !channel->isPaused()) {
|
||||
|
|
@ -451,12 +452,18 @@ void ALSpeaker::setTime(duration_t time) {
|
|||
AL_CHECK(alSourcef(source, AL_SEC_OFFSET, static_cast<float>(time)));
|
||||
}
|
||||
|
||||
void ALSpeaker::setPosition(glm::vec3 pos) {
|
||||
void ALSpeaker::refreshPosition() {
|
||||
glm::vec3 pos(relative ? position : position - al->listenerPosition);
|
||||
AL_CHECK(alSource3f(source, AL_POSITION, pos.x, pos.y, pos.z));
|
||||
}
|
||||
|
||||
glm::vec3 ALSpeaker::getPosition() const {
|
||||
return AL::getSource3f(source, AL_POSITION);
|
||||
void ALSpeaker::setPosition(worldvec3 pos) {
|
||||
position = pos;
|
||||
refreshPosition();
|
||||
}
|
||||
|
||||
worldvec3 ALSpeaker::getPosition() const {
|
||||
return position;
|
||||
}
|
||||
|
||||
void ALSpeaker::setVelocity(glm::vec3 vel) {
|
||||
|
|
@ -468,9 +475,11 @@ glm::vec3 ALSpeaker::getVelocity() const {
|
|||
}
|
||||
|
||||
void ALSpeaker::setRelative(bool relative) {
|
||||
this->relative = relative;
|
||||
AL_CHECK(
|
||||
alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE)
|
||||
);
|
||||
refreshPosition();
|
||||
}
|
||||
|
||||
bool ALSpeaker::isRelative() const {
|
||||
|
|
@ -782,11 +791,12 @@ void ALAudio::freeBuffer(uint buffer) {
|
|||
}
|
||||
|
||||
void ALAudio::setListener(
|
||||
glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
|
||||
worldvec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
|
||||
) {
|
||||
ALfloat listenerOri[] = {at.x, at.y, at.z, up.x, up.y, up.z};
|
||||
|
||||
AL_CHECK(alListener3f(AL_POSITION, position.x, position.y, position.z));
|
||||
listenerPosition = position;
|
||||
AL_CHECK(alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f));
|
||||
AL_CHECK(alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z));
|
||||
AL_CHECK(alListenerfv(AL_ORIENTATION, listenerOri));
|
||||
AL_CHECK(alListenerf(AL_GAIN, get_channel(0)->getVolume()));
|
||||
|
|
|
|||
|
|
@ -121,6 +121,10 @@ namespace audio {
|
|||
int priority;
|
||||
int channel;
|
||||
float volume = 0.0f;
|
||||
worldvec3 position {};
|
||||
bool relative = false;
|
||||
|
||||
void refreshPosition();
|
||||
public:
|
||||
ALStream* stream = nullptr;
|
||||
bool manuallyStopped = true;
|
||||
|
|
@ -153,8 +157,8 @@ namespace audio {
|
|||
duration_t getDuration() const override;
|
||||
void setTime(duration_t time) override;
|
||||
|
||||
void setPosition(glm::vec3 pos) override;
|
||||
glm::vec3 getPosition() const override;
|
||||
void setPosition(worldvec3 pos) override;
|
||||
worldvec3 getPosition() const override;
|
||||
|
||||
void setVelocity(glm::vec3 vel) override;
|
||||
glm::vec3 getVelocity() const override;
|
||||
|
|
@ -188,6 +192,7 @@ namespace audio {
|
|||
std::array<uint, 1> filters;
|
||||
|
||||
bool useEffects;
|
||||
worldvec3 listenerPosition {};
|
||||
|
||||
ALAudio(
|
||||
ALCdevice* device,
|
||||
|
|
@ -221,7 +226,7 @@ namespace audio {
|
|||
std::vector<std::string> getInputDeviceNames() override;
|
||||
|
||||
void setListener(
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
glm::vec3 velocity,
|
||||
glm::vec3 lookAt,
|
||||
glm::vec3 up
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ namespace audio {
|
|||
}
|
||||
|
||||
void setListener(
|
||||
glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
|
||||
worldvec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
|
||||
) override {
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -352,7 +352,7 @@ void audio::set_input_device(const std::string& deviceName) {
|
|||
}
|
||||
|
||||
void audio::set_listener(
|
||||
glm::vec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up
|
||||
worldvec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up
|
||||
) {
|
||||
backend->setListener(position, velocity, lookAt, up);
|
||||
}
|
||||
|
|
@ -380,7 +380,7 @@ void remove_lower_priority_speaker(int priority) {
|
|||
|
||||
speakerid_t audio::play(
|
||||
Sound* sound,
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
bool relative,
|
||||
float volume,
|
||||
float pitch,
|
||||
|
|
@ -419,7 +419,7 @@ speakerid_t audio::play(
|
|||
|
||||
speakerid_t audio::play(
|
||||
const std::shared_ptr<Stream>& stream,
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
bool relative,
|
||||
float volume,
|
||||
float pitch,
|
||||
|
|
@ -451,7 +451,7 @@ speakerid_t audio::play(
|
|||
|
||||
speakerid_t audio::play_stream(
|
||||
const io::path& file,
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
bool relative,
|
||||
float volume,
|
||||
float pitch,
|
||||
|
|
|
|||
|
|
@ -331,11 +331,11 @@ namespace audio {
|
|||
|
||||
/// @brief Set speaker 3D position in the world
|
||||
/// @param pos new position
|
||||
virtual void setPosition(glm::vec3 pos) = 0;
|
||||
virtual void setPosition(worldvec3 pos) = 0;
|
||||
|
||||
/// @brief Get speaker 3D position in the world
|
||||
/// @return position
|
||||
virtual glm::vec3 getPosition() const = 0;
|
||||
virtual worldvec3 getPosition() const = 0;
|
||||
|
||||
/// @brief Set speaker movement velocity used for Doppler effect
|
||||
/// @param vel velocity vector
|
||||
|
|
@ -391,7 +391,7 @@ namespace audio {
|
|||
uint bitsPerSample
|
||||
) = 0;
|
||||
virtual void setListener(
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
glm::vec3 velocity,
|
||||
glm::vec3 lookAt,
|
||||
glm::vec3 up
|
||||
|
|
@ -482,7 +482,7 @@ namespace audio {
|
|||
/// @param lookAt point the listener look at
|
||||
/// @param up camera up vector
|
||||
void set_listener(
|
||||
glm::vec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up
|
||||
worldvec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up
|
||||
);
|
||||
|
||||
/// @brief Play 3D sound in the world
|
||||
|
|
@ -498,7 +498,7 @@ namespace audio {
|
|||
/// @return speaker id or 0
|
||||
speakerid_t play(
|
||||
Sound* sound,
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
bool relative,
|
||||
float volume,
|
||||
float pitch,
|
||||
|
|
@ -518,7 +518,7 @@ namespace audio {
|
|||
/// @return speaker id or 0
|
||||
speakerid_t play(
|
||||
const std::shared_ptr<Stream>& stream,
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
bool relative,
|
||||
float volume,
|
||||
float pitch,
|
||||
|
|
@ -537,7 +537,7 @@ namespace audio {
|
|||
/// @return speaker id or 0
|
||||
speakerid_t play_stream(
|
||||
const io::path& file,
|
||||
glm::vec3 position,
|
||||
worldvec3 position,
|
||||
bool relative,
|
||||
float volume,
|
||||
float pitch,
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ LevelFrontend::LevelFrontend(
|
|||
}
|
||||
|
||||
auto sound = assets.get<audio::Sound>(material->stepsSound);
|
||||
glm::vec3 pos {};
|
||||
worldvec3 pos {};
|
||||
auto soundsCamera = currentPlayer.currentCamera.get();
|
||||
if (currentPlayer.isCurrentCameraBuiltin()) {
|
||||
soundsCamera = currentPlayer.fpCamera.get();
|
||||
|
|
@ -117,7 +117,7 @@ LevelFrontend::LevelFrontend(
|
|||
}
|
||||
audio::play(
|
||||
sound,
|
||||
glm::vec3(pos.x, pos.y, pos.z) + 0.5f,
|
||||
worldvec3(pos.x, pos.y, pos.z) + 0.5,
|
||||
false,
|
||||
1.0f,
|
||||
1.0f + (rand() % 6 - 3) * 0.05f,
|
||||
|
|
|
|||
|
|
@ -42,9 +42,7 @@ static audio::speakerid_t play_sound(
|
|||
}
|
||||
return audio::play(
|
||||
sound,
|
||||
glm::vec3(
|
||||
static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)
|
||||
),
|
||||
worldvec3(x, y, z),
|
||||
relative,
|
||||
volume,
|
||||
pitch,
|
||||
|
|
@ -75,11 +73,7 @@ static audio::speakerid_t play_stream(
|
|||
if (stream) {
|
||||
return audio::play(
|
||||
audio::open_stream(std::move(stream), true),
|
||||
glm::vec3(
|
||||
static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)
|
||||
),
|
||||
worldvec3(x, y, z),
|
||||
relative,
|
||||
volume,
|
||||
pitch,
|
||||
|
|
@ -96,15 +90,7 @@ static audio::speakerid_t play_stream(
|
|||
file = paths.find(filename);
|
||||
}
|
||||
return audio::play_stream(
|
||||
file,
|
||||
glm::vec3(
|
||||
static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)
|
||||
),
|
||||
relative,
|
||||
volume,
|
||||
pitch,
|
||||
loop,
|
||||
channel
|
||||
file, worldvec3(x, y, z), relative, volume, pitch, loop, channel
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -277,9 +263,7 @@ static int l_audio_set_position(lua::State* L) {
|
|||
auto x = lua::tonumber(L, 2);
|
||||
auto y = lua::tonumber(L, 3);
|
||||
auto z = lua::tonumber(L, 4);
|
||||
speaker->setPosition(glm::vec3(
|
||||
static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)
|
||||
));
|
||||
speaker->setPosition(worldvec3(x, y, z));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue