diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index 5b9327bb..1a53916e 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -102,7 +102,7 @@ assetload::postfunc assetload::atlas( continue; } std::set names = builder.getNames(); - Atlas* atlas = builder.build(2); + Atlas* atlas = builder.build(2, false); return [=](auto assets) { atlas->prepare(); assets->store(atlas, name); diff --git a/src/graphics/core/Atlas.cpp b/src/graphics/core/Atlas.cpp index 7d855a1a..2f9faaec 100644 --- a/src/graphics/core/Atlas.cpp +++ b/src/graphics/core/Atlas.cpp @@ -5,10 +5,17 @@ #include "Texture.h" #include "ImageData.h" -Atlas::Atlas(ImageData* image, std::unordered_map regions) - : texture(nullptr), - image(image), - regions(regions) { +Atlas::Atlas( + std::unique_ptr image, + std::unordered_map regions, + bool prepare +) : texture(nullptr), + image(std::move(image)), + regions(regions) +{ + if (prepare) { + this->prepare(); + } } Atlas::~Atlas() { @@ -43,7 +50,10 @@ bool AtlasBuilder::has(const std::string& name) const { return names.find(name) != names.end(); } -Atlas* AtlasBuilder::build(uint extrusion, uint maxResolution) { +Atlas* AtlasBuilder::build(uint extrusion, bool prepare, uint maxResolution) { + if (maxResolution == 0) { + maxResolution = Texture::MAX_RESOLUTION; + } auto sizes = std::make_unique(entries.size() * 2); uint index = 0; for (auto& entry : entries) { @@ -63,8 +73,9 @@ Atlas* AtlasBuilder::build(uint extrusion, uint maxResolution) { width *= 2; } if (width > maxResolution || height > maxResolution) { - throw std::runtime_error("max atlas resolution "+ - std::to_string(maxResolution)+" exceeded"); + throw std::runtime_error( + "max atlas resolution "+std::to_string(maxResolution)+" exceeded" + ); } } @@ -88,5 +99,5 @@ Atlas* AtlasBuilder::build(uint extrusion, uint maxResolution) { unitX * x, unitY * y, unitX * (x + w), unitY * (y + h) ); } - return new Atlas(canvas.release(), regions); + return new Atlas(std::move(canvas), regions, prepare); } diff --git a/src/graphics/core/Atlas.h b/src/graphics/core/Atlas.h index 24ea717a..2aec63f1 100644 --- a/src/graphics/core/Atlas.h +++ b/src/graphics/core/Atlas.h @@ -17,7 +17,14 @@ class Atlas { std::unique_ptr image; std::unordered_map regions; public: - Atlas(ImageData* image, std::unordered_map regions); + /// @param image atlas raster + /// @param regions atlas regions + /// @param prepare generate texture (.prepare()) + Atlas( + std::unique_ptr image, + std::unordered_map regions, + bool prepare + ); ~Atlas(); void prepare(); @@ -43,7 +50,12 @@ public: bool has(const std::string& name) const; const std::set& getNames() { return names; }; - Atlas* build(uint extrusion, uint maxResolution=8192); + /// @brief Build atlas from all added images + /// @param extrusion textures extrusion pixels + /// (greather is less mip-mapping artifacts) + /// @param prepare generate atlas texture (calls .prepare()) + /// @param maxResolution max atlas resolution + Atlas* build(uint extrusion, bool prepare=true, uint maxResolution=0); }; #endif // GRAPHICS_CORE_ATLAS_H_ diff --git a/src/graphics/core/Texture.cpp b/src/graphics/core/Texture.cpp index fbebef1f..473c0a14 100644 --- a/src/graphics/core/Texture.cpp +++ b/src/graphics/core/Texture.cpp @@ -6,6 +6,8 @@ #include "ImageData.h" #include "gl_util.h" +uint Texture::MAX_RESOLUTION = 1024; // Window.initialize overrides it + Texture::Texture(uint id, uint width, uint height) : id(id), width(width), height(height) { } diff --git a/src/graphics/core/Texture.h b/src/graphics/core/Texture.h index e5f0f4f3..227be461 100644 --- a/src/graphics/core/Texture.h +++ b/src/graphics/core/Texture.h @@ -11,6 +11,8 @@ protected: uint width; uint height; public: + static uint MAX_RESOLUTION; + Texture(uint id, uint width, uint height); Texture(ubyte* data, uint width, uint height, ImageFormat format); virtual ~Texture(); diff --git a/src/graphics/render/BlocksPreview.cpp b/src/graphics/render/BlocksPreview.cpp index 7fcc32fe..19d52b64 100644 --- a/src/graphics/render/BlocksPreview.cpp +++ b/src/graphics/render/BlocksPreview.cpp @@ -143,6 +143,5 @@ std::unique_ptr BlocksPreview::build( Window::viewport(0, 0, Window::width, Window::height); auto newAtlas = std::unique_ptr(builder.build(2)); - newAtlas->prepare(); return newAtlas; } diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 6153f6d7..ed571790 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -3,6 +3,7 @@ #include "Events.h" #include "../debug/Logger.h" #include "../graphics/core/ImageData.h" +#include "../graphics/core/Texture.h" #include #include @@ -145,6 +146,13 @@ int Window::initialize(DisplaySettings& settings){ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GLint maxTextureSize[1]{static_cast(Texture::MAX_RESOLUTION)}; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxTextureSize); + if (maxTextureSize[0] > 0) { + Texture::MAX_RESOLUTION = maxTextureSize[0]; + logger.info() << "max texture size is " << Texture::MAX_RESOLUTION; + } + glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, cursor_position_callback);