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);
69 glDeleteTextures(1, &id);
72 void Texture::update_parameter(int mask) const
74 if(TexUnit::current().get_texture()!=this)
76 TexUnit *unit = TexUnit::find_unit(this);
87 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
89 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
90 if(mask&MAX_ANISOTROPY)
91 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
93 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
95 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
97 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
98 if(mask&GENERATE_MIPMAP)
99 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
101 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
102 if(mask&COMPARE_FUNC)
103 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
106 void Texture::set_min_filter(TextureFilter f)
109 update_parameter(MIN_FILTER);
112 void Texture::set_mag_filter(TextureFilter f)
115 update_parameter(MAG_FILTER);
118 void Texture::set_filter(TextureFilter f)
121 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
124 void Texture::set_max_anisotropy(float a)
127 throw invalid_argument("Texture::set_max_anisotropy");
129 static Require _req(EXT_texture_filter_anisotropic);
131 update_parameter(MAX_ANISOTROPY);
134 void Texture::set_wrap(TextureWrap w)
141 void Texture::set_wrap_s(TextureWrap w)
144 update_parameter(WRAP_S);
147 void Texture::set_wrap_t(TextureWrap w)
150 update_parameter(WRAP_T);
153 void Texture::set_wrap_r(TextureWrap w)
156 update_parameter(WRAP_R);
159 void Texture::set_generate_mipmap(bool gm)
162 static Require _req(SGIS_generate_mipmap);
164 update_parameter(GENERATE_MIPMAP);
167 void Texture::set_compare_enabled(bool c)
170 update_parameter(COMPARE);
173 void Texture::set_compare_func(Predicate f)
176 update_parameter(COMPARE_FUNC);
179 void Texture::bind_to(unsigned i) const
181 TexUnit &unit = TexUnit::get_unit(i);
182 const Texture *cur = unit.get_texture();
183 if(unit.set_texture(this))
186 manager->resource_used(*this);
189 if(cur && cur->target!=target)
190 glDisable(cur->target);
191 if(!cur || cur->target!=target)
193 glBindTexture(target, id);
197 update_parameter(dirty_params);
203 const Texture *Texture::current(unsigned i)
205 return TexUnit::get_unit(i).get_texture();
208 void Texture::unbind_from(unsigned i)
210 TexUnit &unit = TexUnit::get_unit(i);
211 const Texture *cur = unit.get_texture();
212 if(unit.set_texture(0))
215 glBindTexture(cur->target, 0);
216 glDisable(cur->target);
221 Texture::Loader::Loader(Texture &t):
222 DataFile::CollectionObjectLoader<Texture>(t, 0)
227 Texture::Loader::Loader(Texture &t, Collection &c):
228 DataFile::CollectionObjectLoader<Texture>(t, &c)
233 void Texture::Loader::init()
235 if(Resources *res = dynamic_cast<Resources *>(coll))
236 srgb = res->get_srgb_conversion();
240 add("filter", &Loader::filter);
241 add("max_anisotropy", &Loader::max_anisotropy);
242 add("generate_mipmap", &Loader::generate_mipmap);
243 add("mag_filter", &Loader::mag_filter);
244 add("min_filter", &Loader::min_filter);
245 add("wrap", &Loader::wrap);
246 add("wrap_r", &Loader::wrap_r);
247 add("wrap_s", &Loader::wrap_s);
248 add("wrap_t", &Loader::wrap_t);
251 void Texture::Loader::filter(TextureFilter f)
256 void Texture::Loader::generate_mipmap(bool gm)
258 obj.set_generate_mipmap(gm);
261 void Texture::Loader::mag_filter(TextureFilter f)
263 obj.set_mag_filter(f);
266 void Texture::Loader::max_anisotropy(float a)
268 obj.set_max_anisotropy(a);
271 void Texture::Loader::min_filter(TextureFilter f)
273 obj.set_min_filter(f);
276 void Texture::Loader::wrap(TextureWrap w)
281 void Texture::Loader::wrap_r(TextureWrap w)
286 void Texture::Loader::wrap_s(TextureWrap w)
291 void Texture::Loader::wrap_t(TextureWrap w)