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>
13 void operator>>(const LexicalConverter &c, TextureFilter &tf)
15 if(c.get()=="NEAREST")
17 else if(c.get()=="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;
28 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
32 void operator>>(const LexicalConverter &c, TextureWrap &tw)
36 else if(c.get()=="CLAMP_TO_EDGE")
38 else if(c.get()=="MIRRORED_REPEAT")
41 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
45 Texture::Texture(GLenum t):
47 min_filter(NEAREST_MIPMAP_LINEAR),
57 glGenTextures(1, &id);
62 glDeleteTextures(1, &id);
65 void Texture::update_parameter(int mask) const
70 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_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);
76 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
78 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
80 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
81 if(mask&GENERATE_MIPMAP)
82 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
84 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
86 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
92 void Texture::set_min_filter(TextureFilter f)
95 update_parameter(MIN_FILTER);
98 void Texture::set_mag_filter(TextureFilter f)
101 update_parameter(MAG_FILTER);
104 void Texture::set_filter(TextureFilter f)
107 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
110 void Texture::set_max_anisotropy(float a)
113 throw invalid_argument("Texture::set_max_anisotropy");
115 static Require _req(EXT_texture_filter_anisotropic);
117 update_parameter(MAX_ANISOTROPY);
120 void Texture::set_wrap(TextureWrap w)
127 void Texture::set_wrap_s(TextureWrap w)
130 update_parameter(WRAP_S);
133 void Texture::set_wrap_t(TextureWrap w)
136 update_parameter(WRAP_T);
139 void Texture::set_wrap_r(TextureWrap w)
142 update_parameter(WRAP_R);
145 void Texture::set_generate_mipmap(bool gm)
148 static Require _req(SGIS_generate_mipmap);
150 update_parameter(GENERATE_MIPMAP);
153 void Texture::set_compare_enabled(bool c)
156 update_parameter(COMPARE);
159 void Texture::set_compare_func(Predicate f)
162 update_parameter(COMPARE_FUNC);
165 void Texture::bind() const
167 const Texture *cur = TexUnit::current().get_texture();
170 if(cur && cur->target!=target)
171 glDisable(cur->target);
172 if(!cur || cur->target!=target)
175 glBindTexture(target, id);
176 TexUnit::current().set_texture(this);
180 update_parameter(dirty_params);
186 void Texture::bind_to(unsigned i) const
188 TexUnit::activate(i);
192 const Texture *Texture::current()
194 return TexUnit::current().get_texture();
197 void Texture::unbind()
199 const Texture *cur = TexUnit::current().get_texture();
203 glBindTexture(cur->target, 0);
204 glDisable(cur->target);
205 TexUnit::current().set_texture(0);
208 void Texture::unbind_from(unsigned i)
210 TexUnit::activate(i);
215 Texture::Loader::Loader(Texture &t):
216 DataFile::ObjectLoader<Texture>(t)
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);
229 void Texture::Loader::filter(TextureFilter f)
234 void Texture::Loader::generate_mipmap(bool gm)
236 obj.set_generate_mipmap(gm);
239 void Texture::Loader::mag_filter(TextureFilter f)
241 obj.set_mag_filter(f);
244 void Texture::Loader::max_anisotropy(float a)
246 obj.set_max_anisotropy(a);
249 void Texture::Loader::min_filter(TextureFilter f)
251 obj.set_min_filter(f);
254 void Texture::Loader::wrap(TextureWrap w)
259 void Texture::Loader::wrap_r(TextureWrap w)
264 void Texture::Loader::wrap_s(TextureWrap w)
269 void Texture::Loader::wrap_t(TextureWrap w)