Stencil Buffer after resizing

Hello everyone,
I have a problem about stencil buffer: I can use it without problem, but when I resize window all my stencil buffer disappear (the 1s writed disappear, I can see only 0).
Could you please help me? How can I update my stencil buffer while resizing?

Thank you

I’d suggest:

  1. Do your stencil rendering to an FBO
  2. When complete, blit that FBO’s color buffer to the system framebuffer (the window)
  3. When your window receives a resize event, resize your FBO as you see fit.

This will allow you to decide if and when your stencil buffer is ever discarded, and how it is preserved across resize events.

Thank you for your answer:

  • I created and bind FBO

  • I attached to FBO a texture to save my stencil buffer (using glTexImage2D(…))

  • In my paint() function, after bind FBO, I did every “before-render” operation (bind vao, set shader program, transformation operation, glDrawArrays)

  • Finally (after glDrawArrays in paint() function) I did:
    glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, GL_STENCIL_BUFFER_BIT, GL_NEAREST);

  • Now, when i resize window how can I resize FBO? And how can I retain my stencil buffer?

Thank you

Create a new stencil attachment of the new size, attach to another FBO (e.g. FBO2), blit stencil from FBO1 to FBO2 (the part you want to keep), delete FBO’s stencil attachment.

I’ve got a question though: why are you blitting stencil from your FBO to the system framebuffer? Couldn’t you just do all of your rendering in your FBO. And then just blit color from your FBO to the system FB?