# OpenGL I want my image to change color based on stencil

I have an image and a rectangle on the screen, using the using this stencil functions I managed to render or not render a certain part of my image.

glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
glStencilFunc( GL_ALWAYS, 1, 0xFF );
graphics.render(texture, changeMaterial);
glStencilFunc( GL_EQUAL, 0, 0xFF );
glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
graphics.render(rectangle);


I want my image to change color based on stencil. Is there a way for me to detect if a pixel is on the stencil so that I can color it to something else.

Here is an example, red one updates the stencil buffer to 1 and when some part of the cyan drawn to those places in the stencil buffer it changes color. Is there a way for me to change the color of those colliding pixels in the shader?

• This sounds like a job for a framebuffer, not a stencil. Draw either the red bit or the cyan bit onto a framebuffer, then draw the other bit onto the screen while using the framebuffer as a texture input in your shader. If you're fancy, you can also use two separate framebuffers for both the red and cyan half, then combine them in a screenspace shader. Oct 28 '21 at 22:42
• Thanks for the advice, I don't think I know what a framebuffer is but I'll learn and try to do what you just said. @Magma Oct 29 '21 at 7:53