Camera component and RenderQueue changes

This commit is contained in:
viktorljung
2016-01-08 16:21:59 +01:00
parent bd82f75dd0
commit a7b43b06a2
23 changed files with 199 additions and 137 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ public:
m_TexturePressed = resourceName; m_TexturePressed = resourceName;
} }
void Draw(RenderQueueCollection& rq) override void Draw(RenderScene& rq) override
{ {
if (m_Texture == nullptr && !m_TextureReleased.empty()) { if (m_Texture == nullptr && !m_TextureReleased.empty()) {
SetTexture(m_TextureReleased); SetTexture(m_TextureReleased);
+2 -2
View File
@@ -212,7 +212,7 @@ public:
virtual void Update(double dt) { } virtual void Update(double dt) { }
void DrawLayered(RenderQueueCollection& rq) void DrawLayered(RenderScene& rq)
{ {
if (this->Hidden()) if (this->Hidden())
return; return;
@@ -232,7 +232,7 @@ public:
} }
} }
virtual void Draw(RenderQueueCollection& rq) { } virtual void Draw(RenderScene& rq) { }
protected: protected:
::EventBroker* m_EventBroker; ::EventBroker* m_EventBroker;
+1 -1
View File
@@ -16,7 +16,7 @@ public:
void EnableScissor() { m_ScissorEnabled = true; } void EnableScissor() { m_ScissorEnabled = true; }
void DisableScissor() { m_ScissorEnabled = false; } void DisableScissor() { m_ScissorEnabled = false; }
void Draw(RenderQueueCollection& rq) override void Draw(RenderScene& rq) override
{ {
if (m_Texture == nullptr) if (m_Texture == nullptr)
return; return;
+2 -8
View File
@@ -26,11 +26,6 @@ public:
glm::quat Orientation() const { return m_Orientation; } glm::quat Orientation() const { return m_Orientation; }
void SetOrientation(glm::quat val); void SetOrientation(glm::quat val);
/*float Pitch() const { return m_Pitch; }
void Pitch(float val);
float Yaw() const { return m_Yaw; }
void Yaw(float val);*/
glm::mat4 ProjectionMatrix() const { return m_ProjectionMatrix; } glm::mat4 ProjectionMatrix() const { return m_ProjectionMatrix; }
glm::mat4 ViewMatrix() const { return m_ViewMatrix; } glm::mat4 ViewMatrix() const { return m_ViewMatrix; }
@@ -46,11 +41,10 @@ public:
float FarClip() const { return m_FarClip; } float FarClip() const { return m_FarClip; }
void SetFarClip(float val); void SetFarClip(float val);
void UpdateViewMatrix();
void UpdateProjectionMatrix();
private: private:
void UpdateViewMatrix();
void UpdateProjectionMatrix();
glm::vec3 m_Position; glm::vec3 m_Position;
glm::quat m_Orientation; glm::quat m_Orientation;
+1 -1
View File
@@ -17,7 +17,7 @@ public:
void InitializeFrameBuffers(); void InitializeFrameBuffers();
void InitializeShaderPrograms(); void InitializeShaderPrograms();
void Draw(RenderQueueCollection& rq); void Draw(RenderFrame& rf);
//Getters //Getters
+1 -1
View File
@@ -9,7 +9,7 @@ class DummyRenderer : public IRenderer
{ {
public: public:
virtual void Initialize() override; virtual void Initialize() override;
virtual void Draw(RenderQueueCollection& rq) override; virtual void Draw(RenderFrame& rq) override;
}; };
#endif #endif
+2 -2
View File
@@ -3,16 +3,16 @@
#include "../Core/EventBroker.h" #include "../Core/EventBroker.h"
#include "../Core/Entity.h" #include "../Core/Entity.h"
#include <string.h>
namespace Events namespace Events
{ {
/** Thrown Every frame, use functions to pick*/
struct SetCamera : Event struct SetCamera : Event
{ {
public: public:
SetCamera() { }; SetCamera() { };
EntityID Entity; std::string Name;
private: private:
+1 -1
View File
@@ -31,7 +31,7 @@ public:
} }
virtual void Initialize() = 0; virtual void Initialize() = 0;
virtual void Update(double dt) = 0; virtual void Update(double dt) = 0;
virtual void Draw(RenderQueueCollection& rq) = 0; virtual void Draw(RenderFrame& rq) = 0;
protected: protected:
Rectangle m_Resolution = Rectangle(1280, 720); Rectangle m_Resolution = Rectangle(1280, 720);
+1 -1
View File
@@ -18,7 +18,7 @@ public:
void InitializeFrameBuffers(); void InitializeFrameBuffers();
void InitializeShaderPrograms(); void InitializeShaderPrograms();
void Draw(RenderQueueCollection& rq); void Draw(RenderFrame& rf);
//Getters //Getters
+35 -3
View File
@@ -39,9 +39,7 @@ struct ModelJob : RenderJob
unsigned int ShaderID = 0; unsigned int ShaderID = 0;
unsigned int TextureID = 0; unsigned int TextureID = 0;
//TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this
EntityID Entity; EntityID Entity;
glm::mat4 Matrix; glm::mat4 Matrix;
const Texture* DiffuseTexture; const Texture* DiffuseTexture;
const Texture* NormalTexture; const Texture* NormalTexture;
@@ -165,7 +163,7 @@ private:
int m_Size = 0; int m_Size = 0;
}; };
struct RenderQueueCollection struct RenderScene
{ {
RenderQueue Forward; RenderQueue Forward;
RenderQueue Lights; RenderQueue Lights;
@@ -184,4 +182,38 @@ struct RenderQueueCollection
} }
}; };
class RenderFrame
{
public:
void Add(RenderScene &scene)
{
RenderScenes.push_back(std::shared_ptr<RenderScene>(new RenderScene(scene)));
m_Size++;
}
void Clear()
{
RenderScenes.clear();
m_Size = 0;
}
int Size() const { return m_Size; }
std::list<std::shared_ptr<RenderScene>>::const_iterator begin()
{
return RenderScenes.begin();
}
std::list<std::shared_ptr<RenderScene>>::const_iterator end()
{
return RenderScenes.end();
}
std::list<std::shared_ptr<RenderScene>> RenderScenes;
private:
int m_Size = 0;
};
#endif #endif
+10 -5
View File
@@ -12,10 +12,11 @@
#include "../Input/EInputCommand.h" #include "../Input/EInputCommand.h"
#include "Camera.h" #include "Camera.h"
class RenderSystem : public ImpureSystem class RenderSystem : public ImpureSystem
{ {
public: public:
RenderSystem(EventBroker* eventBrokerer, RenderQueueCollection* renderQueues); RenderSystem(EventBroker* eventBrokerer, RenderFrame* renderFrame);
virtual void Update(World* world, double dt) override; virtual void Update(World* world, double dt) override;
@@ -26,9 +27,14 @@ public:
private: private:
RenderQueueCollection* m_RenderQueues; World* m_World = nullptr;
RenderFrame* m_RenderFrame;
bool m_SwitchCamera = false; bool m_SwitchCamera = false;
Camera* m_Camera = nullptr; Camera* m_Camera = nullptr;
Camera* m_DefaultCamera = nullptr;
std::list<ComponentWrapper> m_CameraComponents;
EventRelay<RenderSystem, Events::SetCamera> m_ESetCamera; EventRelay<RenderSystem, Events::SetCamera> m_ESetCamera;
bool OnSetCamera(const Events::SetCamera &event); bool OnSetCamera(const Events::SetCamera &event);
@@ -37,13 +43,12 @@ private:
void SwitchCamera(EntityID entity); void SwitchCamera(EntityID entity);
void Initialize(); void Initialize();
void UpdateViewMatrix(ComponentWrapper& cameraTransform);
void UpdateProjectionMatrix(ComponentWrapper& cameraComponent); void UpdateProjectionMatrix(ComponentWrapper& cameraComponent);
glm::mat4 m_ViewMatrix; glm::mat4 m_ViewMatrix;
glm::mat4 m_ProjectionMatrix; glm::mat4 m_ProjectionMatrix;
glm::mat4 ModelMatrix(World* world, EntityID entity); glm::mat4 ModelMatrix(EntityID entity);
void FillModels(World* world, RenderQueue* renderQueue); void FillModels(RenderQueue* renderQueue);
EventRelay<RenderSystem, Events::InputCommand> m_EInputCommand; EventRelay<RenderSystem, Events::InputCommand> m_EInputCommand;
bool OnInputCommand(const Events::InputCommand& e); bool OnInputCommand(const Events::InputCommand& e);
+1 -1
View File
@@ -27,7 +27,7 @@ public:
virtual void Initialize() override; virtual void Initialize() override;
virtual void Update(double dt) override; virtual void Update(double dt) override;
virtual void Draw(RenderQueueCollection& rq) override; virtual void Draw(RenderFrame& rf) override;
private: private:
//----------------------Variables----------------------// //----------------------Variables----------------------//
+1 -1
View File
@@ -38,7 +38,7 @@ private:
GUI::Frame* m_FrameStack; GUI::Frame* m_FrameStack;
World* m_World; World* m_World;
SystemPipeline* m_SystemPipeline; SystemPipeline* m_SystemPipeline;
RenderQueueCollection* m_RenderQueues; RenderFrame* m_RenderFrame;
EventRelay<Game, Events::InputCommand> m_EInputCommand; EventRelay<Game, Events::InputCommand> m_EInputCommand;
bool debugOnInputCommand(const Events::InputCommand& e); bool debugOnInputCommand(const Events::InputCommand& e);
+1
View File
@@ -1,4 +1,5 @@
<c:Camera> <c:Camera>
<Name>cam</Name>
<AspectRatio>1.77</AspectRatio> <AspectRatio>1.77</AspectRatio>
<FOV>60.0</FOV> <FOV>60.0</FOV>
<NearClip>0.01</NearClip> <NearClip>0.01</NearClip>
+1
View File
@@ -9,6 +9,7 @@
</xs:annotation> </xs:annotation>
<xs:complexType> <xs:complexType>
<xs:all> <xs:all>
<xs:element name="Name" type="t:string" minOccurs="0"/>
<xs:element name="AspectRatio" type="t:double" minOccurs="0"/> <xs:element name="AspectRatio" type="t:double" minOccurs="0"/>
<xs:element name="FOV" type="t:double" minOccurs="0"/> <xs:element name="FOV" type="t:double" minOccurs="0"/>
<xs:element name="NearClip" type="t:double" minOccurs="0"/> <xs:element name="NearClip" type="t:double" minOccurs="0"/>
+2
View File
@@ -18,6 +18,7 @@
<Resource>Models/Camera.obj</Resource> <Resource>Models/Camera.obj</Resource>
</c:Model> </c:Model>
<c:Camera> <c:Camera>
<Name>MainCamera</Name>
</c:Camera> </c:Camera>
</Components> </Components>
</Entity> </Entity>
@@ -30,6 +31,7 @@
<Resource>Models/Camera.obj</Resource> <Resource>Models/Camera.obj</Resource>
</c:Model> </c:Model>
<c:Camera> <c:Camera>
<Name>ActionCamera</Name>
</c:Camera> </c:Camera>
</Components> </Components>
</Entity> </Entity>
-9
View File
@@ -64,15 +64,6 @@ void Camera::SetOrientation(glm::quat val)
void Camera::UpdateProjectionMatrix() void Camera::UpdateProjectionMatrix()
{ {
// m_ProjectionMatrix = glm::ortho(
// -16.f,
// 16.f,
// -9.f,
// 9.f,
// m_NearClip,
// m_FarClip
// );
m_ProjectionMatrix = glm::perspective(m_FOV, m_AspectRatio, m_NearClip, m_FarClip); m_ProjectionMatrix = glm::perspective(m_FOV, m_AspectRatio, m_NearClip, m_FarClip);
} }
+24 -24
View File
@@ -25,40 +25,40 @@ void DrawScenePass::InitializeShaderPrograms()
} }
void DrawScenePass::Draw(RenderQueueCollection& rq) void DrawScenePass::Draw(RenderFrame& rf)
{ {
//glBindFramebuffer(GL_FRAMEBUFFER, 0); //glBindFramebuffer(GL_FRAMEBUFFER, 0);
GLERROR("Renderer::Draw PickingPass"); GLERROR("Renderer::Draw PickingPass");
DrawScenePassState state; DrawScenePassState state;
for (auto scene : rf.RenderScenes) {
scene->Forward.Jobs.sort(DrawScenePass::DepthSort);
rq.Forward.Jobs.sort(DrawScenePass::DepthSort); for (auto &job : scene->Forward) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob) {
GLuint ShaderHandle = m_BasicForwardProgram->GetHandle();
//TODO: Render: Add code for more jobs than modeljobs. m_BasicForwardProgram->Bind();
for (auto &job : rq.Forward) { //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job); glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
if (modelJob) { glUniform4fv(glGetUniformLocation(ShaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color));
GLuint ShaderHandle = m_BasicForwardProgram->GetHandle();
m_BasicForwardProgram->Bind(); //TODO: Renderer: bättre textur felhantering samt fler texturer stöd
//TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms if (modelJob->DiffuseTexture != nullptr) {
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); glActiveTexture(GL_TEXTURE0);
glUniform4fv(glGetUniformLocation(ShaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture);
} else {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture);
}
//TODO: Renderer: bättre textur felhantering samt fler texturer stöd glBindVertexArray(modelJob->Model->VAO);
if (modelJob->DiffuseTexture != nullptr) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer);
glActiveTexture(GL_TEXTURE0); glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex);
glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture);
} else { continue;
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"); GLERROR("DrawScene Error");
+1 -1
View File
@@ -42,7 +42,7 @@ void DummyRenderer::Initialize()
glfwSwapInterval(m_VSYNC); glfwSwapInterval(m_VSYNC);
} }
void DummyRenderer::Draw(RenderQueueCollection& rq) void DummyRenderer::Draw(RenderFrame& rq)
{ {
glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 0.f); glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 0.f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
+30 -26
View File
@@ -43,7 +43,7 @@ void PickingPass::InitializeShaderPrograms()
m_PickingProgram->Link(); m_PickingProgram->Link();
} }
void PickingPass::Draw(RenderQueueCollection& rq) void PickingPass::Draw(RenderFrame& rf)
{ {
m_PickingColorsToEntity.clear(); m_PickingColorsToEntity.clear();
PickingPassState* state = new PickingPassState(m_PickingBuffer.GetHandle()); PickingPassState* state = new PickingPassState(m_PickingBuffer.GetHandle());
@@ -57,34 +57,38 @@ void PickingPass::Draw(RenderQueueCollection& rq)
std::map<EntityID, glm::vec2> entityColors; std::map<EntityID, glm::vec2> entityColors;
for (auto &job : rq.Forward) { for(auto scene : rf.RenderScenes)
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job); {
for (auto &job : scene->Forward) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob) { if (modelJob) {
int pickColor[2] = { r, g }; int pickColor[2] = { r, g };
auto color = entityColors.find(modelJob->Entity); auto color = entityColors.find(modelJob->Entity);
if (color != entityColors.end()) { if (color != entityColors.end()) {
pickColor[0] = color->second[0]; pickColor[0] = color->second[0];
pickColor[1] = color->second[1]; pickColor[1] = color->second[1];
} else {
entityColors[modelJob->Entity] = glm::vec2(pickColor[0], pickColor[1]);
if (r + 10 > 255) {
r = 0;
g += 1;
} else { } else {
r += 1; entityColors[modelJob->Entity] = glm::vec2(pickColor[0], pickColor[1]);
if (r + 10 > 255) {
r = 0;
g += 1;
} else {
r += 1;
}
} }
m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity;
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1])));
glBindVertexArray(modelJob->Model->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, nullptr, modelJob->StartIndex);
} }
m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity;
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "Matrix"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1])));
glBindVertexArray(modelJob->Model->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, nullptr, modelJob->StartIndex);
} }
} }
m_PickingBuffer.Unbind(); m_PickingBuffer.Unbind();
GLERROR("PickingPass Error"); GLERROR("PickingPass Error");
@@ -92,15 +96,15 @@ void PickingPass::Draw(RenderQueueCollection& rq)
int fbWidth; int fbWidth;
int fbHeight; int fbHeight;
glfwGetFramebufferSize(m_Renderer->Window(), &fbWidth, &fbHeight); glfwGetFramebufferSize(m_Renderer->Window(), &fbWidth, &fbHeight);
/* Events::Picking pickEvent = Events::Picking( Events::Picking pickEvent = Events::Picking(
&m_PickingBuffer, &m_PickingBuffer,
&m_DepthBuffer, &m_DepthBuffer,
m_Renderer->Camera()->ProjectionMatrix(), m_Renderer->Camera()->ProjectionMatrix(),
m_Renderer->Camera()->ViewMatrix(), m_Renderer->Camera()->ViewMatrix(),
Rectangle(fbWidth, fbHeight), Rectangle(fbWidth, fbHeight),
&m_PickingColorsToEntity);*/ &m_PickingColorsToEntity);
//m_EventBroker->Publish(pickEvent); m_EventBroker->Publish(pickEvent);
delete state; delete state;
} }
+74 -42
View File
@@ -1,29 +1,56 @@
#include "Rendering/RenderSystem.h" #include "Rendering/RenderSystem.h"
#include "Rendering/DebugCameraInputController.h" #include "Rendering/DebugCameraInputController.h"
RenderSystem::RenderSystem(EventBroker* eventBrokerer, RenderQueueCollection* renderQueues) RenderSystem::RenderSystem(EventBroker* eventBrokerer, RenderFrame* renderFrame)
:ImpureSystem(eventBrokerer) :ImpureSystem(eventBrokerer)
{ {
m_RenderQueues = renderQueues; m_RenderFrame = renderFrame;
Initialize(); Initialize();
EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera); EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand);
m_DefaultCamera = new Camera(1.77f /* should be based on width and height */, glm::radians(45.f), 0.01f, 5000.f);
m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10));
if (m_Camera == nullptr) {
m_Camera = m_DefaultCamera;
}
} }
bool RenderSystem::OnSetCamera(const Events::SetCamera &event) bool RenderSystem::OnSetCamera(const Events::SetCamera &event)
{ {
m_CurrentCamera = event.Entity; auto cameras = m_World->GetComponents("Camera");
if (cameras != nullptr) {
for (auto it = cameras->begin(); it != cameras->end(); it++) {
if ((std::string)(*it)["Name"] == event.Name) {
SwitchCamera((*it).EntityID);
}
}
}
return true; return true;
} }
void RenderSystem::SwitchCamera(EntityID entity) void RenderSystem::SwitchCamera(EntityID entity)
{ {
m_CurrentCamera = entity; if(m_World->HasComponent(entity, "Camera")) {
m_SwitchCamera = false; if (m_World->HasComponent(m_CurrentCamera, "Model")) {
LOG_INFO("Switched to camera %i", m_CurrentCamera); m_World->GetComponent(m_CurrentCamera, "Model")["Visible"] = true;
}
if (m_World->HasComponent(entity, "Model")) {
m_World->GetComponent(entity, "Model")["Visible"] = false;
}
m_CurrentCamera = entity;
m_SwitchCamera = false;
LOG_INFO("Switched to %s", m_World->GetComponent(m_CurrentCamera, "Camera")["Name"]);
} else {
LOG_ERROR("Entity %i does not have a CameraComponent", entity);
m_SwitchCamera = false;
}
} }
void RenderSystem::Initialize() void RenderSystem::Initialize()
@@ -31,24 +58,21 @@ void RenderSystem::Initialize()
} }
void RenderSystem::UpdateViewMatrix(ComponentWrapper& cameraTransform)
{
glm::quat orientation = glm::quat((glm::vec3)cameraTransform["Orientation"]);
glm::vec3 position = cameraTransform["Position"];
m_ViewMatrix = glm::toMat4(glm::inverse(orientation)) * glm::translate(-position);
}
void RenderSystem::UpdateProjectionMatrix(ComponentWrapper& cameraComponent) void RenderSystem::UpdateProjectionMatrix(ComponentWrapper& cameraComponent)
{ {
double fov = (double&)cameraComponent["FOV"]; double fov = cameraComponent["FOV"];
double aspectRatio = (double&)cameraComponent["AspectRatio"]; double aspectRatio = cameraComponent["AspectRatio"];
double nearClip = (double&)cameraComponent["NearClip"]; double nearClip = cameraComponent["NearClip"];
double farClip = (double&)cameraComponent["FarClip"]; double farClip = cameraComponent["FarClip"];
double fovY = atan(tan(glm::radians(fov)/2.0) * aspectRatio) * 2.0; double fovY = atan(tan(glm::radians(fov)/2.0) * aspectRatio) * 2.0;
m_ProjectionMatrix = glm::perspective(fovY, aspectRatio, nearClip, farClip); m_ProjectionMatrix = glm::perspective(fovY, aspectRatio, nearClip, farClip);
m_Camera->SetFOV(fovY);
m_Camera->SetAspectRatio(aspectRatio);
m_Camera->SetNearClip(nearClip);
m_Camera->SetFarClip(farClip);
m_Camera->UpdateProjectionMatrix();
} }
glm::vec3 RenderSystem::AbsolutePosition(World* world, EntityID entity) glm::vec3 RenderSystem::AbsolutePosition(World* world, EntityID entity)
@@ -95,20 +119,19 @@ glm::vec3 RenderSystem::AbsoluteScale(World* world, EntityID entity)
return scale; return scale;
} }
glm::mat4 RenderSystem::ModelMatrix(EntityID entity)
glm::mat4 RenderSystem::ModelMatrix(World* world, EntityID entity)
{ {
glm::vec3 position = AbsolutePosition(world, entity); glm::vec3 position = AbsolutePosition(m_World, entity);
glm::quat orientation = AbsoluteOrientation(world, entity); glm::quat orientation = AbsoluteOrientation(m_World, entity);
glm::vec3 scale = AbsoluteScale(world, entity); glm::vec3 scale = AbsoluteScale(m_World, entity);
glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale);
return modelMatrix; return modelMatrix;
} }
void RenderSystem::FillModels(World* world, RenderQueue* renderQueue) void RenderSystem::FillModels(RenderQueue* renderQueue)
{ {
auto models = world->GetComponents("Model"); auto models = m_World->GetComponents("Model");
if (models == nullptr) { if (models == nullptr) {
return; return;
} }
@@ -140,7 +163,7 @@ void RenderSystem::FillModels(World* world, RenderQueue* renderQueue)
job.Model = model; job.Model = model;
job.StartIndex = texGroup.StartIndex; job.StartIndex = texGroup.StartIndex;
job.EndIndex = texGroup.EndIndex; job.EndIndex = texGroup.EndIndex;
job.Matrix = m_ProjectionMatrix * m_ViewMatrix * (model->m_Matrix * ModelMatrix(world, modelC.EntityID)); job.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID));
job.Color = color; job.Color = color;
job.Entity = modelC.EntityID; job.Entity = modelC.EntityID;
@@ -156,7 +179,7 @@ void RenderSystem::FillModels(World* world, RenderQueue* renderQueue)
job.Model = model; job.Model = model;
job.StartIndex = texGroup.StartIndex; job.StartIndex = texGroup.StartIndex;
job.EndIndex = texGroup.EndIndex; job.EndIndex = texGroup.EndIndex;
job.Matrix = m_ProjectionMatrix * m_ViewMatrix * (model->m_Matrix * ModelMatrix(world, modelC.EntityID)); job.Matrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * (model->m_Matrix * ModelMatrix(modelC.EntityID));
job.Color = color; job.Color = color;
job.Entity = modelC.EntityID; job.Entity = modelC.EntityID;
@@ -176,9 +199,10 @@ bool RenderSystem::OnInputCommand(const Events::InputCommand& e)
} }
} }
void RenderSystem::Update(World* world, double dt) void RenderSystem::Update(World* world, double dt)
{ {
m_World = world;
m_EventBroker->Process<RenderSystem>(); m_EventBroker->Process<RenderSystem>();
static DebugCameraInputController<RenderSystem> firstPersonInputController(m_EventBroker, -1); static DebugCameraInputController<RenderSystem> firstPersonInputController(m_EventBroker, -1);
@@ -201,34 +225,42 @@ void RenderSystem::Update(World* world, double dt)
ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform");
firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"]));
firstPersonInputController.SetPosition((glm::vec3)cameraTransform["Position"]); firstPersonInputController.SetPosition(cameraTransform["Position"]);
} }
} }
if(world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) { if(world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) {
ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera");
ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform");
firstPersonInputController.Update(dt); if(world->GetParent(m_CurrentCamera) == 1) { // world is entity 1, is this ok?
(glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(firstPersonInputController.Orientation()); firstPersonInputController.Update(dt);
(glm::vec3&)cameraTransform["Position"] = firstPersonInputController.Position(); (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);
m_Camera->SetPosition(position);
m_Camera->SetOrientation(orientation);
UpdateProjectionMatrix(cameraComponent); UpdateProjectionMatrix(cameraComponent);
UpdateViewMatrix(cameraTransform); m_Camera->UpdateViewMatrix();
} else { } else {
m_ProjectionMatrix = glm::perspective(glm::radians(40.f), 1.77f, 0.05f, 5000.f); m_Camera = m_DefaultCamera;
m_ViewMatrix = glm::mat4();
auto cameras = world->GetComponents("Camera"); auto cameras = world->GetComponents("Camera");
if (cameras != nullptr) { if (cameras != nullptr) {
ComponentWrapper& cameraC = *cameras->begin(); ComponentWrapper& cameraC = *cameras->begin();
m_CurrentCamera = cameraC.EntityID; SwitchCamera(cameraC.EntityID);
world->GetComponent(m_CurrentCamera, "Model")["Visible"] = false;
} }
} }
m_RenderQueues->Clear(); m_RenderFrame->Clear();
FillModels(world, &m_RenderQueues->Forward); RenderScene* rs = new RenderScene();
rs->Camera = m_Camera;
FillModels(&rs->Forward);
m_RenderFrame->Add(*rs);
} }
+4 -4
View File
@@ -90,14 +90,14 @@ void Renderer::Update(double dt)
m_ImGuiRenderPass->Update(dt); m_ImGuiRenderPass->Update(dt);
} }
void Renderer::Draw(RenderQueueCollection& rq) void Renderer::Draw(RenderFrame& rf)
{ {
m_PickingPass->Draw(rq); m_Camera = (*rf.begin())->Camera;
//DrawScreenQuad(m_PickingPass->PickingTexture()); m_PickingPass->Draw(rf);
glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 1.f); glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 1.f);
m_DrawScenePass->Draw(rq); m_DrawScenePass->Draw(rf);
GLERROR("Renderer::Draw m_DrawScenePass->Draw"); GLERROR("Renderer::Draw m_DrawScenePass->Draw");
m_ImGuiRenderPass->Draw(); m_ImGuiRenderPass->Draw();
glfwSwapBuffers(m_Window); glfwSwapBuffers(m_Window);
+3 -3
View File
@@ -47,14 +47,14 @@ Game::Game(int argc, char* argv[])
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World); ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
} }
m_RenderQueues = new RenderQueueCollection(); m_RenderFrame = new RenderFrame();
// Create system pipeline // Create system pipeline
m_SystemPipeline = new SystemPipeline(m_EventBroker); m_SystemPipeline = new SystemPipeline(m_EventBroker);
m_SystemPipeline->AddSystem<RaptorCopterSystem>(); m_SystemPipeline->AddSystem<RaptorCopterSystem>();
m_SystemPipeline->AddSystem<PlayerSystem>(); m_SystemPipeline->AddSystem<PlayerSystem>();
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer); m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer);
m_SystemPipeline->AddSystem<RenderSystem>(m_RenderQueues); m_SystemPipeline->AddSystem<RenderSystem>(m_RenderFrame);
m_LastTime = glfwGetTime(); m_LastTime = glfwGetTime();
@@ -91,7 +91,7 @@ void Game::Tick()
m_Renderer->Update(dt); m_Renderer->Update(dt);
GLERROR("Game::Tick m_RenderQueueFactory->Update"); GLERROR("Game::Tick m_RenderQueueFactory->Update");
m_Renderer->Draw(*m_RenderQueues); m_Renderer->Draw(*m_RenderFrame);
GLERROR("Game::Tick m_Renderer->Draw"); GLERROR("Game::Tick m_Renderer->Draw");
m_EventBroker->Swap(); m_EventBroker->Swap();
m_EventBroker->Clear(); m_EventBroker->Clear();