This commit is contained in:
ZY4N
2024-12-22 16:58:40 +01:00
parent 2704814de2
commit db8db8f9d7
161 changed files with 17102 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
#ifndef INCLUDE_TEXTURE_DATA_IMPLEMENTATION
# error Never include this file directly include 'texture_data.hpp'
#endif
namespace zgl
{
inline texture_data::texture_data(const GLuint texture_id)
: m_handle{ texture_id } {}
inline texture_data::texture_data(texture_data&& other) noexcept
: m_handle{ other.m_handle }
{
other.m_handle.texture_id = 0;
}
inline texture_data& texture_data::operator=(texture_data&& other) noexcept
{
if (&other != this)
{
this->~texture_data();
m_handle = other.m_handle;
other.m_handle.texture_id = 0;
}
return *this;
}
template<typename T>
std::error_code texture_data::build_from(
std::span<const T> buffer,
const GLenum format,
const GLenum type,
const GLsizei width,
const GLsizei height,
texture_data& data
) {
GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0,
GL_RGBA8,
width,
height,
0,
format, type,
buffer.data()
);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
data = texture_data(texture_id);
return {};
}
inline texture_data::~texture_data()
{
if (m_handle.texture_id)
{
glDeleteTextures(1, &m_handle.texture_id);
}
}
inline texture_handle texture_data::handle() const
{
return { m_handle.texture_id };
}
}