37 lines
1.1 KiB
C++
37 lines
1.1 KiB
C++
#include "pch.h"
|
|
#include "texture.hpp"
|
|
#include "image.hpp"
|
|
|
|
bool Texture2D::create(int width, int height, GLint format, const uint8_t* data, GLint filter, GLint wrap)
|
|
{
|
|
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);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
|
|
unbind();
|
|
return true;
|
|
}
|
|
bool Texture2D::create(const Image& img)
|
|
{
|
|
static GLint formats[] = { GL_R, 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);
|
|
}
|