1 #include <msp/gl/extensions/arb_shadow.h>
2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
4 #include <msp/gl/extensions/sgis_generate_mipmap.h>
5 #include <msp/strings/format.h>
7 #include "resourcemanager.h"
17 void operator>>(const LexicalConverter &c, TextureFilter &tf)
19 if(c.get()=="NEAREST")
21 else if(c.get()=="LINEAR")
23 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
24 tf = NEAREST_MIPMAP_NEAREST;
25 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
26 tf = NEAREST_MIPMAP_LINEAR;
27 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
28 tf = LINEAR_MIPMAP_NEAREST;
29 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
30 tf = LINEAR_MIPMAP_LINEAR;
32 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
36 void operator>>(const LexicalConverter &c, TextureWrap &tw)
40 else if(c.get()=="CLAMP_TO_EDGE")
42 else if(c.get()=="MIRRORED_REPEAT")
45 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
49 Texture::Texture(GLenum t, ResourceManager *m):
52 min_filter(NEAREST_MIPMAP_LINEAR),
65 glGenTextures(1, &id);
70 while(TexUnit *unit = TexUnit::find_unit(this))
71 unbind_from(unit->get_index());
74 glDeleteTextures(1, &id);
77 void Texture::update_parameter(int mask) const
79 if(TexUnit::current().get_texture()!=this)
81 TexUnit *unit = TexUnit::find_unit(this);
92 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
94 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
95 if(mask&MAX_ANISOTROPY)
96 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
98 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
100 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
102 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
103 if(mask&GENERATE_MIPMAP)
104 glTexParameteri(target, GL_GENERATE_MIPMAP, gen_mipmap);
106 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
107 if(mask&COMPARE_FUNC)
108 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
111 void Texture::set_min_filter(TextureFilter f)
114 update_parameter(MIN_FILTER);
117 void Texture::set_mag_filter(TextureFilter f)
120 update_parameter(MAG_FILTER);
123 void Texture::set_filter(TextureFilter f)
126 set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
129 void Texture::set_max_anisotropy(float a)
132 throw invalid_argument("Texture::set_max_anisotropy");
134 static Require _req(EXT_texture_filter_anisotropic);
136 update_parameter(MAX_ANISOTROPY);
139 void Texture::set_wrap(TextureWrap w)
147 void Texture::set_wrap_s(TextureWrap w)
150 update_parameter(WRAP_S);
153 void Texture::set_wrap_t(TextureWrap w)
156 update_parameter(WRAP_T);
159 void Texture::set_wrap_r(TextureWrap w)
161 static Require _req(EXT_texture3D);
163 update_parameter(WRAP_R);
166 void Texture::set_generate_mipmap(bool gm)
169 static Require _req(SGIS_generate_mipmap);
171 update_parameter(GENERATE_MIPMAP);
174 void Texture::set_compare_enabled(bool c)
176 static Require _req(ARB_shadow);
178 update_parameter(COMPARE);
181 void Texture::set_compare_func(Predicate f)
183 static Require _req(ARB_shadow);
185 update_parameter(COMPARE_FUNC);
188 void Texture::bind_to(unsigned i) const
193 manager->resource_used(*this);
201 TexUnit &unit = TexUnit::get_unit(i);
202 const Texture *old = unit.get_texture();
203 if(unit.set_texture(this))
206 manager->resource_used(*this);
209 if(unit.supports_legacy())
211 if(old && old->target!=target)
212 glDisable(old->target);
213 if(!old || old->target!=target)
216 glBindTexture(target, id);
220 update_parameter(dirty_params);
226 const Texture *Texture::current(unsigned i)
228 return TexUnit::get_unit(i).get_texture();
231 void Texture::unbind_from(unsigned i)
233 TexUnit &unit = TexUnit::get_unit(i);
234 const Texture *cur = unit.get_texture();
235 if(unit.set_texture(0))
238 glBindTexture(cur->target, 0);
239 if(unit.supports_legacy())
240 glDisable(cur->target);
245 Texture::Loader::Loader(Texture &t):
246 DataFile::CollectionObjectLoader<Texture>(t, 0)
251 Texture::Loader::Loader(Texture &t, Collection &c):
252 DataFile::CollectionObjectLoader<Texture>(t, &c)
257 void Texture::Loader::init()
259 if(Resources *res = dynamic_cast<Resources *>(coll))
260 srgb = res->get_srgb_conversion();
264 add("filter", &Loader::filter);
265 add("max_anisotropy", &Loader::max_anisotropy);
266 add("generate_mipmap", &Loader::generate_mipmap);
267 add("mag_filter", &Loader::mag_filter);
268 add("min_filter", &Loader::min_filter);
269 add("wrap", &Loader::wrap);
270 add("wrap_r", &Loader::wrap_r);
271 add("wrap_s", &Loader::wrap_s);
272 add("wrap_t", &Loader::wrap_t);
275 void Texture::Loader::filter(TextureFilter f)
280 void Texture::Loader::generate_mipmap(bool gm)
282 obj.set_generate_mipmap(gm);
285 void Texture::Loader::mag_filter(TextureFilter f)
287 obj.set_mag_filter(f);
290 void Texture::Loader::max_anisotropy(float a)
292 obj.set_max_anisotropy(a);
295 void Texture::Loader::min_filter(TextureFilter f)
297 obj.set_min_filter(f);
300 void Texture::Loader::wrap(TextureWrap w)
305 void Texture::Loader::wrap_r(TextureWrap w)
310 void Texture::Loader::wrap_s(TextureWrap w)
315 void Texture::Loader::wrap_t(TextureWrap w)