title: OpenGL slows down when rendering nearby objects on top of others
body: <p>I am writing an iOS app using OpenGL ES 2.0 to render a number of objects to the screen.
  Currently, those objects are simple shapes (squares, spheres, and cylinders).</p>
  
  <p>When none of the objects overlap each other, the program runs smoothly at 30 fps.</p>
  
  <p>My problem arises when I add objects that appear behind the rest of my models (a background rectangle, for example).  When I attempt to draw a background rectangle, I can only draw objects in front of it that take up less than half the screen.  Any larger than that and the frame rate drops to between 15 and 20 fps.</p>
  
  <p>As it stands, all of my models, including the background, are drawn with the following code:</p>
  
  <pre><code>- (void)drawSingleModel:(Model *)model
  {
      //Create a model transform matrix.
      CC3GLMatrix *modelView = [CC3GLMatrix matrix];
  
      //Transform model view
      // ...
      //Pass matrix to shader.
      glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
  
      //Bind the correct buffers to openGL.
      glBindBuffer(GL_ARRAY_BUFFER, [model vertexBuffer]);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, [model indexBuffer]);
      glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
  
      glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
  
      //Load vertex texture coordinate attributes into the texture buffer.
      glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, [model textureIndex]);
      glUniform1i(_textureUniform, 0);
  
      glDrawElements([model drawMode], [model numIndices], GL_UNSIGNED_SHORT, 0);
  }
  </code></pre>
  
  <p>This code is called from my draw method, which is defined as follows:</p>
  
  <pre><code>- (void)draw
  {
      glUseProgram(_programHandle);
      //Perform OpenGL rendering here.
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glEnable(GL_BLEND);
  
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_CULL_FACE);
  
      _camera = [CC3GLMatrix matrix];
  
      //Camera orientation code.
      //...
  
      //Pass the camera matrix to the shader program.
      glUniformMatrix4fv(_projectionUniform, 1, 0, _camera.glMatrix);
  
      glViewport(0, 0, self.frame.size.width, self.frame.size.height);
      //Render the background.
      [self drawSingleModel:_background];
  
      //Render the objects.
      for(int x = 0; x &lt; [_models count]; ++x)
      {
          [self drawSingleModel:[_models objectAtIndex:x]];
      }
  
      //Send the contents of the render buffer to the UI View.
      [_context presentRenderbuffer:GL_RENDERBUFFER];
  }
  </code></pre>
  
  <p>I found that by changing the render order as follows:</p>
  
  <pre><code>    for(int x = 0; x &lt; [_models count]; ++x)
      {
          [self drawSingleModel:[_models objectAtIndex:x]];
      } 
      [self drawSingleModel:_background];
  </code></pre>
  
  <p>my frame rate when rendering on top of the background is 30 fps.</p>
  
  <p>Of course, the slowdown still occurs if any objects in _models must render in front of each other.  Additionally, rendering in this order causes translucent and transparent objects to be drawn black.</p>
  
  <p>I'm still somewhat new to OpenGL, so I don't quite know where my problem lies.  My assumption is that there is a slowdown in performing depth testing, and I also realize I'm working on a mobile device.  But I can't believe that iOS devices are simply too slow to do this.  The program is only rendering 5 models, with around 180 triangles each.</p>
  
  <p>Is there something I'm not seeing, or some sort of workaround for this?
  Any suggestions or pointers would be greatly appreciated.</p>