Using absolute transform
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Rendering/RenderQueueFactory.h" // Absolute transform
|
||||
#include "Sound/Sound.h"
|
||||
#include "Sound/EPlaySound.h"
|
||||
|
||||
@@ -27,35 +28,27 @@ public:
|
||||
~SoundSystem();
|
||||
void Update(); // Update emitters
|
||||
private:
|
||||
// Private setters and getters for working with glm
|
||||
// Help functions for working with OpenaAL
|
||||
void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); };
|
||||
glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; };
|
||||
void setListenerVel(glm::vec3 vel) { alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z); };
|
||||
glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; };
|
||||
void setListenerOri(glm::vec3 ori)
|
||||
{
|
||||
glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0);
|
||||
forward = glm::rotateX(forward, ori.x);
|
||||
forward = glm::rotateY(forward, ori.y);
|
||||
forward = glm::rotateZ(forward, ori.z);
|
||||
glm::normalize(forward);
|
||||
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0);
|
||||
up = glm::rotateX(up, ori.x);
|
||||
up = glm::rotateY(up, ori.y);
|
||||
up = glm::rotateZ(up, ori.z);
|
||||
glm::normalize(up);
|
||||
ALfloat lori[6] = { forward.x, forward.y, forward.z , up.x, up.y, up.z };
|
||||
alListenerfv(AL_ORIENTATION, lori);
|
||||
};
|
||||
void setListenerOri(glm::vec3 ori);
|
||||
glm::vec3 listenerOri() { glm::vec3 ori; alGetListener3f(AL_ORIENTATION, &ori.x, &ori.y, &ori.z); return ori; };
|
||||
void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); };
|
||||
void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); };
|
||||
void setSourceOri(ALuint source, glm::vec3 ori) { ALfloat sori[3] = { ori.x, ori.y, ori.z }; alSourcefv(source, AL_ORIENTATION, sori); };
|
||||
|
||||
bool isPlaying(ALuint source);
|
||||
|
||||
// Logic
|
||||
World* m_World;
|
||||
EventBroker* m_EventBroker;
|
||||
|
||||
void initOpenAL();
|
||||
void updateEmitters();
|
||||
void updateListener();
|
||||
void deleteInactiveEmitters();
|
||||
void addNewEmitters();
|
||||
ALuint createSource();
|
||||
void playSound(Source source);
|
||||
void stopSound(Source source);
|
||||
@@ -71,7 +64,6 @@ private:
|
||||
// Events
|
||||
EventRelay<SoundSystem, Events::PlaySound> m_EPlaySound;
|
||||
bool OnPlaySound(const Events::PlaySound &e);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,5 @@
|
||||
<c:SoundEmitter>
|
||||
<FilePath>Audio/crosscounter.wav</FilePath>
|
||||
<Gain>1.0</Gain>
|
||||
<Pitch>1.0</Pitch>
|
||||
<MaxDistance>20.0</MaxDistance>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
<xs:element name="SoundEmitter">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="FilePath" type="t:string" minOccurs="0"/>
|
||||
<xs:element name="Gain" type="t:float" minOccurs="0"/>
|
||||
<xs:annotation><xs:documentation>The "volume" of the emitter. A value betweeen 0-1</xs:documentation></xs:annotation>
|
||||
<xs:element name="Pitch" type="t:float" minOccurs="0"/>
|
||||
|
||||
@@ -4,6 +4,17 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker)
|
||||
{
|
||||
m_EventBroker = eventBroker;
|
||||
m_World = world;
|
||||
|
||||
initOpenAL();
|
||||
|
||||
alSpeedOfSound(340.29f); // Speed of sound
|
||||
alDistanceModel(AL_INVERSE_DISTANCE);
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound);
|
||||
}
|
||||
|
||||
void SoundSystem::initOpenAL()
|
||||
{
|
||||
// Initialize OpenAL
|
||||
m_ALCdevice = alcOpenDevice(nullptr);
|
||||
if (m_ALCdevice != nullptr) {
|
||||
@@ -12,11 +23,6 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker)
|
||||
} else {
|
||||
LOG_ERROR("OpenAL failed to initialize.");
|
||||
}
|
||||
|
||||
alSpeedOfSound(340.29f); // Speed of sound
|
||||
alDistanceModel(AL_INVERSE_DISTANCE);
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound);
|
||||
}
|
||||
|
||||
SoundSystem::~SoundSystem()
|
||||
@@ -28,14 +34,67 @@ SoundSystem::~SoundSystem()
|
||||
}
|
||||
|
||||
void SoundSystem::Update()
|
||||
{
|
||||
//deleteInactiveEmitters(); // Not tested
|
||||
addNewEmitters();
|
||||
updateEmitters();
|
||||
updateListener();
|
||||
}
|
||||
|
||||
void SoundSystem::deleteInactiveEmitters()
|
||||
{
|
||||
auto emitterComponents = m_World->GetComponents("SoundEmitter");
|
||||
for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) {
|
||||
EntityID emitter = (*it).EntityID;
|
||||
if (isPlaying(m_Sources[emitter].ALsource)) { // The sound is still playing, do not remove
|
||||
continue;
|
||||
}
|
||||
alDeleteBuffers(1, &m_Sources[emitter].ALsource);
|
||||
delete m_Sources[emitter].SoundResource;
|
||||
m_Sources.erase(emitter);
|
||||
}
|
||||
}
|
||||
|
||||
void SoundSystem::addNewEmitters()
|
||||
{
|
||||
auto emitterComponents = m_World->GetComponents("SoundEmitter");
|
||||
for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) {
|
||||
EntityID emitter = (*it).EntityID;
|
||||
std::unordered_map<EntityID, Source>::iterator i;
|
||||
i = m_Sources.find(emitter);
|
||||
if (i == m_Sources.end()) { // Did not exist, add it
|
||||
Source source;
|
||||
source.ALsource = createSource();
|
||||
source.SoundResource = ResourceManager::Load<Sound>((std::string)(*it)["FilePath"]);
|
||||
m_Sources[emitter] = source;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SoundSystem::updateEmitters()
|
||||
{
|
||||
auto emitterComponents = m_World->GetComponents("SoundEmitter");
|
||||
for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) {
|
||||
EntityID emitter = (*it).EntityID;
|
||||
std::unordered_map<EntityID, Source>::iterator i;
|
||||
i = m_Sources.find(emitter);
|
||||
if (i != m_Sources.end()) {
|
||||
setSourcePos(m_Sources[emitter].ALsource, RenderQueueFactory::AbsolutePosition(m_World, emitter));
|
||||
}
|
||||
// No orientation, emitts in all directions
|
||||
// Velocity for doppler effect
|
||||
}
|
||||
}
|
||||
|
||||
void SoundSystem::updateListener()
|
||||
{
|
||||
// Should only be one listener.
|
||||
auto listenerComponents = m_World->GetComponents("Listener");
|
||||
for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) {
|
||||
EntityID listener = (*it).EntityID;
|
||||
auto transform = m_World->GetComponent(listener, "Transform");
|
||||
setListenerPos(transform["Position"]);
|
||||
setListenerOri(transform["Orientation"]);
|
||||
setListenerPos(RenderQueueFactory::AbsolutePosition(m_World, listener));
|
||||
setListenerOri(glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, listener)));
|
||||
// Velocity for doppler effect
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,15 +121,38 @@ void SoundSystem::stopEmitter(EntityID emitter)
|
||||
|
||||
bool SoundSystem::OnPlaySound(const Events::PlaySound & e)
|
||||
{
|
||||
Sound *sound = ResourceManager::Load<Sound>(e.FilePath);
|
||||
if (sound == nullptr) {
|
||||
return false;
|
||||
}
|
||||
ALuint source = createSource();
|
||||
Source sauce;
|
||||
sauce.ALsource = source;
|
||||
sauce.SoundResource = sound;
|
||||
playSound(sauce);
|
||||
//Sound *sound = ResourceManager::Load<Sound>(e.FilePath);
|
||||
//if (sound == nullptr) {
|
||||
// return false;
|
||||
//}
|
||||
//ALuint source = createSource();
|
||||
//Source sauce;
|
||||
//sauce.ALsource = source;
|
||||
//sauce.SoundResource = sound;
|
||||
//playSound(sauce);
|
||||
return false;
|
||||
}
|
||||
|
||||
void SoundSystem::setListenerOri(glm::vec3 ori)
|
||||
{
|
||||
glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0);
|
||||
forward = glm::rotateX(forward, ori.x);
|
||||
forward = glm::rotateY(forward, ori.y);
|
||||
forward = glm::rotateZ(forward, ori.z);
|
||||
glm::normalize(forward);
|
||||
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0);
|
||||
up = glm::rotateX(up, ori.x);
|
||||
up = glm::rotateY(up, ori.y);
|
||||
up = glm::rotateZ(up, ori.z);
|
||||
glm::normalize(up);
|
||||
ALfloat lOri[6] = { forward.x, forward.y, forward.z, up.x, up.y, up.z };
|
||||
alListenerfv(AL_ORIENTATION, lOri);
|
||||
}
|
||||
|
||||
bool SoundSystem::isPlaying(ALuint source)
|
||||
{
|
||||
ALenum state;
|
||||
alGetSourcei(source, AL_SOURCE_STATE, &state);
|
||||
return (state == AL_PLAYING);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user