title: Draw 2D text on 3D scene in OpenGL (C++)
body: <p>I'm experimenting with 3D graphics in OpenGL and I've managed to create a 3D level. However, I also want to display some 2D text over the screen that stays in place (similar to an HP or ammo stat that you'd see in a first person shooter). Here is the entire method that I am using to render the level. Everything works exactly as I want it to, except for the text (which does not appear on screen at all). How can I make the text appear properly?</p>
  
  <pre><code>void render_Scene(void)
  {
      int i;
      int j;
  
      glClearColor(0.0, 0.0, 0.0, 1.0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();
  
  
  
      //Camera perspective
      gluLookAt(x, y, 1.0, x + lx, y + ly, 1.0, 0.0, 0.0, 1.0);
      glColor3f(0.4, 0.4, 0.4);
  
  
  
      //Draw walls around the level
      glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, wall_Texture);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  
      glBegin(GL_QUADS);
      glNormal3f(0.0, 1.0f, 0.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-75, -75, 0);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f(-75, -75, 1.2);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(75, -75, 1.2);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(75, -75, 0);
      glEnd();
  
      glBegin(GL_QUADS);
      glNormal3f(0.0, 1.0f, 0.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-75, 72, 0);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f(-75, 72, 1.2);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(75, 72, 1.2);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(75, 72, 0);
      glEnd();
  
      glBegin(GL_QUADS);
      glNormal3f(0.0, 1.0f, 0.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-75, -75, 0);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f(-75, 72, 0);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(-75, 72, 1.2);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-75, -75, 1.2);
      glEnd();
  
      glBegin(GL_QUADS);
      glNormal3f(0.0, 1.0f, 0.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(75, -75, 0);
      glTexCoord2f(1.0f, 0.0f);
      glVertex3f(75, 72, 0);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(75, 72, 1.2);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(75, -75, 1.2);
      glEnd();
  
      glDisable(GL_TEXTURE_2D);
  
  
  
      //Draw ground texture
      for (int m = -150; m &lt;= 140; m += 10)
      {
          for (int n = -150; n &lt;= 140; n += 10)
          {
               glEnable(GL_TEXTURE_2D);
               glBindTexture(GL_TEXTURE_2D, ground_Texture);
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  
               glBegin(GL_QUADS);
               glNormal3f(0.0, 1.0f, 0.0f);
               glTexCoord2f(0.0f, 0.0f);
               glVertex3f(n, m, 0.0);
               glTexCoord2f(1.0f, 0.0f);
               glVertex3f(n, m + 10, 0.0);
               glTexCoord2f(1.0f, 1.0f);
               glVertex3f(n + 10, m + 10, 0.0);
               glTexCoord2f(0.0f, 1.0f);
               glVertex3f(n + 10, m, 0.0);
               glEnd();
  
               glDisable(GL_TEXTURE_2D);
           }
      }
  
  
  
      //Draw trees in level (uses a seperate method)
      for (i = -9; i &lt; 9; i++)
      {
           for (j = -9; j &lt; 9; j++)
           {
               glPushMatrix();
               glTranslatef(i*7.5, j*7.5, 0);
               draw_Trees();
               glPopMatrix();
           }
      }
  
      //Create light
      GLfloat lightColor0[] = { 1.0, 1.0f, 1.0f, 1.0f };
      GLfloat lightPos0[] = { lx, ly, deltaMove, 1.0f };
  
      glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
      glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  
  
  
      //Here's where I'm trying to render the text (I've heard that glPushAttrib and glPopAttrib may be important for this part) but it does not appear anywhere on the screen
      glColor3f(1.0, 1.0, 1.0);
      glPushMatrix();
      glPushAttrib(GL_LIGHTING);
      glDisable(GL_TEXTURE_2D);
      glDisable(GL_LIGHTING);
      glLoadIdentity();
  
      glRasterPos2i(10, 30);
  
      void * font = GLUT_BITMAP_8_BY_13;
  
      for (string::iterator i = strings[stage].begin(); i != strings[stage].end(); ++i)
      {
          glutBitmapCharacter(font, *i);
      }
  
      glPopAttrib();
      glPopMatrix();
  
      glutSwapBuffers();
  }  
  </code></pre>
  
  <p>Edit: I've been told that I should use glWindowPos2i instead of glRasterPos2i. The problem with this solution was that glWindowPos2i wasn't recognised and resulted in a build error. Following some online research I added the following lines of code:</p>
  
  <pre><code>PFNGLWINDOWPOS2IPROC glWindowPos2i;
  
  glWindowPos2i = (PFNGLWINDOWPOS2IPROC)glutGetProcAddress("glWindowPos2i");
  </code></pre>
  
  <p>Now the program runs properly, but it crashes when I try to use the glWindowPos2i function. I am using  and  as included headers in case that helps anyone come up with a solution to get glWindowPos2i to work properly.</p>