#include #include "texture.h" #include "gl.h" Texture::Texture() { glGenTextures(1, &texture); } Texture::Texture(const Image& image) { glGenTextures(1, &texture); load(image); } Texture::Texture(const std::string& filename) { glGenTextures(1, &texture); load(Image(filename)); } void Texture::load(const Image& image) { build(image.d(), image.f(), image.w(), image.h()); } void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, texture); } unsigned int Texture::tex() const { return texture; } unsigned int Texture::w() const { return width; } unsigned int Texture::h() const { return height; } void Texture::build(const void* data, unsigned int format, unsigned int w, unsigned int h) { if(!data) { throw(std::runtime_error("No texture data")); } width = w; height = h; glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, format, GL_UNSIGNED_BYTE, data); }