glutStrokeCharacter() 翻转文本

glutStrokeCharacter() flipped text

我想制作一个非常简单的 OpenGL 应用程序(使用 freeglut),它只是将文本放在屏幕上。但是,文本被翻转(x 轴):

static char *string = "Hello World!\nHi";
void draw(void);

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("hello!");
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, WIN_WIDTH, WIN_HEIGHT, 0.0f, 0.0f, 1.0f);
    glutDisplayFunc(&draw);
    glutMainLoop();
    return(0);
}

void draw(void) {

    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    for (char* p = string; *p; p++)
        glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *p);
    glPopMatrix();
    glFlush();
}

您已经使用正交投影镜像了 y 轴。标准 y 轴指向上方,而不是下方。一种可能是更改投影:

glOrtho(0.0f, WIN_WIDTH, WIN_HEIGHT, 0.0f, 0.0f, 1.0f);

glOrtho(0.0f, WIN_WIDTH, 0.0f, WIN_HEIGHT, 0.0f, 1.0f);

另一种可能性是翻转 y 坐标 glScalef and adjust the position with glTranslatef:

void draw(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    
    glTranslatef(0.0f, 100.0f, 0.0f);
    glScalef(1.0f, -1.0f, 1.0f);
    
    for (char* p = string; *p; p++)
        glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *p);
    
    glPopMatrix();
    glFlush();
}