我试图通过glReadPixels
函数从深度缓冲区获取深度值。它总是返回0给我。
首先,我创建深度缓冲附件:
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[self.context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
if (USE_DEPTH_BUFFER)
{
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
}
然后我调用glReadPixels
函数从深度缓冲区获取深度值:
float depth = 0.0f;
glReadPixels(point.x, self.renderUIView.frame.size.height - point.y, 1, 1, GL_DEPTH_COMPONENT16_OES, GL_FLOAT, &depth);
但它不起作用,深度值始终为0.我尝试从glGetError()
获取错误代码,它返回1280
。我错过了什么设置?
答案 0 :(得分:0)
OpenGL ES(任何API版本)都不支持您尝试的操作 - 无法在深度缓冲区上使用glReadPixels
。
另外一般情况下,将glReadPixels
用于任何真正的3D游戏作为主渲染循环的一部分是一个“非常糟糕的主意” - 它会强制管道冲洗,这将完全扼杀你的表现。