Search code examples
c++linuxqtopengltextures

OpenGL texture not displayed (solid color instead texture) or displayed with artifacts


I'm using code to draw text as textures in OpenGL (Qt4.8.3 + Linux (debian-like)). Code was ported from 2D project, where it is working good.

2D project was using gluOrtho2D, now I use gluLookAt for 3D.

The issue is that instead of text I'm seing colored rectangle. If I turn on GL_DEPTH_TEST I see artifacts instead of text. BTW artifacts change if I move camera, which is quite strange.

issue

Here's the code:

void GLWidget::paintGL() {
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Set blending function.
    glEnable(GL_BLEND); //Enable blending.
    glShadeModel(GL_SMOOTH);
    glEnable(GL_MULTISAMPLE);

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 60.0f, (GLdouble) width() / (GLdouble) height(), 0.001f, 10000.0f );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // Set up current camera
    gluLookAt( cameraDistance * sin(cameraTheta * M_PI / 180) * cos(cameraPhi * M_PI / 180),
               cameraDistance * sin(cameraTheta * M_PI / 180) * sin(cameraPhi * M_PI / 180),
               cameraDistance * cos(cameraTheta * M_PI / 180),
               0.0, 0.0, 0.0,
               0.0, 0.0, 1.0);

    glTranslatef(-4.5355, -4.5355, 0.0);
    glPushMatrix();

    // draw text labels
    drawLabel(1, 0, 90, "1");
    drawLabel(2, 0, 90, "2");
    drawLabel(3, 0, 90, "3");
    drawLabel(4, 0, 90, "4");
    drawLabel(5, 0, 90, "5");

    glPopMatrix();

    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);

    glPopMatrix();
}

void GLWidget::drawLabel(float xpos, float ypos, float angle, char *txt) {
    float labelHeight = 0.3;
    float labelWidth = labelHeight / 2;
    float margin = labelWidth / 10;
    float len = (float) strlen(txt);

    glPushMatrix();

    glRotatef(-angle, 0, 0, -1);
    glTranslatef(xpos, ypos, 0.0f);
    glRotatef(angle, 0, 0, -1);
    glScalef(1.0, -1.0, 1.0);

    glTranslatef(- len * labelWidth / 2, -labelHeight / 2 + margin, 0.0f);

    // background
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
    glVertex3f(-margin, -margin, 0);
    glVertex3f(len * labelWidth + margin, -margin, 0);
    glVertex3f(len * labelWidth + margin, labelHeight + margin, 0);
    glVertex3f(-margin, labelHeight + margin, 0);
    glEnd();

    // text
    glColor3f(0.5f, 0.5f, 0.5f);
    glEnable(GL_TEXTURE_2D);
    glBindTexture( GL_TEXTURE_2D, glFont->getTextureID() );
    glFont->drawText(labelWidth, labelHeight, txt);
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);

    glPopMatrix();
}

void oglFont::drawText(GLfloat cw, GLfloat ch, char *txt)
{
    glBegin(GL_QUADS);

    //character location and dimensions
    GLfloat cx = 0.0f;
    GLfloat cy = 0.0f;

    //calculate how wide each character is in term of texture coords
    GLfloat dtx = float(c_width) / float(m_width);
    GLfloat dty = float(c_height) / float(m_height);

    for (char * c = txt; *c != 0; c++, cx += cw) {
        int index = getCharIndex(c);
        int row = index / c_per_row;
        int col = index % c_per_row;

        if (index < 0) {
            //qDebug() << "glFont: Character outside of font! char: " << c;
        }

        // find the texture coords
        GLfloat tx = float(col * c_width) / float(m_width);
        GLfloat ty = float(row * c_height) / float(m_height);

        glTexCoord2f(0, 0);
        glVertex2f(cx, cy);
        glTexCoord2f(1, 0);
        glVertex2f(cx + cw, cy);
        glTexCoord2f(1, 1);
        glVertex2f(cx + cw, cy + ch);
        glTexCoord2f(0, 1);
        glVertex2f(cx, cy + ch);
    }
    glEnd();
}

Solution

  • It turns out that because texture was loaded in QGLWidget constructor, context was either not created or not set.

    I moved texture loading in initializeGL() method and it works great.