vrchess

annotate 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
rev   line source
nuclear@0 1 #include "texture.h"
nuclear@0 2 #include "opengl.h"
nuclear@0 3
nuclear@0 4 Texture::Texture()
nuclear@0 5 {
nuclear@0 6 tex = 0;
nuclear@0 7 type = GL_TEXTURE_2D;
nuclear@0 8 }
nuclear@0 9
nuclear@0 10 Texture::~Texture()
nuclear@0 11 {
nuclear@0 12 destroy();
nuclear@0 13 }
nuclear@0 14
nuclear@0 15 void Texture::create2d(int xsz, int ysz)
nuclear@0 16 {
nuclear@0 17 destroy();
nuclear@0 18
nuclear@0 19 type = GL_TEXTURE_2D;
nuclear@0 20 img.create(xsz, ysz);
nuclear@0 21
nuclear@0 22 if(!tex) {
nuclear@0 23 glGenTextures(1, &tex);
nuclear@0 24 }
nuclear@0 25 glBindTexture(type, tex);
nuclear@0 26 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 27 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@0 28 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
nuclear@0 29 }
nuclear@0 30
nuclear@0 31 void Texture::destroy()
nuclear@0 32 {
nuclear@0 33 if(tex) {
nuclear@0 34 glDeleteTextures(1, &tex);
nuclear@0 35 }
nuclear@0 36 }
nuclear@0 37
nuclear@0 38 void Texture::set_image(const Image &img)
nuclear@0 39 {
nuclear@0 40 this->img = img;
nuclear@0 41 create2d(img.get_width(), img.get_height());
nuclear@0 42
nuclear@0 43 glTexSubImage2D(type, 0, 0, 0, img.get_width(), img.get_height(),
nuclear@0 44 GL_RGBA, GL_UNSIGNED_BYTE, img.get_pixels());
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 Image &Texture::get_image()
nuclear@0 48 {
nuclear@0 49 return img;
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 const Image &Texture::get_image() const
nuclear@0 53 {
nuclear@0 54 return img;
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 unsigned int Texture::get_texture_id() const
nuclear@0 58 {
nuclear@0 59 return tex;
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 void Texture::bind(int tunit) const
nuclear@0 63 {
nuclear@0 64 glActiveTextureARB(GL_TEXTURE0_ARB + tunit);
nuclear@0 65 glBindTexture(type, tex);
nuclear@0 66 glActiveTextureARB(GL_TEXTURE0_ARB);
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 bool Texture::load(const char *fname)
nuclear@0 70 {
nuclear@0 71 Image image;
nuclear@0 72 if(!image.load(fname)) {
nuclear@0 73 return false;
nuclear@0 74 }
nuclear@0 75 set_image(image);
nuclear@0 76 return true;
nuclear@0 77 }