0

我正在尝试使用 openGL 绘制文本,该文本将显示在 kinect 相机图像上的窗口中。该程序可以很好地绘制正方形和其他形状,但是当我调用该方法来绘制文本时,它会崩溃。它似乎正在崩溃

glutStrokeCharacter(font, c);

其他一切正常,当我只注释掉这一行时,程序仍然运行良好。下面是我如何尝试绘制文本的代码片段。

void Button::DrawSquare(bool selected)
{

    glEnable( GL_POINT_SMOOTH );
    glLineWidth(7);
    if (selected == true) glColor3f(0,1,0);
    else glColor3f((123.0/255.0f),(205/255.0f),(237.0/255.0f));
    glBegin(GL_LINE_LOOP);
        glVertex2f(X, Y);
        glVertex2f(X+length, Y);
        glVertex2f(X+length, Y+width);
        glVertex2f(X, Y+width);
    glEnd();
    glLineWidth(3);
    glColor3f(0,0,0);
    glBegin(GL_LINE_LOOP);
        glVertex2f(X, Y);
        glVertex2f(X+length, Y);
        glVertex2f(X+length, Y+width);
        glVertex2f(X, Y+width);
    glEnd();
    glLineWidth(4);


    //Start drawing text
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    if (selected == true) glColor3f(0,1,0);
    else glColor3f(1,1,0);
    void *font = GLUT_BITMAP_TIMES_ROMAN_10;
    glRasterPos2i(this->X+15,this->Y+35);
    string s(*this->trackname); 

    for (string::iterator i = s.begin(); i != s.end(); ++i)
    {
        char c = *i;
        glutStrokeCharacter(font, c);// <<-- Line that gives error
    }
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glColor3f(255,255,255);
}

在文本之前绘制正方形效果很好。

4

1 回答 1

0

这是一个关于如何使用 glutBitmapCharacter 呈现文本的最小工作示例:

static const std::string text_to_render = "testing 1 2 3 4";

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glPushMatrix();
    glLoadIdentity();
    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();
    glLoadIdentity();

    glDisable( GL_DEPTH_TEST );  // disable to render text in front of anything else

    glRasterPos2f(0, 0); // center of screen
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    GLvoid * font = GLUT_BITMAP_TIMES_ROMAN_10;
    glColor3f(1.0f, 0.0f, 0.0f);

    for(char chr : text_to_render)
    {
        glutBitmapCharacter(font, chr);
    }

    glEnable(GL_DEPTH_TEST); // re-enable depth test

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glutSwapBuffers();
}

这里唯一的区别是我禁用了 OpenGL GL_DEPTH_TEST,并且在推送当前矩阵后加载了恒等矩阵。

让我知道这是否有帮助。

于 2013-10-18T13:32:23.877 回答