glutBitmapCharacter() doesn't display anything?

1.1k Views Asked by At

I am trying to make a simple text output with OpenGL, FreeGlut using glutBitmapCharacter(). My problem is, that i do not get any text shown. The triangle works fine. I guess the problem is not the displayText() function itself but maybe i call it at the wrong place or redraw/clear the text? I compile with gcc main.c -lGL -lglut -o filename

#include <stdio.h>
#include <string.h>
#include <GL/freeglut.h>


int WindowHeight = 500;
int WindowWidth = 500;

void displayText(float x, float y, int r, int g, int b, const char *string) {

    int j = strlen(string);
    glColor3f(r, g, b);
    glRasterPos2f(x, y);
    for (int i = 0; i < j; i++) {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
    }
    printf("Lange: %i - Raster: %f %f", j, x, y);

}

void keyboard(unsigned char key, int x, int y) {
    if (key == 27)
        exit(0);
    else
        printf("Sie haben %c gedruckt.\n", key);
}

void display(void) {


    glClearColor(1.0, 1.0, 1.0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();

    glBegin(GL_LINE_LOOP);
    glVertex3f(-0.3, -0.3, 0);
    glVertex3f(0, 0.3, 0);
    glVertex3f(0.3, -0.3, 0);
    glEnd();

    displayText(200, 200, 0, 0, 255, "test");

    glFlush();
}

int main(int argc, char **argv) {


    glutInit(&argc, argv);
    glutInitWindowSize(WindowWidth, WindowHeight);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Test");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
    return 0;
}
1

There are 1 best solutions below

0
genpfault On

glRasterPos2f() takes the current matrix stack into account, either switch to glWindowPos() (which works directly in window coordinates, bypassing the matrix stacks and viewport transform) or change your modelview/projection matrices to make a transform where (200, 200) isn't off-screen.