Sign in to follow this  
ElectroDruid

Stencil buffer for masking stuff?

Recommended Posts

Hi, I'm having some trouble getting the stencil buffer to do what I want it to in the 2D game I'm working on. I'm trying to draw a tiled texture across my level, but only having it appear in places where there is solid level geometry. I'm defining "solid level geometry" as "anywhere I've drawn a sprite into the stencil buffer". My sprites are just textured quads, which are solid white where I want to see the tiled texture, and transparent where I don't. So, my code goes:
// (Draw background, game sprites and stuff...)

// Enable writing into the stencil buffer
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);

// (Draw my solid white/transparent stencil sprites)

// Set new stencil stuff to use the stencil buffer to decide where to write to the screen
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);

// (Draw my tiled, textured quad)
This doesn't work, though. Basically, all of my attempts to draw the sprites into the stencil buffer seem to result the whole quad getting written to the buffer, rather than just the solid white texels. What am I doing wrong?

Share this post


Link to post
Share on other sites
It's been a while since I've done this, but from quickly looking at your code, I have a suspicion you're mixing up the stencil test with writing to the stencil buffer. First thing to do is make sure your surface actually has a stencil buffer. Second thing is what I suggested, enabling the stencil test means you're checking what's in the stencil buffer already and writing if the condition is satisfied. You want to enable writing to the stencil buffer, draw some stuff, and then flick on stencil testing to draw other stuff that will get masked.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this