   Dynamic packing of textures.
  I am about to start working on improving batching in my chunked LOD terrain renderer, and need to find an algorithm for dynamically packing multiple textures into a single texture. I imagine my system will be set up to allow a max number of active hi-res textures (e.g. 1024x1024) and dynamically pack my terrain textures (128x128) into these, attempting to use as few as possible. Obviously the terrain chunks themselves go active and then deactivate later and their texture is no longer needed, and can be overwritten in the packed textures. I guess what I am looking for is an allocation algorithm that works well for this kind of problem.Any ideas?   
  I think the algorithm is called "Rectangle Packing". Google it for more info.   ;  Great thanks a lot! I tried googling for "texture packing" and variations to no-avail.   ;Quote:Original post by bluntmanI am about to start working on improving batching in my chunked LOD terrain renderer, and need to find an algorithm for dynamically packing multiple textures into a single texture. I imagine my system will be set up to allow a max number of active hi-res textures (e.g. 1024x1024) and dynamically pack my terrain textures (128x128) into these, attempting to use as few as possible. Obviously the terrain chunks themselves go active and then deactivate later and their texture is no longer needed, and can be overwritten in the packed textures. I guess what I am looking for is an allocation algorithm that works well for this kind of problem.Any ideas?You're not really looking for a packaging algorithm if you want to put multiple quad sized textures on one single texture (the packaging is not the problem). The solution to your problem is quite simple. You have to allocate X large textures which could hold up to Y sub-textures. Then you have to map your terrain chunk T to a texture like T->(X,Y). In combination with a LRU cache (least recent used) you have to do something like this:for each visible terrain chunk T  lookup (X,Y) in LRU cache for T  if no (X,Y) has been cached then    get least recent used (X',Y') in LRU cache    upload sub-texture of T to texture X' at location Y'    (X,Y)=(X',Y')  end    move (X,Y) to top of LRU cacheend//optional sort visible terrain chunks by texture Xfor each visible terrain chunk T   activate texture X   set sub texture to location Y   render chunk Tend ;  Yeah I realised this after looking at info on rectangle packing. I really wanted an allocation algorithm (like you have shown), but its really the more tricky bits I was worried about, e.g. fragmentation, if/when to de-fragment the packed textures. Still I'm not that worried about it for now, I will just do the most naive implementation to being with.Thanks!   