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)
189 static Require _req(ARB_shadow);
191 update_parameter(COMPARE);
194 void Texture::set_compare_func(Predicate f)
196 static Require _req(ARB_shadow);
198 update_parameter(COMPARE_FUNC);
201 void Texture::bind_to(unsigned i) const
206 manager->resource_used(*this);
214 TexUnit &unit = TexUnit::get_unit(i);
215 const Texture *old = unit.get_texture();
216 if(unit.set_texture(this))
219 manager->resource_used(*this);
222 if(unit.supports_legacy())
224 if(old && old->target!=target)
225 glDisable(old->target);
226 if(!old || old->target!=target)
229 glBindTexture(target, id);
233 update_parameter(dirty_params);
239 const Texture *Texture::current(unsigned i)
241 return TexUnit::get_unit(i).get_texture();
244 void Texture::unbind_from(unsigned i)
246 TexUnit &unit = TexUnit::get_unit(i);
247 const Texture *cur = unit.get_texture();
248 if(unit.set_texture(0))
251 glBindTexture(cur->target, 0);
252 if(unit.supports_legacy())
253 glDisable(cur->target);
258 Texture::Loader::Loader(Texture &t):
259 DataFile::CollectionObjectLoader<Texture>(t, 0)
264 Texture::Loader::Loader(Texture &t, Collection &c):
265 DataFile::CollectionObjectLoader<Texture>(t, &c)
270 void Texture::Loader::init()
272 if(Resources *res = dynamic_cast<Resources *>(coll))
273 srgb = res->get_srgb_conversion();
277 add("filter", &Loader::filter);
278 add("max_anisotropy", &Loader::max_anisotropy);
279 add("generate_mipmap", &Loader::generate_mipmap);
280 add("mag_filter", &Loader::mag_filter);
281 add("min_filter", &Loader::min_filter);
282 add("wrap", &Loader::wrap);
283 add("wrap_r", &Loader::wrap_r);
284 add("wrap_s", &Loader::wrap_s);
285 add("wrap_t", &Loader::wrap_t);
288 void Texture::Loader::filter(TextureFilter f)
293 void Texture::Loader::generate_mipmap(bool gm)
295 obj.set_generate_mipmap(gm);
298 void Texture::Loader::mag_filter(TextureFilter f)
300 obj.set_mag_filter(f);
303 void Texture::Loader::max_anisotropy(float a)
305 obj.set_max_anisotropy(a);
308 void Texture::Loader::min_filter(TextureFilter f)
310 obj.set_min_filter(f);
313 void Texture::Loader::wrap(TextureWrap w)
318 void Texture::Loader::wrap_r(TextureWrap w)
323 void Texture::Loader::wrap_s(TextureWrap w)
328 void Texture::Loader::wrap_t(TextureWrap w)