refactor + auto max atlas resolution

This commit is contained in:
MihailRis 2024-04-10 18:18:55 +03:00
parent befc9cf09c
commit eb70e1c0df
7 changed files with 46 additions and 12 deletions

View File

@ -102,7 +102,7 @@ assetload::postfunc assetload::atlas(
continue;
}
std::set<std::string> names = builder.getNames();
Atlas* atlas = builder.build(2);
Atlas* atlas = builder.build(2, false);
return [=](auto assets) {
atlas->prepare();
assets->store(atlas, name);

View File

@ -5,10 +5,17 @@
#include "Texture.h"
#include "ImageData.h"
Atlas::Atlas(ImageData* image, std::unordered_map<std::string, UVRegion> regions)
: texture(nullptr),
image(image),
regions(regions) {
Atlas::Atlas(
std::unique_ptr<ImageData> image,
std::unordered_map<std::string, UVRegion> 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<uint[]>(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);
}

View File

@ -17,7 +17,14 @@ class Atlas {
std::unique_ptr<ImageData> image;
std::unordered_map<std::string, UVRegion> regions;
public:
Atlas(ImageData* image, std::unordered_map<std::string, UVRegion> regions);
/// @param image atlas raster
/// @param regions atlas regions
/// @param prepare generate texture (.prepare())
Atlas(
std::unique_ptr<ImageData> image,
std::unordered_map<std::string, UVRegion> regions,
bool prepare
);
~Atlas();
void prepare();
@ -43,7 +50,12 @@ public:
bool has(const std::string& name) const;
const std::set<std::string>& 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_

View File

@ -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) {
}

View File

@ -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();

View File

@ -143,6 +143,5 @@ std::unique_ptr<Atlas> BlocksPreview::build(
Window::viewport(0, 0, Window::width, Window::height);
auto newAtlas = std::unique_ptr<Atlas>(builder.build(2));
newAtlas->prepare();
return newAtlas;
}

View File

@ -3,6 +3,7 @@
#include "Events.h"
#include "../debug/Logger.h"
#include "../graphics/core/ImageData.h"
#include "../graphics/core/Texture.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@ -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<GLint>(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);