2

For my last few projects I have been using some of the utility files that I found whilst looking at a few demos here.

Namely a file called opengl.h - mainly used to manage shaders a bit like glew and another file gl_font.

gl_font is a class they use to render fonts on screen using vertex buffer objects.

However, when I use this to render the framerate in my game it draws everything but the skybox correctly. For some reason the skybox is rendered white as seen here, if I do not render the font it looks like this.

Here are some parts of the gl_font class that I think are most important:

void GLFont::begin()
{
    HWND hWnd = GetForegroundWindow();
    RECT rcClient;

    GetClientRect(hWnd, &rcClient);

    int w = rcClient.right - rcClient.left;
    int h = rcClient.bottom - rcClient.top;

    glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT);

    glDisable(GL_LIGHTING);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, m_fontTexture);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0f, w, h, 0.0f, -1.0f, 1.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);

    drawTextBegin();
}

I have trie changing glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT); to glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); and the background texture returns, but the font isn't rendered.

void GLFont::end()
{
    drawTextEnd();

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);

    glDisable(GL_BLEND);

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    glPopAttrib();
}

This is an image of the depth buffer when the font is rendered and this is what is looks like when it is not.

Could anyone shed some light on this problem please?

Any help would be much appreciated!

Thanks.

skaffman
  • 398,947
  • 96
  • 818
  • 769
henryprescott
  • 483
  • 4
  • 19

1 Answers1

1

Looks like begin() lacks a glPushMatrix() after glMatrixMode(GL_MODELVIEW). This might cause the scene to be rendered incorrectly when some text is also rendered.

Didn't glGetError() report a GL_STACK_UNDERFLOW error?

Kos
  • 70,399
  • 25
  • 169
  • 233
  • I'm rendering the text last, so the pushmatrix shouldn't matter should it? – henryprescott Jan 05 '11 at 18:48
  • Depends if you create modelview from scratch per frame. Anyway: if you have `GL_LIGHTING_BIT` also present in `glPopMatrix` and you draw the text last, are you sure that it passes depth test? Have you tried disabling GL_DEPTH_TEST for the time of rendering text? – Kos Jan 05 '11 at 18:59
  • @DeadMG, . They've already cleared that mess in recent API, there's nothing to complain about in GL 3 and GL 4... I guess :) – Kos Jan 05 '11 at 19:00
  • @DeadMG, and too bad that all those stubborn CAD software engineers use OpenGL anyway, despite it's horrifying PushMatrices! (And *why* are we continuing this holy war...? We have a Question to answer here!) – Kos Jan 05 '11 at 19:05
  • @Kos: This isn't about OGL vs D3D, it's about that the very idea of PushMatrix *sucks* and should never even have been considered. – Puppy Jan 05 '11 at 19:06
  • @DeadMG, The only thing which sucks is that it was part of the OGL API itself. It's quite a natural tool for implementing a scene tree. – Kos Jan 05 '11 at 19:52
  • I am resetting the matrix each frame. I have tried disabling depth test, it does nothing. – henryprescott Jan 05 '11 at 20:03
  • @henryprescott - okay... in that case, please prepare a scene with just the skybox and text (invisible) and append a GLintercept frame log to your question - it'll be easier to diagnose it in that way I think. – Kos Jan 05 '11 at 22:11
  • I decided just to go with FTGL it doesn't have VBOs but I have it working fine. – henryprescott Jan 06 '11 at 15:33