Merge remote-tracking branch 'origin/master' into MSAA

This commit is contained in:
Teejoon
2016-03-13 00:09:07 +01:00
28 changed files with 379 additions and 26567 deletions
+56 -26
View File
@@ -94,8 +94,30 @@ EntityWrapper EntityWrapper::Clone(EntityWrapper parent /*= Invalid*/)
return EntityWrapper::Invalid;
}
EntityWrapper clone = cloneRecursive(*this, EntityWrapper::Invalid);
this->World->SetParent(clone.ID, parent.ID);
// Create a relationship map of children of this entity
std::unordered_multimap<EntityWrapper, EntityWrapper> relationships;
fillRelationships(relationships, *this);
::World* targetWorld = this->World;
if (parent.Valid()) {
targetWorld = parent.World;
}
// Create root entity
EntityWrapper clone(targetWorld, targetWorld->CreateEntity(parent.ID));
// Copy name
clone.World->SetName(clone.ID, this->Name());
// Clone components
for (auto& kv : this->World->GetComponentPools()) {
if (kv.second->KnowsEntity(this->ID)) {
ComponentWrapper c1 = kv.second->GetByEntity(this->ID);
ComponentWrapper c2 = clone.World->AttachComponent(clone.ID, kv.first);
c1.Copy(c2);
}
}
// Recreate entity tree
recreateRelationships(relationships, *this, clone);
return clone;
}
@@ -207,30 +229,6 @@ EntityWrapper EntityWrapper::firstChildByNameRecursive(const std::string& name,
return EntityWrapper::Invalid;
}
EntityWrapper EntityWrapper::cloneRecursive(EntityWrapper entity, EntityWrapper parent)
{
EntityWrapper clone = EntityWrapper(entity.World, entity.World->CreateEntity(parent.ID));
entity.World->SetName(clone.ID, entity.Name());
// Clone components
for (auto& kv : entity.World->GetComponentPools()) {
if (kv.second->KnowsEntity(entity.ID)) {
ComponentWrapper c1 = kv.second->GetByEntity(entity.ID);
ComponentWrapper c2 = entity.World->AttachComponent(clone.ID, kv.first);
c1.Copy(c2);
}
}
// Clone children
auto children = entity.World->GetDirectChildren(entity.ID);
for (auto it = children.first; it != children.second; ++it) {
EntityWrapper child(entity.World, it->second);
cloneRecursive(child, clone);
}
return clone;
}
void EntityWrapper::childrenWithComponentRecursive(const std::string& componentType, EntityWrapper& entity, std::vector<EntityWrapper>& childrenWithComponent)
{
auto itPair = this->World->GetDirectChildren(entity.ID);
@@ -246,3 +244,35 @@ void EntityWrapper::childrenWithComponentRecursive(const std::string& componentT
childrenWithComponentRecursive(componentType, child, childrenWithComponent);
}
}
void EntityWrapper::fillRelationships(std::unordered_multimap<EntityWrapper, EntityWrapper>& relationMap, EntityWrapper entity)
{
auto children = entity.World->GetDirectChildren(entity.ID);
for (auto it = children.first; it != children.second; ++it) {
EntityWrapper child(entity.World, it->second);
relationMap.insert(std::make_pair(entity, child));
fillRelationships(relationMap, child);
}
}
void EntityWrapper::recreateRelationships(const std::unordered_multimap<EntityWrapper, EntityWrapper>& relationMap, EntityWrapper templateEntity, EntityWrapper parent /*= EntityWrapper::Invalid*/)
{
// Recursively create children
auto children = relationMap.equal_range(templateEntity);
for (auto it = children.first; it != children.second; ++it) {
EntityWrapper child = it->second;
// Create clone entity
EntityWrapper clone(parent.World, parent.World->CreateEntity(parent.ID));
// Copy name
clone.World->SetName(clone.ID, child.Name());
// Clone components
for (auto& kv : child.World->GetComponentPools()) {
if (kv.second->KnowsEntity(child.ID)) {
ComponentWrapper c1 = kv.second->GetByEntity(child.ID);
ComponentWrapper c2 = clone.World->AttachComponent(clone.ID, kv.first);
c1.Copy(c2);
}
}
recreateRelationships(relationMap, child, clone);
}
}
+8 -8
View File
@@ -6,18 +6,16 @@ CubeMapPass::CubeMapPass(IRenderer* renderer)
LoadTextures("Nevada");
}
void CubeMapPass::LoadTextures(std::string input)
void CubeMapPass::LoadTextures(std::string cubemapName)
{
if (m_PreviusCubeMapTexture != input) {
if (m_PreviusCubeMapTexture != cubemapName) {
m_CubeMapTextures.clear();
for (int i = 0; i < 6; i++) {
std::string str;
str = "Textures/Test/CubeMap/" + input + "/CubeMapTest0" + std::to_string(i) + ".png";
Texture* img = ResourceManager::Load<Texture>(str);
m_CubeMapTextures.push_back(img);
std::string path = "Textures/Test/CubeMap/" + cubemapName + "/CubeMapTest0" + std::to_string(i) + ".png";
m_CubeMapTextures.push_back(path);
}
GenerateCubeMapTexture();
m_PreviusCubeMapTexture = input;
m_PreviusCubeMapTexture = cubemapName;
}
}
@@ -29,7 +27,9 @@ void CubeMapPass::GenerateCubeMapTexture()
glBindTexture(GL_TEXTURE_CUBE_MAP, m_CubeMapTexture);
for (int i = 0; i < 6; i++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA32F, m_CubeMapTextures[0]->Width, m_CubeMapTextures[0]->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_CubeMapTextures[i]->Data);
PNG* img = ResourceManager::Load<PNG>(m_CubeMapTextures[i]);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA32F, img->Width, img->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->Data);
ResourceManager::Release("PNG", m_CubeMapTextures[i]);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+2 -1
View File
@@ -19,7 +19,6 @@ Texture::Texture(std::string path)
}
// Construct the OpenGL texture
glGenTextures(1, &m_Texture);
glBindTexture(GL_TEXTURE_2D, m_Texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -30,6 +29,8 @@ Texture::Texture(std::string path)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLERROR("Texture load");
ResourceManager::Release("PNG", path);
}
Texture::~Texture()
+128 -12
View File
@@ -7,6 +7,7 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker)
m_World = world;
m_BGMVolumeChannel = config->Get<float>("Sound.BGMVolume", 1.f);
m_SFXVolumeChannel = config->Get<float>("Sound.SFXVolume", 1.f);
m_AnnouncerVolumeChannel = config->Get<float>("Sound.AnnouncerVolume", 1.f);
initOpenAL();
alSpeedOfSound(340.29f);
@@ -16,16 +17,23 @@ SoundManager::SoundManager(World* world, EventBroker* eventBroker)
EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnEntity, &SoundManager::OnPlaySoundOnEntity);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySoundOnPosition, &SoundManager::OnPlaySoundOnPosition);
EVENT_SUBSCRIBE_MEMBER(m_EPlayBackgroundMusic, &SoundManager::OnPlayBackgroundMusic);
EVENT_SUBSCRIBE_MEMBER(m_EPlayAnnouncerVoice, &SoundManager::OnPlayAnnouncerVoice);
EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundManager::OnStopSound);
EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundManager::OnPauseSound);
EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundManager::OnContinueSound);
EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundManager::OnSetBGMGain);
EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundManager::OnSetSFXGain);
EVENT_SUBSCRIBE_MEMBER(m_ESetAnnouncerGain, &SoundManager::OnSetAnnouncerGain);
EVENT_SUBSCRIBE_MEMBER(m_EPause, &SoundManager::OnPause);
EVENT_SUBSCRIBE_MEMBER(m_EResume, &SoundManager::OnResume);
EVENT_SUBSCRIBE_MEMBER(m_EComponentAttached, &SoundManager::OnComponentAttached);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &SoundManager::OnPlayerSpawned);
EVENT_SUBSCRIBE_MEMBER(m_EPlayQueueOnEntity, &SoundManager::OnPlayQueueOnEntity);
EVENT_SUBSCRIBE_MEMBER(m_EChangeBGM, &SoundManager::OnChangeBGM);
Events::ChangeBGM e;
e.FilePath = "Audio/bgm/MenuMusic.wav";
m_EventBroker->Publish(e);
}
SoundManager::~SoundManager()
@@ -56,13 +64,11 @@ void SoundManager::stopEmitters()
void SoundManager::Update(double dt)
{
m_EventBroker->Process<SoundManager>();
deleteInactiveEmitters(); // can be optimized with "EEntityDeleted"
deleteInactiveEmitters();
updateEmitters(dt);
updateListener(dt);
// Editor debug info
ImGui::SliderFloat("BGM", &m_BGMVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f);
ImGui::SliderFloat("SFX", &m_SFXVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f);
if (m_DrumLoopHasBeenStarted)
matchBGMLoop();
}
void SoundManager::deleteInactiveEmitters()
@@ -117,7 +123,7 @@ void SoundManager::updateEmitters(double dt)
// Calculate velocity
glm::vec3 velocity = glm::vec3(nextPos - previousPos) / (float)dt;
setSourcePos(it->second->ALsource, nextPos);
setSourceVel(it->second->ALsource, velocity);
//setSourceVel(it->second->ALsource, glm::vec3(0));
auto emitter = m_World->GetComponent(it->first, "SoundEmitter");
setSoundProperties(it->second, &emitter);
@@ -166,9 +172,34 @@ Source* SoundManager::createSource(std::string filePath)
Source* source = new Source();
source->ALsource = alSource;
source->SoundResource = ResourceManager::Load<Sound>(filePath);
source->Duration = getDurationSeconds(source);
return source;
}
void SoundManager::matchBGMLoop()
{
if (m_CurrentBGMCombo == nullptr)
return;
auto cCapturePoints = m_World->GetComponents("CapturePoint");
for (auto it = cCapturePoints->begin(); it != cCapturePoints->end(); it++) {
float timeCaptured = (float)(double)(*it)["CaptureTimer"];
float maxTimer = (float)(double)(*it)["CapturePointMaxTimer"];
int capturePointIndex = (int)(*it)["CapturePointNumber"];
if (capturePointIndex == 0) { // Home for red team
if (timeCaptured < 0 && glm::abs(timeCaptured) < maxTimer) {
float gain = glm::abs(timeCaptured) / maxTimer;
setGain(m_CurrentBGMCombo, gain);
}
} else if (capturePointIndex == 4) { // Home for blue team
if (timeCaptured > 0 && timeCaptured < maxTimer) {
float gain = timeCaptured / maxTimer;
setGain(m_CurrentBGMCombo, gain);
}
}
}
}
void SoundManager::playSound(Source* source)
{
alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer());
@@ -192,10 +223,12 @@ bool SoundManager::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e)
{
Source* source = createSource(e.FilePath);
source->Type = SoundType::SFX;
EntityID child = m_World->CreateEntity(e.EmitterID);
EntityID child = m_World->CreateEntity(e.Emitter.ID);
m_World->AttachComponent(child, "Transform");
m_World->AttachComponent(child, "SoundEmitter");
auto cEmitter = m_World->AttachComponent(child, "SoundEmitter");
(double&)(float)cEmitter["Gain"] = e.Gain;
m_Sources[child] = source;
setGain(source, cEmitter["Gain"]);
playSound(source);
return false;
}
@@ -242,7 +275,7 @@ bool SoundManager::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e)
auto listenerComponents = m_World->GetComponents("Listener");
for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) {
if ((*it).EntityID != m_LocalPlayer.ID) {
break;
continue;
}
auto emitterChild = m_World->CreateEntity((*it).EntityID);
auto emitter = m_World->AttachComponent(emitterChild, "SoundEmitter");
@@ -258,6 +291,28 @@ bool SoundManager::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e)
return true;
}
bool SoundManager::OnPlayAnnouncerVoice(const Events::PlayAnonuncerVoice& e)
{
auto listenerComponents = m_World->GetComponents("Listener");
for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) {
if ((*it).EntityID != m_LocalPlayer.ID) {
continue;
}
auto emitterChild = m_World->CreateEntity((*it).EntityID);
auto emitter = m_World->AttachComponent(emitterChild, "SoundEmitter");
(bool&)emitter["Loop"] = false;
(std::string&)emitter["FilePath"] = e.FilePath;
m_World->AttachComponent(emitterChild, "Transform");
Source* source = createSource(e.FilePath);
source->Type = SoundType::Announcer;
setSoundProperties(source, &emitter);
m_Sources[emitterChild] = source;
playSound(source);
}
return true;
}
bool SoundManager::OnSetBGMGain(const Events::SetBGMGain & e)
{
m_BGMVolumeChannel = e.Gain;
@@ -270,6 +325,13 @@ bool SoundManager::OnSetSFXGain(const Events::SetSFXGain & e)
return true;
}
bool SoundManager::OnSetAnnouncerGain(const Events::SetAnnouncerGain& e)
{
m_AnnouncerVolumeChannel = e.Gain;
return true;
}
bool SoundManager::OnComponentAttached(const Events::ComponentAttached & e)
{
if (e.Component.Info.Name == "SoundEmitter") {
@@ -301,12 +363,20 @@ bool SoundManager::OnPlayerSpawned(const Events::PlayerSpawned &e)
{
if (e.PlayerID == -1) { // Local player
m_LocalPlayer = e.Player;
if (m_DrumLoopHasBeenStarted) {
return true;
}
m_CurrentBGMCombo = createSource("Audio/BGM/Layer2.wav");
m_CurrentBGMCombo->Type = SoundType::BGM;
alSourcei(m_CurrentBGMCombo->ALsource, AL_LOOPING, 1);
setGain(m_CurrentBGMCombo, 0);
playSound(m_CurrentBGMCombo);
m_DrumLoopHasBeenStarted = true;
return true;
}
return false;
}
bool SoundManager::OnPlayQueueOnEntity(const Events::PlayQueueOnEntity &e)
{
Source* source = createSource(*e.FilePaths.begin());
@@ -321,6 +391,18 @@ bool SoundManager::OnPlayQueueOnEntity(const Events::PlayQueueOnEntity &e)
return true;
}
bool SoundManager::OnChangeBGM(const Events::ChangeBGM &e)
{
if (m_CurrentBGM != nullptr) {
stopSound(m_CurrentBGM);
}
m_CurrentBGM = createSource(e.FilePath);
m_CurrentBGM->Type = SoundType::BGM;
alSourcei(m_CurrentBGM->ALsource, AL_LOOPING, 1);
playSound(m_CurrentBGM);
return true;
}
ALenum SoundManager::getSourceState(ALuint source)
{
ALenum state;
@@ -335,15 +417,49 @@ void SoundManager::setGain(Source * source, float gain)
void SoundManager::setSoundProperties(Source* source, ComponentWrapper* soundComponent)
{
float gain = (source->Type == SoundType::SFX) ? m_SFXVolumeChannel : m_BGMVolumeChannel;
float gain;
switch (source->Type) {
case SoundType::SFX:
gain = m_SFXVolumeChannel;
break;
case SoundType::BGM:
gain = m_BGMVolumeChannel;
break;
case SoundType::Announcer:
gain = m_AnnouncerVolumeChannel;
break;
default:
gain = 1.f;
break;
}
alSourcef(source->ALsource, AL_GAIN, (float)(double)(*soundComponent)["Gain"] * gain);
alSourcef(source->ALsource, AL_PITCH, (float)(double)(*soundComponent)["Pitch"]);
alSourcei(source->ALsource, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]); // YOLO
alSourcei(source->ALsource, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]);
alSourcef(source->ALsource, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]);
alSourcef(source->ALsource, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]);
alSourcef(source->ALsource, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]);
}
float SoundManager::getDurationSeconds(Source* source)
{
ALuint buffer = source->SoundResource->Buffer();
ALint sizeBytes, channels, bits, frequenzy;
alGetBufferi(buffer, AL_SIZE, &sizeBytes);
alGetBufferi(buffer, AL_CHANNELS, &channels);
alGetBufferi(buffer, AL_BITS, &bits);
alGetBufferi(buffer, AL_FREQUENCY, &frequenzy);
float sampleLength = (float)sizeBytes * 8 / (channels * bits);
return (sampleLength / frequenzy);
}
float SoundManager::getTimeOffsetSeconds(Source* source)
{
float time;
alGetSourcef(source->ALsource, AL_SEC_OFFSET, &time);
return time;
}
void SoundManager::initOpenAL()
{
// Initialize OpenAL