what is glut drawText doing

Viewed 45

I wish ppl can help explain the code below.

The code is from here, and it works.

The main questions can include the uses of the following functions in order to draw the text.

  • glGetDoublev
  • glPushMatrix
  • glPopMatrix
  • glLoadMatrixd(matrix)

Why we use them ?

The full code is presented again as below :

void drawText(const char* text, int length, int x, int y) {

    glMatrixMode(GL_PROJECTION);

    double* matrix = new double[16];
    glGetDoublev(GL_PROJECTION_MATRIX, matrix);
    glLoadIdentity();

    glOrtho(0, 800, 0, 600, -5, 5);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    glLoadIdentity();

    glRasterPos2i(x, y);
    for (int i=0; i < length; i++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
        printf("Hello world!\n");
    }

    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glLoadMatrixd(matrix);

    glMatrixMode(GL_MODELVIEW);
}
1 Answers

The position of the bitmap text is transformed by the current model view and projection matrix, because the coordinates of glRasterPos2i are transformed by the current model view and projection matrices. If you do not want to transform the coordinates, or if you want to use a different transformation such as the orthographic projection in the example, you must load the identity matrices or load a different transformation matrix.
Alternatively you can use glWindowPos. The coordinates of glWindowPos are not transformed:

 void drawText(const char* text, int length, int x, int y) {

    glWindowPos2f((float)x / 400.0f - 1.0f, (float)y / 300.0f - 1.0f);
    for (int i=0; i < length; i++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
        printf("Hello world!\n");
    }
}
Related