From 8bed5194d81827e3d4d292d9da886d0e2148025c Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 27 Jan 2016 11:36:24 +0100 Subject: [PATCH] "Proper" handling of conditional rendering of entities parented to camera or entities that should be hidden from the active local player perspective --- include/Engine/Rendering/RenderSystem.h | 12 +++--- resources/Schema/Components.xsd | 1 + .../Components/HiddenForLocalPlayer.xml | 2 + .../Components/HiddenForLocalPlayer.xsd | 11 ++++++ resources/Schema/Components/Lifetime.xml | 4 +- resources/Schema/Components/Model.xsd | 2 +- resources/Schema/Entities/Player.xml | 5 ++- resources/Schema/Types/Entity.xsd | 3 ++ src/Engine/Core/EntityFilePreprocessor.cpp | 3 +- src/Engine/Core/World.cpp | 8 +++- src/Engine/Rendering/RenderSystem.cpp | 37 ++++++++++++------- 11 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 resources/Schema/Components/HiddenForLocalPlayer.xml create mode 100644 resources/Schema/Components/HiddenForLocalPlayer.xsd diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index 37f99341..d9b0d779 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -35,17 +35,19 @@ private: EventRelay m_ESetCamera; bool OnSetCamera(Events::SetCamera &event); + EventRelay m_EInputCommand; + bool OnInputCommand(const Events::InputCommand& e); + EventRelay m_EPlayerSpawned; + bool OnPlayerSpawned(Events::PlayerSpawned& e); + void fillText(std::list>& jobs, World* world); void fillPointLights(std::list>& jobs, World* world); void fillDirectionalLights(std::list>& jobs, World* world); - EventRelay m_EInputCommand; - bool OnInputCommand(const Events::InputCommand& e); - void fillModels(std::list>& jobs); void fillLight(std::list>& jobs); + bool isChildOfACamera(EntityWrapper entity); + bool isChildOfCurrentCamera(EntityWrapper entity); - EventRelay m_EPlayerSpawned; - bool OnPlayerSpawned(Events::PlayerSpawned& e); }; #endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 5ce7fa5f..e9c6a813 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -27,4 +27,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/HiddenForLocalPlayer.xml b/resources/Schema/Components/HiddenForLocalPlayer.xml new file mode 100644 index 00000000..5840385c --- /dev/null +++ b/resources/Schema/Components/HiddenForLocalPlayer.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/resources/Schema/Components/HiddenForLocalPlayer.xsd b/resources/Schema/Components/HiddenForLocalPlayer.xsd new file mode 100644 index 00000000..7f47fba5 --- /dev/null +++ b/resources/Schema/Components/HiddenForLocalPlayer.xsd @@ -0,0 +1,11 @@ + + + + + + + + Used to make the entity invisible if it's parented to the current local player entity (for first person player model and such) + + + \ No newline at end of file diff --git a/resources/Schema/Components/Lifetime.xml b/resources/Schema/Components/Lifetime.xml index d7e230d7..302429b1 100644 --- a/resources/Schema/Components/Lifetime.xml +++ b/resources/Schema/Components/Lifetime.xml @@ -1,4 +1,4 @@ - + 0 - \ No newline at end of file + \ No newline at end of file diff --git a/resources/Schema/Components/Model.xsd b/resources/Schema/Components/Model.xsd index fb0774f8..540ab1bf 100644 --- a/resources/Schema/Components/Model.xsd +++ b/resources/Schema/Components/Model.xsd @@ -16,7 +16,7 @@ Color tint - Wether the model is visible or not + Whether the model is visible or not diff --git a/resources/Schema/Entities/Player.xml b/resources/Schema/Entities/Player.xml index 7914f82b..e81ec5aa 100644 --- a/resources/Schema/Entities/Player.xml +++ b/resources/Schema/Entities/Player.xml @@ -20,7 +20,7 @@ - + @@ -141,9 +141,10 @@ Hold Pos - + 1 + Models/AssaultAnimated.mesh diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 5d135847..25aad787 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -33,6 +33,9 @@ + + + diff --git a/src/Engine/Core/EntityFilePreprocessor.cpp b/src/Engine/Core/EntityFilePreprocessor.cpp index 7b4b2bb7..2a1d22d6 100644 --- a/src/Engine/Core/EntityFilePreprocessor.cpp +++ b/src/Engine/Core/EntityFilePreprocessor.cpp @@ -65,6 +65,7 @@ void EntityFilePreprocessor::parseComponentInfo() // Name compInfo.Name = XS::ToString(element->getName()); + bool brk = compInfo.Name == "HiddenForLocalPlayer"; // Known allocation compInfo.Meta->Allocation = m_ComponentCounts[compInfo.Name]; // Annotation @@ -90,7 +91,7 @@ void EntityFilePreprocessor::parseComponentInfo() // auto modelGroupParticle = complexTypeDefinition->getParticle(); if (modelGroupParticle == nullptr || modelGroupParticle->getTermType() != XSParticle::TERM_MODELGROUP) { - //LOG_ERROR("Failed to parse component definition for \"%s\": Model group particle was null or wasn't TERM_MODELGROUP!", compInfo.Name.c_str()); + LOG_ERROR("Failed to parse component definition for \"%s\": Model group particle was null or wasn't TERM_MODELGROUP!", compInfo.Name.c_str()); continue; } auto modelGroup = modelGroupParticle->getModelGroupTerm(); diff --git a/src/Engine/Core/World.cpp b/src/Engine/Core/World.cpp index 0c4c9ce4..8b212d31 100644 --- a/src/Engine/Core/World.cpp +++ b/src/Engine/Core/World.cpp @@ -54,8 +54,12 @@ ComponentWrapper World::AttachComponent(EntityID entity, const std::string& comp bool World::HasComponent(EntityID entity, const std::string& componentType) const { - ComponentPool* pool = m_ComponentPools.at(componentType); - return pool->KnowsEntity(entity); + auto it = m_ComponentPools.find(componentType); + if (it == m_ComponentPools.end()) { + return false; + } else { + return it->second->KnowsEntity(entity); + } } ComponentWrapper World::GetComponent(EntityID entity, const std::string& componentType) diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index a67f2c75..e2aba63e 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -31,6 +31,16 @@ bool RenderSystem::OnSetCamera(Events::SetCamera& e) return true; } +bool RenderSystem::isChildOfACamera(EntityWrapper entity) +{ + return entity.FirstParentWithComponent("Camera").Valid(); +} + +bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity) +{ + return entity == m_CurrentCamera || entity.IsChildOf(m_CurrentCamera); +} + void RenderSystem::fillModels(std::list>& jobs) { auto models = m_World->GetComponents("Model"); @@ -38,26 +48,25 @@ void RenderSystem::fillModels(std::list>& jobs) return; } - for (auto& modelComponent : *models) { - bool visible = modelComponent["Visible"]; + for (auto& cModel : *models) { + bool visible = cModel["Visible"]; if (!visible) { continue; } - std::string resource = modelComponent["Resource"]; + std::string resource = cModel["Resource"]; if (resource.empty()) { continue; } - EntityWrapper entity(m_World, modelComponent.EntityID); + EntityWrapper entity(m_World, cModel.EntityID); - // Don't render the local player - if (entity == m_LocalPlayer || entity.IsChildOf(m_LocalPlayer)) { - if (!entity.HasComponent("HealthHUD") && entity.Name() != "Crosshair" && entity.Name() != "Weapon") { //Should work but needs to be fixed. Should only render the things "childed" to the local player camera - continue; - } + // Only render children of a camera if that camera is currently active + if (isChildOfACamera(entity) && !isChildOfCurrentCamera(entity)) { + continue; } - if ((entity.Name() == "Weapon" || entity.HasComponent("HealthHUD")) && !entity.IsChildOf(m_LocalPlayer)) { + // Hide things parented to local player if they have the HiddenFromLocalPlayer component + if (entity.HasComponent("HiddenForLocalPlayer") && (entity == m_LocalPlayer || entity.IsChildOf(m_LocalPlayer))) { continue; } @@ -79,17 +88,17 @@ void RenderSystem::fillModels(std::list>& jobs) float fillPercentage = 0.f; glm::vec4 fillColor = glm::vec4(0); - if(m_World->HasComponent(modelComponent.EntityID, "Fill")) { - auto fillComponent = m_World->GetComponent(modelComponent.EntityID, "Fill"); + if(m_World->HasComponent(cModel.EntityID, "Fill")) { + auto fillComponent = m_World->GetComponent(cModel.EntityID, "Fill"); fillPercentage = (float)(double)fillComponent["Percentage"]; fillColor = (glm::vec4)fillComponent["Color"]; } - glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, m_World); + glm::mat4 modelMatrix = Transform::ModelMatrix(cModel.EntityID, m_World); for (auto matGroup : model->MaterialGroups()) { - std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, m_World, fillColor, fillPercentage)); + std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, cModel, m_World, fillColor, fillPercentage)); jobs.push_back(modelJob); } }