Class STBRectPack
- java.lang.Object
-
- org.lwjgl.stb.STBRectPack
-
public class STBRectPack extends java.lang.ObjectNative bindings to stb_rect_pack.h from the stb library.Useful for e.g. packing rectangular textures into an atlas. Does not do rotation.
This library currently uses the Skyline Bottom-Left algorithm. Not necessarily the awesomest packing method, but better than the totally naive one in stb_truetype (which is primarily what this is meant to replace).
-
-
Field Summary
Fields Modifier and Type Field and Description static intSTBRP_HEURISTIC_Skyline_BF_sortHeight
STBRP_HEURISTIC_Skyline_BL_sortHeight
STBRP_HEURISTIC_Skyline_defaultPacking heuristics
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static voidstbrp_init_target(STBRPContext context, int width, int height, STBRPNode.Buffer nodes)Initialize a rectangle packer to: pack a rectangle that iswidthbyheightin dimensions using temporary storage provided by the arraynodes, which isnum_nodeslong.static voidstbrp_pack_rects(STBRPContext context, STBRPRect.Buffer rects)Assigns packed locations to rectangles.static voidstbrp_setup_allow_out_of_mem(STBRPContext context, boolean allow_out_of_mem)Optionally call this function after init but before doing any packing to change the handling of the out-of-temp-memory scenario, described ininit_target.static voidstbrp_setup_heuristic(STBRPContext context, int heuristic)Optionally select which packing heuristic the library should use.
-
-
-
Method Detail
-
stbrp_pack_rects
public static void stbrp_pack_rects(STBRPContext context, STBRPRect.Buffer rects)
Assigns packed locations to rectangles. The rectangles are of typeSTBRPRect, stored in the arrayrects, and there arenum_rectsmany of them.Rectangles which are successfully packed have the
was_packedflag set to a non-zero value andxandystore the minimum location on each axis (i.e. bottom-left in cartesian coordinates, top-left if you imagine y increasing downwards). Rectangles which do not fit have thewas_packedflag set to 0.You should not try to access the
rectsarray from another thread while this function is running, as the function temporarily reorders the array while it executes.To pack into another rectangle, you need to call
init_targetagain. To continue packing into the same rectangle, you can call this function again. Calling this multiple times with multiple rect arrays will probably produce worse packing results than calling it a single time with the full rectangle array, but the option is available.- Parameters:
context- anSTBRPContextstructrects- an array ofSTBRPRectstructs
-
stbrp_init_target
public static void stbrp_init_target(STBRPContext context, int width, int height, STBRPNode.Buffer nodes)
Initialize a rectangle packer to: pack a rectangle that iswidthbyheightin dimensions using temporary storage provided by the arraynodes, which isnum_nodeslong.You must call this function every time you start packing into a new target.
There is no "shutdown" function. The
nodesmemory must stay valid for the followingpack_rectscall (or calls), but can be freed after the call (or calls) finish.Note: to guarantee best results, either:
- make sure
num_nodes ≥ width - or, call
setup_allow_out_of_memwithallow_out_of_mem = 1
If you don't do either of the above things, widths will be quantized to multiples of small integers to guarantee the algorithm doesn't run out of temporary storage.
If you do #2, then the non-quantized algorithm will be used, but the algorithm may run out of temporary storage and be unable to pack some rectangles.
- Parameters:
context- anSTBRPContextstructwidth- the rectangle widthheight- the rectangle heightnodes- an array ofSTBRPNodestructs
- make sure
-
stbrp_setup_allow_out_of_mem
public static void stbrp_setup_allow_out_of_mem(STBRPContext context, boolean allow_out_of_mem)
Optionally call this function after init but before doing any packing to change the handling of the out-of-temp-memory scenario, described ininit_target. If you call init again, this will be reset to the default (false).- Parameters:
context- anSTBRPContextstructallow_out_of_mem- 1 to allow running out of temporary storage
-
stbrp_setup_heuristic
public static void stbrp_setup_heuristic(STBRPContext context, int heuristic)
Optionally select which packing heuristic the library should use. Different heuristics will produce better/worse results for different data sets. If you call init again, this will be reset to the default.- Parameters:
context- anSTBRPContextstructheuristic- the packing heuristic
-
-