rev |
line source |
nuclear@1
|
1 #include "opengl.h"
|
nuclear@1
|
2 #include "texture.h"
|
nuclear@1
|
3
|
nuclear@1
|
4 void bind_texture(const Texture *tex, int texunit)
|
nuclear@1
|
5 {
|
nuclear@1
|
6 static const Texture *curr_tex[8];
|
nuclear@1
|
7
|
nuclear@1
|
8 if(tex == curr_tex[texunit]) {
|
nuclear@1
|
9 return;
|
nuclear@1
|
10 }
|
nuclear@1
|
11
|
nuclear@1
|
12 glActiveTexture(GL_TEXTURE0 + texunit);
|
nuclear@1
|
13 if(tex) {
|
nuclear@1
|
14 glEnable(tex->type);
|
nuclear@1
|
15 glBindTexture(tex->type, tex->tex);
|
nuclear@1
|
16 } else {
|
nuclear@1
|
17 glDisable(tex->type);
|
nuclear@1
|
18 }
|
nuclear@1
|
19 glActiveTexture(GL_TEXTURE0);
|
nuclear@1
|
20
|
nuclear@1
|
21 curr_tex[texunit] = tex;
|
nuclear@1
|
22 }
|
nuclear@1
|
23
|
nuclear@1
|
24
|
nuclear@1
|
25 Texture::Texture()
|
nuclear@1
|
26 {
|
nuclear@1
|
27 type = 0;
|
nuclear@1
|
28
|
nuclear@1
|
29 size[0] = size[1] = size[2] = 0;
|
nuclear@1
|
30
|
nuclear@1
|
31 glGenTextures(1, &tex);
|
nuclear@1
|
32 }
|
nuclear@1
|
33
|
nuclear@1
|
34 Texture::~Texture()
|
nuclear@1
|
35 {
|
nuclear@1
|
36 if(tex) {
|
nuclear@1
|
37 glDeleteTextures(1, &tex);
|
nuclear@1
|
38 }
|
nuclear@1
|
39 }
|
nuclear@1
|
40
|
nuclear@1
|
41 int Texture::get_size(int idx) const
|
nuclear@1
|
42 {
|
nuclear@1
|
43 return idx >= 0 && idx < 3 ? size[idx] : 0;
|
nuclear@1
|
44 }
|
nuclear@1
|
45
|
nuclear@1
|
46 Texture2D::Texture2D()
|
nuclear@1
|
47 {
|
nuclear@1
|
48 type = GL_TEXTURE_2D;
|
nuclear@1
|
49
|
nuclear@1
|
50 glBindTexture(type, tex);
|
nuclear@1
|
51 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@1
|
52 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@1
|
53 }
|
nuclear@1
|
54
|
nuclear@1
|
55 void Texture2D::create(int xsz, int ysz, float *data)
|
nuclear@1
|
56 {
|
nuclear@1
|
57 glBindTexture(type, tex);
|
nuclear@1
|
58 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
|
nuclear@1
|
59
|
nuclear@1
|
60 size[0] = xsz;
|
nuclear@1
|
61 size[1] = ysz;
|
nuclear@1
|
62 }
|
nuclear@1
|
63
|
nuclear@1
|
64 Texture3D::Texture3D()
|
nuclear@1
|
65 {
|
nuclear@1
|
66 type = GL_TEXTURE_3D;
|
nuclear@1
|
67
|
nuclear@1
|
68 glBindTexture(type, tex);
|
nuclear@1
|
69 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@1
|
70 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@1
|
71 }
|
nuclear@1
|
72
|
nuclear@1
|
73 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
|
nuclear@1
|
74 {
|
nuclear@1
|
75 glBindTexture(type, tex);
|
nuclear@1
|
76 glTexImage3D(type, 0, GL_RGBA, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
|
nuclear@1
|
77
|
nuclear@1
|
78 size[0] = xsz;
|
nuclear@1
|
79 size[1] = ysz;
|
nuclear@1
|
80 size[2] = zsz;
|
nuclear@1
|
81 }
|