未显示OpenGL纹理(纯色而非纹理)或使用工件显示

时间:2023-01-06 19:21:35

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.

我正在使用代码在OpenGL中将文本绘制为纹理(Qt4.8.3 + Linux(类似于debian))。代码从2D项目移植,它工作得很好。

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

2D项目正在使用gluOrtho2D,现在我使用gluLookAt进行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.

问题是,而不是文本,我正在看彩色矩形。如果我打开GL_DEPTH_TEST,我会看到工件而不是文本。如果我移动相机,BTW神器会改变,这很奇怪。

未显示OpenGL纹理(纯色而非纹理)或使用工件显示

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();
}

2 个解决方案

#1


0  

The reason for the artifacts is Z fighting.

这些文物的原因是Z战斗。

This problem can be solved by increasing the depth buffer precision OR by making the different objects further apart from each-other (the ones from drawlable ).

这个问题可以通过增加深度缓冲精度或通过使不同的对象彼此远离(可绘制的对象)来解决。

Also make sure that your generated text doesn't get culled by the far plane / near plane (maybe you generate the geometry very near the edges ? ).

还要确保生成的文本不会被远平面/*面剔除(也许你会在边缘附近生成几何体?)。

#2


0  

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

事实证明,因为纹理是在QGLWidget构造函数中加载的,所以上下文未创建或未设置。

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

我在initializeGL()方法中移动了纹理加载,效果很好。

#1


0  

The reason for the artifacts is Z fighting.

这些文物的原因是Z战斗。

This problem can be solved by increasing the depth buffer precision OR by making the different objects further apart from each-other (the ones from drawlable ).

这个问题可以通过增加深度缓冲精度或通过使不同的对象彼此远离(可绘制的对象)来解决。

Also make sure that your generated text doesn't get culled by the far plane / near plane (maybe you generate the geometry very near the edges ? ).

还要确保生成的文本不会被远平面/*面剔除(也许你会在边缘附近生成几何体?)。

#2


0  

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

事实证明,因为纹理是在QGLWidget构造函数中加载的,所以上下文未创建或未设置。

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

我在initializeGL()方法中移动了纹理加载,效果很好。