Added RenderSystem and removed RenderQueueFactory

This commit is contained in:
viktorljung
2015-12-14 20:07:02 +01:00
parent ef07a9d536
commit d9ea0192d1
6 changed files with 51 additions and 57 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
#include "../Core/ConfigFile.h"
#include "../Input/EInputCommand.h"
#include "../Rendering/EPicking.h"
#include "../Rendering/RenderQueueFactory.h"
#include "../Rendering/RenderSystem.h"
class EditorSystem : public ImpureSystem
{
+140
View File
@@ -0,0 +1,140 @@
#include "RenderSystem.h"
RenderSystem::RenderSystem(EventBroker* eventBrokerer, RenderQueueCollection* renderQueues)
:ImpureSystem(eventBrokerer)
{
m_RenderQueues = renderQueues;
Initialize();
}
void RenderSystem::Initialize()
{
}
glm::vec3 RenderSystem::AbsolutePosition(World* world, EntityID entity)
{
glm::vec3 position;
do {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
EntityID parent = world->GetParent(entity);
if (parent != 0) {
position += AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"];
} else {
position += (glm::vec3)transform["Position"];
}
entity = parent;
} while (entity != 0);
return position;
}
glm::quat RenderSystem::AbsoluteOrientation(World* world, EntityID entity)
{
glm::quat orientation;
do {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation;
entity = world->GetParent(entity);
} while (entity != 0);
return orientation;
}
glm::vec3 RenderSystem::AbsoluteScale(World* world, EntityID entity)
{
glm::vec3 scale(1.f);
do {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
scale *= (glm::vec3)transform["Scale"];
entity = world->GetParent(entity);
} while (entity != 0);
return scale;
}
glm::mat4 RenderSystem::ModelMatrix(World* world, EntityID entity)
{
glm::vec3 position = AbsolutePosition(world, entity);
glm::quat orientation = AbsoluteOrientation(world, entity);
glm::vec3 scale = AbsoluteScale(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)
{
auto models = world->GetComponents("Model");
if (models == nullptr) {
return;
}
for (auto& modelC : *models) {
bool visible = modelC["Visible"];
if (!visible) {
continue;
}
std::string resource = modelC["Resource"];
if (resource.empty()) {
continue;
}
glm::vec4 color = modelC["Color"];
Model* model = ResourceManager::Load<Model>(resource);
if (model == nullptr) {
model = ResourceManager::Load<Model>("Models/Core/Error.obj");
}
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.ModelMatrix = model->m_Matrix * ModelMatrix(world, 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.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);
}
}
}
}
void RenderSystem::Update(World* world, double dt)
{
m_RenderQueues->Clear();
FillModels(world, &m_RenderQueues->Forward);
}
@@ -1,37 +1,33 @@
#ifndef RenderQueueFactory_h__
#define RenderQueueFactory_h__
#ifndef RenderSystem_h__
#define RenderSystem_h__
#include "../Core/World.h"
#include "../Core/System.h"
#include "RenderQueue.h"
#include "../Core/ResourceManager.h"
#include "Model.h"
#include "../GLM.h"
#include "../Core/EventBroker.h"
#include "../OpenGL.h"
#include "../Core/ResourceManager.h"
#include "ESetCamera.h"
#include "Model.h"
class RenderQueueFactory
class RenderSystem : public ImpureSystem
{
public:
RenderQueueFactory(EventBroker* eventBroker);
void Update(World* world);
RenderQueueCollection RenderQueues() const { return m_RenderQueues; }
RenderSystem(EventBroker* eventBrokerer, RenderQueueCollection* renderQueues);
virtual void Update(World* world, double dt) override;
static glm::vec3 AbsolutePosition(World* world, EntityID entity);
static glm::quat AbsoluteOrientation(World* world, EntityID entity);
static glm::vec3 AbsoluteScale(World* world, EntityID entity);
private:
EventBroker* m_EventBroker;
RenderQueueCollection m_RenderQueues;
void FillModels(World* world, RenderQueue* renderQueue);
void FillLights(World* world, RenderQueue* renderQueue);
RenderQueueCollection* m_RenderQueues;
void Initialize();
glm::mat4 ModelMatrix(World* world, EntityID entity);
EntityID m_CurrentCamera;
void FillModels(World* world, RenderQueue* renderQueue);
};
#endif