1 #include <msp/gl/extensions/arb_shadow.h>
2 #include <msp/gl/extensions/ext_framebuffer_object.h>
3 #include <msp/gl/extensions/ext_texture3d.h>
4 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
5 #include <msp/gl/extensions/sgis_generate_mipmap.h>
6 #include <msp/strings/format.h>
8 #include "resourcemanager.h"
18 void operator>>(const LexicalConverter &c, TextureFilter &tf)
20 if(c.get()=="NEAREST")
22 else if(c.get()=="LINEAR")
24 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
25 tf = NEAREST_MIPMAP_NEAREST;
26 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
27 tf = NEAREST_MIPMAP_LINEAR;
28 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
29 tf = LINEAR_MIPMAP_NEAREST;
30 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
31 tf = LINEAR_MIPMAP_LINEAR;
33 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
37 void operator>>(const LexicalConverter &c, TextureWrap &tw)
41 else if(c.get()=="CLAMP_TO_EDGE")
43 else if(c.get()=="MIRRORED_REPEAT")
46 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
50 Texture::Texture(GLenum t, ResourceManager *m):
53 min_filter(NEAREST_MIPMAP_LINEAR),
66 glGenTextures(1, &id);
71 while(TexUnit *unit = TexUnit::find_unit(this))
72 unbind_from(unit->get_index());
75 glDeleteTextures(1, &id);
78 void Texture::update_parameter(int mask) const
80 if(TexUnit::current().get_texture()!=this)
82 TexUnit *unit = TexUnit::find_unit(this);
93 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
95 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
96 if(mask&MAX_ANISOTROPY)
97 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
99 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
101 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
103 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
104 if(mask&GENERATE_MIPMAP)
105 glTexParameteri(target, GL_GENERATE_MIPMAP, gen_mipmap);
107 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
108 if(mask&COMPARE_FUNC)
109 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
112 void Texture::set_min_filter(TextureFilter f)
115 update_parameter(MIN_FILTER);
118 void Texture::set_mag_filter(TextureFilter f)
121 update_parameter(MAG_FILTER);
124 void Texture::set_filter(TextureFilter f)
127 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
130 void Texture::set_max_anisotropy(float a)
133 throw invalid_argument("Texture::set_max_anisotropy");
135 static Require _req(EXT_texture_filter_anisotropic);
137 update_parameter(MAX_ANISOTROPY);
140 void Texture::set_wrap(TextureWrap w)
148 void Texture::set_wrap_s(TextureWrap w)
151 update_parameter(WRAP_S);
154 void Texture::set_wrap_t(TextureWrap w)
157 update_parameter(WRAP_T);
160 void Texture::set_wrap_r(TextureWrap w)
162 static Require _req(EXT_texture3D);
164 update_parameter(WRAP_R);
167 void Texture::set_generate_mipmap(bool gm)
170 static Require _req(SGIS_generate_mipmap);
172 if(get_gl_api()!=OPENGL_ES2)
173 update_parameter(GENERATE_MIPMAP);
176 void Texture::auto_generate_mipmap()
178 if(get_gl_api()==OPENGL_ES2)
180 // glGenerateMipmap is defined here
181 static Require _req(EXT_framebuffer_object);
182 glGenerateMipmap(target);
186 void Texture::set_compare_enabled(bool c)
188 static Require _req(ARB_shadow);
190 update_parameter(COMPARE);
193 void Texture::set_compare_func(Predicate f)
195 static Require _req(ARB_shadow);
197 update_parameter(COMPARE_FUNC);
200 void Texture::bind_to(unsigned i) const
205 manager->resource_used(*this);
213 TexUnit &unit = TexUnit::get_unit(i);
214 const Texture *old = unit.get_texture();
215 if(unit.set_texture(this))
218 manager->resource_used(*this);
221 if(unit.supports_legacy())
223 if(old && old->target!=target)
224 glDisable(old->target);
225 if(!old || old->target!=target)
228 glBindTexture(target, id);
232 update_parameter(dirty_params);
238 const Texture *Texture::current(unsigned i)
240 return TexUnit::get_unit(i).get_texture();
243 void Texture::unbind_from(unsigned i)
245 TexUnit &unit = TexUnit::get_unit(i);
246 const Texture *cur = unit.get_texture();
247 if(unit.set_texture(0))
250 glBindTexture(cur->target, 0);
251 if(unit.supports_legacy())
252 glDisable(cur->target);
257 Texture::Loader::Loader(Texture &t):
258 DataFile::CollectionObjectLoader<Texture>(t, 0)
263 Texture::Loader::Loader(Texture &t, Collection &c):
264 DataFile::CollectionObjectLoader<Texture>(t, &c)
269 void Texture::Loader::init()
271 if(Resources *res = dynamic_cast<Resources *>(coll))
272 srgb = res->get_srgb_conversion();
276 add("filter", &Loader::filter);
277 add("max_anisotropy", &Loader::max_anisotropy);
278 add("generate_mipmap", &Loader::generate_mipmap);
279 add("mag_filter", &Loader::mag_filter);
280 add("min_filter", &Loader::min_filter);
281 add("wrap", &Loader::wrap);
282 add("wrap_r", &Loader::wrap_r);
283 add("wrap_s", &Loader::wrap_s);
284 add("wrap_t", &Loader::wrap_t);
287 void Texture::Loader::filter(TextureFilter f)
292 void Texture::Loader::generate_mipmap(bool gm)
294 obj.set_generate_mipmap(gm);
297 void Texture::Loader::mag_filter(TextureFilter f)
299 obj.set_mag_filter(f);
302 void Texture::Loader::max_anisotropy(float a)
304 obj.set_max_anisotropy(a);
307 void Texture::Loader::min_filter(TextureFilter f)
309 obj.set_min_filter(f);
312 void Texture::Loader::wrap(TextureWrap w)
317 void Texture::Loader::wrap_r(TextureWrap w)
322 void Texture::Loader::wrap_s(TextureWrap w)
327 void Texture::Loader::wrap_t(TextureWrap w)