Bram
Bram

Reputation: 8263

Reading depth buffer using OpenGLES3

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?

Upvotes: 2

Views: 749

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

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:

std::set<std::string> ogl_es_extensins;
GLint no_of_extensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &no_of_extensions);
for ( int i = 0; i < no_of_extensions; ++i )
{
    std::string ext_name = reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i));
    ogl_es_extensins.insert(ext_name);
}

Note, you can either try to read the depth buffer (NV_read_depth) or the depth and stencil buffer (NV_read_depth_stencil);

Upvotes: 1

Related Questions