Files
panopainter/engine/texture.cpp
Omar Mohamed Ali Mudhir 13d8e6e563 base setup for osx in place
2017-01-14 18:30:19 +00:00

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);
}