I want to use the stencil buffer to mask out my background in a deferred renderer, so the full-screen directional light pass only draws to pixels that have had something drawn to them, and for other things. So far it is not working in a predictable way.
The stencil buffer is cleared like this:
glStencilMask(0xFF);
glClearNamedFramebufferfi(fbo, GL_DEPTH_STENCIL, 0, 1.0f, 0);
glStencilMask(0x1);
I prepare to draw objects like this:
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilMask(0x1);
Now I get ready to draw the full-screen pass for directional lights:
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilFunc(GL_EQUAL, 0, 0xFF);// Background pixels get drawn to
//glStencilFunc(GL_EQUAL, 1, 0xFF);// Nothing gets drawn
glStencilMask(0);
If I use glStencilFunc(GL_EQUAL, 0, 0xFF) then the full-screen pass draws to the background pixels (where nothing has been drawn).
This indicates that the FBO is correctly set up with an active stencil buffer attached.
However, calling glStencilFunc(GL_EQUAL, 1, 0xFF) does not draw lighting on my foreground pixels (where objects have been drawn). I just get a black screen instead.
What am I doing wrong?


