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.

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

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?

Red one sets the stencil buffer to 1

When cyan collides with it, colliding parts change color

  • \$\begingroup\$ 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. \$\endgroup\$
    – Magma
    Oct 28, 2021 at 22:42
  • \$\begingroup\$ 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 \$\endgroup\$
    – shrain
    Oct 29, 2021 at 7:53


You must log in to answer this question.

Browse other questions tagged .