Files
panopainter/engine/texture.cpp

58 lines
1.5 KiB
C++

#include "pch.h"
#include "texture.hpp"
#include "image.hpp"
bool Texture2D::create(int width, int height, GLint format, const uint8_t* data)
{
m_width = width;
m_height = height;
m_format = format;
glGenTextures(1, &m_tex);
bind();
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
unbind();
return true;
}
bool Texture2D::create(const Image& img)
{
static GLint formats[] = { GL_RED, GL_RG, GL_RGB, GL_RGBA };
return create(img.width, img.height, formats[img.comp - 1], img.data());
}
bool Texture2D::load(std::string filename)
{
Image img;
if (!img.load(filename))
return false;
return create(img);
}
void Texture2D::update(const uint8_t* data)
{
bind();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, m_format, GL_UNSIGNED_BYTE, data);
}
bool Sampler::create(GLint filter /*= GL_LINEAR*/, GLint wrap /*= GL_CLAMP_TO_EDGE*/)
{
glGenSamplers(1, &id);
if (id == 0)
return false;
set(filter, wrap);
return true;
}
void Sampler::set(GLint filter /*= GL_LINEAR*/, GLint wrap /*= GL_CLAMP_TO_EDGE*/)
{
glSamplerParameteri(id, GL_TEXTURE_WRAP_S, wrap);
glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap);
glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, filter);
glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, filter);
}
void Sampler::bind(int unit)
{
current_unit = unit;
glBindSampler(unit, id);
}
void Sampler::unbind()
{
glBindSampler(current_unit, 0);
}