1 #include <msp/strings/format.h>
12 void operator>>(const LexicalConverter &c, TextureFilter &tf)
14 if(c.get()=="NEAREST")
16 else if(c.get()=="LINEAR")
18 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
19 tf = NEAREST_MIPMAP_NEAREST;
20 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
21 tf = NEAREST_MIPMAP_LINEAR;
22 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
23 tf = LINEAR_MIPMAP_NEAREST;
24 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
25 tf = LINEAR_MIPMAP_LINEAR;
27 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
31 void operator>>(const LexicalConverter &c, TextureWrap &tw)
35 else if(c.get()=="CLAMP_TO_EDGE")
37 else if(c.get()=="MIRRORED_REPEAT")
40 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
44 Texture::Texture(GLenum t):
46 min_filter(NEAREST_MIPMAP_LINEAR),
56 glGenTextures(1, &id);
61 glDeleteTextures(1, &id);
64 void Texture::update_parameter(int mask) const
69 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
71 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
73 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
75 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
77 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
78 if(mask&GENERATE_MIPMAP)
79 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
81 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
83 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
89 void Texture::set_min_filter(TextureFilter f)
92 update_parameter(MIN_FILTER);
95 void Texture::set_mag_filter(TextureFilter f)
98 update_parameter(MAG_FILTER);
101 void Texture::set_wrap(TextureWrap w)
108 void Texture::set_wrap_s(TextureWrap w)
111 update_parameter(WRAP_S);
114 void Texture::set_wrap_t(TextureWrap w)
117 update_parameter(WRAP_T);
120 void Texture::set_wrap_r(TextureWrap w)
123 update_parameter(WRAP_R);
126 void Texture::set_generate_mipmap(bool gm)
129 static RequireExtension _ext("GL_SGIS_generate_mipmap");
131 update_parameter(GENERATE_MIPMAP);
134 void Texture::set_compare_enabled(bool c)
137 update_parameter(COMPARE);
140 void Texture::set_compare_func(Predicate f)
143 update_parameter(COMPARE_FUNC);
146 void Texture::bind() const
148 const Texture *cur = TexUnit::current().get_texture();
151 if(cur && cur->target!=target)
152 glDisable(cur->target);
153 if(!cur || cur->target!=target)
156 glBindTexture(target, id);
157 TexUnit::current().set_texture(this);
161 update_parameter(dirty_params);
167 void Texture::bind_to(unsigned i) const
169 TexUnit::activate(i);
173 const Texture *Texture::current()
175 return TexUnit::current().get_texture();
178 void Texture::unbind()
180 const Texture *cur = TexUnit::current().get_texture();
184 glBindTexture(cur->target, 0);
185 glDisable(cur->target);
186 TexUnit::current().set_texture(0);
189 void Texture::unbind_from(unsigned i)
191 TexUnit::activate(i);
196 Texture::Loader::Loader(Texture &t):
197 DataFile::ObjectLoader<Texture>(t)
199 add("generate_mipmap", &Loader::generate_mipmap);
200 add("mag_filter", &Loader::mag_filter);
201 add("min_filter", &Loader::min_filter);
202 add("wrap", &Loader::wrap);
203 add("wrap_r", &Loader::wrap_r);
204 add("wrap_s", &Loader::wrap_s);
205 add("wrap_t", &Loader::wrap_t);
208 void Texture::Loader::generate_mipmap(bool gm)
210 obj.set_generate_mipmap(gm);
213 void Texture::Loader::mag_filter(TextureFilter f)
215 obj.set_mag_filter(f);
218 void Texture::Loader::min_filter(TextureFilter f)
220 obj.set_min_filter(f);
223 void Texture::Loader::wrap(TextureWrap w)
228 void Texture::Loader::wrap_r(TextureWrap w)
233 void Texture::Loader::wrap_s(TextureWrap w)
238 void Texture::Loader::wrap_t(TextureWrap w)