OpenGL ES 深度缓冲渲染(iOS 模拟器 VS 真实设备)

OpenGL ES Depth Buffer rendering (iOS simulator VS real device)

使用 iOS 模拟器 Getting the true z value from the depth buffer 渲染深度缓冲区时,一切正常。 但是在真实设备上的渲染给出了糟糕的结果:深度只用很少的值渲染(没有淡入淡出),就像当你将图像显示为 256 值颜色范围时。

这里是fbo生成的代码:

glGenFramebuffers(1, &sceneFBO);
glGenTextures(2, textures_scene);
glBindTexture(GL_TEXTURE_2D, textures_scene[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, widthResolution, heightResolution, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glBindTexture(GL_TEXTURE_2D, textures_scene[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, widthResolution, heightResolution, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);

//TODO: GL_DEPTH_COMPONENT cannot be GL_UNSIGNED_BYTE ?

glBindFramebuffer(GL_FRAMEBUFFER, sceneFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures_scene[0], 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textures_scene[1], 0);

似乎 GL_DEPTH_ATTACHMENT fbo 的唯一工作值是 GL_UNSIGNED_INT。但是渲染深度不够...

PS:我不想再次将场景显示为具有 z 值的 GL_COLOR_ATTACHMENT0 以获得正确的深度(出于性能原因)也不使用 OpenGL ES3.0 或新金属(iPhone4s 支持)。

有什么想法吗?

ES 1 或 2 中不支持深度缓冲区。请参阅此问题的答案:glReadPixels doesn't read depth buffer values on iOS

似乎无法将 GL_UNSIGNED_BYTE 作为 type 参数与 GL_DEPTH_COMPONENT 作为 internalformat 一起使用,根据 GL_OES_depth_texture 扩展规范,仅允许 GL_UNSIGNED_SHORTGL_UNSIGNED_INThttps://www.khronos.org/registry/gles/extensions/OES/OES_depth_texture.txt

我终于把 z 值放在场景的 alpha 部分(我没有为这个 fbo 使用透明度)。

 color.w=(gl_postion.z-near)/(far-near)

一点 mcgyver 但它完成了工作。