title: Background object is drawn in front of foreground object in OpenGL?
body: <p>For testing purposes let's assume I've draw 2 teapots with <code>glutSolidTeapot()</code>, like this:</p>
  
  <pre><code>glColor3f(1.0f, 0.0f, 0.0f); // Red teapot
  glutWireTeapot(1.0f);
  
  glColor3f(0.0f, 1.0f, 0.0f); // Green teapot
  glTranslatef(0.0f, 0.0f, 3.0f);
  glutWireTeapot(1.0f);
  </code></pre>
  
  <p>The camera is initially located at <code>(x,y,z) = (0.0f, 0.0f, 5.0f)</code> and I'm looking at <code>z = -1</code> (this is camera position #1). I'm sure you understand that the green teapot is the closest object to the camera and the red one is behind it. The problem is that the red one is drawn like it's in front of the green one, which doesn't look right:</p>
  
  <p>Example: <a href="http://i.stack.imgur.com/8WoEn.png" rel="nofollow">http://i.stack.imgur.com/8WoEn.png</a></p>
  
  <p>Now, if I move the camera too <code>(x,y,z) = (0.0f, 0.0f, -5.0f)</code> and look at <code>z = 1</code> (this is camera position #2), I will see the red teapot in front of the green one, this is normal behavior. The red is now the object closest to the camera and the green one is behind the red one. Everything is fine.</p>
  
  <p>Example: <a href="http://i.stack.imgur.com/eJvPE.png" rel="nofollow">http://i.stack.imgur.com/eJvPE.png</a></p>
  
  <p>I know this has something to do with the order, if I switch the code above (green teapot code first), than it will fix the problem in the camera position #1, but the problem will now be visible in camera position #2.</p>
  
  <p><strong>Why is this happening and how can I keep the objects behind other objects from being drawn in front, or at all?</strong></p>