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
67 if(TexUnit::current().get_texture()!=this)
69 TexUnit *unit = TexUnit::find_unit(this);
80 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
82 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
83 if(mask&MAX_ANISOTROPY)
84 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
86 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
88 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
90 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
91 if(mask&GENERATE_MIPMAP)
92 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
94 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
96 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
99 void Texture::set_min_filter(TextureFilter f)
102 update_parameter(MIN_FILTER);
105 void Texture::set_mag_filter(TextureFilter f)
108 update_parameter(MAG_FILTER);
111 void Texture::set_filter(TextureFilter f)
114 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
117 void Texture::set_max_anisotropy(float a)
120 throw invalid_argument("Texture::set_max_anisotropy");
122 static Require _req(EXT_texture_filter_anisotropic);
124 update_parameter(MAX_ANISOTROPY);
127 void Texture::set_wrap(TextureWrap w)
134 void Texture::set_wrap_s(TextureWrap w)
137 update_parameter(WRAP_S);
140 void Texture::set_wrap_t(TextureWrap w)
143 update_parameter(WRAP_T);
146 void Texture::set_wrap_r(TextureWrap w)
149 update_parameter(WRAP_R);
152 void Texture::set_generate_mipmap(bool gm)
155 static Require _req(SGIS_generate_mipmap);
157 update_parameter(GENERATE_MIPMAP);
160 void Texture::set_compare_enabled(bool c)
163 update_parameter(COMPARE);
166 void Texture::set_compare_func(Predicate f)
169 update_parameter(COMPARE_FUNC);
172 void Texture::bind_to(unsigned i) const
174 TexUnit &unit = TexUnit::get_unit(i);
175 const Texture *cur = unit.get_texture();
176 if(unit.set_texture(this))
179 if(cur && cur->target!=target)
180 glDisable(cur->target);
181 if(!cur || cur->target!=target)
183 glBindTexture(target, id);
187 update_parameter(dirty_params);
193 const Texture *Texture::current(unsigned i)
195 return TexUnit::get_unit(i).get_texture();
198 void Texture::unbind_from(unsigned i)
200 TexUnit &unit = TexUnit::get_unit(i);
201 const Texture *cur = unit.get_texture();
202 if(unit.set_texture(0))
205 glBindTexture(cur->target, 0);
206 glDisable(cur->target);
211 Texture::Loader::Loader(Texture &t):
212 DataFile::ObjectLoader<Texture>(t)
214 add("filter", &Loader::filter);
215 add("max_anisotropy", &Loader::max_anisotropy);
216 add("generate_mipmap", &Loader::generate_mipmap);
217 add("mag_filter", &Loader::mag_filter);
218 add("min_filter", &Loader::min_filter);
219 add("wrap", &Loader::wrap);
220 add("wrap_r", &Loader::wrap_r);
221 add("wrap_s", &Loader::wrap_s);
222 add("wrap_t", &Loader::wrap_t);
225 void Texture::Loader::filter(TextureFilter f)
230 void Texture::Loader::generate_mipmap(bool gm)
232 obj.set_generate_mipmap(gm);
235 void Texture::Loader::mag_filter(TextureFilter f)
237 obj.set_mag_filter(f);
240 void Texture::Loader::max_anisotropy(float a)
242 obj.set_max_anisotropy(a);
245 void Texture::Loader::min_filter(TextureFilter f)
247 obj.set_min_filter(f);
250 void Texture::Loader::wrap(TextureWrap w)
255 void Texture::Loader::wrap_r(TextureWrap w)
260 void Texture::Loader::wrap_s(TextureWrap w)
265 void Texture::Loader::wrap_t(TextureWrap w)