Jump to content
  • Advertisement
Sign in to follow this  

Stencil buffer for masking stuff?

This topic is 3417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);

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

// Set new stencil stuff to use the stencil buffer to decide where to write to the screen
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!