refactor + auto max atlas resolution
This commit is contained in:
@@ -102,7 +102,7 @@ assetload::postfunc assetload::atlas(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::set<std::string> names = builder.getNames();
|
std::set<std::string> names = builder.getNames();
|
||||||
Atlas* atlas = builder.build(2);
|
Atlas* atlas = builder.build(2, false);
|
||||||
return [=](auto assets) {
|
return [=](auto assets) {
|
||||||
atlas->prepare();
|
atlas->prepare();
|
||||||
assets->store(atlas, name);
|
assets->store(atlas, name);
|
||||||
|
|||||||
@@ -5,10 +5,17 @@
|
|||||||
#include "Texture.h"
|
#include "Texture.h"
|
||||||
#include "ImageData.h"
|
#include "ImageData.h"
|
||||||
|
|
||||||
Atlas::Atlas(ImageData* image, std::unordered_map<std::string, UVRegion> regions)
|
Atlas::Atlas(
|
||||||
: texture(nullptr),
|
std::unique_ptr<ImageData> image,
|
||||||
image(image),
|
std::unordered_map<std::string, UVRegion> regions,
|
||||||
regions(regions) {
|
bool prepare
|
||||||
|
) : texture(nullptr),
|
||||||
|
image(std::move(image)),
|
||||||
|
regions(regions)
|
||||||
|
{
|
||||||
|
if (prepare) {
|
||||||
|
this->prepare();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Atlas::~Atlas() {
|
Atlas::~Atlas() {
|
||||||
@@ -43,7 +50,10 @@ bool AtlasBuilder::has(const std::string& name) const {
|
|||||||
return names.find(name) != names.end();
|
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);
|
auto sizes = std::make_unique<uint[]>(entries.size() * 2);
|
||||||
uint index = 0;
|
uint index = 0;
|
||||||
for (auto& entry : entries) {
|
for (auto& entry : entries) {
|
||||||
@@ -63,8 +73,9 @@ Atlas* AtlasBuilder::build(uint extrusion, uint maxResolution) {
|
|||||||
width *= 2;
|
width *= 2;
|
||||||
}
|
}
|
||||||
if (width > maxResolution || height > maxResolution) {
|
if (width > maxResolution || height > maxResolution) {
|
||||||
throw std::runtime_error("max atlas resolution "+
|
throw std::runtime_error(
|
||||||
std::to_string(maxResolution)+" exceeded");
|
"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)
|
unitX * x, unitY * y, unitX * (x + w), unitY * (y + h)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return new Atlas(canvas.release(), regions);
|
return new Atlas(std::move(canvas), regions, prepare);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,14 @@ class Atlas {
|
|||||||
std::unique_ptr<ImageData> image;
|
std::unique_ptr<ImageData> image;
|
||||||
std::unordered_map<std::string, UVRegion> regions;
|
std::unordered_map<std::string, UVRegion> regions;
|
||||||
public:
|
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();
|
~Atlas();
|
||||||
|
|
||||||
void prepare();
|
void prepare();
|
||||||
@@ -43,7 +50,12 @@ public:
|
|||||||
bool has(const std::string& name) const;
|
bool has(const std::string& name) const;
|
||||||
const std::set<std::string>& getNames() { return names; };
|
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_
|
#endif // GRAPHICS_CORE_ATLAS_H_
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
#include "ImageData.h"
|
#include "ImageData.h"
|
||||||
#include "gl_util.h"
|
#include "gl_util.h"
|
||||||
|
|
||||||
|
uint Texture::MAX_RESOLUTION = 1024; // Window.initialize overrides it
|
||||||
|
|
||||||
Texture::Texture(uint id, uint width, uint height)
|
Texture::Texture(uint id, uint width, uint height)
|
||||||
: id(id), width(width), height(height) {
|
: id(id), width(width), height(height) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ protected:
|
|||||||
uint width;
|
uint width;
|
||||||
uint height;
|
uint height;
|
||||||
public:
|
public:
|
||||||
|
static uint MAX_RESOLUTION;
|
||||||
|
|
||||||
Texture(uint id, uint width, uint height);
|
Texture(uint id, uint width, uint height);
|
||||||
Texture(ubyte* data, uint width, uint height, ImageFormat format);
|
Texture(ubyte* data, uint width, uint height, ImageFormat format);
|
||||||
virtual ~Texture();
|
virtual ~Texture();
|
||||||
|
|||||||
@@ -143,6 +143,5 @@ std::unique_ptr<Atlas> BlocksPreview::build(
|
|||||||
|
|
||||||
Window::viewport(0, 0, Window::width, Window::height);
|
Window::viewport(0, 0, Window::width, Window::height);
|
||||||
auto newAtlas = std::unique_ptr<Atlas>(builder.build(2));
|
auto newAtlas = std::unique_ptr<Atlas>(builder.build(2));
|
||||||
newAtlas->prepare();
|
|
||||||
return newAtlas;
|
return newAtlas;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "Events.h"
|
#include "Events.h"
|
||||||
#include "../debug/Logger.h"
|
#include "../debug/Logger.h"
|
||||||
#include "../graphics/core/ImageData.h"
|
#include "../graphics/core/ImageData.h"
|
||||||
|
#include "../graphics/core/Texture.h"
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
@@ -145,6 +146,13 @@ int Window::initialize(DisplaySettings& settings){
|
|||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
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);
|
glfwSetKeyCallback(window, key_callback);
|
||||||
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||||
glfwSetCursorPosCallback(window, cursor_position_callback);
|
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||||||
|
|||||||
Reference in New Issue
Block a user