CmTexAtlasGenerator.cpp 8.3 KB

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