nuclear@2: #include "texture.h" nuclear@2: #include "opengl.h" nuclear@2: nuclear@2: Texture::Texture() nuclear@2: { nuclear@2: tex = 0; nuclear@2: type = GL_TEXTURE_2D; nuclear@2: } nuclear@2: nuclear@2: Texture::~Texture() nuclear@2: { nuclear@2: destroy(); nuclear@2: } nuclear@2: nuclear@2: void Texture::create2d(int xsz, int ysz) nuclear@2: { nuclear@2: destroy(); nuclear@2: nuclear@2: type = GL_TEXTURE_2D; nuclear@2: img.create(xsz, ysz); nuclear@2: nuclear@2: if(!tex) { nuclear@2: glGenTextures(1, &tex); nuclear@2: } nuclear@2: glBindTexture(type, tex); nuclear@2: glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR); nuclear@2: glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@2: glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); nuclear@2: } nuclear@2: nuclear@2: void Texture::destroy() nuclear@2: { nuclear@2: if(tex) { nuclear@2: glDeleteTextures(1, &tex); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: void Texture::set_image(const Image &img) nuclear@2: { nuclear@2: this->img = img; nuclear@2: create2d(img.get_width(), img.get_height()); nuclear@2: nuclear@2: glTexSubImage2D(type, 0, 0, 0, img.get_width(), img.get_height(), nuclear@2: GL_RGBA, GL_UNSIGNED_BYTE, img.get_pixels()); nuclear@2: } nuclear@2: nuclear@2: Image &Texture::get_image() nuclear@2: { nuclear@2: return img; nuclear@2: } nuclear@2: nuclear@2: const Image &Texture::get_image() const nuclear@2: { nuclear@2: return img; nuclear@2: } nuclear@2: nuclear@2: unsigned int Texture::get_texture_id() const nuclear@2: { nuclear@2: return tex; nuclear@2: } nuclear@2: nuclear@2: void Texture::bind(int tunit) const nuclear@2: { nuclear@2: glActiveTextureARB(GL_TEXTURE0_ARB + tunit); nuclear@2: glBindTexture(type, tex); nuclear@2: glActiveTextureARB(GL_TEXTURE0_ARB); nuclear@2: } nuclear@2: nuclear@2: bool Texture::load(const char *fname) nuclear@2: { nuclear@2: Image image; nuclear@2: if(!image.load(fname)) { nuclear@2: return false; nuclear@2: } nuclear@2: set_image(image); nuclear@2: return true; nuclear@2: }