stencil buffer is working, no more skybox artifacts + wireframe mode is unexpectedly interesting

This commit is contained in:
Anselme 2017-08-25 14:16:44 +02:00
parent 2baf7a5d95
commit f4e71649ad
8 changed files with 51 additions and 31 deletions

View File

@ -2,11 +2,13 @@
in vec2 varTexCoord; in vec2 varTexCoord;
// LIGHT BUFFER #ifdef COLOR
uniform vec3 color;
#else
uniform sampler2DRect debuggedBuffer; uniform sampler2DRect debuggedBuffer;
#endif
// OUTPUT LIGHT // OUTPUT
layout(location = 0)out vec4 outColor; layout(location = 0)out vec4 outColor;

View File

@ -7,8 +7,6 @@ uniform sampler2DRect positionBuffer;
uniform sampler2DRect albedoBuffer; uniform sampler2DRect albedoBuffer;
// - Normal buffer // - Normal buffer
uniform sampler2DRect normalBuffer; uniform sampler2DRect normalBuffer;
// - depth buffer
uniform sampler2DRect depthBuffer;
#endif #endif
// - Emission + Metallic // - Emission + Metallic
uniform sampler2DRect emissionBuffer; uniform sampler2DRect emissionBuffer;
@ -170,7 +168,6 @@ void main(void) {
vec3 emission = emissionTexel.rgb; vec3 emission = emissionTexel.rgb;
float metallic = emissionTexel.a; float metallic = emissionTexel.a;
vec4 fragPos = texelFetch(positionBuffer, texCoord); vec4 fragPos = texelFetch(positionBuffer, texCoord);
float depth = texelFetch(depthBuffer, texCoord).r;
// compute shadow // compute shadow
#ifdef SHADOWMAP #ifdef SHADOWMAP

View File

@ -26,7 +26,7 @@
RESOURCE_PACK(shaders) RESOURCE_PACK(shaders)
GBuffer::GBuffer(int width, int height) : FrameBuffer() GBuffer::GBuffer(int width, int height, GLuint renderBuffer) : FrameBuffer()
{ {
Texture* tex; Texture* tex;
@ -54,11 +54,8 @@ GBuffer::GBuffer(int width, int height) : FrameBuffer()
tex->setUnit(EMISSION); tex->setUnit(EMISSION);
addTexture(tex, GL_COLOR_ATTACHMENT3); addTexture(tex, GL_COLOR_ATTACHMENT3);
// - depth and stencil buffer addRenderBuffer(renderBuffer, GL_DEPTH_ATTACHMENT);
tex = new Texture(GL_DEPTH_STENCIL, GL_DEPTH24_STENCIL8, width, height, GL_UNSIGNED_INT_24_8, GL_TEXTURE_RECTANGLE); addRenderBuffer(renderBuffer, GL_STENCIL_ATTACHMENT);
tex->setFiltering(GL_NEAREST);
tex->setUnit(DEPTH);
addTexture(tex, GL_DEPTH_STENCIL_ATTACHMENT);
initColorAttachments(); initColorAttachments();
} }
@ -75,13 +72,16 @@ void GBuffer::unbindTextures()
getTexture(i)->unbind(); getTexture(i)->unbind();
} }
LightingBuffer::LightingBuffer(int width, int height) : FrameBuffer() LightingBuffer::LightingBuffer(int width, int height, GLuint renderBuffer) : FrameBuffer()
{ {
// colors are encoded in float to allow tonemapping // colors are encoded in float to allow tonemapping
Texture* tex = new Texture(GL_RGBA, GL_RGBA16F, width, height, GL_FLOAT, GL_TEXTURE_RECTANGLE); Texture* tex = new Texture(GL_RGBA, GL_RGBA16F, width, height, GL_FLOAT, GL_TEXTURE_RECTANGLE);
tex->setFiltering(GL_NEAREST); tex->setFiltering(GL_NEAREST);
addTexture(tex, GL_COLOR_ATTACHMENT0); addTexture(tex, GL_COLOR_ATTACHMENT0);
addRenderBuffer(renderBuffer, GL_DEPTH_ATTACHMENT);
addRenderBuffer(renderBuffer, GL_STENCIL_ATTACHMENT);
initColorAttachments(); initColorAttachments();
} }
@ -90,6 +90,7 @@ DeferredPipeline::DeferredPipeline() :
m_skybox(nullptr), m_skybox(nullptr),
m_width(512), m_width(512),
m_height(512), m_height(512),
m_depth_stencil_renderBuffer(0),
m_gBuffer(nullptr), m_gBuffer(nullptr),
m_lightingBuffer(nullptr), m_lightingBuffer(nullptr),
m_renderTarget(nullptr), m_renderTarget(nullptr),
@ -137,13 +138,14 @@ void DeferredPipeline::renderGL(Scene *scene)
glDisable(GL_BLEND); glDisable(GL_BLEND);
glClearColor(0.0f, 0.0f, 0.0f, 1.f); glClearColor(0.0f, 0.0f, 0.0f, 1.f);
glClearDepth(1.0f); glClearDepth(1.0f);
glClearStencil(1); glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glStencilMask(0xFF); glStencilMask(0x01);
glStencilFunc(GL_ALWAYS, 1, 0xFF); // always draw fragments (ignore the stencil buffer values for now) glStencilFunc(GL_ALWAYS, 0x01, 0x01); // always draw fragments (ignore the stencil buffer values for now)
// draw skybox // draw skybox
if(m_skybox != nullptr) if(m_skybox != nullptr)
{ {
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
m_skybox->renderGL(m_camera); m_skybox->renderGL(m_camera);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
} }
@ -180,24 +182,25 @@ void DeferredPipeline::renderGL(Scene *scene)
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glEnable(GL_STENCIL_TEST);
glBlendFunc(GL_ONE, GL_ONE); glBlendFunc(GL_ONE, GL_ONE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
m_gBuffer->bindTextures(); m_gBuffer->bindTextures();
// draw unlit fragments // draw unlit fragments
/*
Shader *shader = m_lightShaders[1 << Light::UNLIT_FLAG]; Shader *shader = m_lightShaders[1 << Light::UNLIT_FLAG];
if(shader != nullptr) if(shader != nullptr)
{ {
shader->bind(); shader->bind();
shader->bindInteger(shader->getLocation("emissionBuffer"), GBuffer::EMISSION); shader->bindInteger(shader->getLocation("emissionBuffer"), GBuffer::EMISSION);
//glStencilFunc(GL_EQUAL, 1, 0xFF); glStencilFunc(GL_EQUAL, 1, 0x01);
SparrowRenderer::drawQuad(); SparrowRenderer::drawQuad();
} }
*/
// loop on light sources // loop on light sources
//glStencilFunc(GL_EQUAL, 0, 0xFF); glStencilFunc(GL_EQUAL, 0, 0x01);
for(SceneIterator<Light*>* lightIt = scene->getLights(); for(SceneIterator<Light*>* lightIt = scene->getLights();
lightIt->isValid(); lightIt->next()) lightIt->isValid(); lightIt->next())
{ {
@ -215,7 +218,6 @@ void DeferredPipeline::renderGL(Scene *scene)
shader->bindInteger(shader->getLocation("albedoBuffer"), GBuffer::ALBEDO); shader->bindInteger(shader->getLocation("albedoBuffer"), GBuffer::ALBEDO);
shader->bindInteger(shader->getLocation("normalBuffer"), GBuffer::NORMAL); shader->bindInteger(shader->getLocation("normalBuffer"), GBuffer::NORMAL);
shader->bindInteger(shader->getLocation("emissionBuffer"), GBuffer::EMISSION); shader->bindInteger(shader->getLocation("emissionBuffer"), GBuffer::EMISSION);
shader->bindInteger(shader->getLocation("depthBuffer"), GBuffer::DEPTH);
// bind light // bind light
light->bindAttributes(shader, m_camera); light->bindAttributes(shader, m_camera);
@ -229,6 +231,7 @@ void DeferredPipeline::renderGL(Scene *scene)
m_renderTarget->bindFBO(); m_renderTarget->bindFBO();
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDisable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST);
glStencilMask(0);
m_lightingBuffer->getTexture(0)->bind(0); m_lightingBuffer->getTexture(0)->bind(0);
m_postEffectsShader->bind(); m_postEffectsShader->bind();
m_postEffectsShader->bindInteger(m_postEffectsShader->getLocation("lightBuffer"), 0); m_postEffectsShader->bindInteger(m_postEffectsShader->getLocation("lightBuffer"), 0);
@ -344,14 +347,22 @@ void DeferredPipeline::resizeGL(int w, int h)
if(m_skybox != nullptr) if(m_skybox != nullptr)
m_skybox->resizeGL(w, h); m_skybox->resizeGL(w, h);
if(m_depth_stencil_renderBuffer != 0)
glDeleteRenderbuffers(1, &m_depth_stencil_renderBuffer);
// rebuilding FrameBuffers // rebuilding FrameBuffers
if(m_gBuffer != NULL) if(m_gBuffer != NULL)
{ {
delete m_gBuffer; delete m_gBuffer;
delete m_lightingBuffer; delete m_lightingBuffer;
} }
m_gBuffer = new GBuffer(w, h);
m_lightingBuffer = new LightingBuffer(w, h); glGenRenderbuffers(1, &m_depth_stencil_renderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_depth_stencil_renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
m_gBuffer = new GBuffer(w, h, m_depth_stencil_renderBuffer);
m_lightingBuffer = new LightingBuffer(w, h, m_depth_stencil_renderBuffer);
m_orthoMatrix = glm::ortho(0.f, float(m_width), float(m_height), 0.f); m_orthoMatrix = glm::ortho(0.f, float(m_width), float(m_height), 0.f);
m_guiMesh->resizeGL(w, h); m_guiMesh->resizeGL(w, h);
} }
@ -403,8 +414,7 @@ void DeferredPipeline::refreshScene(Scene *scene)
glm::vec4 DeferredPipeline::pick(int x, int y) glm::vec4 DeferredPipeline::pick(int x, int y)
{ {
m_gBuffer->setTarget(GL_READ_FRAMEBUFFER); m_gBuffer->setTarget(GL_READ_FRAMEBUFFER);
m_gBuffer->bindFBO(); m_gBuffer->bindFBO();
//m_gBuffer->getTexture(0)->
float values[4] = {1}; float values[4] = {1};
glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, values); glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, values);
GLenum err = glGetError(); GLenum err = glGetError();

View File

@ -14,8 +14,8 @@ class GuiMesh;
class GBuffer : public FrameBuffer class GBuffer : public FrameBuffer
{ {
public: public:
enum Buffers { POSITION, ALBEDO, NORMAL, EMISSION, DEPTH, NB_BUFFERS }; enum Buffers { POSITION, ALBEDO, NORMAL, EMISSION, NB_BUFFERS };
GBuffer(int width, int height); GBuffer(int width, int height, GLuint renderBuffer);
void bindTextures(); void bindTextures();
void unbindTextures(); void unbindTextures();
}; };
@ -23,7 +23,7 @@ public:
class LightingBuffer : public FrameBuffer class LightingBuffer : public FrameBuffer
{ {
public: public:
LightingBuffer(int width, int height); LightingBuffer(int width, int height, GLuint renderBuffer);
}; };
class DeferredPipeline : public Pipeline class DeferredPipeline : public Pipeline
@ -54,6 +54,7 @@ class DeferredPipeline : public Pipeline
GuiMesh * m_guiMesh; GuiMesh * m_guiMesh;
// framebuffers // framebuffers
GLuint m_depth_stencil_renderBuffer;
GBuffer *m_gBuffer; GBuffer *m_gBuffer;
LightingBuffer *m_lightingBuffer; LightingBuffer *m_lightingBuffer;
const FrameBuffer *m_renderTarget; const FrameBuffer *m_renderTarget;

View File

@ -38,6 +38,15 @@ void FrameBuffer::addTexture(Texture* tex, GLenum attachment)
} }
} }
void FrameBuffer::addRenderBuffer(GLuint renderBufferId, GLenum attachment)
{
if(m_frameBufferId != 0)
{
bindFBO();
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, renderBufferId);
}
}
void FrameBuffer::initColorAttachments() void FrameBuffer::initColorAttachments()
{ {
if(m_frameBufferId != 0 && m_attachments.size() != 0) if(m_frameBufferId != 0 && m_attachments.size() != 0)

View File

@ -40,6 +40,8 @@ public:
void addTexture(Texture* tex, GLenum attachment); void addTexture(Texture* tex, GLenum attachment);
void addRenderBuffer(GLuint renderBufferId, GLenum attachment);
void initColorAttachments(); void initColorAttachments();
void deleteTextures(); void deleteTextures();

View File

@ -174,9 +174,9 @@ void Mesh::initGL()
void Mesh::draw(Shader* shader) void Mesh::draw(Shader* shader)
{ {
if(isWireframe) if(isWireframe)
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
else else
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
if(isDoubleSided) if(isDoubleSided)
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
if(material != NULL) if(material != NULL)

View File

@ -54,7 +54,6 @@ void Skybox::renderGL(Camera* myCamera)
cubeMap->bind(0); cubeMap->bind(0);
glBindVertexArray(vao); glBindVertexArray(vao);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL);
glBindVertexArray(0); glBindVertexArray(0);