Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a stencil render buffer to a frame buffer in opengl

Have anyone done this succesfully? It seems whatever index format I use in the stencil render buffer glCheckFramebufferStatus(...) returns GL_FRAMEBUFFER_UNSUPPORTED. I've succesfully bound both a depth\color render buffer, but whenever I try to do the same thing with my stencil buffer I get (as I said) GL_FRAMEBUFFER_UNSUPPORTED.

Here is snippets of my code:

// Create frame buffer
GLuint fb;
glGenFramebuffers(1, &fb);

// Create stencil render buffer (note that I create depth buffer the exact same way, and It works.
GLuint sb;
glGenRenderbuffers(1, &sb);
glBindRenderbuffer(GL_RENDERBUFFER, sb);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h);

// Attach color
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cb, 0);

// Attach stencil buffer
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
// And here I get an GL_FRAMEBUFFER_UNSUPPORTED when doing glCheckFramebufferStatus()

Any ideas?

Note: The color attachement is a texture and not a renderbuffer

like image 784
Viktor Sehr Avatar asked Jun 18 '12 14:06

Viktor Sehr


People also ask

What does stencil buffer do in OpenGL?

A stencil buffer (usually) contains 8 bits per stencil value that amounts to a total of 256 different stencil values per pixel. We can set these stencil values to values of our liking and we can discard or keep fragments whenever a particular fragment has a certain stencil value.

What is frame buffer in OpenGL?

A Framebuffer is a collection of buffers that can be used as the destination for rendering. OpenGL has two kinds of framebuffers: the Default Framebuffer, which is provided by the OpenGL Context; and user-created framebuffers called Framebuffer Objects (FBOs).

What is a framebuffer attachment?

Framebuffers represent a collection of memory attachments that are used by a render pass instance. Examples of these memory attachments include the color image buffers and depth buffer that we created in previous samples. A framebuffer provides the attachments that a render pass needs while rendering.

How do you make a frame buffer?

First thing to do is to create an actual framebuffer object and bind it, this is all relatively straightforward: unsigned int framebuffer; glGenFramebuffers (1, &framebuffer); glBindFramebuffer (GL_FRAMEBUFFER, framebuffer); Next we create a texture image that we attach as a color attachment to the framebuffer.


2 Answers

Never use a free-standing stencil buffer. If you need stencil, always use a depth+stencil image format. Note that the stencil index formats are not required image formats.

Even though you're not using a depth buffer here, you still should use GL_DEPTH24_STENCIL8, which you should attach to GL_DEPTH_STENCIL_ATTACHMENT​.

like image 55
Nicol Bolas Avatar answered Oct 02 '22 08:10

Nicol Bolas


You can use stencil-only on recent nvidia hardware/drivers

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, fboStencilTexture, 0);

Still no support for both separate depth and stencil

like image 45
Anton Duzenko Avatar answered Oct 02 '22 09:10

Anton Duzenko