Pro OpenGL ES for iOS

(singke) #1

CHAPTER 7: Well-Rendered Miscellany (^221)
„ Line 4 calls our rendering function, pretty much as you would normally
call it. In this case, it is writing both to the stencil buffer and to one of
the color channels at the same time, so we can get a glint of sorts off
of our new shiny stage or platform. Meanwhile, in the stencil buffer, the
background will remain zeros, while the image will produce stencil
pixels that are greater than 0, so it permits image data to write to it
later.
„ Lines 5 and 6 prepare the buffer now for normal use. Line 5 says that if
the value in the currently addressed stencil pixel is 1, keep it
unchanged as given in line 6. Otherwise, pass the fragment through to
be processed as if the stencil buffer wasn’t there (although it may still
be ignored if it fails the depth test). So, any stencil pixel that is 0, the
test will fail, and the incoming fragment will be locked out.
Table 7-1. Possible values for glStencilOp()
Op Type Action
GL_KEEP (^) Keeps the current value.
GL_ZERO Sets the stencil buffer value to 0.
GL_REPLACE Sets the stencil buffer value to ref, as specified by glStencilFunc.
GL_INCR Increments the current stencil buffer value. Clamps to the
maximum representable unsigned value.
GL_INCR_WRAP Increments the current stencil buffer value. Wraps stencil buffer
value to zero when incrementing the maximum representable
unsigned value.
GL_DECR (^) Decrements the current stencil buffer value. Clamps to 0.
GL_DECR_WRAP Decrements the current stencil buffer value. Wraps stencil buffer
value to the maximum representable unsigned value when
decrementing a stencil buffer value of zero.
GL_INVERT Bitwise inverts the current stencil buffer value.
As you can see, the stencil buffer is a very powerful instrument with a lot of subtlety. But
any more extravagant use is reserved for future books as yet unnamed.
Now it’s time for the renderStage() method, as shown in Listing 7-9.
Listing 7-9. Rendering the reflective area to the stencil buffer only
-(void)renderStage
{
static const GLfloat flatSquareVertices[] =
{
-0.5, 0.0, -.5f,

Free download pdf