title: Draworder OpenGL - larger objects over smaller
body: <p>In my OpenGL ES Android game I am trying draw shapes in succesive order in accordance with their z-value (that is position in the z-plane). This means to draw the smallest most distant shapes (textures) first and then draw the larger. I have stored the shapes properties in an arraylist and when drawing and updating I am using an Iterator. Let say the this is the arrayList with its z-values (just an example)</p>
  
  <p>ArrayList  arraylistBricks</p>
  
  <pre><code>obj (0) ... z = -20 
  obj (1) ... z = -37
  obj (2) ... z = -31
  obj (3) -...z = -20
  </code></pre>
  
  <p>I sort this arrayList using Collections.sort(..) for every frame and after the sorting the list look like this</p>
  
  <pre><code>obj (0) ... z = -37 
  obj (1) ... z = -31
  obj (2) ... z = -20
  obj (3) -...z = -20
  </code></pre>
  
  <p>That is - the smallest shapes are first in the list and are first to be drawn. I can see that the arraylist is really sorted but somehow nothing happends.What I want is that the smaller shapes should be behind the larger ones. Does the GPU have some sort of algortitm of drawingorder? I just migrated from skia to opengl and was surprised over this. </p>
  
  <p>What do I miss here? How could I solve the problem?</p>
  
  <pre><code> private void drawShape() {
  
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboDataListLevelSprites.get(iName).getBuff_id_vertices());
      GLES20.glEnableVertexAttribArray(mPositionHandle);
      GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, 0);
      for (int i = 0; i &lt; BrickProperties.get_buff_id_uvs().length; i++) {
          GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, BrickProperties.get_buff_id_uvs()[i]);
          GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
          GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 0, 0);
  
          for (Iterator&lt;BrickProperties&gt; shapeIterator = arrayListBricks.iterator(); shapeIterator.hasNext(); ) {
  
              BrickProperties bp = shapeIterator.next();
              int buffIndexVal = bp.get_status_diff();
  
              if (buffIndexVal == i) {
                  Matrix.setIdentityM(mModelMatrix, 0);
                  Matrix.translateM(mModelMatrix, 0, bp.getTranslateData()[0], bp.getTranslateData()[1], bp.getTranslateData()[2]);
  
                  if (bp.get_status() == 0) {
                      Matrix.rotateM(mModelMatrix, 0, bp.getAngleInDegrees(), 0, 1, 0);
                  }
  
                  render();
              }
          }
      }
  
  }
  </code></pre>
  
  <p>the render-method</p>
  
  <pre><code>   private void render() {
  
      Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
      GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);
      Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
      GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
      GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
  }
  </code></pre>
  
  <p>update (snippet)</p>
  
  <pre><code>   public void update() {
  
      Collections.sort(arrayListBricks, new Comparator&lt;BrickProperties&gt;() {
          @Override
          public int compare(BrickProperties bp2, BrickProperties bp1)
          {
              int result = Float.compare(bp1.getTranslateData()[2], bp2.getTranslateData()[2]);
              return result;
  
          }
      });
      Collections.reverse(arrayListBricks);
  
  ....
  </code></pre>