OpenGL es 2.0 读取深度缓冲区 [英] OpenGL es 2.0 Read depth buffer

查看:35
本文介绍了OpenGL es 2.0 读取深度缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我们无法在 OpenGL ES 2.0 中读取 Z(深度)值.所以我想知道我们如何从 2D 屏幕上的一个点获取 3D 世界坐标?

As far as i know, we can't read the Z(depth) value in OpenGL ES 2.0. So I am wondering how we can get the 3D world coordinates from a point on the 2D screen?

其实我有一些随机的想法可能会奏效.由于我们可以使用 glReadPixels 读取 RGBA 值,我们如何复制深度缓冲区并将其存储在颜色缓冲区中(比如 ColorforDepth).当然需要有一些好的约定,这样我们就不会丢失深度缓冲区的任何信息.然后当我们需要一个点的世界坐标时,我们将这个 ColorforDepth 颜色缓冲区附加到帧缓冲区,然后渲染它.所以当我们使用 glReadPixels 读取这一帧的深度信息时.

Actually I have some random thoughts might work. Since we can read the RGBA value by using glReadPixels, how about we duplicate the depth buffer and store it in a color buffer(say ColorforDepth). Of course there need to be some nice convention so that we don't lose any information of the depth buffer. And then when we need a point's world coordinates , we attach this ColorforDepth color buffer to the framebuffer and then render it. So when we use glReadPixels to read the depth information at this frame.

但是,这将导致 1 帧闪烁,因为颜色缓冲区是从深度缓冲区转换而来的奇怪缓冲区.我仍然想知道是否有一些标准方法可以在 OpenGL es 2.0 中获得深度?

However, this will lead to 1 frame flash since the colorbuffer is a weird buffer translated from the depth buffer. I am still wondering if there is some standard way to get the depth in OpenGL es 2.0?

提前谢谢!:)

推荐答案

使用 FBO,您可以在不显示结果的情况下进行渲染.如果您在 ES 2.0 中,您的片段着色器可以访问当前片段的深度(在窗口坐标中)作为 gl_FragCoord 的一部分,因此您可以将其写入颜色缓冲区,使用 glReadPixels 取回结果并继续.或者,您可以将世界空间 z 作为变量加载并从片段着色器中写入,以防万一这样更简单.

Using an FBO, you can render without displaying the results. If you're in ES 2.0, your fragment shader can access the current fragment's depth (in window coordinates) as part of gl_FragCoord, so you can write that to the colour buffer, use glReadPixels to get the result back and proceed. Alternatively, you can load world-space z as a varying and write that from your fragment shader, in case that's an easier way around.

为了说服自己,请尝试编写一个快速着色器,以低精度快速将 gl_FragCoord.z 输出,例如只是

To convince yourself, try writing a quick shader that puts gl_FragCoord.z out hastily in low precision, e.g. just

gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0);

你应该得到一个灰度,颜色的强度代表深度.因为您在窗口坐标中,所以强度范围从 0.0(可能最近的未剪辑片段)到 1.0(可能最远的未剪辑片段).为了不损失很多精度,在组件之间拆分值可能更有帮助,因为您的供应商几乎肯定不支持浮点目标缓冲区.

You should get a greyscale with the intensity of the colour representing depth. Because you're in window coordinates, intensity will range from 0.0 (closest possible unclipped fragment) to 1.0 (farthest possible unclipped fragment). In order not to lose quite a lot of precision, it's probably more helpful to split the value between components, as your vendor almost certainly doesn't support floating point target buffers.

这篇关于OpenGL es 2.0 读取深度缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆