文字颜色不正确

时间:2013-12-04 01:46:27

标签: c++ opengl

void text(string str)
{
    for (int i = 0; i < str.length(); i++)
    {
        glColor3f(0.0f, 0.0f, 0.0f);
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);
    }
}

void render(void)
{
    int width  = glutGet(GLUT_WINDOW_WIDTH);
    int height = glutGet(GLUT_WINDOW_HEIGHT);
    if (height == 0) height = 1;
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_SCISSOR_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, aspect, 0.1f, 100.0f);

    // Top view - top left
    glViewport(0, 0, width/2, height/2);
    glScissor(0, 0, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(0.0f, 0.0f, -5.0f, 0.0f, 0.0f, 0.0f, 1.0f);
    glRasterPos3f(-0.1f, -0.1f, 4.0f);
    text("Front");
    diode();

    // Corner view - top right
    glViewport(width/2, 0, width/2, height/2);
    glScissor(width/2, 0, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(0.0f, 0.0f, -5.0f, 0.0f, -90.0f, 0.0f, 1.0f);
    glRasterPos3f(4.0f, -0.1f, 0.1f);
    text("Right");
    diode();

    // Front view - bottom left
    glViewport(0, height/2, width/2, height/2);
    glScissor(0, height/2, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(0.0f, 0.0f, -5.0f, 90.0f, 0.0f, 0.0f, 1.0f);
    glRasterPos3f(-0.1f, 4.0f, 0.0f);
    text("Top");
    diode();

    // Right view - bottom right
    glViewport(width/2, height/2, width/2, height/2);
    glScissor(width/2, height/2, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(0.0f, 0.0f, -5.0f, 20.0f, 0.0f, 0.0f, 1.0f);
    glRasterPos3f(-0.1f, 4.0f, 0.0f);
    text("Fro4nt");
    diode();

    glDisable(GL_SCISSOR_TEST);
    glutSwapBuffers();
}

我不确定白色“Front”和黄色“Top”/“Right”的来源(颜色方面)。他们都应该是黑人。有谁知道这是什么问题?

以下是输出结果: enter image description here

1 个答案:

答案 0 :(得分:2)

正如所怀疑的那样,这可能会让人震惊,但glRasterPos (...)实际上会在您调用该功能时跟踪“当前”颜色。也就是说,在调用glRasterPos (...)之前设置的任何颜色都适用于该位置的绘制操作的“当前颜色”。可以把它想象成光栅化器与glVertex (...)的模拟,我将在下面解释。

您需要在致电glRasterPos (...)之前设置当前颜色,为此您应该完全从glColor3f (...)功能中删除text (...)来电,或者修改该功能以同时执行这两项操作 - 设置颜色,然后设置栅格位置,然后绘制文本。

<小时/>

glRasterPos — specify the raster position for pixel operations

  

当前光栅位置由三个窗口坐标(x,y,z),一个剪辑坐标值(w),一个眼睛坐标距离,一个有效位以及相关的颜色数据和纹理坐标组成。