title: Unable to Draw 3D (Frustum) on top of 2D (Ortho) in OpenGL
body: <p>I'm trying to render some meshes with Frustum projection on top of a background image (orthographic projection). No matter what I do, the background image keeps being on top of the scene (hiding the meshes).</p>
  
  <p>I've tried a little test - when I've rendered them both with the same projection matrix the were
  in the right order - the background image was behind the meshes.</p>
  
  <p>These are the projection matrices and the Depth-Buffer init:</p>
  
  <pre><code>glEnable(GL_DEPTH_TEST);
  glClearDepthf( 1.0f );
  glDepthFunc( GL_LEQUAL );
  glDepthMask (GL_TRUE);
  
  EGLConfig eglconfig;
  EGLint const attrib_list[] = {
      EGL_DEPTH_SIZE, 16,
      EGL_NONE
  };
  EGLint numreturned;
  CHECK(eglChooseConfig(esContext.eglDisplay, attrib_list, &amp;eglconfig, 1, &amp;numreturned) != EGL_FALSE);
  
  float fieldOfView = 60.0;
  float znear = 0.1f;
  float zfar = 100000;
  float size = (float)(znear * tanf((fieldOfView * M_PI /180.0f) / 2.0f)); 
  
  m_orthoProjMatrix.loadOrtho(0, 
                              screenWidth, 
                              0, 
                              screenHeight, 
                              znear, 
                              zfar);
  
  m_frustProjMatrix.loadFrustum(-size, 
                                 size, 
                                -size / (screenWidth / screenHeight), 
                                 size / (screenWidth / screenHeight), 
                                 znear, 
                                 zfar);
  </code></pre>
  
  <p>I'm cleaning the buffers with:
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);</p>
  
  <p>One last thing..
  When I disable GL_DEPTH_TEST, the meshes are being drawn in front of the background image because the background is being called first..</p>
  
  <p>I can't tell which, the Depth-Buffer or the Projection matrices, causing this problem..
  The values of the matrices with screenWidth=960 and screenHeight = 640:</p>
  
  <pre><code>Orthographic Proj Matrix:
  0.00208 0.00000 0.00000 -1.00000
  0.00000 0.00313 0.00000 -1.00000
  0.00000 0.00000 0.00000 -1.00000
  0.00000 0.00000 0.00000  1.00000
  
  Frustum Proj Matrix:
  1.73205 0.00000  0.00000  0.00000
  0.00000 2.59808  0.00000  0.00000
  0.00000 0.00000 -1.00000 -0.20000
  0.00000 0.00000 -1.00000  0.00000
  
  Camera Settings:
  Vector3 pos(0,10,50);
  Vector3 at(0,0,0);
  Vector3 up(0,1,0);
  LookAt(pos,at,up);
  
  Scene Settings:
  Vector3 BackgroundImagePosition (0,0, -430);
  Vector3 SomeMesh(0,0,0);
  </code></pre>
  
  <p>What am i doing wrong?
  Amir.</p>
  
  <p>Edit:
  This how i'm drawing the images with the orthographic projection:</p>
  
  <pre><code>GLfloat verts[] = { 
  image-&gt;x + image-&gt;width  , image-&gt;y                , image-&gt;z,
  image-&gt;x + image-&gt;width  , image-&gt;y + image-&gt;height, image-&gt;z,
  image-&gt;x                 , image-&gt;y + image-&gt;height, image-&gt;z,
  image-&gt;x                 , image-&gt;y                , image-&gt;z};
  Matrix4s mv(m_camera-&gt;getCameraViewMatrix());
  Matrix4f proj(ResManPtr-&gt;getOrthoProjMatrix() * mv);
  glUniformMatrix4fv(prog-&gt;getUniformLocation("u_projMatrix"), 1, GL_FALSE, &amp;(*proj));
  glActiveTexture(GL_TEXTURE0);
  image-&gt;tex-&gt;bind();
  glUniform1i(prog-&gt;getUniformLocation("s_texture"), 0);
  glEnableVertexAttribArray(prog-&gt;getAttribLocation("a_position")); 
  glVertexAttribPointer(prog-&gt;getAttribLocation("a_position"), 3, GL_FLOAT, GL_FALSE, 0, verts); 
  glEnableVertexAttribArray(prog-&gt;getAttribLocation("a_texCoord"));
  glVertexAttribPointer(prog-&gt;getAttribLocation("a_texCoord"), 2, GL_FLOAT, GL_FALSE, 0, m_texcoord);
  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  </code></pre>
  
  <p>and this is how I'm drawing the meshes with the frustum projection:</p>
  
  <pre><code>Matrix4f mv(world-&gt;getCamera()-&gt;getCameraViewMatrix());
  const btVector3&amp; dir = getDirection();
  Scalar xzAngle = atan2s(dir.x(), dir.z()) - (Scalar)M_PI_2;
  btVector3 origin = getGhostObject()-&gt;getWorldTransform().getOrigin();
  mv.applyTranslate(origin.x(), origin.y() - m_width/2, origin.z());
  mv.applyRotateY(xzAngle);    
  GLProgramPtr prog = ResManPtr-&gt;getProgramMap()[Consts::DEFAULT_PROGRAM_KEY];
  Matrix4f proj(ResManPtr-&gt;getFrustProjMatrix() * mv);
  glUniformMatrix4fv(prog-&gt;getUniformLocation("u_projMatrix"), 1, GL_FALSE, &amp;(*proj));
  glActiveTexture(GL_TEXTURE0);
  m_texture-&gt;bind();  
  glUniform1i(prog-&gt;getUniformLocation("s_texture") , 0);
  m_model-&gt;render(frame_number);
  </code></pre>