title: OpenGL ES 2.0 Objects disappearing when depth test enabled
body: <p><strong>Edit</strong>: This turned out to be correct, hopefully it still helps others with similar issues.</p>
  
  <p>Is there a piece I'm missing in setting up the depth testing pipeline in OpenGL ES 2.0 (using EGL)? </p>
  
  <p>I've found many questions about this but all were solved by either correctly setting up the depth buffer on context initialization:</p>
  
  <pre><code>EGLint egl_attributes[] = {
          ...
          EGL_DEPTH_SIZE, 16,
          ...
          EGL_NONE };
  
  if (!eglChooseConfig(
          m_eglDisplay, egl_attributes, &amp;m_eglConfig, 1, &amp;numConfigs)) {
      cerr &lt;&lt; "Failed to set EGL configuration" &lt;&lt; endl;
      return EGL_FALSE;
  }
  </code></pre>
  
  <p>or by properly enabling and clearing the depth buffer, and doing so after the context has been initialized:</p>
  
  <pre><code>// Set the viewport
  glViewport(0, 0, m_display-&gt;width(), m_display-&gt;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-&gt;drawAll();
  </code></pre>
  
  <p>Commenting out the <code>glEnable(GL_DEPTH_TEST)</code> I have a scene but without the depth test occlusion I would love to have.</p>
  
  <p>In a shader, outputting the <code>z</code> component of <code>gl_Position</code> visually works as expected (<code>z</code> values are in the range [0, 1]):</p>
  
  <pre><code>// 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 &lt; 0.0) ? 1.0 : 0.0,
                          v_depth,
                          (v_depth &gt; 1.0) ? 1.0 : 0.0,
                          1.0);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Any ideas what I'm missing? (If nothing else I hope this summarises some issues others have been having).</p>