Jump to content
  • Advertisement
Sign in to follow this  
Vexator

allocating a texture large enough to hold all font glyphs

This topic is 3367 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

i'm loading truetype fonts with freetype in ogl. i store all glyphs in one single texture. i load all the glyphs in small buffers and then fill them in the texture's buffer. example output: so far i'm just guessing the needed texture dimensions, and that's of course not a good idea. but i'm not quite sure how to do it without wasting too much space. my only idea was to loop through all glyphs and save the biggest width and height, multiply that with the number of characters and count to the next power of two; but this results in way to big (and of course always square) textures with a lot of space wasted. any idea how to do this in a smarter way? thank you :)

Share this post


Link to post
Share on other sites
Advertisement
Well, you've obviously already got code which can layout the glyphs in a texture. My suggestion would be to do a "dry-run" assuming ever-increasing texture sizes (start off with 64x64, then 128x128, etc - you can choose a bunch of sizes which seem appropriate). Then do a dry-run (without actually creating the texture/copying the glyphs; just measuring the sizes) and if you run out of space in the 64x64 dry run, move onto the 128x128. Keep going until you find a texture size that fits.

Another method would be to use a fixed width (say 256) and then use the dry-run method to find the height you need: just keep filling in lines as you go down, when you hit the last character, the height that you're currently at is the minimum height of the texture you'll need.

Share this post


Link to post
Share on other sites
well, that's basically what i had in mind - either doing a dry run or using/writing an external tool. i just thought there has to be a smarter way and i'm just not getting it. i guess i'll go for the dry-run concept, thank you guys!

Share this post


Link to post
Share on other sites
If it were me i'd just do a dry run before you create your texture to see how big it needs to be. This sort of stuff is done offline anyways.

Share this post


Link to post
Share on other sites
Putting all Unicode glyphs in a single texture is bloat.

I suggest you use a library that is already capable of doing it right, such as FTGL.

If you insist on doing it yourself, I suggest you read http://dmedia.dprogramming.com/?n=Tutorials.TextRendering1

Share this post


Link to post
Share on other sites
You could always take a look at some more sophisticated algorithms. How about using multiple textures? ie. you may save space by using a 512x512 and a 128x128 instead of packing it all into 1 texture which could result in a 1024x1024 texture.

Also, how about rotating some of the glyphs? Then in whatever binary you're loading with the glyphs simply mark that one as rotated.

Share this post


Link to post
Share on other sites
of course i could simply use a 3rd party library, but i'm doing this to learn :) another problem i have is choosing which characters to store in the texture. as you can see in my first post, i just store the most basic characters so far, no umlauts, no letters with accents etc. how would you do it? or is it an option to have freetype render characters on the fly, i.e. each time the text string is changed?

Share this post


Link to post
Share on other sites
Most font code I've seen treats the texture as a cache and writes characters into it on demand. For some languages this is pretty much a requirement - there are too many characters in many asian languages to pre-allocate all the characters you might need if you support arbitrary text (e.g. in game chat). To handle running out of space you can either use standard caching schemes for throwing out the least recently used characters or simply reallocate a new texture with more space as needed (might cause a slight frame rate hiccup but should be a pretty infrequent event). If you go with a fixed size texture with a caching scheme you probably want to support repacking all the existing characters/clearing the cache or you may eventually get 'fragmentation' problems when trying to add new characters.

Unless you are targeting very low system specs you should be able to afford to allocate a sufficiently large texture for a worst case scenario without major problems. Even a 1024x1024 8 bit grayscale or DXT compressed texture isn't a huge deal on a modern PC.

Share this post


Link to post
Share on other sites
There are more than one million characters in Unicode. You can't prerender them to a texture.

You need to do the rendering on demand, which involves fairly complicated Unicode stuff, kerning, hinting, antialiasing, etc.
That is why you should really just use an existing library.

You should really go see the link I posted, it explains everything.

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!