diff --git a/include/Engine/Core/Transform.h b/include/Engine/Core/Transform.h index c43e4386..474a7bdb 100644 --- a/include/Engine/Core/Transform.h +++ b/include/Engine/Core/Transform.h @@ -4,59 +4,14 @@ #include "../GLM.h" #include "World.h" -static class Transform +namespace Transform { -public: - static glm::vec3 AbsolutePosition(World* world, EntityID entity) - { - glm::vec3 position; - while (entity != EntityID_Invalid) { - ComponentWrapper transform = world->GetComponent(entity, "Transform"); - EntityID parent = world->GetParent(entity); - position += AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"]; - entity = parent; - } +glm::vec3 AbsolutePosition(World* world, EntityID entity); +glm::quat AbsoluteOrientation(World* world, EntityID entity); +glm::vec3 AbsoluteScale(World* world, EntityID entity); +glm::mat4 ModelMatrix(EntityID entity, World* world); - return position; - }; - - static glm::quat AbsoluteOrientation(World* world, EntityID entity) - { - glm::quat orientation; - - while (entity != EntityID_Invalid) { - ComponentWrapper transform = world->GetComponent(entity, "Transform"); - orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation; - entity = world->GetParent(entity); - } - - return orientation; - }; - - static glm::vec3 AbsoluteScale(World* world, EntityID entity) - { - glm::vec3 scale(1.f); - - while (entity != EntityID_Invalid) { - ComponentWrapper transform = world->GetComponent(entity, "Transform"); - scale *= (glm::vec3)transform["Scale"]; - entity = world->GetParent(entity); - } - - return scale; - }; - - static glm::mat4 ModelMatrix(EntityID entity, World* world) - { - glm::vec3 position = Transform::AbsolutePosition(world, entity); - glm::quat orientation = Transform::AbsoluteOrientation(world, entity); - glm::vec3 scale = Transform::AbsoluteScale(world, entity); - - glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); - return modelMatrix; - } - -}; +} #endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index 93e7233f..efe3b88c 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -14,22 +14,24 @@ #include "ModelJob.h" #include "Renderer.h" #include "../Core/Transform.h" +#include "DebugCameraInputController.h" class RenderSystem : public ImpureSystem { public: RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame); + ~RenderSystem(); virtual void Update(World* world, double dt) override; private: World* m_World = nullptr; - const IRenderer* m_Renderer = nullptr; + const IRenderer* m_Renderer; RenderFrame* m_RenderFrame; bool m_SwitchCamera = false; - Camera* m_Camera = nullptr; - Camera* m_DefaultCamera = nullptr; + Camera* m_Camera; + DebugCameraInputController* m_DebugCameraInputController; std::list m_CameraComponents; @@ -41,8 +43,6 @@ private: void updateCamera(World* world, double dt); void updateProjectionMatrix(ComponentWrapper& cameraComponent); - glm::mat4 m_ViewMatrix; - glm::mat4 m_ProjectionMatrix; void fillModels(std::list>& jobs, World* world); diff --git a/src/Engine/Core/Transform.cpp b/src/Engine/Core/Transform.cpp new file mode 100644 index 00000000..cbc405a3 --- /dev/null +++ b/src/Engine/Core/Transform.cpp @@ -0,0 +1,52 @@ +#include "Core/Transform.h" + +glm::vec3 Transform::AbsolutePosition(World* world, EntityID entity) +{ + glm::vec3 position; + + while (entity != EntityID_Invalid) { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + EntityID parent = world->GetParent(entity); + position += Transform::AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"]; + entity = parent; + } + + return position; +} + +glm::quat Transform::AbsoluteOrientation(World* world, EntityID entity) +{ + glm::quat orientation; + + while (entity != EntityID_Invalid) { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation; + entity = world->GetParent(entity); + } + + return orientation; +} + +glm::vec3 Transform::AbsoluteScale(World* world, EntityID entity) +{ + glm::vec3 scale(1.f); + + while (entity != EntityID_Invalid) { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + scale *= (glm::vec3)transform["Scale"]; + entity = world->GetParent(entity); + } + + return scale; +} + +glm::mat4 Transform::ModelMatrix(EntityID entity, World* world) +{ + glm::vec3 position = Transform::AbsolutePosition(world, entity); + glm::quat orientation = Transform::AbsoluteOrientation(world, entity); + glm::vec3 scale = Transform::AbsoluteScale(world, entity); + + glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); + return modelMatrix; +} + diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index de57c705..94e848c0 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -124,6 +124,9 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e) if (m_Selection == 0) { return false; } + if (m_Camera == nullptr) { + return false; + } auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); glm::vec3 widgetOrientation = widgetTransform["Orientation"]; diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index aa42a961..6800df1d 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -109,7 +109,7 @@ void PickingPass::ClearPicking() { m_PickingColorsToEntity.clear(); m_EntityColors.clear(); - m_ColorCounter[0] = 0; + m_ColorCounter[0] = 1; m_ColorCounter[1] = 0; m_PickingBuffer.Bind(); @@ -138,10 +138,10 @@ PickData PickingPass::Pick(glm::vec2 screenCoord) pickInfo = it->second; } else { pickData.Entity = EntityID_Invalid; + return pickData; } - pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, resolution, pickInfo.Camera->ProjectionMatrix(), pickInfo.Camera->ViewMatrix()); - + pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, resolution, pickInfo.Camera->ProjectionMatrix(), pickInfo.Camera->ViewMatrix()); pickData.Entity = pickInfo.Entity; pickData.Camera = pickInfo.Camera; pickData.World = pickInfo.World; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index d6cdb9b3..1f2bac9d 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -1,5 +1,4 @@ #include "Rendering/RenderSystem.h" -#include "Rendering/DebugCameraInputController.h" RenderSystem::RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame) :ImpureSystem(eventBrokerer) { @@ -8,11 +7,14 @@ RenderSystem::RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand); - m_DefaultCamera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); - m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10)); - if (m_Camera == nullptr) { - m_Camera = m_DefaultCamera; - } + m_Camera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); + m_DebugCameraInputController = new DebugCameraInputController(eventBrokerer, -1); +} + +RenderSystem::~RenderSystem() +{ + delete m_Camera; + delete m_DebugCameraInputController; } bool RenderSystem::OnSetCamera(const Events::SetCamera &event) @@ -54,14 +56,11 @@ void RenderSystem::switchCamera(EntityID entity) void RenderSystem::updateProjectionMatrix(ComponentWrapper& cameraComponent) { double fov = cameraComponent["FOV"]; - double aspectRatio = m_Renderer->Resolution().Width / m_Renderer->Resolution().Height; + double aspectRatio = (float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height; double nearClip = cameraComponent["NearClip"]; double farClip = cameraComponent["FarClip"]; - double fovY = atan(tan(glm::radians(fov)/2.0) * aspectRatio) * 2.0; - m_ProjectionMatrix = glm::perspective(fovY, aspectRatio, nearClip, farClip); - - m_Camera->SetFOV(fovY); + m_Camera->SetFOV(glm::radians(fov)); m_Camera->SetAspectRatio(aspectRatio); m_Camera->SetNearClip(nearClip); m_Camera->SetFarClip(farClip); @@ -129,66 +128,61 @@ void RenderSystem::Update(World* world, double dt) void RenderSystem::updateCamera(World* world, double dt) { - - static DebugCameraInputController firstPersonInputController(m_EventBroker, -1); - - if (m_SwitchCamera) { - auto cameras = world->GetComponents("Camera"); - for (auto it = cameras->begin(); it != cameras->end(); it++) { - if ((*it).EntityID == m_CurrentCamera) { - it++; - if (it != cameras->end()) { - switchCamera((*it).EntityID); - } else { - switchCamera((*cameras->begin()).EntityID); - } - break; + if (m_SwitchCamera) { + auto cameras = world->GetComponents("Camera"); + for (auto it = cameras->begin(); it != cameras->end(); it++) { + if ((*it).EntityID == m_CurrentCamera) { + it++; + if (it != cameras->end()) { + switchCamera((*it).EntityID); + } else { + switchCamera((*cameras->begin()).EntityID); } + break; } + } + ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); + ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); + + m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); + m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); + } + + if (m_World->ValidEntity(m_CurrentCamera)) { + if (world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) { ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - firstPersonInputController.SetPosition(cameraTransform["Position"]); - - } + m_DebugCameraInputController->Update(dt); + (glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(m_DebugCameraInputController->Orientation()); + (glm::vec3&)cameraTransform["Position"] = m_DebugCameraInputController->Position(); + + glm::vec3 position = Transform::AbsolutePosition(world, m_CurrentCamera); + glm::quat orientation = Transform::AbsoluteOrientation(world, m_CurrentCamera); + + m_Camera->SetPosition(position); + m_Camera->SetOrientation(orientation); + + updateProjectionMatrix(cameraComponent); + + } + } else { + m_Camera = m_Camera; + + auto cameras = world->GetComponents("Camera"); + if (cameras != nullptr) { + if (cameras->begin() != cameras->end()) { + ComponentWrapper& cameraC = *cameras->begin(); + switchCamera(cameraC.EntityID); - if (m_World->ValidEntity(m_CurrentCamera)) { - if (world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) { ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - firstPersonInputController.Update(dt); - (glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(firstPersonInputController.Orientation()); - (glm::vec3&)cameraTransform["Position"] = firstPersonInputController.Position(); - - glm::vec3 position = Transform::AbsolutePosition(world, m_CurrentCamera); - glm::quat orientation = Transform::AbsoluteOrientation(world, m_CurrentCamera); - - m_Camera->SetPosition(position); - m_Camera->SetOrientation(orientation); - - updateProjectionMatrix(cameraComponent); - - } - } else { - m_Camera = m_DefaultCamera; - - auto cameras = world->GetComponents("Camera"); - if (cameras != nullptr) { - if (cameras->begin() != cameras->end()) { - ComponentWrapper& cameraC = *cameras->begin(); - switchCamera(cameraC.EntityID); - - ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); - ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); - - firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - firstPersonInputController.SetPosition(cameraTransform["Position"]); - } + m_DebugCameraInputController->SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); + m_DebugCameraInputController->SetPosition(cameraTransform["Position"]); } } + } - m_Camera->UpdateViewMatrix(); -} - + m_Camera->UpdateViewMatrix(); +} \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 1cfb95e0..ba660a7d 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -31,6 +31,7 @@ Game::Game(int argc, char* argv[]) )); m_Renderer->Initialize(); //m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); + m_RenderFrame = new RenderFrame(); // Create input manager m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); @@ -55,8 +56,6 @@ Game::Game(int argc, char* argv[]) fp.MergeEntities(m_World); } - m_RenderFrame = new RenderFrame(); - // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker); @@ -72,7 +71,6 @@ Game::Game(int argc, char* argv[]) ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); - ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame);