Search code examples
qtopengltexttexturesglut

How to render text over the QOpenGLTexture?


So my task is to render some text over the texture in QOpenGLWidget. I'm using simple glutBitmapCharacter for text and it works fine without texture, but when i'm adding texture before the text like that:

// in initializeGL
QOpenGLTexture t = new QOenGLTexture(img);
t->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
t->setMagnificationFilter(QOpenGLTexture::Linear);

// in printGL
t->bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2i(0, 0);
glTexCoord2f(0, 1); glVertex2i(0, 255);
glTexCoord2f(1, 1); glVertex2i(255, 255);
glTexCoord2f(1, 0); glVertex2i(255, 0);
glEnd();

... it starts showing me texture without any text. Do i need to somehow unbind the texture after usage or what?


Solution

  • Two-dimensional texturing has to be enabled, see glEnable:

    glEnable(GL_TEXTURE_2D);
    

    If texturing is enabled, then by default the color of the texel is multiplied by the current color, because by default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv.

    This causes that the color of the texels of the texture is "mixed" by the last color which you have set by glColor.

    Set a "white" color before you render the texture, to solve your issue:

    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    

    Disable 2 dimensional texturing after drawing the quad:

    glEnable(GL_TEXTURE_2D);
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2i(0, 0);
    glTexCoord2f(0, 1); glVertex2i(0, 255);
    glTexCoord2f(1, 1); glVertex2i(255, 255);
    glTexCoord2f(1, 0); glVertex2i(255, 0);
    glEnd();
    
    glDisable(GL_TEXTURE_2D);