stencil buffer

I have a bunch of different objects in the scene that I need to use the stencil buffer to fill their interiors. I have a different texture to overlay for each one. The problem is, I don’t want one texture to write on top of another object’s stencil.

Should I use glClearStencil before drawing each one? That sounds slow.

Is there a better way?


glClearStencil is used to tell OpenGL what value the stencil should be cleared to, when you use the above.

I think I will still have the same problem though. I’ll try to explain better:

Say that I have 2 stenciled areas and I want a different texture drawn for each stencil. The stencils might be close together and when I draw my big texture, it will draw into the other stencil.

How do I avoid this?

I could clear the stencil buffer before drawing each stencil but that doesn’t seem very optimal. I want to generate all of my stencils and THEN draw them all with different textures.


When you draw each area you can specify a replace in stencil op with a ref value in stencil func.

Then when you want to fill you can perform a stencil equals test with a ref value that is equal to the region value used earlier for the area you want to fill.