vrchess

annotate src/texture.cc @ 7:bd8202d6d28d

some progress...
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 16:55:16 +0300
parents 3c36bc28c6c2
children
rev   line source
nuclear@2 1 #include "texture.h"
nuclear@2 2 #include "opengl.h"
nuclear@2 3
nuclear@2 4 Texture::Texture()
nuclear@2 5 {
nuclear@2 6 tex = 0;
nuclear@2 7 type = GL_TEXTURE_2D;
nuclear@2 8 }
nuclear@2 9
nuclear@2 10 Texture::~Texture()
nuclear@2 11 {
nuclear@2 12 destroy();
nuclear@2 13 }
nuclear@2 14
nuclear@7 15 int Texture::get_width() const
nuclear@7 16 {
nuclear@7 17 return img.get_width();
nuclear@7 18 }
nuclear@7 19
nuclear@7 20 int Texture::get_height() const
nuclear@7 21 {
nuclear@7 22 return img.get_height();
nuclear@7 23 }
nuclear@7 24
nuclear@2 25 void Texture::create2d(int xsz, int ysz)
nuclear@2 26 {
nuclear@2 27 destroy();
nuclear@2 28
nuclear@2 29 type = GL_TEXTURE_2D;
nuclear@2 30 img.create(xsz, ysz);
nuclear@2 31
nuclear@2 32 if(!tex) {
nuclear@2 33 glGenTextures(1, &tex);
nuclear@2 34 }
nuclear@2 35 glBindTexture(type, tex);
nuclear@2 36 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@2 37 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@2 38 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
nuclear@2 39 }
nuclear@2 40
nuclear@2 41 void Texture::destroy()
nuclear@2 42 {
nuclear@2 43 if(tex) {
nuclear@2 44 glDeleteTextures(1, &tex);
nuclear@2 45 }
nuclear@2 46 }
nuclear@2 47
nuclear@2 48 void Texture::set_image(const Image &img)
nuclear@2 49 {
nuclear@2 50 this->img = img;
nuclear@2 51 create2d(img.get_width(), img.get_height());
nuclear@2 52
nuclear@2 53 glTexSubImage2D(type, 0, 0, 0, img.get_width(), img.get_height(),
nuclear@2 54 GL_RGBA, GL_UNSIGNED_BYTE, img.get_pixels());
nuclear@2 55 }
nuclear@2 56
nuclear@2 57 Image &Texture::get_image()
nuclear@2 58 {
nuclear@2 59 return img;
nuclear@2 60 }
nuclear@2 61
nuclear@2 62 const Image &Texture::get_image() const
nuclear@2 63 {
nuclear@2 64 return img;
nuclear@2 65 }
nuclear@2 66
nuclear@2 67 unsigned int Texture::get_texture_id() const
nuclear@2 68 {
nuclear@2 69 return tex;
nuclear@2 70 }
nuclear@2 71
nuclear@2 72 void Texture::bind(int tunit) const
nuclear@2 73 {
nuclear@2 74 glActiveTextureARB(GL_TEXTURE0_ARB + tunit);
nuclear@2 75 glBindTexture(type, tex);
nuclear@2 76 glActiveTextureARB(GL_TEXTURE0_ARB);
nuclear@2 77 }
nuclear@2 78
nuclear@2 79 bool Texture::load(const char *fname)
nuclear@2 80 {
nuclear@2 81 Image image;
nuclear@2 82 if(!image.load(fname)) {
nuclear@2 83 return false;
nuclear@2 84 }
nuclear@2 85 set_image(image);
nuclear@2 86 return true;
nuclear@2 87 }
nuclear@6 88
nuclear@6 89 unsigned int next_pow2(unsigned int x)
nuclear@6 90 {
nuclear@6 91 x -= 1;
nuclear@6 92 x |= x >> 1;
nuclear@6 93 x |= x >> 2;
nuclear@6 94 x |= x >> 4;
nuclear@6 95 x |= x >> 8;
nuclear@6 96 x |= x >> 16;
nuclear@6 97 return x + 1;
nuclear@6 98 }