OpenGL es 2.0 Read Depth Buffer

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

In fact, I may have some random thoughts. Since we can read the RGBA value with glReadPixels, how about duplicating the depth buffer and storing it in a color buffer (say ColorforDepth). Of course, there must be some good agreement so that we do not lose information about the depth buffer. And then, when we need the coordinates of the world points, we attach this ColorforDepth color buffer to the framebuffer, and then render it. Therefore, when we use glReadPixels to read depth information in this frame.

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

thanks in advance!:)

+5
source share
3 answers

Using FBO, you can display the results without displaying the results. If you are in ES 2.0, your fragment shader can access the current fragment depth (in window coordinates) as part of gl_FragCoord, so you can write this to the color buffer, use glReadPixels to return the result and continue. Alternatively, you can load the world space z as changing and write it from your fragment shader, if that is easier.

, , gl_FragCoord.z, , .

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

, . , 0.0 ( unclipped ) 1.0 ( unclipped ). , , , .

+7

3D- . , , . .

2D- ( 3D- ), 2D- () , .

0

, , .

, , , -, , .

tho, ( ) , , . , - ( , ). ( GL ES ?)

- , .

0

All Articles