Faster Text?

Started by
2 comments, last by mkaltner 22 years, 7 months ago
Is there a faster way to display text other than using glCallLists? But also about as easy to implement? In my program, I'm getting 315 FPS without text, 105 with. It's a pretty substantial difference. Oh, and it's a simply 2D game so yeah, that's a lot of frames per second. =) Thanks, - Mike Edited by - mkaltner on September 11, 2001 3:29:47 AM
"The important thing to remember when programming is: When you're 90% complete, there's still 50% more to go."
Advertisement
hi there,

the fastest way is to use texture fonts rather than bitmap
fonts.

use 1 texture for your entire font ie 1x256x256 and just
calculate the position for your letters texture coordinates
in this way you only need to bind the texture once per
text output.

Hope it helps

Mark
Well, I''m still pretty new to OpenGL so I woulnd''t know how to go about that. I can load and show a bitmap just fine but how would I do the coordinates of the letters to show? Could you give a little explaination of or even a code snippet?

Thanks,
- Mike
"The important thing to remember when programming is: When you're 90% complete, there's still 50% more to go."
Ok, it''s been a while since i did some code in openGL, so i leave the openGL part and just show you some code on how to get the appropriate coordinates on the texture.

First of all, create a bitmap containing the characters you want. I''d sudgest you choose the size 256x256 on the bitmap.
Now, make a grid of 16 pixels per grid on the bitmap.

This allows you to paint a character of 16x16 bytes, which should be enough. You also have room for 256 characters, which is the entire ASCII-list.

Now, give each grid with a index, like this:

0-15
16-31
32-47
48-63
and so on.


Now you just need to paint the appropriate characters in those grids.

For example, the number ''0'' has the ASCII value 48, which means the 4''th column and the first row. (See the example above)

Just fill in the blanks! find an ASCII definition table and paint the appropriate characters.

And now the code:

struct FRECT
{
float left;
float top;
float right;
float bottom;
};

void getcharposition(unsigned char character,FRECT *pos)
{
//define some variables
float row;
float column;

//Now we find out the position of the character.
row = character % 16;
column = character / 16;

//..And scale them to the range of 0.0f - 1.0f
row = 256/row;
column = 256/column;

pos->left = row;
pos->top = row;
pos->right = row+(16/256) // (16/256) scaled size of one grid.
pos->bottom = row+(16/256)
}



There! Now it''s just to omplement it!
(Could look something like this)

void glWriteTxt(FRECT *textposition,char *string)
{

glLoadTexture(GL_TEXTURE_2D,chartexture);
float num;
float charsizeinwindow = (textposition->right - textposition->left) / strlen(string);
FRECT charpos;
glBegin(GL_QUADS);
for (num = 0; num < strlen(string); num++)
{
getcharposition(string[num],&charpos);
glTexCoord2f(charpos.left);
glVertex3f(textposition->left+charsizewindow*num,textposition->top,0);
glTexCoord2f(charpos.right);
glVertex3f(textposition->left+charsizewindow*(num+1),textposition->top,0);
glTexCoord2f(charpos.right);
glVertex3f(textposition->left+charsizewindow*(num+1),textposition->bottom,0);
glTexCoord2f(charpos.left);
glVertex3f(textposition->left+charsizewindow*num,textposition->bottom,0);

}
glEnd();

}


That should do it...

--Electron"The truth can be changed simply by the way you accept it.""'General failure trying to read from file' - who is General Failure, and why is he reading my file??"

This topic is closed to new replies.

Advertisement