vrchess
diff src/texture.cc @ 0:b326d53321f7
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 25 Apr 2014 05:20:53 +0300 |
parents | |
children | 879194e4b1f0 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/texture.cc Fri Apr 25 05:20:53 2014 +0300 1.3 @@ -0,0 +1,77 @@ 1.4 +#include "texture.h" 1.5 +#include "opengl.h" 1.6 + 1.7 +Texture::Texture() 1.8 +{ 1.9 + tex = 0; 1.10 + type = GL_TEXTURE_2D; 1.11 +} 1.12 + 1.13 +Texture::~Texture() 1.14 +{ 1.15 + destroy(); 1.16 +} 1.17 + 1.18 +void Texture::create2d(int xsz, int ysz) 1.19 +{ 1.20 + destroy(); 1.21 + 1.22 + type = GL_TEXTURE_2D; 1.23 + img.create(xsz, ysz); 1.24 + 1.25 + if(!tex) { 1.26 + glGenTextures(1, &tex); 1.27 + } 1.28 + glBindTexture(type, tex); 1.29 + glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.30 + glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.31 + glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); 1.32 +} 1.33 + 1.34 +void Texture::destroy() 1.35 +{ 1.36 + if(tex) { 1.37 + glDeleteTextures(1, &tex); 1.38 + } 1.39 +} 1.40 + 1.41 +void Texture::set_image(const Image &img) 1.42 +{ 1.43 + this->img = img; 1.44 + create2d(img.get_width(), img.get_height()); 1.45 + 1.46 + glTexSubImage2D(type, 0, 0, 0, img.get_width(), img.get_height(), 1.47 + GL_RGBA, GL_UNSIGNED_BYTE, img.get_pixels()); 1.48 +} 1.49 + 1.50 +Image &Texture::get_image() 1.51 +{ 1.52 + return img; 1.53 +} 1.54 + 1.55 +const Image &Texture::get_image() const 1.56 +{ 1.57 + return img; 1.58 +} 1.59 + 1.60 +unsigned int Texture::get_texture_id() const 1.61 +{ 1.62 + return tex; 1.63 +} 1.64 + 1.65 +void Texture::bind(int tunit) const 1.66 +{ 1.67 + glActiveTextureARB(GL_TEXTURE0_ARB + tunit); 1.68 + glBindTexture(type, tex); 1.69 + glActiveTextureARB(GL_TEXTURE0_ARB); 1.70 +} 1.71 + 1.72 +bool Texture::load(const char *fname) 1.73 +{ 1.74 + Image image; 1.75 + if(!image.load(fname)) { 1.76 + return false; 1.77 + } 1.78 + set_image(image); 1.79 + return true; 1.80 +} 1.81 \ No newline at end of file