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@3
|
6 static const Texture *cur_tex[8];
|
nuclear@1
|
7
|
nuclear@3
|
8 if(tex == cur_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@3
|
17 glDisable(cur_tex[texunit]->type);
|
nuclear@1
|
18 }
|
nuclear@1
|
19 glActiveTexture(GL_TEXTURE0);
|
nuclear@1
|
20
|
nuclear@3
|
21 cur_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@4
|
41 void Texture::set_filtering(unsigned int min_filter, unsigned int mag_filter)
|
nuclear@4
|
42 {
|
nuclear@4
|
43 if(mag_filter == 0) {
|
nuclear@4
|
44 mag_filter = min_filter;
|
nuclear@4
|
45 }
|
nuclear@4
|
46
|
nuclear@4
|
47 glBindTexture(type, tex);
|
nuclear@4
|
48 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, min_filter);
|
nuclear@4
|
49 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, mag_filter);
|
nuclear@4
|
50 }
|
nuclear@4
|
51
|
nuclear@4
|
52 void Texture::set_wrapping(unsigned int wrap)
|
nuclear@4
|
53 {
|
nuclear@4
|
54 glBindTexture(type, tex);
|
nuclear@4
|
55 glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap);
|
nuclear@4
|
56 glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap);
|
nuclear@4
|
57 glTexParameteri(type, GL_TEXTURE_WRAP_R, wrap);
|
nuclear@4
|
58 }
|
nuclear@4
|
59
|
nuclear@1
|
60 int Texture::get_size(int idx) const
|
nuclear@1
|
61 {
|
nuclear@1
|
62 return idx >= 0 && idx < 3 ? size[idx] : 0;
|
nuclear@1
|
63 }
|
nuclear@1
|
64
|
nuclear@4
|
65 Texture1D::Texture1D()
|
nuclear@4
|
66 {
|
nuclear@4
|
67 type = GL_TEXTURE_1D;
|
nuclear@4
|
68
|
nuclear@4
|
69 glBindTexture(type, tex);
|
nuclear@4
|
70 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@4
|
71 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@4
|
72 }
|
nuclear@4
|
73
|
nuclear@4
|
74 void Texture1D::create(int sz, float *data)
|
nuclear@4
|
75 {
|
nuclear@4
|
76 glBindTexture(type, tex);
|
nuclear@4
|
77 glTexImage1D(type, 0, GL_RGBA, sz, 0, GL_RGBA, GL_FLOAT, data);
|
nuclear@4
|
78
|
nuclear@4
|
79 size[0] = sz;
|
nuclear@4
|
80 }
|
nuclear@4
|
81
|
nuclear@4
|
82 void Texture1D::update(float *data)
|
nuclear@4
|
83 {
|
nuclear@4
|
84 glBindTexture(type, tex);
|
nuclear@4
|
85 glTexSubImage1D(type, 0, 0, size[0], GL_RGBA, GL_FLOAT, data);
|
nuclear@4
|
86 }
|
nuclear@4
|
87
|
nuclear@1
|
88 Texture2D::Texture2D()
|
nuclear@1
|
89 {
|
nuclear@1
|
90 type = GL_TEXTURE_2D;
|
nuclear@1
|
91
|
nuclear@1
|
92 glBindTexture(type, tex);
|
nuclear@1
|
93 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@1
|
94 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@1
|
95 }
|
nuclear@1
|
96
|
nuclear@1
|
97 void Texture2D::create(int xsz, int ysz, float *data)
|
nuclear@1
|
98 {
|
nuclear@1
|
99 glBindTexture(type, tex);
|
nuclear@1
|
100 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
|
nuclear@1
|
101
|
nuclear@1
|
102 size[0] = xsz;
|
nuclear@1
|
103 size[1] = ysz;
|
nuclear@1
|
104 }
|
nuclear@1
|
105
|
nuclear@3
|
106 void Texture2D::update(float *data)
|
nuclear@3
|
107 {
|
nuclear@3
|
108 glBindTexture(type, tex);
|
nuclear@3
|
109 glTexSubImage2D(type, 0, 0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, data);
|
nuclear@3
|
110 }
|
nuclear@3
|
111
|
nuclear@1
|
112 Texture3D::Texture3D()
|
nuclear@1
|
113 {
|
nuclear@1
|
114 type = GL_TEXTURE_3D;
|
nuclear@1
|
115
|
nuclear@1
|
116 glBindTexture(type, tex);
|
nuclear@1
|
117 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@1
|
118 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@3
|
119 glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
nuclear@3
|
120 glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
nuclear@3
|
121 glTexParameteri(type, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
nuclear@1
|
122 }
|
nuclear@1
|
123
|
nuclear@1
|
124 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
|
nuclear@1
|
125 {
|
nuclear@1
|
126 glBindTexture(type, tex);
|
nuclear@1
|
127 glTexImage3D(type, 0, GL_RGBA, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
|
nuclear@1
|
128
|
nuclear@1
|
129 size[0] = xsz;
|
nuclear@1
|
130 size[1] = ysz;
|
nuclear@1
|
131 size[2] = zsz;
|
nuclear@1
|
132 }
|
nuclear@3
|
133
|
nuclear@3
|
134 void Texture3D::update(float *data)
|
nuclear@3
|
135 {
|
nuclear@3
|
136 glBindTexture(type, tex);
|
nuclear@3
|
137 glTexSubImage3D(type, 0, 0, 0, 0, size[0], size[1], size[2], GL_RGBA, GL_FLOAT, data);
|
nuclear@3
|
138 }
|