Im working on a deferred rendering engine at the moment, and i have multiple render targets. Along with storing positions, normals and diffuse color, i want one of these render targets to be used to store the depth and stencil values.
Heres the code i use to create the texture:
..
glGenTextures(1, &depth_stencil_buffer);
glBindTexture(GL_TEXTURE_2D, depth_stencil_buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT, width, height, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D , depth_stencil_buffer, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D , depth_stencil_buffer, 0);
..
Now i can bind this texture, depth_stencil_buffer in the above code snippet, and render it on a fullscreen quad, and i can correctly see depth of the scene geometry. Heres the not so special code:
..
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,depth_stencil_buffer);
glUniform1i(glGetUniformLocation(m_uiTextureProgram, "tex"),0);
glBindVertexArray(m_ui_texture_VAO);
glDrawArrays(GL_TRIANGLES, 0,6);
glBindVertexArray(0);
glUseProgram(0);
..
And my GLSL shader code looks something like this:
in vec2 tex_c;
out vec4 output0;
uniform sampler2D tex;
void main()
{
output0 = texture(tex,tex_c);
}
Using this code its possible for me to display and observe the depth buffer.
However, if I wish to render the stencil part of the texture, all i get is a white screen. I enable stencil testing, and even with a call to glClearStencil(0), which i think should turn the screen black instead of white, i still see only white tho. The way i try to render the stencil buffer, is by grabbing the .w coordinate (which should be the last 8bit containing stencil value) of the depth/stencil texture in the shader, and outputting just this value to screen. Heres the shader code that i use for this:
in vec2 tex_c;
out vec4 output0;
uniform sampler2D tex;
void main()
{
output0 = vec4(texture(tex,tex_c).w);
}
I read in some OpenGL documentation (something i only vaguely remember), that if .w coordinate is missing (like if its only a vec3), getting .w in a shader always results in the value 1.0f. Could this be whats happening?
This is btw not the only way if tried to render the stencil buffer to screen. But i have yet to find a working solution. Any and all help is appreciated!