From a7b43b06a2672faf86e3579cbd45e9fb73991042 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Fri, 8 Jan 2016 16:21:59 +0100 Subject: [PATCH] Camera component and RenderQueue changes --- include/Engine/GUI/Button.h | 2 +- include/Engine/GUI/Frame.h | 4 +- include/Engine/GUI/TextureFrame.h | 2 +- include/Engine/Rendering/Camera.h | 10 +- include/Engine/Rendering/DrawScenePass.h | 2 +- include/Engine/Rendering/DummyRenderer.h | 2 +- include/Engine/Rendering/ESetCamera.h | 4 +- include/Engine/Rendering/IRenderer.h | 2 +- include/Engine/Rendering/PickingPass.h | 2 +- include/Engine/Rendering/RenderQueue.h | 38 +++++++- include/Engine/Rendering/RenderSystem.h | 15 ++- include/Engine/Rendering/Renderer.h | 2 +- include/Game/Game.h | 2 +- resources/Schema/Components/Camera.xml | 1 + resources/Schema/Components/Camera.xsd | 1 + resources/Schema/Entities/Test.xml | 2 + src/Engine/Rendering/Camera.cpp | 9 -- src/Engine/Rendering/DrawScenePass.cpp | 48 +++++----- src/Engine/Rendering/DummyRenderer.cpp | 2 +- src/Engine/Rendering/PickingPass.cpp | 56 ++++++----- src/Engine/Rendering/RenderSystem.cpp | 116 +++++++++++++++-------- src/Engine/Rendering/Renderer.cpp | 8 +- src/Game/Game.cpp | 6 +- 23 files changed, 199 insertions(+), 137 deletions(-) diff --git a/include/Engine/GUI/Button.h b/include/Engine/GUI/Button.h index 91bd9782..80845cb7 100644 --- a/include/Engine/GUI/Button.h +++ b/include/Engine/GUI/Button.h @@ -40,7 +40,7 @@ public: m_TexturePressed = resourceName; } - void Draw(RenderQueueCollection& rq) override + void Draw(RenderScene& rq) override { if (m_Texture == nullptr && !m_TextureReleased.empty()) { SetTexture(m_TextureReleased); diff --git a/include/Engine/GUI/Frame.h b/include/Engine/GUI/Frame.h index 416b8117..4c5f2eb9 100644 --- a/include/Engine/GUI/Frame.h +++ b/include/Engine/GUI/Frame.h @@ -212,7 +212,7 @@ public: virtual void Update(double dt) { } - void DrawLayered(RenderQueueCollection& rq) + void DrawLayered(RenderScene& rq) { if (this->Hidden()) return; @@ -232,7 +232,7 @@ public: } } - virtual void Draw(RenderQueueCollection& rq) { } + virtual void Draw(RenderScene& rq) { } protected: ::EventBroker* m_EventBroker; diff --git a/include/Engine/GUI/TextureFrame.h b/include/Engine/GUI/TextureFrame.h index f02285ca..2c6d34dd 100644 --- a/include/Engine/GUI/TextureFrame.h +++ b/include/Engine/GUI/TextureFrame.h @@ -16,7 +16,7 @@ public: void EnableScissor() { m_ScissorEnabled = true; } void DisableScissor() { m_ScissorEnabled = false; } - void Draw(RenderQueueCollection& rq) override + void Draw(RenderScene& rq) override { if (m_Texture == nullptr) return; diff --git a/include/Engine/Rendering/Camera.h b/include/Engine/Rendering/Camera.h index 660dfd49..a1aae1bd 100644 --- a/include/Engine/Rendering/Camera.h +++ b/include/Engine/Rendering/Camera.h @@ -26,11 +26,6 @@ public: glm::quat Orientation() const { return m_Orientation; } void SetOrientation(glm::quat val); - /*float Pitch() const { return m_Pitch; } - void Pitch(float val); - float Yaw() const { return m_Yaw; } - void Yaw(float val);*/ - glm::mat4 ProjectionMatrix() const { return m_ProjectionMatrix; } glm::mat4 ViewMatrix() const { return m_ViewMatrix; } @@ -46,11 +41,10 @@ public: float FarClip() const { return m_FarClip; } void SetFarClip(float val); - + void UpdateViewMatrix(); + void UpdateProjectionMatrix(); private: - void UpdateViewMatrix(); - void UpdateProjectionMatrix(); glm::vec3 m_Position; glm::quat m_Orientation; diff --git a/include/Engine/Rendering/DrawScenePass.h b/include/Engine/Rendering/DrawScenePass.h index db52f746..4200383f 100644 --- a/include/Engine/Rendering/DrawScenePass.h +++ b/include/Engine/Rendering/DrawScenePass.h @@ -17,7 +17,7 @@ public: void InitializeFrameBuffers(); void InitializeShaderPrograms(); - void Draw(RenderQueueCollection& rq); + void Draw(RenderFrame& rf); //Getters diff --git a/include/Engine/Rendering/DummyRenderer.h b/include/Engine/Rendering/DummyRenderer.h index 56790fa1..176c5834 100644 --- a/include/Engine/Rendering/DummyRenderer.h +++ b/include/Engine/Rendering/DummyRenderer.h @@ -9,7 +9,7 @@ class DummyRenderer : public IRenderer { public: virtual void Initialize() override; - virtual void Draw(RenderQueueCollection& rq) override; + virtual void Draw(RenderFrame& rq) override; }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/ESetCamera.h b/include/Engine/Rendering/ESetCamera.h index cf5b0a87..650f3b12 100644 --- a/include/Engine/Rendering/ESetCamera.h +++ b/include/Engine/Rendering/ESetCamera.h @@ -3,16 +3,16 @@ #include "../Core/EventBroker.h" #include "../Core/Entity.h" +#include namespace Events { -/** Thrown Every frame, use functions to pick*/ struct SetCamera : Event { public: SetCamera() { }; - EntityID Entity; + std::string Name; private: diff --git a/include/Engine/Rendering/IRenderer.h b/include/Engine/Rendering/IRenderer.h index 405de00e..42729479 100644 --- a/include/Engine/Rendering/IRenderer.h +++ b/include/Engine/Rendering/IRenderer.h @@ -31,7 +31,7 @@ public: } virtual void Initialize() = 0; virtual void Update(double dt) = 0; - virtual void Draw(RenderQueueCollection& rq) = 0; + virtual void Draw(RenderFrame& rq) = 0; protected: Rectangle m_Resolution = Rectangle(1280, 720); diff --git a/include/Engine/Rendering/PickingPass.h b/include/Engine/Rendering/PickingPass.h index e1bc42db..e4ff9e27 100644 --- a/include/Engine/Rendering/PickingPass.h +++ b/include/Engine/Rendering/PickingPass.h @@ -18,7 +18,7 @@ public: void InitializeFrameBuffers(); void InitializeShaderPrograms(); - void Draw(RenderQueueCollection& rq); + void Draw(RenderFrame& rf); //Getters diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 5fb09678..719e5cc7 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -39,9 +39,7 @@ struct ModelJob : RenderJob unsigned int ShaderID = 0; unsigned int TextureID = 0; - //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this EntityID Entity; - glm::mat4 Matrix; const Texture* DiffuseTexture; const Texture* NormalTexture; @@ -165,7 +163,7 @@ private: int m_Size = 0; }; -struct RenderQueueCollection +struct RenderScene { RenderQueue Forward; RenderQueue Lights; @@ -184,4 +182,38 @@ struct RenderQueueCollection } }; +class RenderFrame +{ +public: + + void Add(RenderScene &scene) + { + RenderScenes.push_back(std::shared_ptr(new RenderScene(scene))); + m_Size++; + } + + void Clear() + { + RenderScenes.clear(); + m_Size = 0; + } + + int Size() const { return m_Size; } + std::list>::const_iterator begin() + { + return RenderScenes.begin(); + } + + std::list>::const_iterator end() + { + return RenderScenes.end(); + } + + std::list> RenderScenes; + + +private: + int m_Size = 0; +}; + #endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index e61ade55..34a9c5f9 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -12,10 +12,11 @@ #include "../Input/EInputCommand.h" #include "Camera.h" + class RenderSystem : public ImpureSystem { public: - RenderSystem(EventBroker* eventBrokerer, RenderQueueCollection* renderQueues); + RenderSystem(EventBroker* eventBrokerer, RenderFrame* renderFrame); virtual void Update(World* world, double dt) override; @@ -26,9 +27,14 @@ public: private: - RenderQueueCollection* m_RenderQueues; + World* m_World = nullptr; + + RenderFrame* m_RenderFrame; bool m_SwitchCamera = false; Camera* m_Camera = nullptr; + Camera* m_DefaultCamera = nullptr; + + std::list m_CameraComponents; EventRelay m_ESetCamera; bool OnSetCamera(const Events::SetCamera &event); @@ -37,13 +43,12 @@ private: void SwitchCamera(EntityID entity); void Initialize(); - void UpdateViewMatrix(ComponentWrapper& cameraTransform); void UpdateProjectionMatrix(ComponentWrapper& cameraComponent); glm::mat4 m_ViewMatrix; glm::mat4 m_ProjectionMatrix; - glm::mat4 ModelMatrix(World* world, EntityID entity); - void FillModels(World* world, RenderQueue* renderQueue); + glm::mat4 ModelMatrix(EntityID entity); + void FillModels(RenderQueue* renderQueue); EventRelay m_EInputCommand; bool OnInputCommand(const Events::InputCommand& e); diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index e22b9fec..d89fc4d2 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -27,7 +27,7 @@ public: virtual void Initialize() override; virtual void Update(double dt) override; - virtual void Draw(RenderQueueCollection& rq) override; + virtual void Draw(RenderFrame& rf) override; private: //----------------------Variables----------------------// diff --git a/include/Game/Game.h b/include/Game/Game.h index 85337707..d9e8821f 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -38,7 +38,7 @@ private: GUI::Frame* m_FrameStack; World* m_World; SystemPipeline* m_SystemPipeline; - RenderQueueCollection* m_RenderQueues; + RenderFrame* m_RenderFrame; EventRelay m_EInputCommand; bool debugOnInputCommand(const Events::InputCommand& e); diff --git a/resources/Schema/Components/Camera.xml b/resources/Schema/Components/Camera.xml index 7edb3489..8a93dec5 100644 --- a/resources/Schema/Components/Camera.xml +++ b/resources/Schema/Components/Camera.xml @@ -1,4 +1,5 @@ + cam 1.77 60.0 0.01 diff --git a/resources/Schema/Components/Camera.xsd b/resources/Schema/Components/Camera.xsd index 1ac8c394..3c462a3b 100644 --- a/resources/Schema/Components/Camera.xsd +++ b/resources/Schema/Components/Camera.xsd @@ -9,6 +9,7 @@ + diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 7b5cb820..df739177 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -18,6 +18,7 @@ Models/Camera.obj + MainCamera @@ -30,6 +31,7 @@ Models/Camera.obj + ActionCamera diff --git a/src/Engine/Rendering/Camera.cpp b/src/Engine/Rendering/Camera.cpp index 6ddf1c6c..ecc5d5cc 100644 --- a/src/Engine/Rendering/Camera.cpp +++ b/src/Engine/Rendering/Camera.cpp @@ -64,15 +64,6 @@ void Camera::SetOrientation(glm::quat val) void Camera::UpdateProjectionMatrix() { -// m_ProjectionMatrix = glm::ortho( -// -16.f, -// 16.f, -// -9.f, -// 9.f, -// m_NearClip, -// m_FarClip -// ); - m_ProjectionMatrix = glm::perspective(m_FOV, m_AspectRatio, m_NearClip, m_FarClip); } diff --git a/src/Engine/Rendering/DrawScenePass.cpp b/src/Engine/Rendering/DrawScenePass.cpp index f0dd1405..dd34072f 100644 --- a/src/Engine/Rendering/DrawScenePass.cpp +++ b/src/Engine/Rendering/DrawScenePass.cpp @@ -25,40 +25,40 @@ void DrawScenePass::InitializeShaderPrograms() } -void DrawScenePass::Draw(RenderQueueCollection& rq) +void DrawScenePass::Draw(RenderFrame& rf) { //glBindFramebuffer(GL_FRAMEBUFFER, 0); GLERROR("Renderer::Draw PickingPass"); DrawScenePassState state; + for (auto scene : rf.RenderScenes) { + scene->Forward.Jobs.sort(DrawScenePass::DepthSort); - rq.Forward.Jobs.sort(DrawScenePass::DepthSort); + for (auto &job : scene->Forward) { + auto modelJob = std::dynamic_pointer_cast(job); + if (modelJob) { + GLuint ShaderHandle = m_BasicForwardProgram->GetHandle(); - //TODO: Render: Add code for more jobs than modeljobs. - for (auto &job : rq.Forward) { - auto modelJob = std::dynamic_pointer_cast(job); - if (modelJob) { - GLuint ShaderHandle = m_BasicForwardProgram->GetHandle(); + m_BasicForwardProgram->Bind(); + //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); + glUniform4fv(glGetUniformLocation(ShaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); - m_BasicForwardProgram->Bind(); - //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); - glUniform4fv(glGetUniformLocation(ShaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); + //TODO: Renderer: bättre textur felhantering samt fler texturer stöd + if (modelJob->DiffuseTexture != nullptr) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); + } else { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } - //TODO: Renderer: bättre textur felhantering samt fler texturer stöd - if (modelJob->DiffuseTexture != nullptr) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); - } else { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + glBindVertexArray(modelJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); + glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex); + + continue; } - - glBindVertexArray(modelJob->Model->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); - glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex); - - continue; } } GLERROR("DrawScene Error"); diff --git a/src/Engine/Rendering/DummyRenderer.cpp b/src/Engine/Rendering/DummyRenderer.cpp index 451dfa09..ab529315 100644 --- a/src/Engine/Rendering/DummyRenderer.cpp +++ b/src/Engine/Rendering/DummyRenderer.cpp @@ -42,7 +42,7 @@ void DummyRenderer::Initialize() glfwSwapInterval(m_VSYNC); } -void DummyRenderer::Draw(RenderQueueCollection& rq) +void DummyRenderer::Draw(RenderFrame& rq) { glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 0.f); glClear(GL_COLOR_BUFFER_BIT); diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index 0f821617..f7f0558c 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -43,7 +43,7 @@ void PickingPass::InitializeShaderPrograms() m_PickingProgram->Link(); } -void PickingPass::Draw(RenderQueueCollection& rq) +void PickingPass::Draw(RenderFrame& rf) { m_PickingColorsToEntity.clear(); PickingPassState* state = new PickingPassState(m_PickingBuffer.GetHandle()); @@ -57,34 +57,38 @@ void PickingPass::Draw(RenderQueueCollection& rq) std::map entityColors; - for (auto &job : rq.Forward) { - auto modelJob = std::dynamic_pointer_cast(job); + for(auto scene : rf.RenderScenes) + { + for (auto &job : scene->Forward) { + auto modelJob = std::dynamic_pointer_cast(job); - if (modelJob) { - int pickColor[2] = { r, g }; - auto color = entityColors.find(modelJob->Entity); - if (color != entityColors.end()) { - pickColor[0] = color->second[0]; - pickColor[1] = color->second[1]; - } else { - entityColors[modelJob->Entity] = glm::vec2(pickColor[0], pickColor[1]); - if (r + 10 > 255) { - r = 0; - g += 1; + if (modelJob) { + int pickColor[2] = { r, g }; + auto color = entityColors.find(modelJob->Entity); + if (color != entityColors.end()) { + pickColor[0] = color->second[0]; + pickColor[1] = color->second[1]; } else { - r += 1; + entityColors[modelJob->Entity] = glm::vec2(pickColor[0], pickColor[1]); + if (r + 10 > 255) { + r = 0; + g += 1; + } else { + r += 1; + } } + m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity; + + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); + glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); + + glBindVertexArray(modelJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); + glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, nullptr, modelJob->StartIndex); } - m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity; - - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); - glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); - - glBindVertexArray(modelJob->Model->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); - glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, nullptr, modelJob->StartIndex); } } + m_PickingBuffer.Unbind(); GLERROR("PickingPass Error"); @@ -92,15 +96,15 @@ void PickingPass::Draw(RenderQueueCollection& rq) int fbWidth; int fbHeight; glfwGetFramebufferSize(m_Renderer->Window(), &fbWidth, &fbHeight); - /* Events::Picking pickEvent = Events::Picking( + Events::Picking pickEvent = Events::Picking( &m_PickingBuffer, &m_DepthBuffer, m_Renderer->Camera()->ProjectionMatrix(), m_Renderer->Camera()->ViewMatrix(), Rectangle(fbWidth, fbHeight), - &m_PickingColorsToEntity);*/ + &m_PickingColorsToEntity); - //m_EventBroker->Publish(pickEvent); + m_EventBroker->Publish(pickEvent); delete state; } diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index c08d4971..33c03e8f 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -1,29 +1,56 @@ #include "Rendering/RenderSystem.h" #include "Rendering/DebugCameraInputController.h" -RenderSystem::RenderSystem(EventBroker* eventBrokerer, RenderQueueCollection* renderQueues) +RenderSystem::RenderSystem(EventBroker* eventBrokerer, RenderFrame* renderFrame) :ImpureSystem(eventBrokerer) { - m_RenderQueues = renderQueues; + m_RenderFrame = renderFrame; Initialize(); EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand); - + m_DefaultCamera = new Camera(1.77f /* should be based on width and 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; + } } bool RenderSystem::OnSetCamera(const Events::SetCamera &event) { - m_CurrentCamera = event.Entity; + auto cameras = m_World->GetComponents("Camera"); + + if (cameras != nullptr) { + for (auto it = cameras->begin(); it != cameras->end(); it++) { + if ((std::string)(*it)["Name"] == event.Name) { + SwitchCamera((*it).EntityID); + } + } + } + return true; } void RenderSystem::SwitchCamera(EntityID entity) { - m_CurrentCamera = entity; - m_SwitchCamera = false; - LOG_INFO("Switched to camera %i", m_CurrentCamera); + if(m_World->HasComponent(entity, "Camera")) { + if (m_World->HasComponent(m_CurrentCamera, "Model")) { + m_World->GetComponent(m_CurrentCamera, "Model")["Visible"] = true; + } + + if (m_World->HasComponent(entity, "Model")) { + m_World->GetComponent(entity, "Model")["Visible"] = false; + } + + m_CurrentCamera = entity; + m_SwitchCamera = false; + + LOG_INFO("Switched to %s", m_World->GetComponent(m_CurrentCamera, "Camera")["Name"]); + } else { + LOG_ERROR("Entity %i does not have a CameraComponent", entity); + m_SwitchCamera = false; + } } void RenderSystem::Initialize() @@ -31,24 +58,21 @@ void RenderSystem::Initialize() } -void RenderSystem::UpdateViewMatrix(ComponentWrapper& cameraTransform) -{ - glm::quat orientation = glm::quat((glm::vec3)cameraTransform["Orientation"]); - glm::vec3 position = cameraTransform["Position"]; - - m_ViewMatrix = glm::toMat4(glm::inverse(orientation)) * glm::translate(-position); -} - void RenderSystem::UpdateProjectionMatrix(ComponentWrapper& cameraComponent) { - double fov = (double&)cameraComponent["FOV"]; - double aspectRatio = (double&)cameraComponent["AspectRatio"]; - double nearClip = (double&)cameraComponent["NearClip"]; - double farClip = (double&)cameraComponent["FarClip"]; + double fov = cameraComponent["FOV"]; + double aspectRatio = cameraComponent["AspectRatio"]; + 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->SetAspectRatio(aspectRatio); + m_Camera->SetNearClip(nearClip); + m_Camera->SetFarClip(farClip); + m_Camera->UpdateProjectionMatrix(); } glm::vec3 RenderSystem::AbsolutePosition(World* world, EntityID entity) @@ -95,20 +119,19 @@ glm::vec3 RenderSystem::AbsoluteScale(World* world, EntityID entity) return scale; } - -glm::mat4 RenderSystem::ModelMatrix(World* world, EntityID entity) +glm::mat4 RenderSystem::ModelMatrix(EntityID entity) { - glm::vec3 position = AbsolutePosition(world, entity); - glm::quat orientation = AbsoluteOrientation(world, entity); - glm::vec3 scale = AbsoluteScale(world, entity); + glm::vec3 position = AbsolutePosition(m_World, entity); + glm::quat orientation = AbsoluteOrientation(m_World, entity); + glm::vec3 scale = AbsoluteScale(m_World, entity); glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); return modelMatrix; } -void RenderSystem::FillModels(World* world, RenderQueue* renderQueue) +void RenderSystem::FillModels(RenderQueue* renderQueue) { - auto models = world->GetComponents("Model"); + auto models = m_World->GetComponents("Model"); if (models == nullptr) { return; } @@ -140,7 +163,7 @@ void RenderSystem::FillModels(World* world, RenderQueue* renderQueue) job.Model = model; job.StartIndex = texGroup.StartIndex; job.EndIndex = texGroup.EndIndex; - job.Matrix = m_ProjectionMatrix * m_ViewMatrix * (model->m_Matrix * ModelMatrix(world, modelC.EntityID)); + job.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID)); job.Color = color; job.Entity = modelC.EntityID; @@ -156,7 +179,7 @@ void RenderSystem::FillModels(World* world, RenderQueue* renderQueue) job.Model = model; job.StartIndex = texGroup.StartIndex; job.EndIndex = texGroup.EndIndex; - job.Matrix = m_ProjectionMatrix * m_ViewMatrix * (model->m_Matrix * ModelMatrix(world, modelC.EntityID)); + job.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID)); job.Color = color; job.Entity = modelC.EntityID; @@ -176,9 +199,10 @@ bool RenderSystem::OnInputCommand(const Events::InputCommand& e) } } - void RenderSystem::Update(World* world, double dt) { + m_World = world; + m_EventBroker->Process(); static DebugCameraInputController firstPersonInputController(m_EventBroker, -1); @@ -201,34 +225,42 @@ void RenderSystem::Update(World* world, double dt) ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); - firstPersonInputController.SetPosition((glm::vec3)cameraTransform["Position"]); + firstPersonInputController.SetPosition(cameraTransform["Position"]); } } 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(); + if(world->GetParent(m_CurrentCamera) == 1) { // world is entity 1, is this ok? + firstPersonInputController.Update(dt); + (glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(firstPersonInputController.Orientation()); + (glm::vec3&)cameraTransform["Position"] = firstPersonInputController.Position(); + } + glm::vec3 position = AbsolutePosition(world, m_CurrentCamera); + glm::quat orientation = AbsoluteOrientation(world, m_CurrentCamera); + m_Camera->SetPosition(position); + m_Camera->SetOrientation(orientation); + UpdateProjectionMatrix(cameraComponent); - UpdateViewMatrix(cameraTransform); + m_Camera->UpdateViewMatrix(); } else { - m_ProjectionMatrix = glm::perspective(glm::radians(40.f), 1.77f, 0.05f, 5000.f); - m_ViewMatrix = glm::mat4(); + m_Camera = m_DefaultCamera; auto cameras = world->GetComponents("Camera"); - if (cameras != nullptr) { ComponentWrapper& cameraC = *cameras->begin(); - m_CurrentCamera = cameraC.EntityID; + SwitchCamera(cameraC.EntityID); + world->GetComponent(m_CurrentCamera, "Model")["Visible"] = false; } } - m_RenderQueues->Clear(); - FillModels(world, &m_RenderQueues->Forward); + m_RenderFrame->Clear(); + RenderScene* rs = new RenderScene(); + rs->Camera = m_Camera; + FillModels(&rs->Forward); + m_RenderFrame->Add(*rs); } diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 0ef1e4bf..972a6df8 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -90,14 +90,14 @@ void Renderer::Update(double dt) m_ImGuiRenderPass->Update(dt); } -void Renderer::Draw(RenderQueueCollection& rq) +void Renderer::Draw(RenderFrame& rf) { - m_PickingPass->Draw(rq); - //DrawScreenQuad(m_PickingPass->PickingTexture()); + m_Camera = (*rf.begin())->Camera; + m_PickingPass->Draw(rf); glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 1.f); - m_DrawScenePass->Draw(rq); + m_DrawScenePass->Draw(rf); GLERROR("Renderer::Draw m_DrawScenePass->Draw"); m_ImGuiRenderPass->Draw(); glfwSwapBuffers(m_Window); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 4ab6f410..c104a3ad 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -47,14 +47,14 @@ Game::Game(int argc, char* argv[]) ResourceManager::Load(mapToLoad)->PopulateWorld(m_World); } - m_RenderQueues = new RenderQueueCollection(); + m_RenderFrame = new RenderFrame(); // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker); m_SystemPipeline->AddSystem(); m_SystemPipeline->AddSystem(); m_SystemPipeline->AddSystem(m_Renderer); - m_SystemPipeline->AddSystem(m_RenderQueues); + m_SystemPipeline->AddSystem(m_RenderFrame); m_LastTime = glfwGetTime(); @@ -91,7 +91,7 @@ void Game::Tick() m_Renderer->Update(dt); GLERROR("Game::Tick m_RenderQueueFactory->Update"); - m_Renderer->Draw(*m_RenderQueues); + m_Renderer->Draw(*m_RenderFrame); GLERROR("Game::Tick m_Renderer->Draw"); m_EventBroker->Swap(); m_EventBroker->Clear();