所以我试图在 iOS 中使用模板缓冲区来进行屏蔽/剪辑。你们知道为什么这段代码可能不起作用吗?这就是我与 Stencils 相关的所有内容。在 iOS 4 上,我得到一个黑屏。在 iOS 5 上,我得到了我所期望的。我在模具中绘制的图像的透明区域是稍后要绘制的唯一区域。
代码如下。
这是我设置 frameBuffer、深度和模板的地方。在 iOS 中,深度和模板是结合在一起的。
-(void)setupDepthBuffer
{
glGenRenderbuffers(1, &depthRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, self.frame.size.width * [[UIScreen mainScreen] scale], self.frame.size.height * [[UIScreen mainScreen] scale]);
}
-(void)setupFrameBuffer
{
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
// Check the FBO.
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
NSLog(@"Failure with framebuffer generation: %d", glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
}
这就是我设置和绘制模板的方式。(下面的着色器代码。)
glEnable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilFunc(GL_ALWAYS, 1, -1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glColorMask(0, 0, 0, 0);
glClear(GL_STENCIL_BUFFER_BIT);
machineForeground.shader = [StencilEffect sharedInstance];
[machineForeground draw];
machineForeground.shader = [BasicEffect sharedInstance];
glDisable(GL_STENCIL_TEST);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
这是我使用模板的地方。
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilFunc(GL_EQUAL, 1, -1);
...Draw Stuff here
glDisable(GL_STENCIL_TEST);
最后是我的片段着色器。
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
void main(void)
{
lowp vec4 color = texture2D(Texture, TexCoordOut);
if(color.a < 0.1)
gl_FragColor = color;
else
discard;
}