From 05b78265befea045240e179c6dee501036adf22a Mon Sep 17 00:00:00 2001 From: viktorljung Date: Mon, 14 Dec 2015 19:10:00 +0100 Subject: [PATCH] Fixed some transparency bugs and started on component based camera --- assets | 2 +- include/Engine/Rendering/DrawScenePass.h | 6 +++ include/Engine/Rendering/ESetCamera.h | 23 ++++++++ include/Engine/Rendering/RawModel.h | 1 + include/Engine/Rendering/RenderQueue.h | 31 +++++++++++ include/Engine/Rendering/RenderQueueFactory.h | 8 ++- include/Engine/Rendering/Renderer.h | 4 +- resources/Schema/Components.xsd | 1 + resources/Schema/Components/Camera.xml | 6 +++ resources/Schema/Components/Camera.xsd | 19 +++++++ resources/Schema/Entities/Test.xml | 16 +----- src/Engine/Rendering/DrawScenePass.cpp | 1 + src/Engine/Rendering/DrawScenePassState.cpp | 2 + src/Engine/Rendering/RawModel.cpp | 8 ++- src/Engine/Rendering/RenderQueueFactory.cpp | 52 ++++++++++++++----- src/Engine/Rendering/Renderer.cpp | 32 +----------- src/Game/Game.cpp | 20 +++---- 17 files changed, 160 insertions(+), 72 deletions(-) create mode 100644 include/Engine/Rendering/ESetCamera.h create mode 100644 resources/Schema/Components/Camera.xml create mode 100644 resources/Schema/Components/Camera.xsd diff --git a/assets b/assets index b3746822..c5f67434 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit b37468222e45ec0b2116f1543c578cb9784d43f2 +Subproject commit c5f674349a915ab1a2b4da632d87a9832d1f6fab diff --git a/include/Engine/Rendering/DrawScenePass.h b/include/Engine/Rendering/DrawScenePass.h index 782c5a49..db52f746 100644 --- a/include/Engine/Rendering/DrawScenePass.h +++ b/include/Engine/Rendering/DrawScenePass.h @@ -25,12 +25,18 @@ public: private: void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) const; + static bool DepthSort(const std::shared_ptr &i, const std::shared_ptr &j) + { + return (i->Depth < j->Depth); + }; + Texture* m_WhiteTexture; const IRenderer* m_Renderer; ShaderProgram* m_BasicForwardProgram; + }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/ESetCamera.h b/include/Engine/Rendering/ESetCamera.h new file mode 100644 index 00000000..cf5b0a87 --- /dev/null +++ b/include/Engine/Rendering/ESetCamera.h @@ -0,0 +1,23 @@ +#ifndef Events_SetCamera_h__ +#define Events_SetCamera_h__ + +#include "../Core/EventBroker.h" +#include "../Core/Entity.h" + +namespace Events +{ + +/** Thrown Every frame, use functions to pick*/ +struct SetCamera : Event +{ +public: + SetCamera() { }; + EntityID Entity; + +private: + +}; + +} + +#endif diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index c8226168..d63e46bb 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -46,6 +46,7 @@ public: struct MaterialGroup { float Shininess; + float Transparency; std::shared_ptr<::Texture> Texture; std::shared_ptr<::Texture> NormalMap; std::shared_ptr<::Texture> SpecularMap; diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 2942c743..1586aef6 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -63,6 +63,37 @@ struct ModelJob : RenderJob } }; +struct TransparentModelJob : 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 ModelMatrix; + const Texture* DiffuseTexture; + const Texture* NormalTexture; + const Texture* SpecularTexture; + float Shininess = 0.f; + glm::vec4 Color; + const Model* Model = nullptr; + unsigned int StartIndex = 0; + unsigned int EndIndex = 0; + + // Animation + Skeleton* Skeleton = nullptr; + bool NoRootMotion = true; + std::string AnimationName; + double AnimationTime = 0; + + void CalculateHash() override + { + Hash = TextureID; + } +}; + + struct SpriteJob : RenderJob { unsigned int ShaderID = 0; diff --git a/include/Engine/Rendering/RenderQueueFactory.h b/include/Engine/Rendering/RenderQueueFactory.h index b273b672..ff51a515 100644 --- a/include/Engine/Rendering/RenderQueueFactory.h +++ b/include/Engine/Rendering/RenderQueueFactory.h @@ -6,16 +6,19 @@ #include "../Core/ResourceManager.h" #include "Model.h" #include "../GLM.h" +#include "../Core/EventBroker.h" +#include "ESetCamera.h" class RenderQueueFactory { public: - RenderQueueFactory(); + RenderQueueFactory(EventBroker* eventBroker); void Update(World* world); RenderQueueCollection RenderQueues() const { return m_RenderQueues; } private: + EventBroker* m_EventBroker; RenderQueueCollection m_RenderQueues; void FillModels(World* world, RenderQueue* renderQueue); @@ -26,6 +29,9 @@ private: glm::vec3 AbsolutePosition(World* world, EntityID entity); glm::quat AbsoluteOrientation(World* world, EntityID entity); glm::vec3 AbsoluteScale(World* world, EntityID entity); + + EntityID m_CurrentCamera; + }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index 886c98ca..c304e879 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -32,8 +32,9 @@ enum lightType class Renderer : public IRenderer { public: - Renderer(EventBroker* eventBroker) + Renderer(EventBroker* eventBroker, World* world) : m_EventBroker(eventBroker) + , m_World(world) { } virtual void Initialize() override; @@ -43,6 +44,7 @@ public: private: //----------------------Variables----------------------// EventBroker* m_EventBroker; + World* m_World; Texture* m_ErrorTexture; Texture* m_WhiteTexture; diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 12fb870e..448d416d 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -6,4 +6,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/Camera.xml b/resources/Schema/Components/Camera.xml new file mode 100644 index 00000000..6b9e70d4 --- /dev/null +++ b/resources/Schema/Components/Camera.xml @@ -0,0 +1,6 @@ + + 1.77 + 90 + 0.01 + 5000 + \ No newline at end of file diff --git a/resources/Schema/Components/Camera.xsd b/resources/Schema/Components/Camera.xsd new file mode 100644 index 00000000..1ac8c394 --- /dev/null +++ b/resources/Schema/Components/Camera.xsd @@ -0,0 +1,19 @@ + + + + + + + + It's a camera thingy! + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 78494ce1..e59d8029 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -13,22 +13,10 @@ - - - Models/ScaleWidget.obj - - - - - - - - - - Models/RotationWidget.obj - + + diff --git a/src/Engine/Rendering/DrawScenePass.cpp b/src/Engine/Rendering/DrawScenePass.cpp index 559a0f58..36bf7e81 100644 --- a/src/Engine/Rendering/DrawScenePass.cpp +++ b/src/Engine/Rendering/DrawScenePass.cpp @@ -32,6 +32,7 @@ void DrawScenePass::Draw(RenderQueueCollection& rq) DrawScenePassState state; + rq.Forward.Jobs.sort(DrawScenePass::DepthSort); //TODO: Render: Add code for more jobs than modeljobs. for (auto &job : rq.Forward) { diff --git a/src/Engine/Rendering/DrawScenePassState.cpp b/src/Engine/Rendering/DrawScenePassState.cpp index 59654775..fdfe1802 100644 --- a/src/Engine/Rendering/DrawScenePassState.cpp +++ b/src/Engine/Rendering/DrawScenePassState.cpp @@ -8,6 +8,8 @@ DrawScenePassState::DrawScenePassState() GLERROR("---"); Enable(GL_DEPTH_TEST); Enable(GL_CULL_FACE); + Enable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); ClearColor(glm::vec4(255.f / 255, 163.f / 255, 176.f / 255, 0.f)); Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 962c278f..3e7c472c 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -78,7 +78,12 @@ RawModel::RawModel(std::string fileName) // Material diffuse color aiColor4D diffuse; material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse); - desc.DiffuseVertexColor = glm::vec4(diffuse.r, diffuse.g, diffuse.b, diffuse.a); + + float opacity; + material->Get(AI_MATKEY_OPACITY, opacity); + + desc.DiffuseVertexColor = glm::vec4(diffuse.r, diffuse.g, diffuse.b, opacity); + // Material specular color aiColor4D specular; material->Get(AI_MATKEY_COLOR_SPECULAR, specular); @@ -132,6 +137,7 @@ RawModel::RawModel(std::string fileName) matGroup.EndIndex = m_Indices.size() - 1; // Material shininess material->Get(AI_MATKEY_SHININESS, matGroup.Shininess); + material->Get(AI_MATKEY_OPACITY, matGroup.Transparency); //LOG_DEBUG("Shininess: %f", matGroup.Shininess); // Diffuse texture //LOG_DEBUG("%i diffuse textures found", material->GetTextureCount(aiTextureType_DIFFUSE)); diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 4b82647c..77088fc0 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -1,9 +1,10 @@ #include "Rendering/RenderQueueFactory.h" -RenderQueueFactory::RenderQueueFactory() +RenderQueueFactory::RenderQueueFactory(EventBroker* eventBroker) { m_RenderQueues = RenderQueueCollection(); + m_EventBroker = eventBroker; } void RenderQueueFactory::Update(World* world) @@ -79,21 +80,44 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) Model* model = ResourceManager::Load(resource); for (auto texGroup : model->TextureGroups) { - ModelJob job; - job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; - job.DiffuseTexture = texGroup.Texture.get(); - job.NormalTexture = texGroup.NormalMap.get(); - job.SpecularTexture = texGroup.SpecularMap.get(); - job.Model = model; - job.StartIndex = texGroup.StartIndex; - job.EndIndex = texGroup.EndIndex; - job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); - job.Color = color; - //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this - job.Entity = modelC.EntityID; + if (color.a < 1.0f || texGroup.Transparency < 1.0f) { + //transparent stuffs + TransparentModelJob job; + job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; + job.DiffuseTexture = texGroup.Texture.get(); + job.NormalTexture = texGroup.NormalMap.get(); + job.SpecularTexture = texGroup.SpecularMap.get(); + job.Model = model; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); + job.Color = color; - renderQueue->Add(job); + job.Entity = modelC.EntityID; + job.Depth = 10.f; //insert real viewspace depth here + + renderQueue->Add(job); + } else { + ModelJob job; + job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; + job.DiffuseTexture = texGroup.Texture.get(); + job.NormalTexture = texGroup.NormalMap.get(); + job.SpecularTexture = texGroup.SpecularMap.get(); + job.Model = model; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); + job.Color = color; + + //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this + job.Entity = modelC.EntityID; + + renderQueue->Add(job); + } + + + } } } diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index c4fa33c4..13538dfb 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -10,6 +10,7 @@ void Renderer::Initialize() if (m_Camera == nullptr) { m_Camera = m_DefaultCamera; } + TEMPCreateLights(); InitializeRenderPasses(); @@ -88,36 +89,6 @@ void Renderer::InitializeShaders() void Renderer::InputUpdate(double dt) { static DebugCameraInputController firstPersonInputController(m_EventBroker, -1); - - glm::vec3 m_Position = m_Camera->Position(); - if (glfwGetKey(m_Window, GLFW_KEY_O) == GLFW_PRESS) - { - m_Position = glm::vec3(0.f, 0.f, 5.f); - } - if (glfwGetKey(m_Window, GLFW_KEY_W) == GLFW_PRESS) - { - m_Position += m_Camera->Forward() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_S) == GLFW_PRESS) - { - m_Position -= m_Camera->Forward() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_D) == GLFW_PRESS) - { - m_Position += m_Camera->Right() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_A) == GLFW_PRESS) - { - m_Position -= m_Camera->Right() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) - { - m_CameraMoveSpeed = 5.f; - } - else { - m_CameraMoveSpeed = 0.5f; - } - firstPersonInputController.Update(dt); m_Camera->SetOrientation(firstPersonInputController.Orientation()); m_Camera->SetPosition(firstPersonInputController.Position()); @@ -237,7 +208,6 @@ void Renderer::CalculateFrustum() { GLERROR("CalculateFrustum Error-1"); m_CalculateFrustumProgram->Bind(); - GLERROR("CalculateFrustum Error1"); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_FrustumSSBO); GLERROR("CalculateFrustum Error2"); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index af96eb29..02ef90c8 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -14,10 +14,17 @@ Game::Game(int argc, char* argv[]) // Create the core event broker m_EventBroker = new EventBroker(); - m_RenderQueueFactory = new RenderQueueFactory(); + m_RenderQueueFactory = new RenderQueueFactory(m_EventBroker); + + // Create a world + m_World = new World(); + std::string mapToLoad = m_Config->Get("Debug.LoadMap", ""); + if (!mapToLoad.empty()) { + ResourceManager::Load(mapToLoad)->PopulateWorld(m_World); + } // Create the renderer - m_Renderer = new Renderer(m_EventBroker); + m_Renderer = new Renderer(m_EventBroker, m_World); m_Renderer->SetFullscreen(m_Config->Get("Video.Fullscreen", false)); m_Renderer->SetVSYNC(m_Config->Get("Video.VSYNC", false)); m_Renderer->SetResolution(Rectangle( @@ -27,7 +34,7 @@ Game::Game(int argc, char* argv[]) m_Config->Get("Video.Height", 720) )); m_Renderer->Initialize(); - m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); + //m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); // Create input manager m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); @@ -41,12 +48,7 @@ Game::Game(int argc, char* argv[]) m_FrameStack->Width = m_Renderer->Resolution().Width; m_FrameStack->Height = m_Renderer->Resolution().Height; - // Create a world - m_World = new World(); - std::string mapToLoad = m_Config->Get("Debug.LoadMap", ""); - if (!mapToLoad.empty()) { - ResourceManager::Load(mapToLoad)->PopulateWorld(m_World); - } + // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker);