10 istream &operator>>(istream &in, TextureFilter &tf)
17 else if(str=="LINEAR")
19 else if(str=="NEAREST_MIPMAP_NEAREST")
20 tf = NEAREST_MIPMAP_NEAREST;
21 else if(str=="NEAREST_MIPMAP_LINEAR")
22 tf = NEAREST_MIPMAP_LINEAR;
23 else if(str=="LINEAR_MIPMAP_NEAREST")
24 tf = LINEAR_MIPMAP_NEAREST;
25 else if(str=="LINEAR_MIPMAP_LINEAR")
26 tf = LINEAR_MIPMAP_LINEAR;
28 in.setstate(ios_base::failbit);
34 void operator>>(const LexicalConverter &c, TextureWrap &tw)
38 else if(c.get()=="CLAMP_TO_EDGE")
40 else if(c.get()=="MIRRORED_REPEAT")
43 throw LexicalError("Invalid input in TextureWrap conversion");
47 Texture::Texture(GLenum t):
49 min_filter(NEAREST_MIPMAP_LINEAR),
59 glGenTextures(1, &id);
64 glDeleteTextures(1, &id);
67 void Texture::update_parameter(int mask) const
72 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
74 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
76 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
78 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
80 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
81 if(mask&GENERATE_MIPMAP)
82 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
84 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
86 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
92 void Texture::set_min_filter(TextureFilter f)
95 update_parameter(MIN_FILTER);
98 void Texture::set_mag_filter(TextureFilter f)
101 update_parameter(MAG_FILTER);
104 void Texture::set_wrap(TextureWrap w)
111 void Texture::set_wrap_s(TextureWrap w)
114 update_parameter(WRAP_S);
117 void Texture::set_wrap_t(TextureWrap w)
120 update_parameter(WRAP_T);
123 void Texture::set_wrap_r(TextureWrap w)
126 update_parameter(WRAP_R);
129 void Texture::set_generate_mipmap(bool gm)
132 update_parameter(GENERATE_MIPMAP);
135 void Texture::set_compare_enabled(bool c)
138 update_parameter(COMPARE);
141 void Texture::set_compare_func(Predicate f)
144 update_parameter(COMPARE_FUNC);
147 void Texture::bind() const
150 throw InvalidState("Attempt to bind a texture without target (should never happen)");
152 const Texture *cur = TexUnit::current().get_texture();
155 if(cur && cur->target!=target)
156 glDisable(cur->target);
157 if(!cur || cur->target!=target)
160 glBindTexture(target, id);
161 TexUnit::current().set_texture(this);
165 update_parameter(dirty_params);
171 void Texture::bind_to(unsigned i) const
173 TexUnit::activate(i);
177 const Texture *Texture::current()
179 return TexUnit::current().get_texture();
182 void Texture::unbind()
184 const Texture *cur = TexUnit::current().get_texture();
188 glBindTexture(cur->target, 0);
189 glDisable(cur->target);
190 TexUnit::current().set_texture(0);
193 void Texture::unbind_from(unsigned i)
195 TexUnit::activate(i);
200 Texture::Loader::Loader(Texture &t):
201 DataFile::ObjectLoader<Texture>(t)
203 add("generate_mipmap", &Loader::generate_mipmap);
204 add("mag_filter", &Loader::mag_filter);
205 add("min_filter", &Loader::min_filter);
206 add("wrap", &Loader::wrap);
207 add("wrap_r", &Loader::wrap_r);
208 add("wrap_s", &Loader::wrap_s);
209 add("wrap_t", &Loader::wrap_t);
212 void Texture::Loader::generate_mipmap(bool gm)
214 obj.set_generate_mipmap(gm);
217 void Texture::Loader::mag_filter(TextureFilter f)
219 obj.set_mag_filter(f);
222 void Texture::Loader::min_filter(TextureFilter f)
224 obj.set_min_filter(f);
227 void Texture::Loader::wrap(TextureWrap w)
232 void Texture::Loader::wrap_r(TextureWrap w)
237 void Texture::Loader::wrap_s(TextureWrap w)
242 void Texture::Loader::wrap_t(TextureWrap w)