#include <opengl-playground/opengltexture.hpp>

OpenGlTexture::OpenGlTexture()
{
	glGenTextures(1, &this->texture);
	glBindTexture(GL_TEXTURE_2D, this->texture);
	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);
}

OpenGlTexture::~OpenGlTexture()
{
	glDeleteTextures(1, &this->texture);
}

void OpenGlTexture::setRGBDataFromBytes(unsigned int width, unsigned int height, char *buffer)
{
	this->use();
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
	glGenerateMipmap(GL_TEXTURE_2D);
}

void OpenGlTexture::use(unsigned int slot)
{
	glActiveTexture(GL_TEXTURE0 + slot);
	this->use();
}

void OpenGlTexture::use()
{
	glBindTexture(GL_TEXTURE_2D, this->texture);
}
