Changed the structure of how RenderJobs are handled

This commit is contained in:
viktorljung
2016-01-11 17:01:17 +01:00
parent b8db80bc40
commit 3feee7fd35
7 changed files with 121 additions and 141 deletions
+57
View File
@@ -0,0 +1,57 @@
#ifndef ModelJob_h__
#define ModelJob_h__
#include <cstdint>
#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
+32
View File
@@ -0,0 +1,32 @@
#ifndef RenderJob_h__
#define RenderJob_h__
#include <cstdint>
#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
+11 -96
View File
@@ -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 <typename T>
void Add(T &job)
{
job.CalculateHash();
Jobs.push_back(std::shared_ptr<T>(new T(job)));
m_Size++;
}
void Sort()
{
Jobs.sort();
}
void Clear()
{
Jobs.clear();
m_Size = 0;
}
int Size() const { return m_Size; }
std::list<std::shared_ptr<RenderJob>>::const_iterator begin()
{
return Jobs.begin();
}
std::list<std::shared_ptr<RenderJob>>::const_iterator end()
{
return Jobs.end();
}
std::list<std::shared_ptr<RenderJob>> Jobs;
private:
int m_Size = 0;
};
*/
struct RenderScene
{
RenderQueue Forward;
RenderQueue Lights;
Camera* Camera;
::Camera* Camera;
std::list<std::shared_ptr<RenderJob>> ForwardJobs;
std::list<std::shared_ptr<RenderJob>> LightJobs;
void Clear()
{
Forward.Clear();
Lights.Clear();
}
void Sort()
{
Forward.Sort();
Lights.Sort();
ForwardJobs.clear();
LightJobs.clear();
}
};
class RenderFrame
struct RenderFrame
{
public:
+2 -1
View File
@@ -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<std::shared_ptr<RenderJob>>& jobs);
EventRelay<RenderSystem, Events::InputCommand> m_EInputCommand;
bool OnInputCommand(const Events::InputCommand& e);
+1 -2
View File
@@ -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<ModelJob>(job);
if (modelJob) {
GLuint ShaderHandle = m_BasicForwardProgram->GetHandle();
+1 -1
View File
@@ -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<ModelJob>(job);
if (modelJob) {
+17 -41
View File
@@ -129,63 +129,39 @@ glm::mat4 RenderSystem::ModelMatrix(EntityID entity)
return modelMatrix;
}
void RenderSystem::FillModels(RenderQueue* renderQueue)
void RenderSystem::FillModels(std::list<std::shared_ptr<RenderJob>>& 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<Model>(resource);
Model* model = ResourceManager::Load<::Model>(resource);
if (model == nullptr) {
model = ResourceManager::Load<Model>("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> modelJob = std::shared_ptr<ModelJob>(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;
}