Original Post
Ok, I'm new to OpenGL and I've tried some basic things and now I wanted to print some 2D text. For some reason, nothing shows up on the screen. Anyone have any idea what could be wrong? I'm using display lists and bitmap characters. Here's the code: I'm trying to print text with: Text txt(GLUT_BITMAP_TIMES_ROMAN_10); txt.write(100, 100, "TEST"); ...but nothing shows up. Thanx for any help!
Text::Text(void *font)
{
//generate list ids for alla ASCII characters
base = glGenLists(256);
//create lists
for (int i=0; i<256; i++)
{
glNewList(base+i, GL_COMPILE);
glColor3f(1.0f,1.0f,0.0f);
glutBitmapCharacter(font, i);
glEndList();
}
}
void Text::write(int x, int y, const char* str)
{
//set list base to access teh correct list by character ascii code
glListBase(base);
//switch to orthographics projection
setOrtho();
//save matrix
glPushMatrix();
//clear matrix
glLoadIdentity();
//set raster position
glRasterPos2f(x, y);
//draw text
glCallLists((GLint) strlen(str), GL_UNSIGNED_BYTE, str);
//load matrix
glPopMatrix();
//switch to perspective projection
resetPerspective();
}
void Text::setOrtho()
{
//get screen dimensions
int w = glutGet((GLenum)GLUT_WINDOW_WIDTH);
int h = glutGet((GLenum)GLUT_WINDOW_HEIGHT);
//switch to projection mode
glMatrixMode(GL_PROJECTION);
//save matrix
glPushMatrix();
//clear matrix
glLoadIdentity();
//set a 2D orthographic projection
gluOrtho2D(0, w, 0, h);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// move the origin from the bottom left corner
// to the upper left corner
glTranslatef(0, -h, 0);
//switch to modelview mode
glMatrixMode(GL_MODELVIEW);
}
void Text::resetPerspective()
{
//set projection mode
glMatrixMode(GL_PROJECTION);
//load saved matrix
glPopMatrix();
//switch to modelview mode
glMatrixMode(GL_MODELVIEW);
}