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