Semi-working world rendering

This commit is contained in:
2014-05-26 23:28:11 +02:00
parent 74239300bd
commit d4b28a2228
15 changed files with 509 additions and 206 deletions
+1 -1
Submodule assets updated: 6cc38589ed...72b93d5750
+20 -1
View File
@@ -2,6 +2,9 @@
#include <sstream> #include <sstream>
#include "ResourceManager.h" #include "ResourceManager.h"
#include "OBJ.h"
#include "Model.h"
#include "Texture.h"
#include "EventBroker.h" #include "EventBroker.h"
#include "RenderQueue.h" #include "RenderQueue.h"
#include "Renderer.h" #include "Renderer.h"
@@ -15,7 +18,12 @@ public:
Engine(int argc, char* argv[]) Engine(int argc, char* argv[])
{ {
m_EventBroker = std::make_shared<EventBroker>(); m_EventBroker = std::make_shared<EventBroker>();
m_ResourceManager = std::make_shared<ResourceManager>(); m_ResourceManager = std::make_shared<ResourceManager>();
m_ResourceManager->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); });
auto rm = m_ResourceManager;
m_ResourceManager->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load<OBJ>("OBJ", resourceName)); });
m_ResourceManager->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); });
m_Renderer = std::make_shared<Renderer>(); m_Renderer = std::make_shared<Renderer>();
m_Renderer->Initialize(); m_Renderer->Initialize();
@@ -23,6 +31,8 @@ public:
m_InputManager = std::make_shared<InputManager>(m_Renderer->GetWindow(), m_EventBroker); m_InputManager = std::make_shared<InputManager>(m_Renderer->GetWindow(), m_EventBroker);
m_FrameStack = new GUI::Frame(m_EventBroker, m_ResourceManager); m_FrameStack = new GUI::Frame(m_EventBroker, m_ResourceManager);
m_FrameStack->Width = 1280;
m_FrameStack->Height = 720;
new GUI::GameFrame(m_FrameStack, "GameFrame"); new GUI::GameFrame(m_FrameStack, "GameFrame");
//m_World = std::make_shared<GameWorld>(m_EventBroker, m_ResourceManager); //m_World = std::make_shared<GameWorld>(m_EventBroker, m_ResourceManager);
@@ -36,12 +46,21 @@ public:
void Tick() void Tick()
{ {
double currentTime = glfwGetTime(); double currentTime = glfwGetTime();
double dt = currentTime - m_LastTime; double dt = currentTime - m_LastTime;
m_LastTime = currentTime; m_LastTime = currentTime;
// Update input
m_InputManager->Update(dt); m_InputManager->Update(dt);
// Update frame stack
m_EventBroker->Process<GUI::Frame>();
m_FrameStack->UpdateLayered(dt);
// Render scene
m_FrameStack->DrawLayered(m_Renderer); m_FrameStack->DrawLayered(m_Renderer);
m_Renderer->Swap();
// Swap event queues
m_EventBroker->Clear(); m_EventBroker->Clear();
glfwPollEvents(); glfwPollEvents();
+18 -14
View File
@@ -41,7 +41,6 @@ public:
, m_Layer(0) , m_Layer(0)
{ SetParent(std::shared_ptr<Frame>(parent)); } { SetParent(std::shared_ptr<Frame>(parent)); }
::RenderQueue RenderQueue; ::RenderQueue RenderQueue;
std::shared_ptr<Frame> Parent() const { return m_Parent; } std::shared_ptr<Frame> Parent() const { return m_Parent; }
@@ -84,10 +83,7 @@ public:
} }
int Right() const override int Right() const override
{ {
if (m_Parent) return Left() + Width;
return m_Parent->Right() + X + Width;
else
return X + Width;
} }
int Top() const override int Top() const override
{ {
@@ -98,24 +94,31 @@ public:
} }
int Bottom() const override int Bottom() const override
{ {
if (m_Parent) return Top() + Height;
return m_Parent->Bottom() + Y + Height;
else
return Y + Height;
} }
Rectangle AbsoluteRectangle() Rectangle AbsoluteRectangle()
{ {
return Rectangle(Left(), Right(), Width, Height); return Rectangle(Left(), Top(), Width, Height);
} }
virtual void Update(double dt) void UpdateLayered(double dt)
{ {
for (auto &pair : m_Children[m_Layer + 1]) // Update ourselves
this->Update(dt);
// Update children
for (auto &pairLayer : m_Children)
{ {
pair.second->Update(dt); auto children = pairLayer.second;
for (auto &pairChild : children)
{
auto child = pairChild.second;
child->Update(dt);
}
} }
} }
virtual void Update(double dt) { }
void DrawLayered(std::shared_ptr<Renderer> renderer) void DrawLayered(std::shared_ptr<Renderer> renderer)
{ {
@@ -130,7 +133,8 @@ public:
for (auto &pairChild : children) for (auto &pairChild : children)
{ {
auto child = pairChild.second; auto child = pairChild.second;
renderer->SetViewport(child->AbsoluteRectangle()); Rectangle rect = child->AbsoluteRectangle();
renderer->SetViewport(rect);
child->Draw(renderer); child->Draw(renderer);
} }
} }
+13 -14
View File
@@ -4,6 +4,7 @@
#include "GUI/Frame.h" #include "GUI/Frame.h"
#include "GUI/WorldFrame.h" #include "GUI/WorldFrame.h"
#include "GUI/Viewport.h" #include "GUI/Viewport.h"
#include "GUI/TextureFrame.h"
#include "GameWorld.h" #include "GameWorld.h"
@@ -17,27 +18,25 @@ public:
: Frame(parent, name) : Frame(parent, name)
{ {
m_World = std::make_shared<GameWorld>(EventBroker, ResourceManager); m_World = std::make_shared<GameWorld>(EventBroker, ResourceManager);
m_World->Initialize();
auto worldFrame = new WorldFrame(this, "GameWorldFrame", m_World); auto worldFrame = new WorldFrame(this, "GameWorldFrame", m_World);
{ {
new Viewport(worldFrame, "Viewport1", m_World); vp1 = new Viewport(worldFrame, "Viewport1", m_World);
new Viewport(worldFrame, "Viewport2", m_World); vp1->X = 0;
vp1->Width = 640;
vp2 = new Viewport(worldFrame, "Viewport2", m_World);
vp2->X = vp1->Right();
vp2->Width = 640;
auto tex = new TextureFrame(vp1, "TextureFrameThingy");
tex->SetTexture("Textures/GUI/hurt.png");
} }
} m_World->Initialize();
void Update(double dt) override
{
}
void Draw(std::shared_ptr<Renderer> renderer) override
{
} }
private: private:
std::shared_ptr<GameWorld> m_World; std::shared_ptr<GameWorld> m_World;
Viewport* vp1;
Viewport* vp2;
}; };
} }
+2 -2
View File
@@ -10,7 +10,7 @@ namespace GUI
class TextureFrame : public Frame class TextureFrame : public Frame
{ {
public: public:
TextureFrame(std::shared_ptr<Frame> parent, std::string name) TextureFrame(Frame* parent, std::string name)
: Frame(parent, name) { } : Frame(parent, name) { }
void Draw(std::shared_ptr<Renderer> renderer) override void Draw(std::shared_ptr<Renderer> renderer) override
@@ -25,7 +25,7 @@ public:
RenderQueue.Add(job); RenderQueue.Add(job);
renderer->SetCamera(nullptr); renderer->SetCamera(nullptr);
renderer->Draw(RenderQueue); renderer->DrawFrame(RenderQueue);
} }
std::shared_ptr<::Texture> Texture() const { return m_Texture; } std::shared_ptr<::Texture> Texture() const { return m_Texture; }
+8 -4
View File
@@ -20,9 +20,7 @@ public:
Viewport(Frame* parent, std::string name, std::shared_ptr<World> world) Viewport(Frame* parent, std::string name, std::shared_ptr<World> world)
: Frame(parent, name) : Frame(parent, name)
, m_World(world) , m_World(world)
{ { }
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
}
EntityID CameraEntity() const { return m_CameraEntity; } EntityID CameraEntity() const { return m_CameraEntity; }
void SetCameraEntity(EntityID cameraEntity) void SetCameraEntity(EntityID cameraEntity)
@@ -41,6 +39,9 @@ public:
void Update(double dt) override void Update(double dt) override
{ {
if (!m_TransformSystem)
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
auto transformComponent = m_World->GetComponent<Components::Transform>(m_CameraEntity); auto transformComponent = m_World->GetComponent<Components::Transform>(m_CameraEntity);
if (!transformComponent) if (!transformComponent)
return; return;
@@ -60,8 +61,11 @@ public:
void Draw(std::shared_ptr<Renderer> renderer) override void Draw(std::shared_ptr<Renderer> renderer) override
{ {
if (!m_Camera)
return;
renderer->SetCamera(m_Camera); renderer->SetCamera(m_Camera);
renderer->Draw(m_Parent->RenderQueue); renderer->DrawWorld(m_Parent->RenderQueue);
} }
private: private:
+15 -3
View File
@@ -23,17 +23,20 @@ public:
, m_World(world) , m_World(world)
{ {
EVENT_SUBSCRIBE_MEMBER(m_ESetViewportCamera, &WorldFrame::OnSetViewportCamera); EVENT_SUBSCRIBE_MEMBER(m_ESetViewportCamera, &WorldFrame::OnSetViewportCamera);
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
} }
void Update(double dt) override void Update(double dt) override
{ {
if (!m_TransformSystem)
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
m_World->Update(dt); m_World->Update(dt);
} }
void Draw(std::shared_ptr<Renderer> renderer) override void Draw(std::shared_ptr<Renderer> renderer) override
{ {
RenderQueue.Clear(); RenderQueue.Clear();
renderer->ClearPointLights();
for (auto &pair : *m_World->GetEntities()) for (auto &pair : *m_World->GetEntities())
{ {
@@ -75,8 +78,17 @@ public:
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity); auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity);
if (pointLightComponent) if (pointLightComponent)
{ {
// TODO: Push lights to renderer glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
// renderer->PushLight(...); renderer->AddPointLightToDraw(
position,
pointLightComponent->Specular,
pointLightComponent->Diffuse,
pointLightComponent->specularExponent,
pointLightComponent->ConstantAttenuation,
pointLightComponent->LinearAttenuation,
pointLightComponent->QuadraticAttenuation,
pointLightComponent->Radius
);
} }
} }
} }
+25 -30
View File
@@ -45,32 +45,17 @@ void GameWorld::Initialize()
RegisterComponents(); RegisterComponents();
auto camera = CreateEntity(); //auto camera = CreateEntity();
{ //{
auto transform = AddComponent<Components::Transform>(camera); // auto transform = AddComponent<Components::Transform>(camera);
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); // auto cameraComp = AddComponent<Components::Camera>(camera);
cameraComp->FarClip = 2000.f; // cameraComp->FarClip = 2000.f;
auto freeSteering = AddComponent<Components::FreeSteering>(camera); // auto freeSteering = AddComponent<Components::FreeSteering>(camera);
} //}
CommitEntity(camera);
auto viewport1 = CreateEntity();
{
auto viewport = AddComponent<Components::Viewport>(viewport1);
viewport->Right = 0.5f;
viewport->Camera = camera;
}
CommitEntity(viewport1);
auto viewport2 = CreateEntity();
{
auto viewport = AddComponent<Components::Viewport>(viewport2);
viewport->Left = 0.5f;
}
CommitEntity(viewport2);
auto player1 = CreateEntity(); auto player1 = CreateEntity();
{ {
@@ -343,7 +328,12 @@ void GameWorld::Initialize()
//auto freeSteering = AddComponent<Components::FreeSteering>(cameraTower); //auto freeSteering = AddComponent<Components::FreeSteering>(cameraTower);
} }
CommitEntity(cameraTower); CommitEntity(cameraTower);
GetComponent<Components::Viewport>(viewport1)->Camera = cameraTower; {
Events::SetViewportCamera e;
e.CameraEntity = cameraTower;
e.ViewportFrame = "Viewport1";
EventBroker->Publish(e);
}
} }
{ {
@@ -761,7 +751,12 @@ void GameWorld::Initialize()
//auto freeSteering = AddComponent<Components::FreeSteering>(cameraTower); //auto freeSteering = AddComponent<Components::FreeSteering>(cameraTower);
} }
CommitEntity(cameraTower); CommitEntity(cameraTower);
GetComponent<Components::Viewport>(viewport2)->Camera = cameraTower; {
Events::SetViewportCamera e;
e.CameraEntity = cameraTower;
e.ViewportFrame = "Viewport2";
EventBroker->Publish(e);
}
} }
{ {
@@ -1103,7 +1098,7 @@ void GameWorld::Initialize()
CommitEntity(entity); CommitEntity(entity);
}*/ }*/
for(int i = 0; i < 1; i++) /*for(int i = 0; i < 1; i++)
{ {
for (int y = 0; y < 15; y++) for (int y = 0; y < 15; y++)
{ {
@@ -1133,7 +1128,7 @@ void GameWorld::Initialize()
CommitEntity(brick); CommitEntity(brick);
} }
} }
} }*/
/*for (int x = 0; x < 5; x++) /*for (int x = 0; x < 5; x++)
for (int y = 0; y < 5; y++) for (int y = 0; y < 5; y++)
+16
View File
@@ -27,7 +27,10 @@ protected:
struct ModelJob : RenderJob struct ModelJob : RenderJob
{ {
unsigned int ShaderID;
unsigned int TextureID; unsigned int TextureID;
GLuint ShaderProgram;
GLuint DiffuseTexture; GLuint DiffuseTexture;
GLuint NormalTexture; GLuint NormalTexture;
GLuint SpecularTexture; GLuint SpecularTexture;
@@ -44,7 +47,10 @@ struct ModelJob : RenderJob
struct SpriteJob : RenderJob struct SpriteJob : RenderJob
{ {
unsigned int ShaderID;
unsigned int TextureID; unsigned int TextureID;
GLuint ShaderProgram;
GLuint Texture; GLuint Texture;
glm::mat4 ModelMatrix; glm::mat4 ModelMatrix;
@@ -70,6 +76,16 @@ public:
m_Jobs.clear(); m_Jobs.clear();
} }
std::forward_list<std::shared_ptr<RenderJob>>::const_iterator begin()
{
return m_Jobs.begin();
}
std::forward_list<std::shared_ptr<RenderJob>>::const_iterator end()
{
return m_Jobs.end();
}
private: private:
std::forward_list<std::shared_ptr<RenderJob>> m_Jobs; std::forward_list<std::shared_ptr<RenderJob>> m_Jobs;
}; };
+326 -126
View File
@@ -17,12 +17,12 @@ Renderer::Renderer()
CAtt = 1.0f; CAtt = 1.0f;
LAtt = 0.0f; LAtt = 0.0f;
QAtt = 3.0f; QAtt = 3.0f;
m_ShadowMapRes = 2048*8; m_ShadowMapRes = 2048*2;
m_SunPosition = glm::vec3(0.f, 1.0f, 0.5f); m_SunPosition = glm::vec3(0.f, 1.0f, 0.5f);
m_SunTarget = glm::vec3(0, 0, 0); m_SunTarget = glm::vec3(0, 0, 0);
m_SunProjection_height = glm::vec2(-40.f, 40.f); m_SunProjection_height = glm::vec2(-40.f, 40.f);
m_SunProjection_width = glm::vec2(-40.f, 40.f); m_SunProjection_width = glm::vec2(-40.f, 40.f);
m_SunProjection_length = glm::vec2(-50.f, 50.f); m_SunProjection_length = glm::vec2(500.f, -500.f);
m_SunProjection = glm::ortho<float>(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); m_SunProjection = glm::ortho<float>(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y);
/* Lights = 0;*/ /* Lights = 0;*/
} }
@@ -76,6 +76,7 @@ void Renderer::Initialize()
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
LoadContent(); LoadContent();
} }
@@ -246,9 +247,181 @@ void Renderer::Draw(double dt)
glfwSwapBuffers(m_Window); glfwSwapBuffers(m_Window);
} }
void Renderer::Draw(RenderQueue &rq) void Renderer::DrawFrame(RenderQueue &rq)
{ {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(m_Viewport.X, -m_Viewport.Y, m_Viewport.Width, m_Viewport.Height);
glScissor(m_Viewport.X, -m_Viewport.Y, m_Viewport.Width, m_Viewport.Height);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(0.0f, 0.5f, 0.0f, 1.0f);
glDisable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix();
//glm::mat4 MVP;
m_ForwardRendering.Bind();
//for (auto tuple : ModelsToRender) //// Todo: Add so it's TransparentModelsToRender
//{
// Model* model;
// glm::mat4 modelMatrix;
// bool visible;
// std::tie(model, modelMatrix, visible, std::ignore) = tuple;
// if (!visible)
// continue;
// MVP = cameraMatrix * modelMatrix;
// glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
// glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
// glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix()));
// glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix((float)m_Width / m_Height)));
// glBindVertexArray(model->VAO);
// for (auto texGroup : model->TextureGroups)
// {
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, *texGroup.Texture);
// glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
// }
//}
for (auto &job : rq)
{
//auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
//if (modelJob)
//{
// glm::mat4 modelMatrix = modelJob->ModelMatrix;
// MVP = cameraMatrix * modelMatrix;
// depthMVP = depthCameraMatrix * modelMatrix;
// 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(), "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(), "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection));
// //glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview));
// glBindVertexArray(modelJob->VAO);
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture);
// if (modelJob->NormalTexture != 0)
// {
// glActiveTexture(GL_TEXTURE2);
// glBindTexture(GL_TEXTURE_2D, modelJob->NormalTexture);
// }
// if (modelJob->SpecularTexture)
// {
// glActiveTexture(GL_TEXTURE3);
// glBindTexture(GL_TEXTURE_2D, modelJob->SpecularTexture);
// }
// glDrawArrays(GL_TRIANGLES, modelJob->StartIndex, modelJob->EndIndex - modelJob->StartIndex + 1);
// continue;
//}
auto spriteJob = std::dynamic_pointer_cast<SpriteJob>(job);
if (spriteJob)
{
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(glm::mat4()));
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(glm::mat4()));
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(glm::mat4()));
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(glm::mat4()));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, spriteJob->Texture);
glBindVertexArray(m_ScreenQuad);
glDrawArrays(GL_TRIANGLES, 0, 6);
continue;
}
}
}
void Renderer::DrawWorld(RenderQueue &rq)
{
glDisable(GL_BLEND);
DrawShadowMap(rq);
/*
Base pass
*/
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass);
glViewport(m_Viewport.X, -m_Viewport.Y, m_Viewport.Width, m_Viewport.Height);
glScissor(m_Viewport.X, -m_Viewport.Y, m_Viewport.Width, m_Viewport.Height);
//glViewport(0, 0, m_Width, m_Height);
// Clear G-buffer
GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffClear);
glClearColor(0.0f, 0.7f, 0.3f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Execute the first render stage which will fill out the internal buffers with data(??)
m_FirstPassProgram.Bind();
GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffOpaque);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
DrawFBOScene(rq);
/*
Lighting pass
*/
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass);
GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, lightingPassAttachments);
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT);
m_SecondPassProgram.Bind();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fPositionTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture);
glCullFace(GL_FRONT);
DrawLightScene(rq);
/*
Final pass
*/
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
//glViewport(m_Viewport.X, m_Viewport.Y, m_Viewport.Width, m_Viewport.Height);
glViewport(0, 0, m_Width, m_Height);
glScissor(0, 0, m_Width, m_Height);
glClear(GL_DEPTH_BUFFER_BIT);
m_FinalPassProgram.Bind();
// Ambient light
glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.7f)));
glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_fLightingTexture);
glCullFace(GL_BACK);
glBindVertexArray(m_ScreenQuad);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void Renderer::Swap()
{
glfwSwapBuffers(m_Window);
} }
#pragma region TempRegion #pragma region TempRegion
@@ -293,7 +466,7 @@ void Renderer::CreateShadowMap(int resolution)
} }
} }
void Renderer::DrawShadowMap() void Renderer::DrawShadowMap(RenderQueue &rq)
{ {
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
@@ -307,7 +480,8 @@ void Renderer::DrawShadowMap()
//glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//Creates the "camera" for the shadowmap from the direction of the sun. //Creates the "camera" for the shadowmap from the direction of the sun.
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1,0,1)); glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 1, 1));
//glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1, 1, 1));
glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; glm::mat4 depthCamera = m_SunProjection * depthViewMatrix;
glm::mat4 MVP; glm::mat4 MVP;
@@ -315,6 +489,23 @@ void Renderer::DrawShadowMap()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons
//For each model, render them to the shadowmap //For each model, render them to the shadowmap
for (auto &job : rq)
{
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
if (modelJob)
{
glm::mat4 modelMatrix = modelJob->ModelMatrix;
MVP = depthCamera * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
//glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview));
glBindVertexArray(modelJob->VAO);
glDrawArrays(GL_TRIANGLES, modelJob->StartIndex, modelJob->EndIndex - modelJob->StartIndex + 1);
continue;
}
}
for (auto tuple : ModelsToRender) for (auto tuple : ModelsToRender)
{ {
Model* model; Model* model;
@@ -675,84 +866,84 @@ void Renderer::FrameBufferTextures()
void Renderer::DrawFBO() void Renderer::DrawFBO()
{ {
DrawShadowMap(); //DrawShadowMap();
for (auto &pair : m_Viewports) //for (auto &pair : m_Viewports)
{ //{
Viewport &viewport = pair.second; // Viewport &viewport = pair.second;
if (!viewport.Camera) // if (!viewport.Camera)
continue; // continue;
int x = viewport.Left * m_Width; // int x = viewport.Left * m_Width;
int y = viewport.Top * m_Height; // int y = viewport.Top * m_Height;
int width = (viewport.Right - viewport.Left) * m_Width; // int width = (viewport.Right - viewport.Left) * m_Width;
int height = (viewport.Bottom - viewport.Top) * m_Height; // 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); // glViewport(0, 0, m_Width, m_Height);
// Clear G-buffer // // Clear G-buffer
GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; // GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffClear); // glDrawBuffers(4, windowBuffClear);
glClearColor(0.0f, 0.3f, 0.7f, 0.f); // glClearColor(0.0f, 0.3f, 0.7f, 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, GL_COLOR_ATTACHMENT3 }; // GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffOpaque); // glDrawBuffers(4, windowBuffOpaque);
glCullFace(GL_BACK); // glCullFace(GL_BACK);
//
DrawFBOScene(viewport); // DrawFBOScene(viewport);
/* // /*
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);
glActiveTexture(GL_TEXTURE2); // glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); // glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture);
glCullFace(GL_FRONT); // glCullFace(GL_FRONT);
DrawLightScene(viewport); // DrawLightScene(viewport);
/* // /*
Final pass // Final pass
*/ // */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glViewport(x, y, width, height); // glViewport(x, y, width, height);
glClear(GL_DEPTH_BUFFER_BIT); // 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.7f))); // glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.7f)));
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::DrawFBO2() void Renderer::DrawFBO2()
@@ -760,14 +951,15 @@ void Renderer::DrawFBO2()
ForwardRendering(); ForwardRendering();
} }
void Renderer::DrawFBOScene(Viewport &viewport) void Renderer::DrawFBOScene(RenderQueue &rq)
{ {
// 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 = viewport.Camera->ProjectionMatrix((float)m_Width / m_Height) * viewport.Camera->ViewMatrix(); glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height);
glm::mat4 cameraMatrix = cameraProjection * m_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,
@@ -776,81 +968,83 @@ void Renderer::DrawFBOScene(Viewport &viewport)
0.5, 0.5, 0.5, 1.0 0.5, 0.5, 0.5, 1.0
); );
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1,0,1)); //glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1, 1, 1));
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 1, 1));
glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; glm::mat4 depthCamera = m_SunProjection * depthViewMatrix;
glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; glm::mat4 depthCameraMatrix = biasMatrix * depthCamera;
glm::mat4 depthMVP; glm::mat4 depthMVP;
glm::vec3 sunDirection = m_SunTarget - m_SunPosition; glm::vec3 sunDirection = m_SunTarget - m_SunPosition;
glm::vec3 sunDirection_cameraview = glm::vec3(m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix() * glm::vec4(sunDirection, 1.0)); glm::vec3 sunDirection_cameraview = glm::vec3(cameraProjection * m_Camera->ViewMatrix() * glm::vec4(sunDirection, 1.0));
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture);
for (auto tuple : ModelsToRender) for (auto &job : rq)
{ {
Model* model; auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
glm::mat4 modelMatrix; if (modelJob)
bool visible;
std::tie(model, modelMatrix, visible, std::ignore) = tuple;
if (!visible)
continue;
MVP = cameraMatrix * modelMatrix;
depthMVP = depthCameraMatrix * modelMatrix;
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(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
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(viewport.Camera->ProjectionMatrix((float)m_Width / m_Height)));
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview));
glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups)
{ {
glm::mat4 modelMatrix = modelJob->ModelMatrix;
MVP = cameraMatrix * modelMatrix;
depthMVP = depthCameraMatrix * modelMatrix;
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(), "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(), "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection));
//glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview));
glBindVertexArray(modelJob->VAO);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture);
if (texGroup.NormalMap) if (modelJob->NormalTexture != 0)
{ {
glActiveTexture(GL_TEXTURE2); glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, *texGroup.NormalMap); glBindTexture(GL_TEXTURE_2D, modelJob->NormalTexture);
} }
if (texGroup.SpecularMap) if (modelJob->SpecularTexture)
{ {
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, *texGroup.SpecularMap); glBindTexture(GL_TEXTURE_2D, modelJob->SpecularTexture);
} }
glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); glDrawArrays(GL_TRIANGLES, modelJob->StartIndex, modelJob->EndIndex - modelJob->StartIndex + 1);
}
}
for (auto tuple : TexturesToRender)
{
Texture* texture;
glm::mat4 modelMatrix;
glm::mat4 billboardMatrix;
std::tie(texture, modelMatrix, billboardMatrix) = tuple;
//MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix );
MVP = cameraMatrix * modelMatrix * billboardMatrix;
depthMVP = depthCameraMatrix * modelMatrix; continue;
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(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
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(viewport.Camera->ProjectionMatrix((float)m_Width / m_Height)));
glActiveTexture(GL_TEXTURE0); //auto spriteJob = std::dynamic_pointer_cast<SpriteJob>(job);
glBindTexture(GL_TEXTURE_2D, *texture); //if (spriteJob)
glBindVertexArray(m_ScreenQuad); //{
glDrawArrays(GL_TRIANGLES, 0, 6); // Texture* texture;
// glm::mat4 modelMatrix;
// glm::mat4 billboardMatrix;
// std::tie(texture, modelMatrix, billboardMatrix) = tuple;
// //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix );
// MVP = cameraMatrix * modelMatrix * billboardMatrix;
// depthMVP = depthCameraMatrix * modelMatrix;
// 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(), "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(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix((float)m_Width / m_Height)));
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, *texture);
// glBindVertexArray(m_ScreenQuad);
// glDrawArrays(GL_TRIANGLES, 0, 6);
// continue;
//}
} }
} }
void Renderer::DrawLightScene(Viewport &viewport) void Renderer::DrawLightScene(RenderQueue &rq)
{ {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendEquation (GL_FUNC_ADD); glBlendEquation (GL_FUNC_ADD);
@@ -860,7 +1054,8 @@ void Renderer::DrawLightScene(Viewport &viewport)
glDepthMask (GL_FALSE); glDepthMask (GL_FALSE);
glBindVertexArray(m_sphereModel->VAO); glBindVertexArray(m_sphereModel->VAO);
glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix((float)m_Width / m_Height) * viewport.Camera->ViewMatrix(); glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height);
glm::mat4 cameraMatrix = cameraProjection * m_Camera->ViewMatrix();
glm::mat4 MVP; glm::mat4 MVP;
for (auto &light : Lights) for (auto &light : Lights)
@@ -869,13 +1064,13 @@ void Renderer::DrawLightScene(Viewport &viewport)
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(viewport.Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix((float)m_Width / m_Height))); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection));
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"), viewport.Camera->Position().x, viewport.Camera->Position().y, viewport.Camera->Position().z); glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_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);
@@ -1002,4 +1197,9 @@ void Renderer::UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat
m_Cameras[cameraIdentifier]->FOV(FOV); m_Cameras[cameraIdentifier]->FOV(FOV);
m_Cameras[cameraIdentifier]->NearClip(nearClip); m_Cameras[cameraIdentifier]->NearClip(nearClip);
m_Cameras[cameraIdentifier]->FarClip(farClip);*/ m_Cameras[cameraIdentifier]->FarClip(farClip);*/
} }
void Renderer::ClearPointLights()
{
Lights.clear();
}
+8 -4
View File
@@ -53,7 +53,9 @@ public:
m_Camera = camera; m_Camera = camera;
} }
void Draw(RenderQueue &rq); void DrawFrame(RenderQueue &rq);
void DrawWorld(RenderQueue &rq);
void Swap();
#pragma endregion #pragma endregion
@@ -70,6 +72,8 @@ public:
float _QuadraticAttenuation, float _QuadraticAttenuation,
float _radius float _radius
); );
void ClearPointLights();
void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding);
void LoadContent(); void LoadContent();
@@ -178,13 +182,13 @@ private:
void ClearStuff(); void ClearStuff();
void DrawScene(); void DrawScene();
void DrawModels(ShaderProgram &shader); void DrawModels(ShaderProgram &shader);
void DrawShadowMap(); void DrawShadowMap(RenderQueue &rq);
void CreateShadowMap(int resolution); void CreateShadowMap(int resolution);
void FrameBufferTextures(); void FrameBufferTextures();
void DrawFBO(); void DrawFBO();
void DrawFBO2(); void DrawFBO2();
void DrawFBOScene(Viewport &viewport); void DrawFBOScene(RenderQueue &rq);
void DrawLightScene(Viewport &viewport); void DrawLightScene(RenderQueue &rq);
void BindFragDataLocation(); void BindFragDataLocation();
glm::mat4 CreateLightMatrix(Light &_light); glm::mat4 CreateLightMatrix(Light &_light);
void UpdateSunProjection(); void UpdateSunProjection();
+38 -1
View File
@@ -155,4 +155,41 @@ void ShaderProgram::Bind()
void ShaderProgram::Unbind() void ShaderProgram::Unbind()
{ {
glActiveShaderProgram(0, 0); glActiveShaderProgram(0, 0);
} }
void ShaderProgram::LoadFromFolder(std::string folderPath)
{
auto path = boost::filesystem::path(folderPath);
if (!boost::filesystem::is_directory(path))
{
LOG_ERROR("Failed to load shader program: \"%s\" is not a directory", folderPath.c_str());
return;
}
for (auto it = boost::filesystem::directory_iterator(path); it != boost::filesystem::directory_iterator(); it++)
{
std::string filename = it->path().filename().string();
if (filename == "Vertex.glsl")
{
AddShader(std::shared_ptr<Shader>(new VertexShader(filename)));
}
else if (filename == "Fragment.glsl")
{
AddShader(std::shared_ptr<Shader>(new FragmentShader(filename)));
}
else if (filename == "Geometry.glsl")
{
AddShader(std::shared_ptr<Shader>(new GeometryShader(filename)));
}
}
}
void ShaderProgram::BindFragDataLocation(int colorNumber, std::string name)
{
if (m_ShaderProgramHandle == 0)
return;
glBindFragDataLocation(m_ShaderProgramHandle, colorNumber, name.c_str());
}
+16 -2
View File
@@ -6,6 +6,11 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include "ResourceManager.h"
class Shader class Shader
{ {
public: public:
@@ -57,23 +62,32 @@ public:
: ShaderType(fileName) { } : ShaderType(fileName) { }
}; };
class ShaderProgram class ShaderProgram : public Resource
{ {
public: public:
ShaderProgram() ShaderProgram()
: m_ShaderProgramHandle(0) { } : m_ShaderProgramHandle(0)
{ }
ShaderProgram(std::string folderPath)
: m_ShaderProgramHandle(0)
{ }
~ShaderProgram(); ~ShaderProgram();
void AddShader(std::shared_ptr<Shader> shader); void AddShader(std::shared_ptr<Shader> shader);
void BindFragDataLocation(int colorNumber, std::string name);
void Compile(); void Compile();
GLuint Link(); GLuint Link();
GLuint GetHandle(); GLuint GetHandle();
operator GLuint() const { return m_ShaderProgramHandle; }
void Bind(); void Bind();
void Unbind(); void Unbind();
private: private:
GLuint m_ShaderProgramHandle; GLuint m_ShaderProgramHandle;
std::vector<std::shared_ptr<Shader>> m_Shaders; std::vector<std::shared_ptr<Shader>> m_Shaders;
void LoadFromFolder(std::string folderPath);
}; };
#endif // ShaderProgram_h__ #endif // ShaderProgram_h__
+1 -3
View File
@@ -4,9 +4,7 @@
void Systems::RenderSystem::RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm) void Systems::RenderSystem::RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm)
{ {
rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load<OBJ>("OBJ", resourceName)); }); rm->RegisterType("Shader", [](std::string resourceName) { return new ShaderProgram(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) void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf)
+2 -1
View File
@@ -5,6 +5,7 @@
#include "System.h" #include "System.h"
#include "Systems/TransformSystem.h" #include "Systems/TransformSystem.h"
#include "ShaderProgram.h"
#include "Model.h" #include "Model.h"
#include "Texture.h" #include "Texture.h"
#include "Components/Transform.h" #include "Components/Transform.h"
@@ -14,11 +15,11 @@
#include "Components/PointLight.h" #include "Components/PointLight.h"
#include "Components/DirectionalLight.h" #include "Components/DirectionalLight.h"
#include "Components/Viewport.h" #include "Components/Viewport.h"
#include "Components/Template.h" #include "Components/Template.h"
#include "Components/Transform.h" #include "Components/Transform.h"
#include "Renderer.h" #include "Renderer.h"
#include "RenderQueue.h" #include "RenderQueue.h"
#include "Events/SetViewportCamera.h"
namespace Systems namespace Systems
{ {