I’m trying to get OIT working using the stencil routed k-buffer technique, however I can’t seem to initialize the stencil buffer or get it working correctly. I go through and fill each sample of each pixel with an increasing sequence of numbers and then render a quad to the screen wherever stencil = 2. But I get an all black screen. Does anybody know what I’m doing wrong? Thanks.
// Initialize the stencil buffer
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glViewport(0, 0, WIDTH, HEIGHT);
glClearStencil(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilMask(GL_TRUE);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glEnable(GL_MULTISAMPLE);
glEnable(GL_SAMPLE_MASK);
// Fill ALL samples within the quad with a stencil value of 2.
glSampleMaski(0, ~0);
glStencilFunc(GL_ALWAYS, 2, ~0);
drawFullscreenQuad(0.5f);
glDisable(GL_SAMPLE_MASK);
glDisable(GL_MULTISAMPLE);
glDisable(GL_STENCIL_TEST);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glStencilMask(GL_FALSE);
// Manually draw data to the multisample framebuffer.
glDisable(GL_MULTISAMPLE);
glDisable(GL_SAMPLE_MASK);
// Only render the quad into the framebuffer where stencil equals 2, which should be ALL samples within each pixel.
glStencilFunc(GL_EQUAL, 2, ~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilMask(GL_TRUE);
glEnable(GL_STENCIL_TEST);
glColor4f(1, 0, 0, 1);
drawFullscreenQuad(1.0f);
glDisable(GL_STENCIL_TEST);
glStencilMask(GL_FALSE);
I also have a custom resolve shader which is dead simple.
#version 330
uniform sampler2DMS colorBuffer;
uniform int numSamples;
in vec2 vs_TexCoord;
out vec4 FragColor;
void main()
{
vec4 color;
for (int i = 0; i < numSamples; i++)
{
color += texelFetch(colorBuffer, ivec2(vs_TexCoord), i);
}
FragColor = color / float(numSamples);
}