From 680a12d88fc1f81dcc5f5b184f468d40716062b7 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Sat, 23 Jan 2016 14:53:17 +0100 Subject: [PATCH 1/3] HealthMeter --- assets | 2 +- include/Engine/Rendering/ModelJob.h | 9 +- include/Game/Systems/PlayerHUD.h | 23 +++ resources/Schema/Components.xsd | 2 + resources/Schema/Components/Fill.xml | 5 + resources/Schema/Components/Fill.xsd | 14 ++ resources/Schema/Components/HealthHUD.xml | 2 + resources/Schema/Components/HealthHUD.xsd | 8 + resources/Schema/Entities/RenderingWorld.xml | 163 +++++++++++++------ resources/Shaders/ForwardPlus.frag.glsl | 9 + src/Engine/Editor/EditorRenderSystem.cpp | 2 +- src/Engine/Rendering/DrawFinalPass.cpp | 4 + src/Engine/Rendering/RenderSystem.cpp | 18 +- src/Game/Game.cpp | 3 + src/Game/Systems/PlayerHUD.cpp | 79 +++++++++ 15 files changed, 286 insertions(+), 57 deletions(-) create mode 100644 include/Game/Systems/PlayerHUD.h create mode 100644 resources/Schema/Components/Fill.xml create mode 100644 resources/Schema/Components/Fill.xsd create mode 100644 resources/Schema/Components/HealthHUD.xml create mode 100644 resources/Schema/Components/HealthHUD.xsd create mode 100644 src/Game/Systems/PlayerHUD.cpp diff --git a/assets b/assets index b6592dbb..e8174f63 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit b6592dbb0216bbac00ccea43a0afd0e27d0b185e +Subproject commit e8174f630fc3242e15ada1346b42c72f44cbc854 diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index a88530e7..6b8bd55b 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -16,7 +16,7 @@ struct ModelJob : RenderJob { - ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialGroup matGroup, ComponentWrapper modelComponent, World* world) + ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialGroup matGroup, ComponentWrapper modelComponent, World* world, glm::vec4 fillColor, float fillPercentage) : RenderJob() { Model = model; @@ -33,6 +33,10 @@ struct ModelJob : RenderJob glm::vec3 worldpos = glm::vec3(camera->ViewMatrix() * glm::vec4(abspos, 1)); Depth = worldpos.z; World = world; + + + FillColor = fillColor; + FillPercentage = fillPercentage; }; unsigned int TextureID; @@ -50,6 +54,9 @@ struct ModelJob : RenderJob unsigned int EndIndex = 0; World* World; + glm::vec4 FillColor = glm::vec4(0); + float FillPercentage = 0.0; + void CalculateHash() override { Hash = TextureID; diff --git a/include/Game/Systems/PlayerHUD.h b/include/Game/Systems/PlayerHUD.h new file mode 100644 index 00000000..180f5a2a --- /dev/null +++ b/include/Game/Systems/PlayerHUD.h @@ -0,0 +1,23 @@ +#ifndef PlayerHUD_h__ +#define PlayerHUD_h__ + +#include "../../Engine/Core/System.h" +#include "../../Engine/GLM.h" +#include "../../Engine/Rendering/ESetCamera.h" +#include + +class PlayerHUD : public ImpureSystem +{ +public: + PlayerHUD(World* world, EventBroker* eventBrokerer); + ~PlayerHUD(); + + virtual void Update(double dt) override; + +private: + World* m_World; + EventBroker* m_EventBroker; + +}; + +#endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 12d94611..96f93eab 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -23,4 +23,6 @@ + + \ No newline at end of file diff --git a/resources/Schema/Components/Fill.xml b/resources/Schema/Components/Fill.xml new file mode 100644 index 00000000..397f8e49 --- /dev/null +++ b/resources/Schema/Components/Fill.xml @@ -0,0 +1,5 @@ + + + 0.0 + + \ No newline at end of file diff --git a/resources/Schema/Components/Fill.xsd b/resources/Schema/Components/Fill.xsd new file mode 100644 index 00000000..2bf6548f --- /dev/null +++ b/resources/Schema/Components/Fill.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Components/HealthHUD.xml b/resources/Schema/Components/HealthHUD.xml new file mode 100644 index 00000000..d7e20f92 --- /dev/null +++ b/resources/Schema/Components/HealthHUD.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/resources/Schema/Components/HealthHUD.xsd b/resources/Schema/Components/HealthHUD.xsd new file mode 100644 index 00000000..821a65a2 --- /dev/null +++ b/resources/Schema/Components/HealthHUD.xsd @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/RenderingWorld.xml b/resources/Schema/Entities/RenderingWorld.xml index 69b18982..7ecd25c9 100644 --- a/resources/Schema/Entities/RenderingWorld.xml +++ b/resources/Schema/Entities/RenderingWorld.xml @@ -6,34 +6,127 @@ - + - - ActionCamera - - - Models/Camera.obj - + + 69.990005493164063 + + - - + - + - - Camera #2 - Fonts/DroidSans.ttf,64 - 0 - + + Models/Assault.obj + - - + + + + + 80 + + + Models/Camera.obj + + + + + + + + + + + + + + + + + + + 0.69990003108978271 + + + + Models/Core/UnitHexagon.obj + + + + + + + + + + + + + Models/Core/UnitHexagon.obj + + + + + + + + + + + + Models/SecondaryWeapon.fbx + + + + + + + + + + + + + + + Models/Core/UnitHexagon.obj + + + + + + + + + + + + Models/SecondaryWeapon.fbx + + + + + + + + + + + + + + + + + @@ -42,7 +135,7 @@ Models/Core/UnitPlane.obj - + @@ -79,7 +172,7 @@ - + @@ -198,38 +291,6 @@ - - - - MainCamera - - - - Models/Camera.obj - false - - - - - - - - - - - Taiwan #1 - Fonts/DroidSans.ttf,64 - 0 - - - - - - - - - - diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 78a2125e..8b1ba686 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -6,6 +6,8 @@ uniform mat4 P; uniform vec4 Color; uniform vec2 ScreenDimensions; uniform sampler2D texture0; +uniform vec4 FillColor; +uniform float FillPercentage; #define TILE_SIZE 16 @@ -133,6 +135,13 @@ void main() //fragmentColor += Input.DiffuseColor; fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; + + float pos = ((P * vec4(Input.Position, 1)).y + 1.0)/2.0; + + if(pos <= FillPercentage) { + fragmentColor += FillColor; + } + //fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse) * texel * Color; //fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3, 0, 1); //fragmentColor = texel * Input.DiffuseColor * Color; diff --git a/src/Engine/Editor/EditorRenderSystem.cpp b/src/Engine/Editor/EditorRenderSystem.cpp index 2e31e3f0..9e9b5206 100644 --- a/src/Engine/Editor/EditorRenderSystem.cpp +++ b/src/Engine/Editor/EditorRenderSystem.cpp @@ -48,7 +48,7 @@ void EditorRenderSystem::Update(double dt) EntityWrapper entity(m_World, cModel.EntityID); glm::mat4 modelMatrix = Transform::ModelMatrix(entity.ID, entity.World); for (auto matGroup : model->MaterialGroups()) { - std::shared_ptr modelJob = std::make_shared(model, scene.Camera, modelMatrix, matGroup, cModel, entity.World); + std::shared_ptr modelJob = std::make_shared(model, scene.Camera, modelMatrix, matGroup, cModel, entity.World, glm::vec4(0), 0.f); scene.ForwardJobs.push_back(modelJob); } } diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index eeaa7bb3..51bc33ba 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -51,6 +51,10 @@ void DrawFinalPass::Draw(RenderScene& scene) glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); + glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(modelJob->FillColor)); + glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), modelJob->FillPercentage); + + if(modelJob->DiffuseTexture != nullptr) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 430992f6..577fddf6 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -61,9 +61,21 @@ 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"); + fillPercentage = (float)(double)fillComponent["Percentage"]; + fillColor = (glm::vec4)fillComponent["Color"]; + } + + + glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.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)); + std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, m_World, fillColor, fillPercentage)); jobs.push_back(modelJob); } } @@ -141,8 +153,8 @@ void RenderSystem::fillText(std::list>& jobs, World* } glm::mat4 modelMatrix = Transform::ModelMatrix(textComponent.EntityID, world); - std::shared_ptr modelJob = std::shared_ptr(new TextJob(modelMatrix, font, textComponent)); - jobs.push_back(modelJob); + std::shared_ptr textJob = std::shared_ptr(new TextJob(modelMatrix, font, textComponent)); + jobs.push_back(textJob); } } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 384c4a5b..6343cffe 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -10,6 +10,7 @@ #include "Core/EntityFileWriter.h" #include "Game/Systems/CapturePointSystem.h" #include "Game/Systems/WeaponSystem.h" +#include "Game/Systems/PlayerHUD.h" Game::Game(int argc, char* argv[]) { @@ -86,6 +87,8 @@ Game::Game(int argc, char* argv[]) // Populate Octree with collidables ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); + m_SystemPipeline->AddSystem(updateOrderLevel); + // Collision and TriggerSystem should update after player. ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); diff --git a/src/Game/Systems/PlayerHUD.cpp b/src/Game/Systems/PlayerHUD.cpp new file mode 100644 index 00000000..57e0e2f8 --- /dev/null +++ b/src/Game/Systems/PlayerHUD.cpp @@ -0,0 +1,79 @@ +#include "Game/Systems/PlayerHUD.h" + +PlayerHUD::PlayerHUD(World* world, EventBroker* eventBrokerer) + :System(world, eventBrokerer) + , m_World(world) + , m_EventBroker(eventBrokerer) +{ + + +} + +PlayerHUD::~PlayerHUD() +{ +// + +} + +void PlayerHUD::Update(double dt) +{ + + if(ImGui::Button("Set Camera", ImVec2(70, 20))){ + auto cameras = m_World->GetComponents("Camera"); + + if(cameras == nullptr) { + return; + } + + EntityWrapper ew = EntityWrapper(m_World, (*cameras->begin()).EntityID); + + if (ew.Valid()) { + Events::SetCamera e; + e.CameraEntity = ew; + m_EventBroker->Publish(e); + } else { + LOG_INFO("Coulnd't find a camera"); + } + } + + + auto healthHUDs = m_World->GetComponents("HealthHUD"); + if (healthHUDs == nullptr) { + return; + } + + for (auto& healthHUDComponent : *healthHUDs) { + EntityWrapper entity = EntityWrapper(m_World, healthHUDComponent.EntityID); + + EntityWrapper entityIDParent = entity; + while (entityIDParent.Parent().Valid()) { + entityIDParent = entityIDParent.Parent(); + + if (entityIDParent.HasComponent("Health")) { + break; + } + } + + if (entityIDParent.HasComponent("Health")) { + + if (entity.HasComponent("Text")) { + + std::string s = ""; + s = s + std::to_string((int)(double)entityIDParent["Health"]["Health"]); + s = s + "/"; + s = s + std::to_string((int)(double)entityIDParent["Health"]["MaxHealth"]); + + float healthPercentage = (double)entityIDParent["Health"]["Health"]/(double)entityIDParent["Health"]["MaxHealth"]; + (glm::vec4&) entity["Text"]["Color"] = glm::vec4(1.0 - healthPercentage, healthPercentage, 0.f, 0.f); + entity["Text"]["Content"] = s; + } + + if(entity.HasComponent("Fill")) { + float healthPercentage = (double)entityIDParent["Health"]["Health"]/(double)entityIDParent["Health"]["MaxHealth"]; + (glm::vec4&)entity["Fill"]["Color"] = glm::vec4(1.0 - healthPercentage, 0.f, healthPercentage, 0.f); + (double&)entity["Fill"]["Percentage"] = healthPercentage; + + } + } + } +} From aa3b8cd652808a6193cf7953031327de42195c96 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Sun, 24 Jan 2016 13:37:07 +0100 Subject: [PATCH 2/3] Fixed text color and removed debugging code --- resources/Schema/Entities/RenderingWorld.xml | 141 ++++++++++++++++--- src/Game/Systems/PlayerHUD.cpp | 26 +--- 2 files changed, 123 insertions(+), 44 deletions(-) diff --git a/resources/Schema/Entities/RenderingWorld.xml b/resources/Schema/Entities/RenderingWorld.xml index 7ecd25c9..73cafdb9 100644 --- a/resources/Schema/Entities/RenderingWorld.xml +++ b/resources/Schema/Entities/RenderingWorld.xml @@ -9,7 +9,7 @@ - 69.990005493164063 + 65.990005493164063 @@ -21,6 +21,7 @@ Models/Assault.obj + @@ -53,8 +54,8 @@ - 0.69990003108978271 - + 0.65990006923675537 + Models/Core/UnitHexagon.obj @@ -83,7 +84,7 @@ Models/SecondaryWeapon.fbx - + @@ -110,7 +111,7 @@ Models/SecondaryWeapon.fbx - + @@ -121,8 +122,68 @@ + + + + 24 + Fonts/DroidSans.ttf,60 + + + + + + + + + + + 262 + Fonts/DroidSans.ttf,60 + + + + + + + + + + + + + + + 65/100 + Fonts/DroidSans.ttf,60 + + + + + + + + + + + + + 0.65990006923675537 + + + + Models/Core/UnitRaptor.obj + + + + + + + + + + @@ -145,6 +206,7 @@ Models/Assault.obj + @@ -157,6 +219,7 @@ Welcome! Fonts/DroidSans.ttf,1280 + @@ -167,32 +230,40 @@ - 2 - + 3 + - - + + + Models/Core/UnitSphere.obj + + - 6 + 70 0.5 + + + Models/Core/UnitSphere.obj + + - 8 + 70 @@ -204,21 +275,30 @@ + + Models/Core/UnitSphere.obj + + - 6 + 70 0.5 + + + Models/Core/UnitSphere.obj + + - 8 + 70 @@ -230,21 +310,30 @@ + + Models/Core/UnitSphere.obj + + - 6 + 70 0.5 + + + Models/Core/UnitSphere.obj + + - - 8 + + 70 @@ -256,19 +345,28 @@ + + Models/Core/UnitSphere.obj + + - 6 + 70 0.5 + + + Models/Core/UnitSphere.obj + + - 8 + 70 @@ -284,6 +382,7 @@ Models/Assault.obj + @@ -291,14 +390,15 @@ - + - 0.80000001192092896 + 0.10000000149011612 Models/DirectionalLightWidget.obj + @@ -311,6 +411,7 @@ Models/Core/UnitCube.obj + diff --git a/src/Game/Systems/PlayerHUD.cpp b/src/Game/Systems/PlayerHUD.cpp index 57e0e2f8..55d0c3f1 100644 --- a/src/Game/Systems/PlayerHUD.cpp +++ b/src/Game/Systems/PlayerHUD.cpp @@ -11,32 +11,12 @@ PlayerHUD::PlayerHUD(World* world, EventBroker* eventBrokerer) PlayerHUD::~PlayerHUD() { -// + } void PlayerHUD::Update(double dt) { - - if(ImGui::Button("Set Camera", ImVec2(70, 20))){ - auto cameras = m_World->GetComponents("Camera"); - - if(cameras == nullptr) { - return; - } - - EntityWrapper ew = EntityWrapper(m_World, (*cameras->begin()).EntityID); - - if (ew.Valid()) { - Events::SetCamera e; - e.CameraEntity = ew; - m_EventBroker->Publish(e); - } else { - LOG_INFO("Coulnd't find a camera"); - } - } - - auto healthHUDs = m_World->GetComponents("HealthHUD"); if (healthHUDs == nullptr) { return; @@ -57,14 +37,12 @@ void PlayerHUD::Update(double dt) if (entityIDParent.HasComponent("Health")) { if (entity.HasComponent("Text")) { - std::string s = ""; s = s + std::to_string((int)(double)entityIDParent["Health"]["Health"]); s = s + "/"; s = s + std::to_string((int)(double)entityIDParent["Health"]["MaxHealth"]); - float healthPercentage = (double)entityIDParent["Health"]["Health"]/(double)entityIDParent["Health"]["MaxHealth"]; - (glm::vec4&) entity["Text"]["Color"] = glm::vec4(1.0 - healthPercentage, healthPercentage, 0.f, 0.f); + (glm::vec4&)entity["Text"]["Color"] = glm::vec4(1.0 - healthPercentage, 0.f, healthPercentage, 1.f); entity["Text"]["Content"] = s; } From 7e92d6f53f1d83fe03ab7115a04d799f4f59afe6 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Sun, 24 Jan 2016 22:21:10 +0100 Subject: [PATCH 3/3] Healthbar on player --- resources/Schema/Entities/Player.xml | 40 +++++++++++++++++++++------ src/Engine/Editor/EditorGUI.cpp | 9 ++++++ src/Engine/Editor/EditorSystem.cpp | 2 +- src/Engine/Rendering/RenderSystem.cpp | 4 ++- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/resources/Schema/Entities/Player.xml b/resources/Schema/Entities/Player.xml index 4316091b..dce6154e 100644 --- a/resources/Schema/Entities/Player.xml +++ b/resources/Schema/Entities/Player.xml @@ -2,15 +2,14 @@ + - - - + 5 @@ -19,10 +18,7 @@ - - - - + @@ -31,7 +27,6 @@ - @@ -62,6 +57,34 @@ + + + + + + + + + + + + 1 + + + + Models/Core/UnitHexagon.mesh + + + + + + + + + + + + @@ -82,6 +105,7 @@ Models/AssaultHeadless.mesh + diff --git a/src/Engine/Editor/EditorGUI.cpp b/src/Engine/Editor/EditorGUI.cpp index 390e69a5..1200ce34 100644 --- a/src/Engine/Editor/EditorGUI.cpp +++ b/src/Engine/Editor/EditorGUI.cpp @@ -1,4 +1,5 @@ #include "Editor/EditorGUI.h" +#include "Rendering/ESetCamera.h" EditorGUI::EditorGUI(World* world, EventBroker* eventBroker) : m_World(world) @@ -305,6 +306,14 @@ bool EditorGUI::drawComponentNode(EntityWrapper entity, const ComponentInfo& ci) } } + if (ci.Name == "Camera") { + if (ImGui::Button("Activate")) { + Events::SetCamera e; + e.CameraEntity = entity; + m_EventBroker->Publish(e); + } + } + return true; } diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 9c0e2e47..6ffd0e24 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -203,7 +203,7 @@ bool EditorSystem::OnSetCamera(const Events::SetCamera& e) m_ActualCamera = e.CameraEntity; Events::SetCamera e2; e2.CameraEntity = m_EditorCamera; - m_EventBroker->Publish(e2); + //m_EventBroker->Publish(e2); } return true; } diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 27f19452..60a90960 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -52,7 +52,9 @@ void RenderSystem::fillModels(std::list>& jobs) // Don't render the local player if (entity == m_LocalPlayer || entity.IsChildOf(m_LocalPlayer)) { - continue; + if (!entity.HasComponent("HealthHUD")) { //Should work but needs to be fixed. Should only render the things "childed" to the local player camera + continue; + } } Model* model;