Refactor...

This commit is contained in:
shweet committed 2025-10-21 20:23:27 -04:00
1 parent 7f07eaa128
commit 5b0f9a39c4
104 files changed
+17010 -13171

No files matched your search

+99 -87
View File
@@ -1,116 +1,128 @@
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#include "texture.h"
#include <lunasvg.h>
#include <memory>
#include <utility>
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
#define STBI_ONLY_PNG
#define STBI_NO_FAILURE_STRINGS
#define STBI_NO_HDR
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
static void _texture_gl_set(Texture* self, const uint8_t* data) {
if (self->id == GL_ID_NONE)
glGenTextures(1, &self->id);
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
glBindTexture(GL_TEXTURE_2D, self->id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self->size.x, self->size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
}
using namespace glm;
std::vector<uint8_t> texture_download(const Texture* self) {
std::vector<uint8_t> pixels(self->size.x * self->size.y * TEXTURE_CHANNELS);
glBindTexture(GL_TEXTURE_2D, self->id);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
return pixels;
}
bool texture_from_path_init(Texture* self, const std::string& path) {
uint8_t* data = stbi_load(path.c_str(), &self->size.x, &self->size.y, nullptr, TEXTURE_CHANNELS);
if (!data) {
log_error(std::format(TEXTURE_INIT_ERROR, path));
return false;
namespace anm2ed::texture
{
bool Texture::is_valid()
{
return id != 0;
}
self->isInvalid = false;
void Texture::download(std::vector<uint8_t>& pixels)
{
pixels.resize(size.x * size.y * CHANNELS);
glBindTexture(GL_TEXTURE_2D, id);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
glBindTexture(GL_TEXTURE_2D, 0);
}
log_info(std::format(TEXTURE_INIT_INFO, path));
void Texture::init(const uint8_t* data, bool isDownload)
{
glGenTextures(1, &id);
_texture_gl_set(self, data);
glBindTexture(GL_TEXTURE_2D, id);
return true;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
bool texture_from_rgba_init(Texture* self, ivec2 size, const uint8_t* data) {
*self = Texture{};
self->size = size;
self->isInvalid = false;
glBindTexture(GL_TEXTURE_2D, 0);
_texture_gl_set(self, data);
if (isDownload) download(pixels);
}
return true;
}
Texture::Texture() = default;
bool texture_from_rgba_write(const std::string& path, const uint8_t* data, ivec2 size) {
bool isSuccess = stbi_write_png(path.c_str(), size.x, size.y, TEXTURE_CHANNELS, data, size.x * TEXTURE_CHANNELS);
if (!isSuccess)
log_error(std::format(TEXTURE_SAVE_ERROR, path));
else
log_info(std::format(TEXTURE_SAVE_INFO, path));
Texture::~Texture()
{
if (is_valid()) glDeleteTextures(1, &id);
}
return isSuccess;
}
Texture::Texture(Texture&& other)
{
*this = std::move(other);
}
bool texture_from_memory_init(Texture* self, ivec2 size, const uint8_t* data, size_t length) {
*self = Texture{};
self->size = size;
Texture& Texture::operator=(Texture&& other)
{
if (this != &other)
{
if (is_valid()) glDeleteTextures(1, &id);
id = std::exchange(other.id, 0);
size = std::exchange(other.size, {});
filter = other.filter;
channels = other.channels;
pixels = std::move(other.pixels);
}
return *this;
}
u8* textureData = stbi_load_from_memory(data, length, &self->size.x, &self->size.y, nullptr, TEXTURE_CHANNELS);
Texture::Texture(const char* svgData, size_t svgDataLength, ivec2 svgSize)
{
if (!svgData) return;
if (!textureData)
return false;
const std::unique_ptr<lunasvg::Document> document = lunasvg::Document::loadFromData(svgData, svgDataLength);
if (!document) return;
self->isInvalid = false;
const lunasvg::Bitmap bitmap = document->renderToBitmap(svgSize.x, svgSize.y, 0);
if (bitmap.width() == 0 || bitmap.height() == 0) return;
_texture_gl_set(self, textureData);
size = svgSize;
filter = GL_LINEAR;
init(bitmap.data());
}
return true;
}
Texture::Texture(const std::string& pngPath, bool isDownload)
{
if (const uint8* data = stbi_load(pngPath.c_str(), &size.x, &size.y, nullptr, CHANNELS); data)
{
init(data, isDownload);
stbi_image_free((void*)data);
}
}
bool texture_from_gl_write(Texture* self, const std::string& path) { return texture_from_rgba_write(path, texture_download(self).data(), self->size); }
bool Texture::write_png(const std::string& path)
{
std::vector<uint8_t> pixels;
download(pixels);
const bool isSuccess = stbi_write_png(path.c_str(), size.x, size.y, CHANNELS, pixels.data(), size.x * CHANNELS);
return isSuccess;
}
void texture_free(Texture* self) {
if (self->isInvalid)
return;
void Texture::bind(GLuint unit)
{
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, id);
}
glDeleteTextures(1, &self->id);
*self = Texture{};
}
bool texture_pixel_set(Texture* self, ivec2 position, vec4 color) {
if (position.x < 0 || position.y < 0 || position.x >= self->size.x || position.y >= self->size.y)
return false;
uint8_t rgba8[4] = {FLOAT_TO_UINT8(color.r), FLOAT_TO_UINT8(color.g), FLOAT_TO_UINT8(color.b), FLOAT_TO_UINT8(color.a)};
glBindTexture(GL_TEXTURE_2D, self->id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, position.x, position.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba8);
return true;
}
void Texture::unbind(GLuint unit)
{
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, 0);
}
}