openGL文本显示的未处理异常

时间:2013-10-18 12:18:17

标签: c++ opengl

我正在尝试使用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);
}

在文本正常工作之前绘制正方形。

1 个答案:

答案 0 :(得分: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并且我在推送当前矩阵后加载了身份矩阵。

如果这有任何帮助,请告诉我。