OpenGL: Clear the Stencil buffer, except for certain bits

转载自:  https://stackoverflow.com/questions/27414593/opengl-clear-the-stencil-buffer-except-for-certain-bits




I'm using the stencil buffer for two jobs. The first is to allow masking to happen, and the second is to write masks for objects that can be 'seen' through. In this particular case, the reserved bit is 0x80, the very last bit in the byte, with the rest left for regular masking.

The first purpose requires that the buffer be cleared after around 127 uses, or else past stencils will become "valid" again when testing, since the value must wrap back to 1. The second purpose requires the reserved bits in the buffer to stay alive through the entire frame.

Is there any way to clear the entire stencil buffer, while keeping the reserved bits set?

share improve this question
 
 
Searched this problem, using color clearing instead since I imagine that's more common, and found my answer; glClear() will heed glColorMask(), so I imagine that applies to glStencilMask() as well –  Anne Quinn  Dec 11 '14 at 3:14

1 Answer

up vote 1 down vote accepted

Your theory in the comment is correct. glStencilMask() is applied to the values written by glClear() and glClearBuffer().

From section "17.4.3 Clearing the Buffers" in the OpenGL 4.5 spec (emphasis added):

When Clear is called, the only per-fragment operations that are applied (if enabled) are the pixel ownership test, the scissor test, sRGB conversion (see section17.3.9), and dithering. The masking operations described in section 17.4.2 are also applied.

Where section 17.4.2 is titled "Fine Control of Buffer Updates", and includes the documentation of glStencilMask(). For glStencilMaskSeparate(), which is a more general version of glStencilMask(), it even says explicitly:

Fragments generated by front-facing primitives use the front mask and fragments generated by back-facing primitives use the back mask (see section 17.3.5). The clear operation always uses the front stencil write mask when clearing the stencil buffer.

So to clear the bottom 7 bits of the stencil buffer, you can use:

glStencilMask(0x7f);
glClear(GL_STENCIL_BUFFER_BIT);
share improve this answer

I'm using the stencil buffer for two jobs. The first is to allow masking to happen, and the second is to write masks for objects that can be 'seen' through. In this particular case, the reserved bit is 0x80, the very last bit in the byte, with the rest left for regular masking.

The first purpose requires that the buffer be cleared after around 127 uses, or else past stencils will become "valid" again when testing, since the value must wrap back to 1. The second purpose requires the reserved bits in the buffer to stay alive through the entire frame.

Is there any way to clear the entire stencil buffer, while keeping the reserved bits set?

share improve this question
 
    
Searched this problem, using color clearing instead since I imagine that's more common, and found my answer; glClear() will heed glColorMask(), so I imagine that applies to glStencilMask() as well –  Anne Quinn  Dec 11 '14 at 3:14

1 Answer

up vote 1 down vote accepted

Your theory in the comment is correct. glStencilMask() is applied to the values written by glClear() and glClearBuffer().

From section "17.4.3 Clearing the Buffers" in the OpenGL 4.5 spec (emphasis added):

When Clear is called, the only per-fragment operations that are applied (if enabled) are the pixel ownership test, the scissor test, sRGB conversion (see section17.3.9), and dithering. The masking operations described in section 17.4.2 are also applied.

Where section 17.4.2 is titled "Fine Control of Buffer Updates", and includes the documentation of glStencilMask(). For glStencilMaskSeparate(), which is a more general version of glStencilMask(), it even says explicitly:

Fragments generated by front-facing primitives use the front mask and fragments generated by back-facing primitives use the back mask (see section 17.3.5). The clear operation always uses the front stencil write mask when clearing the stencil buffer.

So to clear the bottom 7 bits of the stencil buffer, you can use:

glStencilMask(0x7f);
glClear(GL_STENCIL_BUFFER_BIT);
share improve this answer
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值