From 55af11d831080aed0738a15c5edad7ea19ba6657 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 28 Sep 2022 14:15:52 +0200 Subject: [PATCH] Engine: Add constructor for raw data texture --- src/inferno/render/texture.cpp | 51 +++++++++++++++++++++------------- src/inferno/render/texture.h | 6 ++-- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/inferno/render/texture.cpp b/src/inferno/render/texture.cpp index e3125bd..d2aa0d2 100644 --- a/src/inferno/render/texture.cpp +++ b/src/inferno/render/texture.cpp @@ -5,6 +5,7 @@ */ #include // UINT_MAX +#include // uint8_t, uint32_t #include // std::shared_ptr #include // std::move @@ -21,34 +22,27 @@ namespace Inferno { Texture::Texture(const std::string& path) : m_path(std::move(path)) { - int width; - int height; - int channels; + unsigned char* data = nullptr; + int width = 0; + int height = 0; + int channels = 0; // Load image data stbi_set_flip_vertically_on_load(1); - unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, STBI_default); + data = stbi_load(path.c_str(), &width, &height, &channels, STBI_default); + VERIFY(data, "failed to load image: '{}'", path); - VERIFY(data, "Failed to load image: '{}'", path); - - m_width = width; - m_height = height; - - if (channels == 4) { - m_internalFormat = GL_RGBA8; - m_dataFormat = GL_RGBA; - } - else if (channels == 3) { - m_internalFormat = GL_RGB8; - m_dataFormat = GL_RGB; - } - - create(data); + init(data, width, height, channels); // Clean resources stbi_image_free(data); } +Texture::Texture(unsigned char* data, uint32_t width, uint32_t height, uint8_t channels) +{ + init(data, width, height, channels); +} + Texture::~Texture() { glDeleteTextures(1, &m_id); @@ -70,6 +64,25 @@ void Texture::unbind() const glBindTexture(GL_TEXTURE_2D, 0); } +// ----------------------------------------- + +void Texture::init(unsigned char* data, uint32_t width, uint32_t height, uint8_t channels) +{ + m_width = width; + m_height = height; + + if (channels == 4) { + m_internalFormat = GL_RGBA8; + m_dataFormat = GL_RGBA; + } + else if (channels == 3) { + m_internalFormat = GL_RGB8; + m_dataFormat = GL_RGB; + } + + create(data); +} + void Texture::create(unsigned char* data) { m_id = UINT_MAX; diff --git a/src/inferno/render/texture.h b/src/inferno/render/texture.h index ca57190..a75f379 100644 --- a/src/inferno/render/texture.h +++ b/src/inferno/render/texture.h @@ -6,7 +6,7 @@ #pragma once -#include // uint32_t +#include // uint8_t, uint32_t #include // std::shared_ptr #include // std::string #include // std::unordered_map @@ -18,6 +18,7 @@ namespace Inferno { class Texture { public: Texture(const std::string& path); + Texture(unsigned char* data, uint32_t width, uint32_t height, uint8_t channels = 3); virtual ~Texture(); void bind(uint32_t unit = 0) const; @@ -31,6 +32,7 @@ public: inline uint32_t dataFormat() const { return m_dataFormat; } protected: + void init(unsigned char* data, uint32_t width, uint32_t height, uint8_t channels); void create(unsigned char* data); private: @@ -47,7 +49,7 @@ private: class TextureManager final : public ruc::Singleton { public: TextureManager(s); - virtual ~TextureManager(); + ~TextureManager(); void add(const std::string& path, std::shared_ptr texture); std::shared_ptr load(const std::string& path);