]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Add a shortcut for specifying both texture filters
[libs/gl.git] / source / texture.cpp
1 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
2 #include <msp/gl/extensions/sgis_generate_mipmap.h>
3 #include <msp/strings/format.h>
4 #include "error.h"
5 #include "texture.h"
6 #include "texunit.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 void operator>>(const LexicalConverter &c, TextureFilter &tf)
14 {
15         if(c.get()=="NEAREST")
16                 tf = NEAREST;
17         else if(c.get()=="LINEAR")
18                 tf = LINEAR;
19         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
20                 tf = NEAREST_MIPMAP_NEAREST;
21         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
22                 tf = NEAREST_MIPMAP_LINEAR;
23         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
24                 tf = LINEAR_MIPMAP_NEAREST;
25         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
26                 tf = LINEAR_MIPMAP_LINEAR;
27         else
28                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
29 }
30
31
32 void operator>>(const LexicalConverter &c, TextureWrap &tw)
33 {
34         if(c.get()=="REPEAT")
35                 tw = REPEAT;
36         else if(c.get()=="CLAMP_TO_EDGE")
37                 tw = CLAMP_TO_EDGE;
38         else if(c.get()=="MIRRORED_REPEAT")
39                 tw = MIRRORED_REPEAT;
40         else
41                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
42 }
43
44
45 Texture::Texture(GLenum t):
46         target(t),
47         min_filter(NEAREST_MIPMAP_LINEAR),
48         mag_filter(LINEAR),
49         wrap_s(REPEAT),
50         wrap_t(REPEAT),
51         wrap_r(REPEAT),
52         gen_mipmap(false),
53         compare(false),
54         cmp_func(LEQUAL),
55         dirty_params(0)
56 {
57         glGenTextures(1, &id);
58 }
59
60 Texture::~Texture()
61 {
62         glDeleteTextures(1, &id);
63 }
64
65 void Texture::update_parameter(int mask) const
66 {
67         if(current()==this)
68         {
69                 if(mask&MIN_FILTER)
70                         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
71                 if(mask&MAG_FILTER)
72                         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
73                 if(mask&MAX_ANISOTROPY)
74                         glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
75                 if(mask&WRAP_S)
76                         glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
77                 if(mask&WRAP_T)
78                         glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
79                 if(mask&WRAP_R)
80                         glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
81                 if(mask&GENERATE_MIPMAP)
82                         glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
83                 if(mask&COMPARE)
84                         glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
85                 if(mask&COMPARE_FUNC)
86                         glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
87         }
88         else
89                 dirty_params |= mask;
90 }
91
92 void Texture::set_min_filter(TextureFilter f)
93 {
94         min_filter = f;
95         update_parameter(MIN_FILTER);
96 }
97
98 void Texture::set_mag_filter(TextureFilter f)
99 {
100         mag_filter = f;
101         update_parameter(MAG_FILTER);
102 }
103
104 void Texture::set_filter(TextureFilter f)
105 {
106         set_min_filter(f);
107         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
108 }
109
110 void Texture::set_max_anisotropy(float a)
111 {
112         if(a<1.0f)
113                 throw invalid_argument("Texture::set_max_anisotropy");
114         else if(a>1.0f)
115                 static Require _req(EXT_texture_filter_anisotropic);
116         max_anisotropy = a;
117         update_parameter(MAX_ANISOTROPY);
118 }
119
120 void Texture::set_wrap(TextureWrap w)
121 {
122         set_wrap_s(w);
123         set_wrap_t(w);
124         set_wrap_r(w);
125 }
126
127 void Texture::set_wrap_s(TextureWrap w)
128 {
129         wrap_s = w;
130         update_parameter(WRAP_S);
131 }
132
133 void Texture::set_wrap_t(TextureWrap w)
134 {
135         wrap_t = w;
136         update_parameter(WRAP_T);
137 }
138
139 void Texture::set_wrap_r(TextureWrap w)
140 {
141         wrap_r = w;
142         update_parameter(WRAP_R);
143 }
144
145 void Texture::set_generate_mipmap(bool gm)
146 {
147         if(gm)
148                 static Require _req(SGIS_generate_mipmap);
149         gen_mipmap = gm;
150         update_parameter(GENERATE_MIPMAP);
151 }
152
153 void Texture::set_compare_enabled(bool c)
154 {
155         compare = c;
156         update_parameter(COMPARE);
157 }
158
159 void Texture::set_compare_func(Predicate f)
160 {
161         cmp_func = f;
162         update_parameter(COMPARE_FUNC);
163 }
164
165 void Texture::bind() const
166 {
167         const Texture *cur = TexUnit::current().get_texture();
168         if(cur!=this)
169         {
170                 if(cur && cur->target!=target)
171                         glDisable(cur->target);
172                 if(!cur || cur->target!=target)
173                         glEnable(target);
174
175                 glBindTexture(target, id);
176                 TexUnit::current().set_texture(this);
177
178                 if(dirty_params)
179                 {
180                         update_parameter(dirty_params);
181                         dirty_params = 0;
182                 }
183         }
184 }
185
186 void Texture::bind_to(unsigned i) const
187 {
188         TexUnit::activate(i);
189         bind();
190 }
191
192 const Texture *Texture::current()
193 {
194         return TexUnit::current().get_texture();
195 }
196
197 void Texture::unbind()
198 {
199         const Texture *cur = TexUnit::current().get_texture();
200         if(!cur)
201                 return;
202
203         glBindTexture(cur->target, 0);
204         glDisable(cur->target);
205         TexUnit::current().set_texture(0);
206 }
207
208 void Texture::unbind_from(unsigned i)
209 {
210         TexUnit::activate(i);
211         unbind();
212 }
213
214
215 Texture::Loader::Loader(Texture &t):
216         DataFile::ObjectLoader<Texture>(t)
217 {
218         add("filter", &Loader::filter);
219         add("max_anisotropy", &Loader::max_anisotropy);
220         add("generate_mipmap", &Loader::generate_mipmap);
221         add("mag_filter", &Loader::mag_filter);
222         add("min_filter", &Loader::min_filter);
223         add("wrap",       &Loader::wrap);
224         add("wrap_r",     &Loader::wrap_r);
225         add("wrap_s",     &Loader::wrap_s);
226         add("wrap_t",     &Loader::wrap_t);
227 }
228
229 void Texture::Loader::filter(TextureFilter f)
230 {
231         obj.set_filter(f);
232 }
233
234 void Texture::Loader::generate_mipmap(bool gm)
235 {
236         obj.set_generate_mipmap(gm);
237 }
238
239 void Texture::Loader::mag_filter(TextureFilter f)
240 {
241         obj.set_mag_filter(f);
242 }
243
244 void Texture::Loader::max_anisotropy(float a)
245 {
246         obj.set_max_anisotropy(a);
247 }
248
249 void Texture::Loader::min_filter(TextureFilter f)
250 {
251         obj.set_min_filter(f);
252 }
253
254 void Texture::Loader::wrap(TextureWrap w)
255 {
256         obj.set_wrap(w);
257 }
258
259 void Texture::Loader::wrap_r(TextureWrap w)
260 {
261         obj.set_wrap_r(w);
262 }
263
264 void Texture::Loader::wrap_s(TextureWrap w)
265 {
266         obj.set_wrap_s(w);
267 }
268
269 void Texture::Loader::wrap_t(TextureWrap w)
270 {
271         obj.set_wrap_t(w);
272 }
273
274 } // namespace GL
275 } // namespace Msp