2026-02-11 22:30:57 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstring>
|
2026-02-26 20:08:04 +08:00
|
|
|
#include <graphics/texture_atlas.h>
|
|
|
|
|
#include <utils/logger.h>
|
|
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// TextureAtlasPage 实现
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
TextureAtlasPage::TextureAtlasPage(int width, int height)
|
|
|
|
|
: width_(width), height_(height), isFull_(false), usedArea_(0) {
|
|
|
|
|
// 创建空白纹理
|
|
|
|
|
std::vector<uint8_t> emptyData(width * height * 4, 0);
|
2026-02-26 20:08:04 +08:00
|
|
|
texture_ = shared<GLTexture>(width, height, emptyData.data(), 4);
|
|
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 初始化矩形打包根节点
|
|
|
|
|
root_ = std::make_unique<PackNode>(0, 0, width, height);
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
E2D_LOG_INFO("Created texture atlas page: {}x{}", width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextureAtlasPage::~TextureAtlasPage() = default;
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
bool TextureAtlasPage::tryAddTexture(const std::string &name, int texWidth,
|
|
|
|
|
int texHeight, const uint8_t *pixels,
|
|
|
|
|
Rect &outUvRect) {
|
2026-02-11 22:30:57 +08:00
|
|
|
if (isFull_) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 添加边距
|
|
|
|
|
int paddedWidth = texWidth + 2 * PADDING;
|
|
|
|
|
int paddedHeight = texHeight + 2 * PADDING;
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 如果纹理太大,无法放入
|
|
|
|
|
if (paddedWidth > width_ || paddedHeight > height_) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 尝试插入
|
2026-02-26 20:08:04 +08:00
|
|
|
PackNode *node = insert(root_.get(), paddedWidth, paddedHeight);
|
2026-02-11 22:30:57 +08:00
|
|
|
if (node == nullptr) {
|
|
|
|
|
// 无法放入,标记为满
|
|
|
|
|
isFull_ = true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 写入像素数据(跳过边距区域)
|
2026-02-26 20:08:04 +08:00
|
|
|
writePixels(node->x + PADDING, node->y + PADDING, texWidth, texHeight,
|
|
|
|
|
pixels);
|
|
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 创建条目
|
|
|
|
|
AtlasEntry entry;
|
|
|
|
|
entry.name = name;
|
2026-02-26 20:08:04 +08:00
|
|
|
entry.originalSize =
|
|
|
|
|
Vec2(static_cast<float>(texWidth), static_cast<float>(texHeight));
|
2026-02-11 22:30:57 +08:00
|
|
|
entry.padding = PADDING;
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 计算 UV 坐标(考虑边距)
|
|
|
|
|
float u1 = static_cast<float>(node->x + PADDING) / width_;
|
|
|
|
|
float v1 = static_cast<float>(node->y + PADDING) / height_;
|
|
|
|
|
float u2 = static_cast<float>(node->x + PADDING + texWidth) / width_;
|
|
|
|
|
float v2 = static_cast<float>(node->y + PADDING + texHeight) / height_;
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
entry.uvRect = Rect(u1, v1, u2 - u1, v2 - v1);
|
|
|
|
|
outUvRect = entry.uvRect;
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
entries_[name] = std::move(entry);
|
|
|
|
|
usedArea_ += paddedWidth * paddedHeight;
|
2026-02-26 20:08:04 +08:00
|
|
|
|
|
|
|
|
E2D_LOG_DEBUG("Added texture '{}' to atlas: {}x{} at ({}, {})", name,
|
|
|
|
|
texWidth, texHeight, node->x, node->y);
|
|
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
TextureAtlasPage::PackNode *TextureAtlasPage::insert(PackNode *node, int width,
|
|
|
|
|
int height) {
|
2026-02-11 22:30:57 +08:00
|
|
|
if (node == nullptr) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 如果节点已被使用,尝试子节点
|
|
|
|
|
if (node->used) {
|
2026-02-26 20:08:04 +08:00
|
|
|
PackNode *result = insert(node->left.get(), width, height);
|
2026-02-11 22:30:57 +08:00
|
|
|
if (result != nullptr) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return insert(node->right.get(), width, height);
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 检查是否适合
|
|
|
|
|
if (width > node->width || height > node->height) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 如果刚好合适,使用此节点
|
|
|
|
|
if (width == node->width && height == node->height) {
|
|
|
|
|
node->used = true;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 需要分割节点
|
|
|
|
|
int dw = node->width - width;
|
|
|
|
|
int dh = node->height - height;
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
if (dw > dh) {
|
|
|
|
|
// 水平分割
|
2026-02-26 20:08:04 +08:00
|
|
|
node->left =
|
|
|
|
|
std::make_unique<PackNode>(node->x, node->y, width, node->height);
|
|
|
|
|
node->right =
|
|
|
|
|
std::make_unique<PackNode>(node->x + width, node->y, dw, node->height);
|
2026-02-11 22:30:57 +08:00
|
|
|
} else {
|
|
|
|
|
// 垂直分割
|
2026-02-26 20:08:04 +08:00
|
|
|
node->left =
|
|
|
|
|
std::make_unique<PackNode>(node->x, node->y, node->width, height);
|
|
|
|
|
node->right =
|
|
|
|
|
std::make_unique<PackNode>(node->x, node->y + height, node->width, dh);
|
2026-02-11 22:30:57 +08:00
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 递归插入到左子节点
|
|
|
|
|
return insert(node->left.get(), width, height);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
void TextureAtlasPage::writePixels(int x, int y, int w, int h,
|
|
|
|
|
const uint8_t *pixels) {
|
2026-02-11 22:30:57 +08:00
|
|
|
if (texture_ == nullptr || pixels == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 使用 glTexSubImage2D 更新纹理数据
|
|
|
|
|
GLuint texID = static_cast<GLuint>(
|
|
|
|
|
reinterpret_cast<uintptr_t>(texture_->getNativeHandle()));
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texID);
|
2026-02-26 20:08:04 +08:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
|
pixels);
|
2026-02-11 22:30:57 +08:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
const AtlasEntry *TextureAtlasPage::getEntry(const std::string &name) const {
|
2026-02-11 22:30:57 +08:00
|
|
|
auto it = entries_.find(name);
|
|
|
|
|
if (it != entries_.end()) {
|
|
|
|
|
return &it->second;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float TextureAtlasPage::getUsageRatio() const {
|
|
|
|
|
return static_cast<float>(usedArea_) / (width_ * height_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// TextureAtlas 实现
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
TextureAtlas::TextureAtlas()
|
2026-02-26 20:08:04 +08:00
|
|
|
: pageSize_(TextureAtlasPage::DEFAULT_SIZE), sizeThreshold_(256),
|
|
|
|
|
enabled_(true), initialized_(false) {}
|
2026-02-11 22:30:57 +08:00
|
|
|
|
|
|
|
|
TextureAtlas::~TextureAtlas() = default;
|
|
|
|
|
|
|
|
|
|
void TextureAtlas::init(int pageSize) {
|
|
|
|
|
pageSize_ = pageSize;
|
|
|
|
|
initialized_ = true;
|
|
|
|
|
E2D_LOG_INFO("TextureAtlas initialized with page size: {}", pageSize);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
bool TextureAtlas::addTexture(const std::string &name, int width, int height,
|
|
|
|
|
const uint8_t *pixels) {
|
2026-02-11 22:30:57 +08:00
|
|
|
if (!enabled_ || !initialized_) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 检查是否已存在
|
|
|
|
|
if (contains(name)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 检查纹理大小
|
|
|
|
|
if (width > sizeThreshold_ || height > sizeThreshold_) {
|
2026-02-26 20:08:04 +08:00
|
|
|
E2D_LOG_DEBUG("Texture '{}' too large for atlas ({}x{} > {}), skipping",
|
2026-02-11 22:30:57 +08:00
|
|
|
name, width, height, sizeThreshold_);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 尝试添加到现有页面
|
|
|
|
|
Rect uvRect;
|
2026-02-26 20:08:04 +08:00
|
|
|
for (auto &page : pages_) {
|
2026-02-11 22:30:57 +08:00
|
|
|
if (page->tryAddTexture(name, width, height, pixels, uvRect)) {
|
|
|
|
|
entryToPage_[name] = page.get();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
// 创建新页面
|
|
|
|
|
auto newPage = std::make_unique<TextureAtlasPage>(pageSize_, pageSize_);
|
|
|
|
|
if (newPage->tryAddTexture(name, width, height, pixels, uvRect)) {
|
|
|
|
|
entryToPage_[name] = newPage.get();
|
|
|
|
|
pages_.push_back(std::move(newPage));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
E2D_LOG_WARN("Failed to add texture '{}' to atlas", name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
bool TextureAtlas::contains(const std::string &name) const {
|
2026-02-11 22:30:57 +08:00
|
|
|
return entryToPage_.find(name) != entryToPage_.end();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
const Texture *TextureAtlas::getAtlasTexture(const std::string &name) const {
|
2026-02-11 22:30:57 +08:00
|
|
|
auto it = entryToPage_.find(name);
|
|
|
|
|
if (it != entryToPage_.end()) {
|
|
|
|
|
return it->second->getTexture().get();
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
Rect TextureAtlas::getUVRect(const std::string &name) const {
|
2026-02-11 22:30:57 +08:00
|
|
|
auto it = entryToPage_.find(name);
|
|
|
|
|
if (it != entryToPage_.end()) {
|
2026-02-26 20:08:04 +08:00
|
|
|
const AtlasEntry *entry = it->second->getEntry(name);
|
2026-02-11 22:30:57 +08:00
|
|
|
if (entry != nullptr) {
|
|
|
|
|
return entry->uvRect;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Rect(0, 0, 1, 1); // 默认 UV
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
Vec2 TextureAtlas::getOriginalSize(const std::string &name) const {
|
2026-02-11 22:30:57 +08:00
|
|
|
auto it = entryToPage_.find(name);
|
|
|
|
|
if (it != entryToPage_.end()) {
|
2026-02-26 20:08:04 +08:00
|
|
|
const AtlasEntry *entry = it->second->getEntry(name);
|
2026-02-11 22:30:57 +08:00
|
|
|
if (entry != nullptr) {
|
|
|
|
|
return entry->originalSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Vec2(0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float TextureAtlas::getTotalUsageRatio() const {
|
|
|
|
|
if (pages_.empty()) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
2026-02-26 20:08:04 +08:00
|
|
|
|
2026-02-11 22:30:57 +08:00
|
|
|
float total = 0.0f;
|
2026-02-26 20:08:04 +08:00
|
|
|
for (const auto &page : pages_) {
|
2026-02-11 22:30:57 +08:00
|
|
|
total += page->getUsageRatio();
|
|
|
|
|
}
|
|
|
|
|
return total / pages_.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextureAtlas::clear() {
|
|
|
|
|
pages_.clear();
|
|
|
|
|
entryToPage_.clear();
|
|
|
|
|
E2D_LOG_INFO("TextureAtlas cleared");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// TextureAtlasManager 单例实现
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
2026-02-26 20:08:04 +08:00
|
|
|
TextureAtlasManager &TextureAtlasManager::getInstance() {
|
2026-02-11 22:30:57 +08:00
|
|
|
static TextureAtlasManager instance;
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|