Skip to main content
gamedev.net
🔒 Locked

Bitmap font problems

Started by Trillian Jul 27, 2007 at 3:44 PM 3 replies 1.3k views
Original Post
Trillian
Trillian
Hello! I'm trying to implement simple bitmap fonts and I'm running into some problems. First of all, here's the 64x64 image font, it's fixed-width for simplicity. Each character is 9x9 : Here how it renders Each character's uv rect seems to offset to the left as it approaches the right edge of the texture and it also seem to offset up when it approaches the lower edge of the texture. I can't understand why. Here's my setup code :
Real pixelSize = (Real)1/(Real)64; // texture coordinate size of 1 pixel

for(UInt j = 0 ; j < 6 ; ++j) // Each row
{
	for(UInt i = 0 ; i < 7 ; ++i) // Each col
	{
		Math::Rect * uvRect = Null;
		UInt index = i+j*7;
		// m_characters is a std::map<char, Math::Rect>
		if(index <= 25) uvRect = &m_characters['A' + (char)index];
		else if(index <= 35) uvRect = &m_characters['0' + (C8)(index-25)];
		else uvRect = &m_characters[0];

		// setMinSize(minx, miny, width, height)
		uvRect->setMinSize(i*9*pixelSize, j*9*pixelSize, 9*pixelSize, 9*pixelSize);
	}
}

When rendering, I look each character of the string in the character map to get the uvRect and I draw a rectangle with those uv coordinates. Anyone has an idea of why this is happening?
mrcheesewheel
mrcheesewheel
I think your problem is that though your image is 64 pixels across your texture's true extent is from the centre of the first pixel to the centre of the last pixel. That's to say there are actually only 63 pixel widths across your texture... so your 1/64 should probably be 1/63. That would produce a structured offset that got progressively worse (given your approach) across the texture's width and height.
Hope this sorts it,

Dan

EDIT: Your texture could also be running to the centres of the surrounding pixels... in which case your 64 should be 65. It depends on how precisely the texture coordinates are mapped onto pixels... ie does (0.0, 0.0) in texture space the upper left corner or the centre of the first pixel... or to the centre of the (-1,-1) pixel (if this is the case, your texture repeat method will affect the interpolation on that edge pixel).
TheGilb
TheGilb
The problem is that your characters may be only 9 characters wide, but that's 0-9 inclusive, which means the first pixel of the following graphic is at pixel location 10. Change your minx,miny calculations to i*10*pixelSize

You should notice by looking at the output that the uv coordinates are out by just 1 texel each time.
shukapi
shukapi
I think you might need to post your drawing code.
Because your coordinates look good to me.

TheGilb
Multiplying by 10 would give you 6 * 10 = 60 for the left-hand coordinate of the letter G. But by counting this is actually 54 (=9 * 60)
Cygon
Cygon
Counting the number of grid cells, your calculations look all right to me. The rendered characters you show also seem to jump by 1 texel every 2 (!) letters, which indicates a round-off error to me.

What is 'Real' defined as?

Maybe you could achieve better accuracy by calculating the absolute pixel positions of the letters and only then dividing them by the texture's width or height respectively.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.

Topic Locked

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

Sign in to reply to this topic.