|
|
@ -5,6 +5,7 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#include <climits> // UINT_MAX |
|
|
|
#include <climits> // UINT_MAX |
|
|
|
|
|
|
|
#include <cstdint> // uint8_t, uint32_t |
|
|
|
#include <memory> // std::shared_ptr |
|
|
|
#include <memory> // std::shared_ptr |
|
|
|
#include <utility> // std::move |
|
|
|
#include <utility> // std::move |
|
|
|
|
|
|
|
|
|
|
@ -21,34 +22,27 @@ namespace Inferno { |
|
|
|
Texture::Texture(const std::string& path) |
|
|
|
Texture::Texture(const std::string& path) |
|
|
|
: m_path(std::move(path)) |
|
|
|
: m_path(std::move(path)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int width; |
|
|
|
unsigned char* data = nullptr; |
|
|
|
int height; |
|
|
|
int width = 0; |
|
|
|
int channels; |
|
|
|
int height = 0; |
|
|
|
|
|
|
|
int channels = 0; |
|
|
|
|
|
|
|
|
|
|
|
// Load image data
|
|
|
|
// Load image data
|
|
|
|
stbi_set_flip_vertically_on_load(1); |
|
|
|
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); |
|
|
|
init(data, width, height, 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); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Clean resources
|
|
|
|
// Clean resources
|
|
|
|
stbi_image_free(data); |
|
|
|
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() |
|
|
|
Texture::~Texture() |
|
|
|
{ |
|
|
|
{ |
|
|
|
glDeleteTextures(1, &m_id); |
|
|
|
glDeleteTextures(1, &m_id); |
|
|
@ -70,6 +64,25 @@ void Texture::unbind() const |
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
|
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) |
|
|
|
void Texture::create(unsigned char* data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_id = UINT_MAX; |
|
|
|
m_id = UINT_MAX; |
|
|
|