开发者

Packing bitmaps

开发者 https://www.devze.com 2023-03-19 16:57 出处:网络
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 text

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.

  1. Order glyphs from tallest to shortest. The tallest glyphs will be placed first.
  2. Let H be the height of the next tallest unplaced glyph.
  3. Expand your texture vertically by adding a level of height H.
  4. Fill the level with the remaining glyphs (tallest to shortest) until there is no room left for the next glyph.
  5. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号