4

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. Correct Rendering without Stencil Buffer

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.

Incorrect, Garbled Rendering

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.

Chase Walden
  • 1,252
  • 1
  • 14
  • 31

1 Answers1

7
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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Wow, Nicol, it seems I can Always count on you to answer my questions. In this case, how do I make two separate textures one showing the depth and the other showing the stencil. – Chase Walden Nov 01 '12 at 06:17
  • @Chase: You don't. The point is to be able to capture the stencil, right? How many textures this happens to use is irrelevant if you have the data you need. – Nicol Bolas Nov 01 '12 at 06:53
  • @ChaseWalden: Why do you want them separated? – datenwolf Nov 01 '12 at 06:54
  • @datenwolf so that I can render the single texture to the screen for debugging purposes. – Chase Walden Nov 01 '12 at 15:09
  • @ChaseWalden: Well, unless you have access to the recent [ARB_stencil_texturing](http://www.opengl.org/registry/specs/ARB/stencil_texturing.txt) extension, you *can't read* from a stencil texture of any kind in a shader. So you can't render one to the screen for any purpose. And even if you do have access to stencil texturing, you don't need to use separate textures just to draw it to the screen. You draw it in depth mode, then switch and draw it in stencil mode. – Nicol Bolas Nov 01 '12 at 15:39
  • @NicolBolas what is depth and stencil mode??? do you mean `glEnable(GL_DEPTH);` or `glEnable(GL_STENCIL)`? – Chase Walden Nov 01 '12 at 16:45
  • @ChaseWalden: That would be the extension I mentioned that allows you to access the stencil component of a texture. – Nicol Bolas Nov 01 '12 at 17:17
  • @NicolBolas ok I read the docs a little more closely. Thanks for your help. – Chase Walden Nov 01 '12 at 17:21
  • @NicolBolas Is there anyway to do this now (9 years have passed) without the extension? I want to use the stencil value I have set in next render pass and do not seem to find anyway to read the stencil value from the texture. I am using OpenGL ES 3.0. – Pankaj Bansal May 19 '22 at 12:48
  • @PankajBansal: ES 3.2 might have it as a core feature. It's core in desktop GL 4.3+. – Nicol Bolas May 19 '22 at 13:21