环境
这是我正在工作的环境:
- OpenGL ES 2.0
- iPhone模拟器和iPhone 4
- 使用NVIDIA GeForce GTX 680MX的iMac 27“ 2048 MB
希望能有所帮助。
问题
正在从多个来源和多个站点(包括Stackoverflow)进行高低搜索,但是没有有效的模具效果。
我所拥有的是:
黑色的“ S” 不是多边形,而是在与背景图像相同的宽度和高度的矩形四边形上绘制的纹理。
我正在尝试做一个模版效果,其中背景和黄色小家伙只有在黑色“ S”纹理内时才可见。
在我的片段着色器中,我有以下内容:
varying lowp vec4 destinationColor;
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
uniform highp float TexScale;
void main()
{
highp vec4 color = texture2D(Texture, TexCoordOut);
if(color.a == 0.0)
{
discard;
}
gl_FragColor = color;
}
对于我的设置深度模板缓冲区,我将其设置如下:
-(void)setupDepthStencilBuffer
{
GLint width;
GLint height;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
glGenBuffers(1, &depthStencilBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthStencilBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, width, height);
//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_COMPONENT24_OES, width, height);
NSLog(@"depthStencilBuffer = %d", depthStencilBuffer);
}
根据Apple的文档(我认为已经过时):
请注意,当我尝试在Xcode中键入GL_RGBA枚举之类的东西时,这些东西并不存在(我认为Apple必须删除它并使其过时)。
我也尝试了苹果公司在上面的链接中定义所谓的“深度/模板”缓冲区的方法,但是在下面给出了相同的错误。
我上面的代码是创建模板缓冲区的方式。
在我的setupFrameBuffer()方法中,我将其附加为:
-(void)setupFrameBuffer
{
GLuint frameBuffer;
glGenBuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilBuffer);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
{
NSLog(@"failed to make complete framebuffer object %x", status);
}
}
如上所示,将其附加时出现的错误是:
无法制作完整的帧缓冲对象8cd6
对于我的渲染方法,我有:
-(void)render:(CADisplayLink *)displayLink
{
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearStencil(0);
glEnable(GL_STENCIL_TEST);
// ----------------------------------------
// Don't write to color or depth buffer
// ----------------------------------------
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// ----------------------------------------
// First set the alpha test so that
// fragments greather than threshold
// will pass thus will set nonzero
// bits masked by 1 in stencil
// ----------------------------------------
glStencilFunc(GL_ALWAYS, 1, 1);
// ----------------------------------------------------------------
// Drawing our stencil
// ----------------------------------------------------------------
glBindBuffer(GL_ARRAY_BUFFER, bgVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bgIBO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, stencilTexture);
glUniform1i(textureUniform, 0);
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 7));
glDrawElements(GL_TRIANGLE_FAN, sizeof(bgIndices)/sizeof(bgIndices[0]), GL_UNSIGNED_BYTE, 0);
/*
// -----------------------------------------
// Second pass of the fragments less
// or equal than the threshold will pass
// thus will set zero bits masked by 1
// in stencil
// -----------------------------------------
glStencilFunc(GL_ALWAYS, 0, 1);
glBindBuffer(GL_ARRAY_BUFFER, bgVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bgIBO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, stencilTexture);
glUniform1i(textureUniform, 0);
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 7));
glDrawElements(GL_TRIANGLE_STRIP, sizeof(bgIndices)/sizeof(bgIndices[0]), GL_UNSIGNED_BYTE, 0);
*/
// ---------------------------------------------------
// RE-ENABLING THE COLOR AND DEPTH MASK AGAIN
// TO DRAW REST OF THE SCENE AFTER STENCIL
// ---------------------------------------------------
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
Mat4 frustrumMatrix = [CameraMatrix createOrthographicMatrixUsingLeft:-(self.bounds.size.width / 2.0)
Right:(self.bounds.size.width / 2.0)
Bottom:-(self.bounds.size.height / 2.0)
Top:(self.bounds.size.height / 2.0)
Near:-1.0f
Far:1.0f];
glUniformMatrix4fv(projectionUniform, 1, 0, frustrumMatrix.matrix);
glViewport(0, 0, self.bounds.size.width * self.contentScaleFactor, self.bounds.size.height * self.contentScaleFactor);
// ----------------------------------------------------------------
// Drawing our background first
// ----------------------------------------------------------------
glBindBuffer(GL_ARRAY_BUFFER, bgVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bgIBO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, bgTexture);
glUniform1i(textureUniform, 0);
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 7));
glDrawElements(GL_TRIANGLE_FAN, sizeof(bgIndices)/sizeof(bgIndices[0]), GL_UNSIGNED_BYTE, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, floorTexture);
glUniform1i(textureUniform, 0);
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void *)(sizeof(GLfloat) * 7));
glDrawElements(GL_TRIANGLE_FAN, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
结果显然是一个粉红色的屏幕,这意味着我的设置是错误的:
任何人都可以阐明一下吗?