Visualizing the Stencil Buffer to a texture

2019-04-11 12:28发布

问题:

I'm trying to put the stencil buffer into a texture for use in a deferred renderer.

I'm getting other Color and Depth Attachments with glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[color1], 0); and the result is correct.

However when I try to attach my stencil buffer to a texture with glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT ,GL_TEXTURE_2D, textures[stencil], 0); I get a garbled result, as if the FBO isn't clearing its buffers.

I don't know what the problem is. My suspicion is that it is an issue with setting up the stencil texture ...

//Stencil Texture Initialization

glBindTexture(GL_TEXTURE_2D, textures[stencil]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexImage2D( GL_TEXTURE_2D, 0, GL_STENCIL_INDEX8, 512, 512, 0, GL_STENCIL_INDEX, GL_BYTE, 0);

I don't get any error codes in my setup so I am at a loss of what the issue is.

EDIT: Maybe I'm not doing this correctly. If anyone knows how to write the stencil buffer to a texture, I don't really care if I write different cod. I just need a method that works.

回答1:

GL_STENCIL_INDEX8

Never use this. If you need stencil bits, always use a packed depth/stencil image, like GL_DEPTH24_STENCIL8. You should attach this to the GL_DEPTH_STENCIL_ATTACHMENT point.