Jump to content
  • Advertisement
Sign in to follow this  
Flawe

OpenGL 2D Text

This topic is 4416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!