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>
14 void operator>>(const LexicalConverter &c, TextureFilter &tf)
16 if(c.get()=="NEAREST")
18 else if(c.get()=="LINEAR")
20 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
21 tf = NEAREST_MIPMAP_NEAREST;
22 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
23 tf = NEAREST_MIPMAP_LINEAR;
24 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
25 tf = LINEAR_MIPMAP_NEAREST;
26 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
27 tf = LINEAR_MIPMAP_LINEAR;
29 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
33 void operator>>(const LexicalConverter &c, TextureWrap &tw)
37 else if(c.get()=="CLAMP_TO_EDGE")
39 else if(c.get()=="MIRRORED_REPEAT")
42 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
46 Texture::Texture(GLenum t):
48 min_filter(NEAREST_MIPMAP_LINEAR),
58 glGenTextures(1, &id);
63 glDeleteTextures(1, &id);
66 void Texture::update_parameter(int mask) const
68 if(TexUnit::current().get_texture()!=this)
70 TexUnit *unit = TexUnit::find_unit(this);
81 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
83 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
84 if(mask&MAX_ANISOTROPY)
85 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
87 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
89 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
91 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
92 if(mask&GENERATE_MIPMAP)
93 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
95 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
97 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
100 void Texture::set_min_filter(TextureFilter f)
103 update_parameter(MIN_FILTER);
106 void Texture::set_mag_filter(TextureFilter f)
109 update_parameter(MAG_FILTER);
112 void Texture::set_filter(TextureFilter f)
115 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
118 void Texture::set_max_anisotropy(float a)
121 throw invalid_argument("Texture::set_max_anisotropy");
123 static Require _req(EXT_texture_filter_anisotropic);
125 update_parameter(MAX_ANISOTROPY);
128 void Texture::set_wrap(TextureWrap w)
135 void Texture::set_wrap_s(TextureWrap w)
138 update_parameter(WRAP_S);
141 void Texture::set_wrap_t(TextureWrap w)
144 update_parameter(WRAP_T);
147 void Texture::set_wrap_r(TextureWrap w)
150 update_parameter(WRAP_R);
153 void Texture::set_generate_mipmap(bool gm)
156 static Require _req(SGIS_generate_mipmap);
158 update_parameter(GENERATE_MIPMAP);
161 void Texture::set_compare_enabled(bool c)
164 update_parameter(COMPARE);
167 void Texture::set_compare_func(Predicate f)
170 update_parameter(COMPARE_FUNC);
173 void Texture::bind_to(unsigned i) const
175 TexUnit &unit = TexUnit::get_unit(i);
176 const Texture *cur = unit.get_texture();
177 if(unit.set_texture(this))
180 if(cur && cur->target!=target)
181 glDisable(cur->target);
182 if(!cur || cur->target!=target)
184 glBindTexture(target, id);
188 update_parameter(dirty_params);
194 const Texture *Texture::current(unsigned i)
196 return TexUnit::get_unit(i).get_texture();
199 void Texture::unbind_from(unsigned i)
201 TexUnit &unit = TexUnit::get_unit(i);
202 const Texture *cur = unit.get_texture();
203 if(unit.set_texture(0))
206 glBindTexture(cur->target, 0);
207 glDisable(cur->target);
212 Texture::Loader::Loader(Texture &t):
213 DataFile::CollectionObjectLoader<Texture>(t, 0)
218 Texture::Loader::Loader(Texture &t, Collection &c):
219 DataFile::CollectionObjectLoader<Texture>(t, &c)
224 void Texture::Loader::init()
226 if(Resources *res = dynamic_cast<Resources *>(coll))
227 srgb = res->get_srgb_conversion();
231 add("filter", &Loader::filter);
232 add("max_anisotropy", &Loader::max_anisotropy);
233 add("generate_mipmap", &Loader::generate_mipmap);
234 add("mag_filter", &Loader::mag_filter);
235 add("min_filter", &Loader::min_filter);
236 add("wrap", &Loader::wrap);
237 add("wrap_r", &Loader::wrap_r);
238 add("wrap_s", &Loader::wrap_s);
239 add("wrap_t", &Loader::wrap_t);
242 void Texture::Loader::filter(TextureFilter f)
247 void Texture::Loader::generate_mipmap(bool gm)
249 obj.set_generate_mipmap(gm);
252 void Texture::Loader::mag_filter(TextureFilter f)
254 obj.set_mag_filter(f);
257 void Texture::Loader::max_anisotropy(float a)
259 obj.set_max_anisotropy(a);
262 void Texture::Loader::min_filter(TextureFilter f)
264 obj.set_min_filter(f);
267 void Texture::Loader::wrap(TextureWrap w)
272 void Texture::Loader::wrap_r(TextureWrap w)
277 void Texture::Loader::wrap_s(TextureWrap w)
282 void Texture::Loader::wrap_t(TextureWrap w)