From 2b3df2889369aa22b6a1c782bb7fed658720c341 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Apr 2023 01:06:31 +0100 Subject: Have texture packer search harder for a solution, stopping at the reported texture size limit --- assetFactory/texturePacker.cpp | 16 ++++++++++++---- assetFactory/texturePacker.h | 1 + 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/assetFactory/texturePacker.cpp b/assetFactory/texturePacker.cpp index 30f1c37..fcc935f 100644 --- a/assetFactory/texturePacker.cpp +++ b/assetFactory/texturePacker.cpp @@ -14,6 +14,9 @@ TexturePacker::TexturePacker(std::span in) : std::sort(sortedIndexes.rbegin(), sortedIndexes.rend(), [this](const auto a, const auto b) { return area(inputImages[a]) < area(inputImages[b]); }); + int mts; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mts); + maxTextureSize = static_cast(mts); } TexturePacker::Result @@ -25,6 +28,9 @@ TexturePacker::pack() const TexturePacker::Result TexturePacker::pack(Size size) const { + if (size.x > maxTextureSize || size.y > maxTextureSize) { + return {}; + } using Spaces = std::set; Spaces spaces {{{}, size}}; @@ -49,12 +55,14 @@ TexturePacker::pack(Size size) const } } else { - if (size.x < size.y) { - return pack({size.x * 2, size.y}); + const auto x = pack({size.x * 2, size.y}), y = pack({size.x, size.y * 2}); + if (!x.first.empty() && (y.first.empty() || area(x.second) < area(y.second))) { + return x; } - else { - return pack({size.x, size.y * 2}); + else if (!y.first.empty()) { + return y; } + return {}; } } if (GLEW_ARB_texture_non_power_of_two) { diff --git a/assetFactory/texturePacker.h b/assetFactory/texturePacker.h index ca0d67a..a1b270b 100644 --- a/assetFactory/texturePacker.h +++ b/assetFactory/texturePacker.h @@ -38,4 +38,5 @@ public: private: std::span inputImages; std::vector sortedIndexes; + unsigned int maxTextureSize; }; -- cgit v1.2.3