PDA

View Full Version : 2D text



Flawe
09-18-2006, 12:14 PM
So, i'm trying to display some text by switching to orthographioc view and using display lists to show bitmap characters. But nothing shows up. I don't even know how to debug this since nothing happens.
Here's the code i use:



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);
}Anyone have any clues?

Thanx a lot

RigidBody
09-18-2006, 12:47 PM
1. you are using glRasterPos2f with two integer variables, x and y. this probably doesn't cause the problem, but you should fix it.

2. glTranslate, glScale, glRotate should be used on the modelview matrix, not on the projection matrix.

3. if you use glScale before glTranslate, this means that the translation is performed first. maybe exchanging them helps.

Flawe
09-19-2006, 06:12 AM
Well, I tried doing textured fonts and got that to work insted =)
Thanx for taking the time to answer!