In lack of camera entity I created a Listener cube to test 3D sound. Works fine. Updates listener pos each tick.
This commit is contained in:
@@ -22,9 +22,9 @@ class SoundSystem
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SoundSystem() { }
|
SoundSystem() { }
|
||||||
SoundSystem(EventBroker* eventBroker);
|
SoundSystem(World* world, EventBroker* eventBroker);
|
||||||
~SoundSystem();
|
~SoundSystem();
|
||||||
void Update() { } // Update emitters
|
void Update(); // Update emitters
|
||||||
private:
|
private:
|
||||||
// Private setters and getters for working with glm
|
// Private setters and getters for working with glm
|
||||||
void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); };
|
void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); };
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
#include "Sound/SoundSystem.h"
|
#include "Sound/SoundSystem.h"
|
||||||
|
|
||||||
SoundSystem::SoundSystem(EventBroker* eventBroker)
|
SoundSystem::SoundSystem(World* world, EventBroker* eventBroker)
|
||||||
{
|
{
|
||||||
m_EventBroker = eventBroker;
|
m_EventBroker = eventBroker;
|
||||||
|
m_World = world;
|
||||||
// Initialize OpenAL
|
// Initialize OpenAL
|
||||||
m_ALCdevice = alcOpenDevice(nullptr);
|
m_ALCdevice = alcOpenDevice(nullptr);
|
||||||
if (m_ALCdevice != nullptr) {
|
if (m_ALCdevice != nullptr) {
|
||||||
@@ -16,8 +17,6 @@ SoundSystem::SoundSystem(EventBroker* eventBroker)
|
|||||||
alDistanceModel(AL_INVERSE_DISTANCE);
|
alDistanceModel(AL_INVERSE_DISTANCE);
|
||||||
|
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound);
|
EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound);
|
||||||
//m_EPlaySound = decltype(m_EPlaySound)(std::bind(&SoundSystem::OnPlaySound, this, std::placeholders::_1));
|
|
||||||
//m_EventBroker->Subscribe(m_EPlaySound);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SoundSystem::~SoundSystem()
|
SoundSystem::~SoundSystem()
|
||||||
@@ -28,6 +27,17 @@ SoundSystem::~SoundSystem()
|
|||||||
delete m_ALCdevice;
|
delete m_ALCdevice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SoundSystem::Update()
|
||||||
|
{
|
||||||
|
// 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"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ALuint SoundSystem::createSource()
|
ALuint SoundSystem::createSource()
|
||||||
{
|
{
|
||||||
ALuint source;
|
ALuint source;
|
||||||
@@ -60,7 +70,6 @@ bool SoundSystem::OnPlaySound(const Events::PlaySound & e)
|
|||||||
sauce.ALsource = source;
|
sauce.ALsource = source;
|
||||||
sauce.SoundResource = sound;
|
sauce.SoundResource = sound;
|
||||||
playSound(sauce);
|
playSound(sauce);
|
||||||
LOG_INFO("You are playing an imaginary sound now! :D");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-1
@@ -65,7 +65,14 @@ Game::Game(int argc, char* argv[])
|
|||||||
networkFunction();
|
networkFunction();
|
||||||
}
|
}
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand);
|
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand);
|
||||||
m_SoundSystem = new SoundSystem(m_EventBroker);
|
m_SoundSystem = new SoundSystem(m_World, m_EventBroker);
|
||||||
|
|
||||||
|
auto soundListenerCube = m_World->CreateEntity();
|
||||||
|
m_World->AttachComponent(soundListenerCube, "Listener");
|
||||||
|
m_World->AttachComponent(soundListenerCube, "Transform");
|
||||||
|
auto model = m_World->AttachComponent(soundListenerCube, "Model");
|
||||||
|
model["Resource"] = "Models/Core/UnitCube.obj";
|
||||||
|
model["Color"] = glm::vec4(1, 0, 0, 1);
|
||||||
|
|
||||||
m_LastTime = glfwGetTime();
|
m_LastTime = glfwGetTime();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user