In OpenGL I am able to read the z-buffer values, using glReadPixels, like so:
glReadPixels(scrx, scry, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
If I do the same in OpenGL ES 3.2 I get a GL_INVALID_OPERATION error.
Checking the specification, I see that OpenGL allows GL_DEPTH_COMPONENT, but OpenGLES3 does not.
As a work-around, I copied the fragment depth to the alpha value in the colour buffer using this GLSL:
#version 320 es
...
outCol = vec4(psCol.rgb, gl_FragCoord.z);
After doing glReadPixels() on the GL_RGBA part of the framebuffer, I use rgba[3]/255.0 as the depth value.
Although this works, the 8-bit precision on the alpha value is insufficient for my purpose of picking what is under the mouse cursor.
Is there a way to get Z values from the framebuffer in OpenGL ES3?
There is an OpenGL ES Extension NV_read_depth which allows reading from the depth buffer by
glReadPixels. The extension is written against the OpenGL ES 2.0 Specification, but is still not standard in OpenGL ES 3.2.Get a set of the OpenGL es extension names by:
Note, you can either try to read the depth buffer (NV_read_depth) or the depth and stencil buffer (NV_read_depth_stencil);