Question

Edit: This turned out to be correct, hopefully it still helps others with similar issues.

Is there a piece I'm missing in setting up the depth testing pipeline in OpenGL ES 2.0 (using EGL)?

I've found many questions about this but all were solved by either correctly setting up the depth buffer on context initialization:

EGLint egl_attributes[] = {
        ...
        EGL_DEPTH_SIZE, 16,
        ...
        EGL_NONE };

if (!eglChooseConfig(
        m_eglDisplay, egl_attributes, &m_eglConfig, 1, &numConfigs)) {
    cerr << "Failed to set EGL configuration" << endl;
    return EGL_FALSE;
}

or by properly enabling and clearing the depth buffer, and doing so after the context has been initialized:

// Set the viewport
glViewport(0, 0, m_display->width(), m_display->height());

// Enable culling and depth testing
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);

// Clear the color and depth buffers
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepthf(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Draw elements
m_Program->drawAll();

Commenting out the glEnable(GL_DEPTH_TEST) I have a scene but without the depth test occlusion I would love to have.

In a shader, outputting the z component of gl_Position visually works as expected (z values are in the range [0, 1]):

// Vertex Shader
uniform mat4 u_matrix;
attribute vec4 a_position;
varying float v_depth;
void main() {
    vec4 v_position = u_matrix * a_position;
    v_depth = v_position.z / v_position.w;
    gl_Position = v_position;
}

// Fragment shader
varying float v_depth;
void main() {
    gl_FragColor = vec4((v_depth < 0.0) ? 1.0 : 0.0,
                        v_depth,
                        (v_depth > 1.0) ? 1.0 : 0.0,
                        1.0);
}

All objects are a shade of pure green, darker for nearer and brighter for further, as expected. Sadly some further (brighter) objects are drawn over nearer (darker) objects.

Any ideas what I'm missing? (If nothing else I hope this summarises some issues others have been having).

Was it helpful?

Solution

It appears I wasn't missing anything. I had a rogue polygon (in a different shader program) that, when depth was enabled occluded everything. The above is a correct setup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top