diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h new file mode 100644 index 00000000..fcb68244 --- /dev/null +++ b/include/Engine/Rendering/ModelJob.h @@ -0,0 +1,57 @@ +#ifndef ModelJob_h__ +#define ModelJob_h__ + +#include + +#include "../Common.h" +#include "../GLM.h" +#include "../Core/ComponentWrapper.h" +#include "Texture.h" +#include "Model.h" + +#include "RenderJob.h" +#include "../Core/ResourceManager.h" +#include "Camera.h" + +struct ModelJob : RenderJob +{ + ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::Model::MaterialGroup texGroup, ComponentWrapper modelComponent) + : RenderJob() + { + Model = model; + TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; + DiffuseTexture = texGroup.Texture.get(); + NormalTexture = texGroup.NormalMap.get(); + SpecularTexture = texGroup.SpecularMap.get(); + StartIndex = texGroup.StartIndex; + EndIndex = texGroup.EndIndex; + Matrix = matrix; + Color = modelComponent["Color"]; + Entity = modelComponent.EntityID; + }; + + + unsigned int TextureID; + unsigned int ShaderID; + + EntityID Entity; + glm::mat4 Matrix; + 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; + + + void CalculateHash() override + { + Hash = TextureID; + } + + +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderJob.h b/include/Engine/Rendering/RenderJob.h new file mode 100644 index 00000000..4afe0386 --- /dev/null +++ b/include/Engine/Rendering/RenderJob.h @@ -0,0 +1,32 @@ +#ifndef RenderJob_h__ +#define RenderJob_h__ + +#include + +#include "../Common.h" +#include "../GLM.h" +#include "../Core/ComponentWrapper.h" +#include "RenderQueue.h" + + +struct RenderJob +{ + friend class RenderQueue; + +public: + + float Depth; + +protected: + uint64_t Hash; + + virtual void CalculateHash() = 0; + + bool operator<(const RenderJob& rhs) + { + return this->Hash < rhs.Hash; + } + +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 719e5cc7..01e9024a 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -9,58 +9,16 @@ #include "../Core/Util/Rectangle.h" #include "../Core/Entity.h" #include "Camera.h" +#include "RenderJob.h" +#include "ModelJob.h" class Model; class Skeleton; class Texture; -class RenderQueue; //TODO: Render: Remove obsolete RenderJobs and fix standard values on variables. +/* -struct RenderJob -{ - friend class RenderQueue; - - float Depth; - -protected: - uint64_t Hash; - - virtual void CalculateHash() = 0; - - bool operator<(const RenderJob& rhs) - { - return this->Hash < rhs.Hash; - } -}; - -struct ModelJob : RenderJob -{ - unsigned int ShaderID = 0; - unsigned int TextureID = 0; - - EntityID Entity; - glm::mat4 Matrix; - 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 TransparentModelJob : RenderJob { @@ -123,66 +81,23 @@ struct PointLightJob : RenderJob Hash = 0; } }; - -class RenderQueue -{ -public: - template - void Add(T &job) - { - job.CalculateHash(); - Jobs.push_back(std::shared_ptr(new T(job))); - m_Size++; - } - - void Sort() - { - Jobs.sort(); - } - - void Clear() - { - Jobs.clear(); - m_Size = 0; - } - - int Size() const { return m_Size; } - std::list>::const_iterator begin() - { - return Jobs.begin(); - } - - std::list>::const_iterator end() - { - return Jobs.end(); - } - - std::list> Jobs; - -private: - int m_Size = 0; -}; +*/ struct RenderScene { - RenderQueue Forward; - RenderQueue Lights; - Camera* Camera; + ::Camera* Camera; + std::list> ForwardJobs; + std::list> LightJobs; + void Clear() { - Forward.Clear(); - Lights.Clear(); - } - - void Sort() - { - Forward.Sort(); - Lights.Sort(); + ForwardJobs.clear(); + LightJobs.clear(); } }; -class RenderFrame +struct RenderFrame { public: diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index 34a9c5f9..e322d51e 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -11,6 +11,7 @@ #include "../Core/EKeyDown.h" #include "../Input/EInputCommand.h" #include "Camera.h" +#include "ModelJob.h" class RenderSystem : public ImpureSystem @@ -48,7 +49,7 @@ private: glm::mat4 m_ProjectionMatrix; glm::mat4 ModelMatrix(EntityID entity); - void FillModels(RenderQueue* renderQueue); + void FillModels(std::list>& jobs); EventRelay m_EInputCommand; bool OnInputCommand(const Events::InputCommand& e); diff --git a/src/Engine/Rendering/DrawScenePass.cpp b/src/Engine/Rendering/DrawScenePass.cpp index dd34072f..5da2c0c6 100644 --- a/src/Engine/Rendering/DrawScenePass.cpp +++ b/src/Engine/Rendering/DrawScenePass.cpp @@ -32,9 +32,8 @@ void DrawScenePass::Draw(RenderFrame& rf) DrawScenePassState state; for (auto scene : rf.RenderScenes) { - scene->Forward.Jobs.sort(DrawScenePass::DepthSort); - for (auto &job : scene->Forward) { + for (auto &job : scene->ForwardJobs) { auto modelJob = std::dynamic_pointer_cast(job); if (modelJob) { GLuint ShaderHandle = m_BasicForwardProgram->GetHandle(); diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index fbbbd7c0..a09f328e 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -61,7 +61,7 @@ void PickingPass::Draw(RenderFrame& rf) { m_Camera = scene->Camera; - for (auto &job : scene->Forward) { + for (auto &job : scene->ForwardJobs) { auto modelJob = std::dynamic_pointer_cast(job); if (modelJob) { diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index f385037b..8e5f2a34 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -129,63 +129,39 @@ glm::mat4 RenderSystem::ModelMatrix(EntityID entity) return modelMatrix; } -void RenderSystem::FillModels(RenderQueue* renderQueue) +void RenderSystem::FillModels(std::list>& jobs) { auto models = m_World->GetComponents("Model"); if (models == nullptr) { return; } - for (auto& modelC : *models) { - bool visible = modelC["Visible"]; + for (auto& modelComponent : *models) { + bool visible = modelComponent["Visible"]; if (!visible) { continue; } - std::string resource = modelC["Resource"]; + std::string resource = modelComponent["Resource"]; if (resource.empty()) { continue; } - glm::vec4 color = modelC["Color"]; - Model* model = ResourceManager::Load(resource); + + + Model* model = ResourceManager::Load<::Model>(resource); if (model == nullptr) { - model = ResourceManager::Load("Models/Core/Error.obj"); + model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); } + + glm::mat4 modelMatrix = ModelMatrix(modelComponent.EntityID); + glm::mat4 matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * modelMatrix); for (auto texGroup : model->TextureGroups) { - - 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.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID)); - job.Color = color; - 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.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID)); - job.Color = color; - job.Entity = modelC.EntityID; - - renderQueue->Add(job); - } + std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, matrix, texGroup, modelComponent)); + jobs.push_back(modelJob); } + + + } } @@ -261,7 +237,7 @@ void RenderSystem::Update(World* world, double dt) m_RenderFrame->Clear(); RenderScene* rs = new RenderScene(); rs->Camera = m_Camera; - FillModels(&rs->Forward); + FillModels(rs->ForwardJobs); m_RenderFrame->Add(*rs); delete rs; }