Added Transform.h that holds all the AbsoluteTransformation functions. Camera now working better with scenes. Changed Modeljob Matrix to only hold the model matrix instad of all matrix info

This commit is contained in:
viktorljung
2016-01-14 11:12:02 +01:00
parent 0f17f9a888
commit e7f02f14a0
16 changed files with 135 additions and 133 deletions
+62
View File
@@ -0,0 +1,62 @@
#ifndef Transform_h__
#define Transform_h__
#include "../GLM.h"
#include "World.h"
static class Transform
{
public:
static glm::vec3 AbsolutePosition(World* world, EntityID entity)
{
glm::vec3 position;
while (entity != EntityID_Invalid) {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
EntityID parent = world->GetParent(entity);
position += AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"];
entity = parent;
}
return position;
};
static glm::quat AbsoluteOrientation(World* world, EntityID entity)
{
glm::quat orientation;
while (entity != EntityID_Invalid) {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation;
entity = world->GetParent(entity);
}
return orientation;
};
static glm::vec3 AbsoluteScale(World* world, EntityID entity)
{
glm::vec3 scale(1.f);
while (entity != EntityID_Invalid) {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
scale *= (glm::vec3)transform["Scale"];
entity = world->GetParent(entity);
}
return scale;
};
static glm::mat4 ModelMatrix(EntityID entity, World* world)
{
glm::vec3 position = Transform::AbsolutePosition(world, entity);
glm::quat orientation = Transform::AbsoluteOrientation(world, entity);
glm::vec3 scale = Transform::AbsoluteScale(world, entity);
glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale);
return modelMatrix;
}
};
#endif
+1 -1
View File
@@ -9,7 +9,7 @@
#include "../Core/ConfigFile.h"
#include "../Input/EInputCommand.h"
#include "../Rendering/IRenderer.h"
#include "../Rendering/RenderSystem.h"
#include "../Core/Transform.h"
#include "../Core/EFileDropped.h"
#include "../Core/EntityFilePreprocessor.h"
#include "../Core/EntityFileParser.h"
+1 -1
View File
@@ -17,7 +17,7 @@ public:
void InitializeFrameBuffers();
void InitializeShaderPrograms();
void Draw(RenderFrame& rf);
void Draw(RenderScene& scene);
//Getters
+1 -1
View File
@@ -19,7 +19,7 @@ public:
void InitializeFrameBuffers();
void InitializeShaderPrograms();
void Draw(RenderFrame& rf);
void Draw(RenderScene& scene);
//Getters
+1 -1
View File
@@ -52,7 +52,7 @@ struct RenderScene
::Camera* Camera;
std::list<std::shared_ptr<RenderJob>> ForwardJobs;
std::list<std::shared_ptr<RenderJob>> LightJobs;
Rectangle ViewPort;
Rectangle Viewport;
void Clear()
{
+1 -8
View File
@@ -13,6 +13,7 @@
#include "Camera.h"
#include "ModelJob.h"
#include "Renderer.h"
#include "../Core/Transform.h"
class RenderSystem : public ImpureSystem
{
@@ -21,12 +22,6 @@ public:
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:
World* m_World = nullptr;
const IRenderer* m_Renderer = nullptr;
@@ -44,13 +39,11 @@ private:
void switchCamera(EntityID entity);
void initialize();
void updateCamera(World* world, double dt);
void updateProjectionMatrix(ComponentWrapper& cameraComponent);
glm::mat4 m_ViewMatrix;
glm::mat4 m_ProjectionMatrix;
glm::mat4 ModelMatrix(EntityID entity, World* world);
void fillModels(std::list<std::shared_ptr<RenderJob>>& jobs, World* world);
EventRelay<RenderSystem, Events::InputCommand> m_EInputCommand;
+1 -1
View File
@@ -26,7 +26,7 @@ public:
virtual void Initialize() override;
virtual void Update(double dt) override;
virtual void Draw(RenderFrame& rf) override;
virtual void Draw(RenderFrame& frame) override;
virtual PickData Pick(glm::vec2 screenCoord) override;
-1
View File
@@ -1,6 +1,5 @@
#version 430
uniform mat4 Matrix;
uniform vec4 Color;
uniform sampler2D texture0;
+4 -2
View File
@@ -1,6 +1,8 @@
#version 430
uniform mat4 Matrix;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
@@ -23,7 +25,7 @@ out VertexData{
void main()
{
gl_Position = Matrix * vec4(Position, 1.0);
gl_Position = P * V * M * vec4(Position, 1.0);
Output.Position = Position;
Output.TextureCoordinate = TextureCoords;
-3
View File
@@ -1,8 +1,5 @@
#version 430
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform vec2 PickingColor;
in VertexData{
+4 -2
View File
@@ -1,6 +1,8 @@
#version 430
uniform mat4 Matrix;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
@@ -20,7 +22,7 @@ out VertexData{
void main()
{
gl_Position = Matrix * vec4(Position, 1.0);
gl_Position = P * V* M * vec4(Position, 1.0);
Output.Position = Position;
}
+7 -7
View File
@@ -159,7 +159,7 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e)
EntityID parent = m_World->GetParent(m_Selection);
glm::quat inverseParentOrientation;
//if (parent != 0) {
inverseParentOrientation = glm::inverse(RenderSystem::AbsoluteOrientation(m_World, parent));
inverseParentOrientation = glm::inverse(Transform::AbsoluteOrientation(m_World, parent));
//}
(glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Position"] += inverseParentOrientation * movement;
} else if (m_WidgetSpace == WidgetSpace::Local) {
@@ -178,7 +178,7 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e)
// parentOrientation = RenderSystem::AbsoluteOrientation(m_World, parent);
//}
glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"];
glm::quat currentOrientation = RenderSystem::AbsoluteOrientation(m_World, m_Selection);
glm::quat currentOrientation = Transform::AbsoluteOrientation(m_World, m_Selection);
//glm::quat currentOrientation = parentOrientation * glm::quat(selectionOrientation);
glm::quat deltaOrientation(finalMovement);
selectionOrientation = glm::eulerAngles(glm::inverse(parentOrientation) * (deltaOrientation * currentOrientation));
@@ -321,10 +321,10 @@ void EditorSystem::updateWidget()
if (m_Selection != EntityID_Invalid) {
auto widgetTransform = m_World->GetComponent(m_Widget, "Transform");
glm::vec3 selectionPosition = RenderSystem::AbsolutePosition(m_World, m_Selection);
glm::vec3 selectionPosition = Transform::AbsolutePosition(m_World, m_Selection);
widgetTransform["Position"] = selectionPosition;
if (m_WidgetSpace == WidgetSpace::Local) {
widgetTransform["Orientation"] = glm::eulerAngles(RenderSystem::AbsoluteOrientation(m_World, m_Selection));
widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection));
}
}
}
@@ -359,7 +359,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode)
if (m_Selection != EntityID_Invalid) {
if (m_WidgetSpace == WidgetSpace::Local) {
auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
widgetTransform["Orientation"] = glm::eulerAngles(RenderSystem::AbsoluteOrientation(m_World, m_Selection));
widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection));
}
}
} else if (newMode == WidgetMode::Scale) {
@@ -370,7 +370,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode)
m_World->GetComponent(m_WidgetOrigin, "Model")["Resource"] = "Models/ScaleWidgetOrigin.obj";
if (m_Selection != EntityID_Invalid) {
auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
widgetTransform["Orientation"] = glm::eulerAngles(RenderSystem::AbsoluteOrientation(m_World, m_Selection));
widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection));
}
} else if (newMode == WidgetMode::Rotate) {
m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/RotationWidgetX.obj";
@@ -379,7 +379,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode)
if (m_Selection != EntityID_Invalid) {
auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
if (m_WidgetSpace == WidgetSpace::Local) {
widgetTransform["Orientation"] = glm::eulerAngles(RenderSystem::AbsoluteOrientation(m_World, m_Selection));
widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection));
}
}
}
+25 -24
View File
@@ -23,40 +23,41 @@ void DrawScenePass::InitializeShaderPrograms()
m_BasicForwardProgram->Link();
}
void DrawScenePass::Draw(RenderFrame& rf)
void DrawScenePass::Draw(RenderScene& scene)
{
//glBindFramebuffer(GL_FRAMEBUFFER, 0);
GLERROR("Renderer::Draw PickingPass");
DrawScenePassState state;
for (auto scene : rf.RenderScenes) {
for (auto &job : scene->ForwardJobs) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob) {
GLuint ShaderHandle = m_BasicForwardProgram->GetHandle();
for (auto &job : scene.ForwardJobs) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(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, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix()));
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);
}
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;
//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;
}
}
GLERROR("DrawScene Error");
}
+8 -7
View File
@@ -43,7 +43,7 @@ void PickingPass::InitializeShaderPrograms()
m_PickingProgram->Link();
}
void PickingPass::Draw(RenderFrame& rf)
void PickingPass::Draw(RenderScene& scene)
{
m_PickingColorsToEntity.clear();
PickingPassState* state = new PickingPassState(m_PickingBuffer.GetHandle());
@@ -57,11 +57,9 @@ void PickingPass::Draw(RenderFrame& rf)
std::map<EntityID, glm::vec2> entityColors;
for(auto scene : rf.RenderScenes)
{
m_Camera = scene->Camera;
m_Camera = scene.Camera;
for (auto &job : scene->ForwardJobs) {
for (auto &job : scene.ForwardJobs) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob) {
@@ -81,7 +79,10 @@ void PickingPass::Draw(RenderFrame& rf)
}
m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity;
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix()));
glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1])));
glBindVertexArray(modelJob->Model->VAO);
@@ -89,7 +90,7 @@ void PickingPass::Draw(RenderFrame& rf)
glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, nullptr, modelJob->StartIndex);
}
}
}
m_PickingBuffer.Unbind();
GLERROR("PickingPass Error");
+9 -67
View File
@@ -5,7 +5,6 @@ RenderSystem::RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer
{
m_Renderer = renderer;
m_RenderFrame = renderFrame;
initialize();
EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand);
@@ -52,11 +51,6 @@ void RenderSystem::switchCamera(EntityID entity)
}
}
void RenderSystem::initialize()
{
}
void RenderSystem::updateProjectionMatrix(ComponentWrapper& cameraComponent)
{
double fov = cameraComponent["FOV"];
@@ -74,56 +68,6 @@ void RenderSystem::updateProjectionMatrix(ComponentWrapper& cameraComponent)
m_Camera->UpdateProjectionMatrix();
}
glm::vec3 RenderSystem::AbsolutePosition(World* world, EntityID entity)
{
glm::vec3 position;
while (entity != EntityID_Invalid) {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
EntityID parent = world->GetParent(entity);
position += AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"];
entity = parent;
}
return position;
}
glm::quat RenderSystem::AbsoluteOrientation(World* world, EntityID entity)
{
glm::quat orientation;
while (entity != EntityID_Invalid) {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation;
entity = world->GetParent(entity);
}
return orientation;
}
glm::vec3 RenderSystem::AbsoluteScale(World* world, EntityID entity)
{
glm::vec3 scale(1.f);
while (entity != EntityID_Invalid) {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
scale *= (glm::vec3)transform["Scale"];
entity = world->GetParent(entity);
}
return scale;
}
glm::mat4 RenderSystem::ModelMatrix(EntityID entity, World* world)
{
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(std::list<std::shared_ptr<RenderJob>>& jobs, World* world)
{
auto models = world->GetComponents("Model");
@@ -146,11 +90,10 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& jobs, World
model = ResourceManager::Load<::Model>("Models/Core/Error.obj");
}
glm::mat4 modelMatrix = ModelMatrix(modelComponent.EntityID, world);
glm::mat4 matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * modelMatrix);
glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world);
for (auto texGroup : model->TextureGroups) {
std::shared_ptr<ModelJob> modelJob = std::shared_ptr<ModelJob>(new ModelJob(model, m_Camera, matrix, texGroup, modelComponent));
std::shared_ptr<ModelJob> modelJob = std::shared_ptr<ModelJob>(new ModelJob(model, m_Camera, modelMatrix, texGroup, modelComponent));
jobs.push_back(modelJob);
}
}
@@ -175,12 +118,11 @@ void RenderSystem::Update(World* world, double dt)
//Only supports opaque geometry atm
m_RenderFrame->Clear();
RenderScene* rs = new RenderScene();
rs->Camera = m_Camera;
rs->ViewPort = Rectangle(1280, 720);
fillModels(rs->ForwardJobs, world);
m_RenderFrame->Add(*rs);
delete rs;
RenderScene rs;
rs.Camera = m_Camera;
rs.Viewport = Rectangle(1280, 720);
fillModels(rs.ForwardJobs, world);
m_RenderFrame->Add(rs);
}
void RenderSystem::updateCamera(World* world, double dt)
@@ -218,8 +160,8 @@ void RenderSystem::updateCamera(World* world, double 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);
glm::vec3 position = Transform::AbsolutePosition(world, m_CurrentCamera);
glm::quat orientation = Transform::AbsoluteOrientation(world, m_CurrentCamera);
m_Camera->SetPosition(position);
m_Camera->SetOrientation(orientation);
+10 -7
View File
@@ -89,15 +89,18 @@ void Renderer::Update(double dt)
m_ImGuiRenderPass->Update(dt);
}
void Renderer::Draw(RenderFrame& rf)
void Renderer::Draw(RenderFrame& frame)
{
m_Camera = (*rf.begin())->Camera; // Fix this with some better solution
m_PickingPass->Draw(rf);
glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 1.f);
for (auto scene : frame.RenderScenes){
m_Camera = scene->Camera; // remove renderer camera when Editor uses the render scene cameras.
m_PickingPass->Draw(*scene);
glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 1.f);
m_DrawScenePass->Draw(*scene);
GLERROR("Renderer::Draw m_DrawScenePass->Draw");
}
m_DrawScenePass->Draw(rf);
GLERROR("Renderer::Draw m_DrawScenePass->Draw");
m_ImGuiRenderPass->Draw();
glfwSwapBuffers(m_Window);
}