Split screen working

This commit is contained in:
2014-05-19 17:20:43 +02:00
parent e07a0a2b1a
commit bd7ba73bca
7 changed files with 184 additions and 115 deletions
-1
View File
@@ -14,7 +14,6 @@ struct Camera : Component
, NearClip(0.1f) , NearClip(0.1f)
, FarClip(100.f) { } , FarClip(100.f) { }
EntityID Viewport;
float FOV; float FOV;
float NearClip; float NearClip;
float FarClip; float FarClip;
+4 -1
View File
@@ -12,13 +12,16 @@ struct Viewport : Component
: Left(0.f) : Left(0.f)
, Top(0.f) , Top(0.f)
, Right(1.f) , Right(1.f)
, Bottom(1.f) { } , Bottom(1.f)
, Camera(0) { }
float Left; float Left;
float Top; float Top;
float Right; float Right;
float Bottom; float Bottom;
EntityID Camera;
virtual Viewport* Clone() const override { return new Viewport(*this); } virtual Viewport* Clone() const override { return new Viewport(*this); }
}; };
+29 -17
View File
@@ -67,33 +67,32 @@ void GameWorld::Initialize()
RegisterComponents(); RegisterComponents();
auto viewport1 = CreateEntity(); auto camera = CreateEntity();
{ {
auto viewport = AddComponent<Components::Viewport>(viewport1, "Transform");
viewport->Right = 0.5f;
}
CommitEntity(viewport1);
auto viewport2 = CreateEntity();
{
auto viewport = AddComponent<Components::Viewport>(viewport1, "Transform");
viewport->Left = 0.5f;
}
CommitEntity(viewport2);
{
auto camera = CreateEntity();
auto transform = AddComponent<Components::Transform>(camera, "Transform"); auto transform = AddComponent<Components::Transform>(camera, "Transform");
transform->Position.z = 20.f; transform->Position.z = 20.f;
transform->Position.y = 20.f; transform->Position.y = 20.f;
//transform->Orientation = glm::quat(glm::vec3(glm::pi<float>() / 8.f, 0.f, 0.f)); //transform->Orientation = glm::quat(glm::vec3(glm::pi<float>() / 8.f, 0.f, 0.f));
auto cameraComp = AddComponent<Components::Camera>(camera, "Camera"); auto cameraComp = AddComponent<Components::Camera>(camera, "Camera");
cameraComp->Viewport = viewport1;
cameraComp->FarClip = 2000.f; cameraComp->FarClip = 2000.f;
auto freeSteering = AddComponent<Components::FreeSteering>(camera, "FreeSteering"); auto freeSteering = AddComponent<Components::FreeSteering>(camera, "FreeSteering");
CommitEntity(camera);
} }
CommitEntity(camera);
auto viewport1 = CreateEntity();
{
auto viewport = AddComponent<Components::Viewport>(viewport1, "Viewport");
viewport->Right = 0.5f;
viewport->Camera = camera;
}
CommitEntity(viewport1);
auto viewport2 = CreateEntity();
{
auto viewport = AddComponent<Components::Viewport>(viewport2, "Viewport");
viewport->Left = 0.5f;
}
CommitEntity(viewport2);
{ {
auto ground = CreateEntity(); auto ground = CreateEntity();
@@ -334,6 +333,19 @@ void GameWorld::Initialize()
} }
CommitEntity(barrel); CommitEntity(barrel);
} }
auto cameraTower = CreateEntity(tower);
{
auto transform = AddComponent<Components::Transform>(cameraTower, "Transform");
transform->Position.z = 11.f;
transform->Position.y = 4.f;
//transform->Orientation = glm::quat(glm::vec3(glm::pi<float>() / 8.f, 0.f, 0.f));
auto cameraComp = AddComponent<Components::Camera>(cameraTower, "Camera");
cameraComp->FarClip = 2000.f;
auto freeSteering = AddComponent<Components::FreeSteering>(cameraTower, "FreeSteering");
}
CommitEntity(cameraTower);
GetComponent<Components::Viewport>(viewport2, "Viewport")->Camera = cameraTower;
} }
{ {
+91 -59
View File
@@ -636,77 +636,90 @@ void Renderer::DrawFBO()
{ {
DrawShadowMap(); DrawShadowMap();
/* for (auto &pair : m_Viewports)
{
Viewport &viewport = pair.second;
if (!viewport.Camera)
continue;
int x = viewport.Left * m_Width;
int y = viewport.Top * m_Height;
int width = (viewport.Right - viewport.Left) * m_Width;
int height = (viewport.Bottom - viewport.Top) * m_Height;
/*
Base pass Base pass
*/ */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass);
glViewport(0, 0, m_Width, m_Height);
// Clear G-buffer // Clear G-buffer
GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, windowBuffClear); glDrawBuffers(3, windowBuffClear);
glClearColor(0.f, 0.f, 0.f, 0.f); glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Execute the first render stage which will fill out the internal buffers with data(??) // Execute the first render stage which will fill out the internal buffers with data(??)
m_FirstPassProgram.Bind(); m_FirstPassProgram.Bind();
GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, windowBuffOpaque); glDrawBuffers(3, windowBuffOpaque);
glCullFace(GL_BACK); glCullFace(GL_BACK);
DrawFBOScene(viewport);
glViewport(0, 0, m_Width, m_Height); /*
DrawFBOScene();
/*
Lighting pass Lighting pass
*/ */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass);
GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, lightingPassAttachments); glDrawBuffers(1, lightingPassAttachments);
glClearColor(0.f, 0.f, 0.f, 0.f); glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
m_SecondPassProgram.Bind(); m_SecondPassProgram.Bind();
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); glBindTexture(GL_TEXTURE_2D, m_fPositionTexture);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
glCullFace(GL_FRONT); glCullFace(GL_FRONT);
DrawLightScene(); DrawLightScene(viewport);
/* /*
Final pass Final pass
*/ */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(x, y, width, height);
glClear(GL_DEPTH_BUFFER_BIT);
m_FinalPassProgram.Bind(); m_FinalPassProgram.Bind();
// Ambient light // Ambient light
glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f)));
glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); glBindTexture(GL_TEXTURE_2D, m_fLightingTexture);
glCullFace(GL_BACK); glCullFace(GL_BACK);
glBindVertexArray(m_ScreenQuad); glBindVertexArray(m_ScreenQuad);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
}
} }
void Renderer::DrawFBOScene() void Renderer::DrawFBOScene(Viewport &viewport)
{ {
// glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly // glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly
// glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object // glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object
// glCullFace(GL_BACK); //Make it so that only the back faces are rendered // glCullFace(GL_BACK); //Make it so that only the back faces are rendered
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix();
glm::mat4 MVP; glm::mat4 MVP;
glm::mat4 biasMatrix( glm::mat4 biasMatrix(
0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0,
@@ -737,8 +750,8 @@ void Renderer::DrawFBOScene()
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix()));
glBindVertexArray(model->VAO); glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups) for (auto texGroup : model->TextureGroups)
{ {
@@ -767,8 +780,8 @@ void Renderer::DrawFBOScene()
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix()));
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, *texture); glBindTexture(GL_TEXTURE_2D, *texture);
@@ -779,7 +792,7 @@ void Renderer::DrawFBOScene()
void Renderer::DrawLightScene() void Renderer::DrawLightScene(Viewport &viewport)
{ {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendEquation (GL_FUNC_ADD); glBlendEquation (GL_FUNC_ADD);
@@ -789,7 +802,7 @@ void Renderer::DrawLightScene()
glDepthMask (GL_FALSE); glDepthMask (GL_FALSE);
glBindVertexArray(m_sphereModel->VAO); glBindVertexArray(m_sphereModel->VAO);
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix();
glm::mat4 MVP; glm::mat4 MVP;
for (auto &light : Lights) for (auto &light : Lights)
@@ -798,13 +811,13 @@ void Renderer::DrawLightScene()
glUniform2fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ViewportSize"), 1,glm::value_ptr(glm::vec2(m_Width, m_Height))); glUniform2fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ViewportSize"), 1,glm::value_ptr(glm::vec2(m_Width, m_Height)));
glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix()));
glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(light.SphereModelMatrix)); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(light.SphereModelMatrix));
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(light.Specular)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(light.Specular));
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(light.Diffuse)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(light.Diffuse));
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position));
glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z); glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), viewport.Camera->Position().x, viewport.Camera->Position().y, viewport.Camera->Position().z);
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent);
// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), light.ConstantAttenuation); // glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), light.ConstantAttenuation);
// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation); // glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation);
@@ -868,16 +881,35 @@ void Renderer::UpdateSunProjection()
//Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map.
} }
void Renderer::CreateViewport(int identifier, float left, float top, float right, float bottom) void Renderer::RegisterViewport(int identifier, float left, float top, float right, float bottom)
{ {
Viewport v; Viewport v;
v.Left = left; v.Left = left;
v.Top = top; v.Top = top;
v.Right = right; v.Right = right;
v.Bottom = bottom; v.Bottom = bottom;
v.Camera = nullptr;
m_Viewports[identifier] = v; m_Viewports[identifier] = v;
} }
void Renderer::RegisterCamera(int identifier, float FOV, float nearClip, float farClip)
{
m_Cameras[identifier] = std::make_shared<Camera>(FOV, (float)m_Width / m_Height, nearClip, farClip);
}
void Renderer::UpdateViewport(int viewportIdentifier, int cameraIdentifier)
{
auto &viewport = m_Viewports[viewportIdentifier];
auto camera = m_Cameras[cameraIdentifier];
camera->AspectRatio(((viewport.Right - viewport.Left) * m_Width) / ((viewport.Bottom - viewport.Top) * m_Height));
viewport.Camera = camera;
}
void Renderer::UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat orientation, float FOV, float nearClip, float farClip)
{
m_Cameras[cameraIdentifier]->Position(position);
m_Cameras[cameraIdentifier]->Orientation(orientation);
m_Cameras[cameraIdentifier]->FOV(FOV);
m_Cameras[cameraIdentifier]->NearClip(nearClip);
m_Cameras[cameraIdentifier]->FarClip(farClip);
}
+9 -3
View File
@@ -35,7 +35,11 @@ public:
void Draw(double dt); void Draw(double dt);
void DrawText(); void DrawText();
void CreateViewport(int identifier, float left, float top, float right, float bottom); void RegisterViewport(int identifier, float left, float top, float right, float bottom);
void RegisterCamera(int identifier, float FOV, float nearClip, float farClip);
void UpdateViewport(int viewportIdentifier, int cameraIdentifier);
void UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat orientation, float FOV, float nearClip, float farClip);
void AddModelToDraw(Model* model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible, bool shadowCaster); void AddModelToDraw(Model* model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible, bool shadowCaster);
void AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale); void AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale);
void AddTextToDraw(); void AddTextToDraw();
@@ -74,9 +78,11 @@ private:
float Top; float Top;
float Right; float Right;
float Bottom; float Bottom;
std::shared_ptr<Camera> Camera;
}; };
std::unordered_map<int, Viewport> m_Viewports; std::unordered_map<int, Viewport> m_Viewports;
std::unordered_map<int, std::shared_ptr<Camera>> m_Cameras;
struct Light struct Light
{ {
@@ -152,8 +158,8 @@ private:
void CreateShadowMap(int resolution); void CreateShadowMap(int resolution);
void FrameBufferTextures(); void FrameBufferTextures();
void DrawFBO(); void DrawFBO();
void DrawFBOScene(); void DrawFBOScene(Viewport &viewport);
void DrawLightScene(); void DrawLightScene(Viewport &viewport);
void BindFragDataLocation(); void BindFragDataLocation();
glm::mat4 CreateLightMatrix(Light &_light); glm::mat4 CreateLightMatrix(Light &_light);
void UpdateSunProjection(); void UpdateSunProjection();
+50 -33
View File
@@ -2,27 +2,55 @@
#include "RenderSystem.h" #include "RenderSystem.h"
#include "World.h" #include "World.h"
void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm)
{
rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load<OBJ>("OBJ", resourceName)); });
rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); });
rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); });
}
void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register("Camera", []() { return new Components::Camera(); });
cf->Register("Model", []() { return new Components::Model(); });
cf->Register("Sprite", []() { return new Components::Sprite(); });
cf->Register("PointLight", []() { return new Components::PointLight(); });
cf->Register("DirectionalLight", []() { return new Components::DirectionalLight(); });
cf->Register("Viewport", []() { return new Components::Viewport(); });
}
void Systems::RenderSystem::OnEntityCommit(EntityID entity) void Systems::RenderSystem::OnEntityCommit(EntityID entity)
{ {
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
auto camera = m_World->GetComponent<Components::Camera>(entity, "Camera");
if (transform && camera)
{
m_Renderer->RegisterCamera(entity, camera->FOV, camera->NearClip, camera->FarClip);
m_Renderer->UpdateCamera(entity, m_TransformSystem->AbsolutePosition(entity), m_TransformSystem->AbsoluteOrientation(entity), camera->FOV, camera->NearClip, camera->FarClip);
}
auto viewport = m_World->GetComponent<Components::Viewport>(entity, "Viewport"); auto viewport = m_World->GetComponent<Components::Viewport>(entity, "Viewport");
if (viewport) if (viewport)
{ {
m_Renderer->CreateViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom); m_Renderer->RegisterViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom);
if (viewport->Camera != 0)
{
m_Renderer->UpdateViewport(entity, viewport->Camera);
}
} }
} }
void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ {
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform"); auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (transformComponent == nullptr)
return;
// Draw models // Draw models
auto modelComponent = m_World->GetComponent<Components::Model>(entity, "Model"); auto modelComponent = m_World->GetComponent<Components::Model>(entity, "Model");
if (modelComponent != nullptr) if (transformComponent&& modelComponent)
{ {
auto model = m_World->GetResourceManager()->Load<Model>("Model", modelComponent->ModelFile); auto model = m_World->GetResourceManager()->Load<Model>("Model", modelComponent->ModelFile);
if (model != nullptr) if (model)
{ {
/*glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); /*glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity); glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity);
@@ -33,7 +61,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
} }
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity, "PointLight"); auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity, "PointLight");
if (pointLightComponent != nullptr) if (transformComponent && pointLightComponent)
{ {
glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
m_Renderer->AddPointLightToDraw( m_Renderer->AddPointLightToDraw(
@@ -48,18 +76,27 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
} }
auto cameraComponent = m_World->GetComponent<Components::Camera>(entity, "Camera"); auto cameraComponent = m_World->GetComponent<Components::Camera>(entity, "Camera");
if (cameraComponent != nullptr) if (transformComponent && cameraComponent)
{ {
m_Renderer->GetCamera()->Position(m_TransformSystem->AbsolutePosition(entity)); m_Renderer->UpdateCamera(entity
m_Renderer->GetCamera()->Orientation(m_TransformSystem->AbsoluteOrientation(entity)); , m_TransformSystem->AbsolutePosition(entity)
, m_TransformSystem->AbsoluteOrientation(entity)
, cameraComponent->FOV
, cameraComponent->NearClip
, cameraComponent->FarClip);
}
m_Renderer->GetCamera()->FOV(cameraComponent->FOV); auto viewportComponent = m_World->GetComponent<Components::Viewport>(entity, "Viewport");
m_Renderer->GetCamera()->NearClip(cameraComponent->NearClip); if (viewportComponent)
m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip); {
if (viewportComponent->Camera != 0)
{
m_Renderer->UpdateViewport(entity, viewportComponent->Camera);
}
} }
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity, "Sprite"); auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity, "Sprite");
if(spriteComponent != nullptr) if (transformComponent && spriteComponent)
{ {
//TEMP //TEMP
Texture* texture = m_World->GetResourceManager()->Load<Texture>("Texture", spriteComponent->SpriteFile); Texture* texture = m_World->GetResourceManager()->Load<Texture>("Texture", spriteComponent->SpriteFile);
@@ -76,23 +113,3 @@ void Systems::RenderSystem::Initialize()
m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load<Model>("Model", "Models/Placeholders/PhysicsTest/Sphere.obj")); m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load<Model>("Model", "Models/Placeholders/PhysicsTest/Sphere.obj"));
} }
void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register("Camera", []() { return new Components::Camera(); });
cf->Register("Model", []() { return new Components::Model(); });
cf->Register("Sprite", []() { return new Components::Sprite(); });
cf->Register("PointLight", []() { return new Components::PointLight(); });
cf->Register("DirectionalLight", []() { return new Components::DirectionalLight(); });
}
void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm)
{
rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load<OBJ>("OBJ", resourceName)); });
rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); });
rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); });
}
+1 -1
View File
@@ -35,7 +35,7 @@ public:
std::unordered_map<std::string, std::shared_ptr<Model>> m_CachedModels; std::unordered_map<std::string, std::shared_ptr<Model>> m_CachedModels;
void OnEntityCommit(EntityID entity); void OnEntityCommit(EntityID entity) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override;