vrchess

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