1 #include <msp/strings/format.h>
11 void operator>>(const LexicalConverter &c, TextureFilter &tf)
13 if(c.get()=="NEAREST")
15 else if(c.get()=="LINEAR")
17 else if(c.get()=="NEAREST_MIPMAP_NEAREST")
18 tf = NEAREST_MIPMAP_NEAREST;
19 else if(c.get()=="NEAREST_MIPMAP_LINEAR")
20 tf = NEAREST_MIPMAP_LINEAR;
21 else if(c.get()=="LINEAR_MIPMAP_NEAREST")
22 tf = LINEAR_MIPMAP_NEAREST;
23 else if(c.get()=="LINEAR_MIPMAP_LINEAR")
24 tf = LINEAR_MIPMAP_LINEAR;
26 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
30 void operator>>(const LexicalConverter &c, TextureWrap &tw)
34 else if(c.get()=="CLAMP_TO_EDGE")
36 else if(c.get()=="MIRRORED_REPEAT")
39 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
43 Texture::Texture(GLenum t):
45 min_filter(NEAREST_MIPMAP_LINEAR),
55 glGenTextures(1, &id);
60 glDeleteTextures(1, &id);
63 void Texture::update_parameter(int mask) const
68 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
70 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
72 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
74 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
76 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
77 if(mask&GENERATE_MIPMAP)
78 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
80 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
82 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
88 void Texture::set_min_filter(TextureFilter f)
91 update_parameter(MIN_FILTER);
94 void Texture::set_mag_filter(TextureFilter f)
97 update_parameter(MAG_FILTER);
100 void Texture::set_wrap(TextureWrap w)
107 void Texture::set_wrap_s(TextureWrap w)
110 update_parameter(WRAP_S);
113 void Texture::set_wrap_t(TextureWrap w)
116 update_parameter(WRAP_T);
119 void Texture::set_wrap_r(TextureWrap w)
122 update_parameter(WRAP_R);
125 void Texture::set_generate_mipmap(bool gm)
128 update_parameter(GENERATE_MIPMAP);
131 void Texture::set_compare_enabled(bool c)
134 update_parameter(COMPARE);
137 void Texture::set_compare_func(Predicate f)
140 update_parameter(COMPARE_FUNC);
143 void Texture::bind() const
145 const Texture *cur = TexUnit::current().get_texture();
148 if(cur && cur->target!=target)
149 glDisable(cur->target);
150 if(!cur || cur->target!=target)
153 glBindTexture(target, id);
154 TexUnit::current().set_texture(this);
158 update_parameter(dirty_params);
164 void Texture::bind_to(unsigned i) const
166 TexUnit::activate(i);
170 const Texture *Texture::current()
172 return TexUnit::current().get_texture();
175 void Texture::unbind()
177 const Texture *cur = TexUnit::current().get_texture();
181 glBindTexture(cur->target, 0);
182 glDisable(cur->target);
183 TexUnit::current().set_texture(0);
186 void Texture::unbind_from(unsigned i)
188 TexUnit::activate(i);
193 Texture::Loader::Loader(Texture &t):
194 DataFile::ObjectLoader<Texture>(t)
196 add("generate_mipmap", &Loader::generate_mipmap);
197 add("mag_filter", &Loader::mag_filter);
198 add("min_filter", &Loader::min_filter);
199 add("wrap", &Loader::wrap);
200 add("wrap_r", &Loader::wrap_r);
201 add("wrap_s", &Loader::wrap_s);
202 add("wrap_t", &Loader::wrap_t);
205 void Texture::Loader::generate_mipmap(bool gm)
207 obj.set_generate_mipmap(gm);
210 void Texture::Loader::mag_filter(TextureFilter f)
212 obj.set_mag_filter(f);
215 void Texture::Loader::min_filter(TextureFilter f)
217 obj.set_min_filter(f);
220 void Texture::Loader::wrap(TextureWrap w)
225 void Texture::Loader::wrap_r(TextureWrap w)
230 void Texture::Loader::wrap_s(TextureWrap w)
235 void Texture::Loader::wrap_t(TextureWrap w)