iOS5是否同时支持GL_STENCIL_INDEX和GL_STENCIL_INDEX8?

使用以下代码:

GLuint viewRenderbuffer, viewFramebuffer, viewDepthbuffer, stencilBuffer; // Create the framebuffer object glGenFramebuffers(1, &viewFramebuffer); glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer); // Create a render buffer and bind it to the FBO. glGenRenderbuffers(1, &viewRenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer); [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer]; glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &imageWidth); glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &imageHeight); // Create a depth buffer and bind it to the FBO. glGenRenderbuffers(1, &viewDepthbuffer); glBindRenderbuffer(GL_RENDERBUFFER, viewDepthbuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, imageWidth, imageHeight); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, viewDepthbuffer); // Create a stencil buffer to crop the rendered scene and bind it to the FBO. glGenRenderbuffers(1, &stencilBuffer); glBindRenderbuffer(GL_RENDERBUFFER, stencilBuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX, imageWidth, imageHeight); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencilBuffer); // Check the FBO. if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { NSLog(@"Failure with framebuffer generation: %d", glCheckFramebufferStatus(GL_FRAMEBUFFER)); } 

使用GL_STENCIL_INDEX ,我得到GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT错误。 使用GL_STENCIL_INDEX8 ,我得到GL_FRAMEBUFFER_UNSUPPORTED错误。 两者都是由最后一个glFramebufferRenderbuffer()函数引起的,它应该将模板缓冲区绑定到FBO。

此外,当我检查GL_RENDERBUFFER_STENCIL_SIZE值时,我使用GL_RENDERBUFFER_STENCIL_SIZE获得正确的值(8),但是使用GL_STENCIL_INDEX得到0。

有了这个,我无法使用模板缓冲区获得function完备的FBO。 是由于GL_STENCIL_INDEX吗? 这应该用哪一个?

似乎在OpenGL ES 2.0中至少在iOS上(对其他操作系统不确定),您必须创建深度缓冲区和模板缓冲区的组合。 我列出了我的设备支持的所有扩展(带有iOS 5.0.1的iPhone 4),并且唯一与模板缓冲区相关的扩展是:

GL_OES_packed_depth_stencil

这表明你必须创建一个组合深度+模板缓冲区(取自iPhone 3D编程书)

 // Create a packed depth stencil buffer. GLuint depthStencil; glGenRenderbuffersOES(1, &depthStencil); glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthStencil); glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH24_STENCIL8_OES, width, height); // Create the framebuffer object. GLuint framebuffer; glGenFramebuffersOES(1, &framebuffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, color); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthStencil); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_STENCIL_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthStencil); glBindRenderbufferOES(GL_RENDERBUFFER_OES, color);