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>
5 #include "resourcemanager.h"
15 void operator>>(const LexicalConverter &c, TextureFilter &tf)
17 if(c.get()=="NEAREST")
19 else if(c.get()=="LINEAR")
21 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
22 tf = NEAREST_MIPMAP_NEAREST;
23 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
24 tf = NEAREST_MIPMAP_LINEAR;
25 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
26 tf = LINEAR_MIPMAP_NEAREST;
27 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
28 tf = LINEAR_MIPMAP_LINEAR;
30 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
34 void operator>>(const LexicalConverter &c, TextureWrap &tw)
38 else if(c.get()=="CLAMP_TO_EDGE")
40 else if(c.get()=="MIRRORED_REPEAT")
43 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
47 Texture::Texture(GLenum t, ResourceManager *m):
50 min_filter(NEAREST_MIPMAP_LINEAR),
63 glGenTextures(1, &id);
68 while(TexUnit *unit = TexUnit::find_unit(this))
69 unbind_from(unit->get_index());
72 glDeleteTextures(1, &id);
75 void Texture::update_parameter(int mask) const
77 if(TexUnit::current().get_texture()!=this)
79 TexUnit *unit = TexUnit::find_unit(this);
90 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
92 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
93 if(mask&MAX_ANISOTROPY)
94 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
96 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
98 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
100 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
101 if(mask&GENERATE_MIPMAP)
102 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
104 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
105 if(mask&COMPARE_FUNC)
106 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
109 void Texture::set_min_filter(TextureFilter f)
112 update_parameter(MIN_FILTER);
115 void Texture::set_mag_filter(TextureFilter f)
118 update_parameter(MAG_FILTER);
121 void Texture::set_filter(TextureFilter f)
124 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
127 void Texture::set_max_anisotropy(float a)
130 throw invalid_argument("Texture::set_max_anisotropy");
132 static Require _req(EXT_texture_filter_anisotropic);
134 update_parameter(MAX_ANISOTROPY);
137 void Texture::set_wrap(TextureWrap w)
144 void Texture::set_wrap_s(TextureWrap w)
147 update_parameter(WRAP_S);
150 void Texture::set_wrap_t(TextureWrap w)
153 update_parameter(WRAP_T);
156 void Texture::set_wrap_r(TextureWrap w)
159 update_parameter(WRAP_R);
162 void Texture::set_generate_mipmap(bool gm)
165 static Require _req(SGIS_generate_mipmap);
167 update_parameter(GENERATE_MIPMAP);
170 void Texture::set_compare_enabled(bool c)
173 update_parameter(COMPARE);
176 void Texture::set_compare_func(Predicate f)
179 update_parameter(COMPARE_FUNC);
182 void Texture::bind_to(unsigned i) const
184 TexUnit &unit = TexUnit::get_unit(i);
185 const Texture *cur = unit.get_texture();
186 if(unit.set_texture(this))
189 manager->resource_used(*this);
192 if(cur && cur->target!=target)
193 glDisable(cur->target);
194 if(!cur || cur->target!=target)
196 glBindTexture(target, id);
200 update_parameter(dirty_params);
206 const Texture *Texture::current(unsigned i)
208 return TexUnit::get_unit(i).get_texture();
211 void Texture::unbind_from(unsigned i)
213 TexUnit &unit = TexUnit::get_unit(i);
214 const Texture *cur = unit.get_texture();
215 if(unit.set_texture(0))
218 glBindTexture(cur->target, 0);
219 glDisable(cur->target);
224 Texture::Loader::Loader(Texture &t):
225 DataFile::CollectionObjectLoader<Texture>(t, 0)
230 Texture::Loader::Loader(Texture &t, Collection &c):
231 DataFile::CollectionObjectLoader<Texture>(t, &c)
236 void Texture::Loader::init()
238 if(Resources *res = dynamic_cast<Resources *>(coll))
239 srgb = res->get_srgb_conversion();
243 add("filter", &Loader::filter);
244 add("max_anisotropy", &Loader::max_anisotropy);
245 add("generate_mipmap", &Loader::generate_mipmap);
246 add("mag_filter", &Loader::mag_filter);
247 add("min_filter", &Loader::min_filter);
248 add("wrap", &Loader::wrap);
249 add("wrap_r", &Loader::wrap_r);
250 add("wrap_s", &Loader::wrap_s);
251 add("wrap_t", &Loader::wrap_t);
254 void Texture::Loader::filter(TextureFilter f)
259 void Texture::Loader::generate_mipmap(bool gm)
261 obj.set_generate_mipmap(gm);
264 void Texture::Loader::mag_filter(TextureFilter f)
266 obj.set_mag_filter(f);
269 void Texture::Loader::max_anisotropy(float a)
271 obj.set_max_anisotropy(a);
274 void Texture::Loader::min_filter(TextureFilter f)
276 obj.set_min_filter(f);
279 void Texture::Loader::wrap(TextureWrap w)
284 void Texture::Loader::wrap_r(TextureWrap w)
289 void Texture::Loader::wrap_s(TextureWrap w)
294 void Texture::Loader::wrap_t(TextureWrap w)