If you're working with custom font rendering or lightmapping for a 3D application, you might have run into a problem where you want to use a single big texture surface as an atlas for allocating several smaller rectangular image areas. This is a recurring topic at GameDev, so I'm posting here a small piece of C++ code as an example to show how to perform this kind of allocation.

The code is a straightforward implementation of the idea presented at Packing Lightmaps, by Jim Scott. I use the code to build a font cache for glyphs generated by FreeType2.

After playing around with the method a little, I realize it's not the smartest or fastest way to do the packing (see the comments in the code header), but it's simple and performs pretty well. In my font rendering application, I've measured I achieve occupancy rates ranging from 0.85 to 0.89.

Note that this method cannot be used for building atlases if they require mip-mapping. In that case, see nVidia Texture Atlas Tools. In fact, remember to disable the automatic mip chain generation whenever you're building atlases of this type.

For illustrative purposes, below is a video that shows the packing process as it occurs for randomly inserted font glyphs.

Download the code: (to compile, you need to replace Ptr() with boost::shared_ptr or manual memory management)



Last Updated (Saturday, 19 September 2009 22:05)