title: Render in Opengl. Overlapping Objects
body: <p>I'm having trouble rendering overlapping objects in OpenGL.</p>
  
  <p>I am calling to functions that draw objects so that if I call drawB () and then drawA (), A is above B and B may be hidden by A.</p>
  
  <p>Everything works fine until it happens that A is a line (GL_LINES) and B is a polygon (GL_POLYGON). If this happens, B, which should be placed at the bottom, is drawn over A.</p>
  
  <p>At this moment I have disabled GL_DEPTH_TEST. I tried enabling it, I tried glDepthMask and glPolygonOffset but none has worked. I assume that I have been using them wrong.
  Any ideas how can I draw this objects in the correct order?</p>
  
  <p>**Edit:
  Apparently the problem was that i wasn't clearing the GL_COLOR_BUFFER_BIT, but i'm not clearing as a matter of speed. I have to find a way to without erasing the gl_color_buffer_bit, draw the objects in order ** </p>
  
  <p>It has an ortographic projection defined by
  glOrtho(r.left(),r.right(),r.bottom(),r.top(),0,2);</p>
  
  <p>When you call the function, background objects are drawn, then  foregrounds.
  I want that the background objects are drawn belows foregrounds.</p>
  
  <p>The idea is that i have two layouts, one for foreground and one for background. First every object in the background draws itself. Then every object in the foreground do the same. Every object has a Display List wich transform it's own coordinate system acoording their needs. The idea of that display lists is to to allow object nesting, each object with his own coordinate system, in spite of their parent coordinate system.</p>
  
  <p>In a moment i have in background a set of objects that does </p>
  
  <pre><code>glBegin (GL_POLYGON) ;
          for (angle = 0; angle&lt;PI * 2;angle+=1){
              glVertex2f (X + sin(angle) * Radius, Y + cos(angle) * Radius);
          }
          glEnd () ;
  </code></pre>
  
  <p>the another object, in the foreground layout does</p>
  
  <pre><code> glBegin(GL_LINES);
              glVertex2f(m_x[m_currentPositionP1],m_y[m_currentPositionP1]);
              glVertex2f(m_x[m_currentPositionP],m_y[m_currentPositionP]);
          glEnd();
  </code></pre>
  
  <p>But the lines appear hidden by the polygon </p>