CmTexAtlasGenerator.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "CmTexAtlasGenerator.h"
  2. #include "CmDebug.h"
  3. namespace CamelotEngine
  4. {
  5. class TexAtlasNode
  6. {
  7. public:
  8. TexAtlasNode()
  9. :x(0), y(0), width(0), height(0), children(nullptr), nodeFull(false)
  10. { }
  11. TexAtlasNode(UINT32 _x, UINT32 _y, UINT32 _width, UINT32 _height)
  12. :x(_x), y(_y), width(_width), height(_height), children(nullptr), nodeFull(false)
  13. { }
  14. ~TexAtlasNode()
  15. {
  16. if(children != nullptr)
  17. delete[] children;
  18. }
  19. UINT32 x, y, width, height;
  20. TexAtlasNode* children;
  21. bool nodeFull;
  22. bool insert(TexAtlasElementDesc& element)
  23. {
  24. float aspect = width / (float)height;
  25. return insert(element, aspect);
  26. }
  27. bool insert(TexAtlasElementDesc& element, float aspect)
  28. {
  29. if (children != nullptr)
  30. {
  31. if (children[0].insert(element))
  32. return true;
  33. return children[1].insert(element);
  34. }
  35. else
  36. {
  37. if(nodeFull)
  38. return false;
  39. if (element.input.width > width || element.input.height > height)
  40. return false;
  41. if (element.input.width == width && element.input.height == height)
  42. {
  43. element.output.x = x;
  44. element.output.y = y;
  45. nodeFull = true;
  46. return true;
  47. }
  48. float dw = (float)(width - element.input.width);
  49. float dh = (height - element.input.height) * aspect;
  50. children = new TexAtlasNode[2];
  51. if (dw > dh)
  52. {
  53. children[0].x = x;
  54. children[0].y = y;
  55. children[0].width = element.input.width;
  56. children[0].height = height;
  57. children[1].x = x + element.input.width;
  58. children[1].y = y;
  59. children[1].width = width - element.input.width;
  60. children[1].height = height;
  61. }
  62. else
  63. {
  64. children[0].x = x;
  65. children[0].y = y;
  66. children[0].width = width;
  67. children[0].height = element.input.height;
  68. children[1].x = x;
  69. children[1].y = y + element.input.height;
  70. children[1].width = width;
  71. children[1].height = height - element.input.height;
  72. }
  73. return children[0].insert(element);
  74. }
  75. }
  76. };
  77. TexAtlasGenerator::TexAtlasGenerator(bool square, UINT32 maxTexWidth, UINT32 maxTexHeight, bool fixedSize)
  78. :mSquare(square), mMaxTexWidth(maxTexWidth), mMaxTexHeight(maxTexHeight), mFixedSize(fixedSize)
  79. {
  80. if(square)
  81. {
  82. if(maxTexWidth > maxTexHeight)
  83. maxTexWidth = maxTexHeight;
  84. if(maxTexHeight > maxTexWidth)
  85. maxTexHeight = maxTexWidth;
  86. }
  87. }
  88. vector<TexAtlasPageDesc>::type TexAtlasGenerator::createAtlasLayout(vector<TexAtlasElementDesc>::type& elements) const
  89. {
  90. for(size_t i = 0; i < elements.size(); i++)
  91. elements[i].output.page = -1;
  92. //sortBySize(elements);
  93. int numPages = generatePagesForSize(elements, mMaxTexWidth, mMaxTexHeight);
  94. if(numPages == -1)
  95. {
  96. LOGWRN("Some of the provided elements don't fit in an atlas of provided size. Returning empty array of pages.");
  97. return vector<TexAtlasPageDesc>::type();
  98. }
  99. if(numPages == 0)
  100. return vector<TexAtlasPageDesc>::type();
  101. UINT32 lastPageWidth = mMaxTexWidth;
  102. UINT32 lastPageHeight = mMaxTexHeight;
  103. UINT32 lastPageIdx = numPages - 1;
  104. // If size isn't fixed, try to reduce the size of the last page
  105. if(!mFixedSize)
  106. {
  107. while (true)
  108. {
  109. if (lastPageWidth <= 1 || lastPageHeight <= 1)
  110. break;
  111. int newLastPageWidth = lastPageWidth;
  112. int newLastPageHeight = lastPageHeight;
  113. if(mSquare)
  114. {
  115. if (newLastPageWidth > newLastPageHeight)
  116. newLastPageWidth /= 2;
  117. else
  118. newLastPageHeight /= 2;
  119. }
  120. else
  121. {
  122. if (newLastPageWidth > newLastPageHeight)
  123. newLastPageWidth /= 2;
  124. else
  125. newLastPageHeight /= 2;
  126. }
  127. // Clear page indexes so we know which pages to process
  128. for(size_t i = 0; i < elements.size(); i++)
  129. {
  130. if(elements[i].output.page == lastPageIdx)
  131. elements[i].output.page = -1;
  132. }
  133. if(generatePagesForSize(elements, newLastPageWidth, newLastPageHeight, lastPageIdx) == 1)
  134. {
  135. lastPageWidth = newLastPageWidth;
  136. lastPageHeight = newLastPageHeight;
  137. }
  138. else
  139. break;
  140. }
  141. }
  142. // Create page descriptors and return
  143. vector<TexAtlasPageDesc>::type pages;
  144. for(int i = 0; i < numPages - 1; i++)
  145. {
  146. TexAtlasPageDesc pageDesc;
  147. pageDesc.width = mMaxTexWidth;
  148. pageDesc.height = mMaxTexHeight;
  149. pages.push_back(pageDesc);
  150. }
  151. TexAtlasPageDesc lastPageDesc;
  152. lastPageDesc.width = lastPageWidth;
  153. lastPageDesc.height = lastPageHeight;
  154. pages.push_back(lastPageDesc);
  155. return pages;
  156. }
  157. int TexAtlasGenerator::generatePagesForSize(vector<TexAtlasElementDesc>::type& elements, UINT32 width, UINT32 height, UINT32 startPage) const
  158. {
  159. if(elements.size() == 0)
  160. return 0;
  161. int currentPage = startPage;
  162. int numPages = 0;
  163. while (true)
  164. {
  165. int largestTexId = findLargestTextureWithoutPage(elements); // Start with the largest available texture
  166. if (largestTexId == -1) // No more textures, we're done
  167. return numPages;
  168. TexAtlasElementDesc& currentElem = elements[largestTexId];
  169. // If the texture is larger than the atlas size then it can never fit
  170. while (width < currentElem.input.width || height < currentElem.input.height)
  171. return -1;
  172. TexAtlasNode atlasNode(0, 0, width, height);
  173. atlasNode.insert(elements[largestTexId]);
  174. elements[largestTexId].output.page = currentPage;
  175. // Now that the first (largest) texture has been added, do the same for every other texture until atlas is full
  176. while (true)
  177. {
  178. int addedTextureId = addLargestTextureWithoutPageThatFits(elements, atlasNode); // Try to add next largest texture
  179. if (addedTextureId == -1)
  180. break;
  181. elements[addedTextureId].output.page = currentPage;
  182. }
  183. currentPage++;
  184. numPages++;
  185. }
  186. }
  187. int TexAtlasGenerator::addLargestTextureWithoutPageThatFits(vector<TexAtlasElementDesc>::type& elements, TexAtlasNode& node) const
  188. {
  189. UINT32 sizeLimit = std::numeric_limits<UINT32>::max();
  190. while (true)
  191. {
  192. UINT32 largestSize = 0;
  193. INT32 largestId = -1;
  194. for (size_t i = 0; i < elements.size(); i++)
  195. {
  196. if (elements[i].output.page == -1) // Signifies that we haven't processed this node yet
  197. {
  198. UINT32 size = elements[i].input.width * elements[i].input.height;
  199. if (size > largestSize && size < sizeLimit)
  200. {
  201. largestSize = size;
  202. largestId = (INT32)i;
  203. }
  204. }
  205. }
  206. if (largestId == -1)
  207. return -1;
  208. if (node.insert(elements[largestId]))
  209. return largestId;
  210. else
  211. sizeLimit = largestSize;
  212. }
  213. }
  214. int TexAtlasGenerator::findLargestTextureWithoutPage(const vector<TexAtlasElementDesc>::type& elements) const
  215. {
  216. INT32 largestId = -1;
  217. UINT32 largestSize = 0;
  218. for (size_t i = 0; i < elements.size(); i++)
  219. {
  220. if (elements[i].output.page == -1) // Signifies that we haven't processed this node yet
  221. {
  222. UINT32 size = elements[i].input.width * elements[i].input.height;
  223. if (size > largestSize)
  224. {
  225. largestSize = size;
  226. largestId = (INT32)i;
  227. }
  228. }
  229. }
  230. return largestId;
  231. }
  232. void TexAtlasGenerator::sortBySize(vector<TexAtlasElementDesc>::type& elements) const
  233. {
  234. for (size_t i = 0; i < elements.size(); i++)
  235. {
  236. INT32 largestId = -1;
  237. UINT32 largestSize = 0;
  238. for (size_t j = i; j < elements.size(); j++)
  239. {
  240. UINT32 size = elements[j].input.width * elements[j].input.height;
  241. if (size > largestSize)
  242. {
  243. largestSize = size;
  244. largestId = (INT32)j;
  245. }
  246. }
  247. if(largestId != -1)
  248. {
  249. TexAtlasElementDesc temp = elements[i];
  250. elements[i] = elements[largestId];
  251. elements[largestId] = temp;
  252. }
  253. }
  254. }
  255. }