Search code examples
copengltextglutfreeglut

glutStrokeCharacter() flipped text


I want to make a very simple OpenGL application (using freeglut) that just puts text on the screen. However, the Text is flipped (x-axis):

image

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

Solution

  • You've mirrored the y axis with the orthographic projection. The standard y-axis points up, not down. One possibility is to change the projection:

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

    Another possibility is to flip the y-coordinates with 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();
    }
    

    enter image description here