Mixing Deferred and Forward rendering

- TODO: fix Skybox affected by lighting pass
This commit is contained in:
Nigel Barink 2022-12-28 23:17:55 +01:00
parent 2dcc3f1803
commit 3a9c07aff9
2 changed files with 20 additions and 48 deletions

View File

@ -23,29 +23,12 @@ namespace YoggieEngine {
// Create a depth buffer // Create a depth buffer
glGenTextures(1, &DepthAttachment); glGenRenderbuffers(1, &DepthAttachment);
glBindTexture(GL_TEXTURE_2D, DepthAttachment); glBindRenderbuffer(GL_RENDERBUFFER, DepthAttachment);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DepthAttachment);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, DepthAttachment, 0);
/*
* // Render buffer
glGenRenderbuffers(1, &DepthAttachment);
glBindRenderbuffer(GL_RENDERBUFFER, DepthAttachment);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_RENDERBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DepthAttachment);
*/
if (!glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) if (!glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
{ {

View File

@ -6,8 +6,6 @@
#include "../Graphics/Memory/VertexArray.h" #include "../Graphics/Memory/VertexArray.h"
#include "../Graphics/Primitives/DrawCommand.h" #include "../Graphics/Primitives/DrawCommand.h"
#define FORWARD 1
#define DEFERRED 0
extern YoggieEngine::Camera cam; extern YoggieEngine::Camera cam;
namespace YoggieEngine { namespace YoggieEngine {
@ -72,7 +70,6 @@ Renderer::Renderer(RendererConfig& config)
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
#if DEFERRED
// Deferred Rendering // Deferred Rendering
glGenFramebuffers(1, &gBuffer); glGenFramebuffers(1, &gBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
@ -117,7 +114,6 @@ Renderer::Renderer(RendererConfig& config)
} }
#endif
std::vector<std::string> faces{ std::vector<std::string> faces{
"build/Debug/skybox/Open_Water/right.jpg", "build/Debug/skybox/Open_Water/right.jpg",
@ -196,9 +192,6 @@ void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& t
void Renderer::GeometryPass() { void Renderer::GeometryPass() {
// 1.0 Geometry pass // 1.0 Geometry pass
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gBufferShader.Use(); gBufferShader.Use();
for (const DrawCommand& command : commands) for (const DrawCommand& command : commands)
@ -246,8 +239,14 @@ void Renderer::SkyboxPass() {
void Renderer::lightingPass(Scene& scene){ void Renderer::lightingPass(Scene& scene){
// 2.0 Lighting Pass // 2.0 Lighting Pass
// configure shader
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId()); glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId());
lightingPassShader.Use(); lightingPassShader.Use();
lightingPassShader.setUniformInt("gPosition", 0);
lightingPassShader.setUniformInt("gNormal", 1);
lightingPassShader.setUniformInt("gColorSpec", 2);
// Bind all Gbuffer textures // Bind all Gbuffer textures
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -305,34 +304,25 @@ void Renderer::lightingPass(Scene& scene){
void Renderer::Render(Scene& scene) void Renderer::Render(Scene& scene)
{ {
#if DEFERRED
// configure shader
lightingPassShader.Use();
lightingPassShader.setUniformInt("gPosition", 0);
lightingPassShader.setUniformInt("gNormal", 1);
lightingPassShader.setUniformInt("gColorSpec", 2);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SkyboxPass();
GeometryPass(); GeometryPass();
lightingPass(scene); lightingPass(scene);
glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_framebuffer.GetId());
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
#endif
#if FORWARD
// Forward rendering approach // Forward rendering approach
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId()); glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId());
glClearColor(m_clearColor.r, m_clearColor.g, m_clearColor.b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SkyboxPass();
for (const DrawCommand& command : commands) for (const DrawCommand& command : commands)
{ {
glBindVertexArray(command.VAO_identifier); glBindVertexArray(command.VAO_identifier);
@ -372,7 +362,6 @@ void Renderer::Render(Scene& scene)
*/ */
#endif
commands.clear(); commands.clear();
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);