0

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?

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 <= 140; m += 10)
    {
        for (int n = -150; n <= 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 < 9; i++)
    {
         for (j = -9; j < 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();
}  

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:

PFNGLWINDOWPOS2IPROC glWindowPos2i;

glWindowPos2i = (PFNGLWINDOWPOS2IPROC)glutGetProcAddress("glWindowPos2i");

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user3277234
  • 55
  • 3
  • 9
  • 2
    I don't know glutBitmapCharacter but i am pretty sure GL_LIGHTING is no valid parameter for `glPushAttrib`. This takes me to the lack of any `glGetError`, which would let you detect further errors... – Thomas Sep 20 '14 at 21:56
  • Indeed, `glPushAttrib (...)` uses a bitmask, so all of the valid enumerants will end in `_BIT`. – Andon M. Coleman Sep 20 '14 at 22:02
  • Does this help? http://stackoverflow.com/questions/9430852/glutbitmapcharacter-positions-text-wrong – jozxyqk Sep 21 '14 at 06:12

1 Answers1

0

use glWindowPos2i instead of glRasterPos2i. glRasterPos2i uses positions in world coordinates. glWindowPos2i uses positions in screen coordinates. https://www.opengl.org/sdk/docs/man2/xhtml/glWindowPos.xml https://www.opengl.org/sdk/docs/man2/xhtml/glRasterPos.xml

programmerjake
  • 1,794
  • 11
  • 15
  • I tried it, but glWindowPos2i wasn't recognised. Following some online research I added the following lines of code: "PFNGLWINDOWPOS2IPROC glWindowPos2i;" \\ "glWindowPos2i = (PFNGLWINDOWPOS2IPROC)glutGetProcAddress("glWindowPos2i");" Now the program runs, but it crashes when I try to use the glWindowPos2i function. – user3277234 Sep 21 '14 at 12:21