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?