I am trying to pack font glyph images into a single texture. The bitmaps are 1 byte per pixel monochromatic and I wish to pack them all together onto 1 texture. I am able to calculate the minimum texture size required but I am unable to manage an algorithm to packing them all together.
I currently have the bitmaps stored as char pointers and I am able to get the dimensio开发者_开发问答ns of each.
I'm not an expert in bin packing, but here's a simple algorithm you may try.
- Order glyphs from tallest to shortest. The tallest glyphs will be placed first.
- Let H be the height of the next tallest unplaced glyph.
- Expand your texture vertically by adding a level of height H.
- Fill the level with the remaining glyphs (tallest to shortest) until there is no room left for the next glyph.
- Goto #2
This is known as Next-Fit Decreasing Height (NFDH) algorithm. An interactive demo can be seen here.
Since your glyphs are more or less the same height, I think this simple algorithm should give you good results.
Check out this survey for more algorithms.
Simple packing algorithm can be found here: http://www.blackpawn.com/texts/lightmaps/
It is called "Guillotine pack" in Jukka Jylänki's paper "A Thousand Ways To Pack the Bin".
The pseudo-code at blackpawn.com is really simple.
There are also related answers for similar questions: Piece together several images into one big image
精彩评论