2

I'm a beginner at OpenGL and while learning about stenciling this one function has been troubling me (glStencilMask).

I've been told that it can be used to enable or disable stenciling, how is this?

Why are hexadecimal values passed into this function?

Why are is the HEX value 0xff and 0x00 often passed specifically?

Does this function prevent drawing to the color buffer and/or the stencil buffer?

Would you kindly explain what its doing in simple terms?

Barkin MAD
  • 127
  • 1
  • 10
  • The stencil is used as a mask when painting something to the color buffer. Something like a clothing stencil technique. If you set 0 for the function, it will block everything. 1 will accept any coloring. Any other value it will do a [bitwise](http://www.cs.rutgers.edu/~decarlo/428/gl_man/stencilmask.html) comparison. This is how I remember it, unfortunately I dont have time right now to check it. – wendelbsilva Jul 27 '15 at 18:46
  • Did you read the documentation yet? – Lightness Races in Orbit Jul 27 '15 at 19:48

1 Answers1

6

Do you know how bitmasks work? That is what this is.

0xff is 11111111 in binary. That means GL can write to all 8 of the stencil bits.

0x00 is 00000000 in binary, and GL is not allowed to write to any bits when this mask is used.

Since the stencil buffer is effectively one large bitwise machine, it would serve you well to brush up on or learn these concepts in detail. If you are having trouble understanding why you would want to mask off certain bits, you may not be able to make effective use of the stencil buffer.

Masking off certain bits between passes will let you preserve the results stored in parts of the stencil buffer. Why you would want this is entirely application-specific, but this is how the stencil buffer works.


The stencil mask never disables the stencil buffer completely, you'd actually have to call glDisable (GL_STENCIL_TEST) for that. It simply enables or disables writes to portions of it.

On a final note, if you disable GL_STENCIL_TEST or GL_DEPTH_TEST that actually does two things:

  1. Disables the test
  2. Disables writing stencil / depth values

So, if for some reason, you ever wanted to write a constant depth or stencil value and you assumed that disabling the test would accomplish that -- it won't. Use GL_ALWAYS for the test function instead of disabling the test if that is your intention.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106