Skip to main content
gamedev.net
🔒 Locked

2D Text

Started by TinyGameDev 1 reply 750+ views
Original Post
TinyGameDev
TinyGameDev
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:

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

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!
zedzeek
zedzeek
this normally aint the best way of doing text its slow + looks like crap
normally its done with textures eg see nehe for an example

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.