allocating a texture large enough to hold all font glyphs

Started by
10 comments, last by Cygon 14 years, 11 months ago
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 :)
Wunderwerk Engine is an OpenGL-based, shader-driven, cross-platform game engine. It is targeted at aspiring game designers who have been kept from realizing their ideas due to lacking programming skills.

blog.wunderwerk-engine.com
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.
Or you could use an offline tool like BMFont in order to generate your font texture.
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!
Wunderwerk Engine is an OpenGL-based, shader-driven, cross-platform game engine. It is targeted at aspiring game designers who have been kept from realizing their ideas due to lacking programming skills.

blog.wunderwerk-engine.com
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.
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
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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?
Wunderwerk Engine is an OpenGL-based, shader-driven, cross-platform game engine. It is targeted at aspiring game designers who have been kept from realizing their ideas due to lacking programming skills.

blog.wunderwerk-engine.com
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.

Game Programming Blog: www.mattnewport.com/blog

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.

This topic is closed to new replies.

Advertisement