Display stencil part of GL_DEPTH24_STENCIL8_EXT

Started by
5 comments, last by NikolajThorsen 11 years, 6 months ago
First off, id like to thank anyone reading and/or replying to this first thread of mine. I have previous found help in others problems/solutions from this site, but i have been unable to find any help regarding my specific problem. So here i go with my own post.

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! smile.png
Advertisement
i know a working way, but maybe not very elegant.
you can attach your fbo a second color texture and render the stencil component to it (just make the second color buffer your render target, enable the stencil test and render a white quade cover the viewport, you know that).
i am a beginner to shader. few days ago, i encounter the same problem you asked above, and can not solve it, too. then i happened read a post somewhere give that solution, though not perfect, but works, and i use it achieved my goal. the efficiency seems ok.
anyway, always hope a better solution.
----
the attached file is what i got.
why i cannot see the attached file?
ok, you can see here: http://user.qzone.qq.com/350479720/blog/1348470101
why i cannot see the attached file?
ok, you can see here: http://user.qzone.qq.com/350479720/blog/1348470101
Yes, i have used a similar solution, but this doesn't use the built-in GL_DEPTH24_STENCIL8_EXT. Having a separate color buffer just for stencil information seems very unnecessary.

What is the correct way of getting the stencil part of GL_DEPTH24_STENCIL8_EXT, if i wish to display the stencil buffer on screen? If i have omitted any important details of my implementation, please tell me and ill give more information.

Yes, i have used a similar solution, but this doesn't use the built-in GL_DEPTH24_STENCIL8_EXT. Having a separate color buffer just for stencil information seems very unnecessary.

What is the correct way of getting the stencil part of GL_DEPTH24_STENCIL8_EXT, if i wish to display the stencil buffer on screen? If i have omitted any important details of my implementation, please tell me and ill give more information.


so, i want to know the answer, too. hope some one can solve this.
Bump.

This topic is closed to new replies.

Advertisement