FBO and stencil

Hello,

how to use the stencil buffer inside an FBO ? Do you have any examples or good lectures ?

Thanks.

You can use a FBO that has a GL_DEPTH24_STENCIL8 render buffer attachment.


const GLint FBO_WIDTH = 1024;
const GLint FBO_HEIGHT = 768;

GLuint color_buffer_id, depth_stencil_id, fbo_id;
GLenum status;

// create a texture we use as the color buffer
glGenTextures(1, &color_buffer_id);
glEnable(GL_TEXTURE2D);
glBindTexture(GL_TEXTURE_2D, &color_buffer_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FBO_WIDTH, FBO_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);

// create a renderbuffer for the depth/stencil buffer
glGenRenderbuffers(1, &depth_stencil_id);
glBindRenderbuffer(GL_RENDERBUFFER, depth_stencil_id);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, FBO_WIDTH, FBO_HEIGHT);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

glGenFramebuffers(1, &fbo_id);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT_0, GL_TEXTURE_2D, color_buffer_id, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth_stencil_id);

status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if ( GL_FRAMEBUFFER_COMPLETE != status )

{
    // something went wrong :(
}
else
{
    // profit :)
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

Well thanks !

And now, in the “else” part, can I use it as in a normal stencil test ?

That’s right.