title: Strange depth/blending in OpenGL drawing models
body: <p>I'm having a strange problem which I cannot figure out for the life of me. I'm loading an OBJ model file from Maya and rendering it an OpenGL environment. This appears to go well and in my independent model viewer, they display correctly. However, when loaded into the game environment (with a somewhat strange projection matrix that I have no control over), the model does NOT display correctly.</p>
  
  <p>The vertices SEEM to be in the correct place in that rotating the model gives the correct sense of 3D.  However, depending on the depth function, different parts of the model appear (incorrectly). </p>
  
  <p>For example, in a model of a person, when the person is walking from left to right (e.g. the right side of the model is visible to the camera); if the depth function is set to <code>GL_LEQUAL</code>, you are unable to see the right arm of the person when it is in front of the torso. However, when the mode is set to <code>GL_LESS</code>, you are able to see the left arm through the torso. This is one of those things that are easier to see with pictures so here we go:</p>
  
  <p><code>GL_LESS</code> or <code>GL_NOTEQUAL</code>:</p>
  
  <p><img src="https://i.stack.imgur.com/3rxIS.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/utPzw.png" alt="enter image description here"></p>
  
  <p><code>GL_LEQUAL</code> or <code>GL_ALWAYS</code>:</p>
  
  <p><img src="https://i.stack.imgur.com/VoUu7.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/6G2gw.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/raAGD.png" alt="enter image description here"></p>
  
  <p>It should be noted that nothing is displayed with any other depth function. In the first picture of the GL_LEQUAL series, you can just about see the left arm is partially obscured by the torso when it shouldn't be.</p>
  
  <p>Here is the code used to render the model:</p>
  
  <pre><code>gl.glDisable(GL.GL_BLEND);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glPushMatrix();
      gl.glLoadIdentity();
  
      layerTextureShader.updateShader(gl, projection, disparityTop, disparityBottom, fieldMask, 1);
  
      // gl.glDepthFunc(GL.GL_NEVER); // 512
      // gl.glDepthFunc(GL.GL_LESS); // 513
      // gl.glDepthFunc(GL.GL_EQUAL); // 514
      // gl.glDepthFunc(GL.GL_LEQUAL); // 515
      // gl.glDepthFunc(GL.GL_GREATER); // 516
      // gl.glDepthFunc(GL.GL_NOTEQUAL); // 517
      // gl.glDepthFunc(GL.GL_GEQUAL); // 518
      // gl.glDepthFunc(GL.GL_ALWAYS); // 519
      gl.glDepthFunc(currentGlComparison);
  
      gl.glEnable(GL.GL_DEPTH_TEST);
      gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
  
      gl.glDepthMask(true);
      gl.glDepthRange(0, 0.01);
  
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, getVertexBufferObject());
      gl.glBufferData(GL.GL_ARRAY_BUFFER, getNoOfVertices() * 3 * 4, getVertices(), GL.GL_STREAM_DRAW);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
  
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, getTexCoordBufferObject());
      gl.glBufferData(GL.GL_ARRAY_BUFFER, getNoOfVertices() * 2 * 4, getTexCoords(), GL.GL_STREAM_DRAW);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
  
      gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, getIndicesBufferObject());
      gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, getNoOfIndices() * 4, getIndices(), GL.GL_STREAM_DRAW);
      gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
  
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, getColorBufferObject());
      gl.glBufferData(GL.GL_ARRAY_BUFFER, getNoOfVertices() * 4 * 4, getColors(), GL.GL_STREAM_DRAW);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
  
      gl.glEnable(GL.GL_TEXTURE_2D);
      gl.glActiveTexture(GL.GL_TEXTURE0);
  
      layerTextureShader.use(gl);
  
     gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, getVertexBufferObject());
      gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
  
      gl.glEnableClientState(GL.GL_COLOR_ARRAY);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, mask ? getMaskColorBufferObject() : getColorBufferObject());
      gl.glColorPointer(4, GL.GL_FLOAT, 0, 0);
  
      gl.glClientActiveTexture(GL.GL_TEXTURE0);
  
      gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, getTexCoordBufferObject());
      gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);
  
      gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, getIndicesBufferObject());
  
      final int count = getNoOfIndices();
      gl.glDrawElements(GL.GL_TRIANGLES, count, GL.GL_UNSIGNED_INT, 0);
  
      gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
  
      gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
      gl.glDisableClientState(GL.GL_COLOR_ARRAY);
      gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
  
      layerTextureShader.release(gl);
  
      gl.glDisable(GL.GL_TEXTURE_2D);
  
      gl.glDisable(GL.GL_DEPTH_TEST);
      gl.glEnable(GL.GL_BLEND);
      gl.glPopMatrix();
  </code></pre>
  
  <p>Thanks to anyone for any help, this has had me stumped for quite a few days now.</p>