summaryrefslogtreecommitdiff
path: root/assetFactory/texturePacker.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'assetFactory/texturePacker.cpp')
-rw-r--r--assetFactory/texturePacker.cpp29
1 files changed, 25 insertions, 4 deletions
diff --git a/assetFactory/texturePacker.cpp b/assetFactory/texturePacker.cpp
index 68a6010..fcc935f 100644
--- a/assetFactory/texturePacker.cpp
+++ b/assetFactory/texturePacker.cpp
@@ -1,7 +1,9 @@
#include "texturePacker.h"
#include "collections.hpp"
+#include <GL/glew.h>
#include <algorithm>
#include <cstdio>
+#include <glm/common.hpp>
#include <numeric>
#include <ostream>
#include <set>
@@ -12,6 +14,9 @@ TexturePacker::TexturePacker(std::span<const Image> 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<unsigned int>(mts);
}
TexturePacker::Result
@@ -23,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<Space>;
Spaces spaces {{{}, size}};
@@ -47,14 +55,27 @@ 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) {
+ // Crop the size back to minimum size
+ size = std::transform_reduce(
+ result.begin(), result.end(), inputImages.begin(), Size {},
+ [](auto && max, auto && limit) {
+ return glm::max(max, limit);
+ },
+ [](auto && pos, auto && size) {
+ return pos + size;
+ });
+ }
return {result, size};
}