3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
17 istream &operator>>(istream &in, TextureFilter &tf)
24 else if(str=="LINEAR")
26 else if(str=="NEAREST_MIPMAP_NEAREST")
27 tf = NEAREST_MIPMAP_NEAREST;
28 else if(str=="NEAREST_MIPMAP_LINEAR")
29 tf = NEAREST_MIPMAP_LINEAR;
30 else if(str=="LINEAR_MIPMAP_NEAREST")
31 tf = LINEAR_MIPMAP_NEAREST;
32 else if(str=="LINEAR_MIPMAP_LINEAR")
33 tf = LINEAR_MIPMAP_LINEAR;
35 in.setstate(ios_base::failbit);
41 Texture::Texture(GLenum t):
43 min_filter(NEAREST_MIPMAP_LINEAR),
53 glGenTextures(1, &id);
58 glDeleteTextures(1, &id);
61 void Texture::update_parameter(int mask) const
66 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
68 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
70 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
72 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
74 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
75 if(mask&GENERATE_MIPMAP)
76 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
78 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
80 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
86 void Texture::set_min_filter(TextureFilter f)
89 update_parameter(MIN_FILTER);
92 void Texture::set_mag_filter(TextureFilter f)
95 update_parameter(MAG_FILTER);
98 void Texture::set_wrap(TextureWrap w)
105 void Texture::set_wrap_s(TextureWrap w)
108 update_parameter(WRAP_S);
111 void Texture::set_wrap_t(TextureWrap w)
114 update_parameter(WRAP_T);
117 void Texture::set_wrap_r(TextureWrap w)
120 update_parameter(WRAP_R);
123 void Texture::set_generate_mipmap(bool gm)
126 update_parameter(GENERATE_MIPMAP);
129 void Texture::set_compare_enabled(bool c)
132 update_parameter(COMPARE);
135 void Texture::set_compare_func(Predicate f)
138 update_parameter(COMPARE_FUNC);
141 void Texture::bind() const
144 throw InvalidState("Attempt to bind a texture without target (should never happen)");
146 const Texture *cur = TexUnit::current().get_texture();
149 if(cur && cur->target!=target)
150 glDisable(cur->target);
151 if(!cur || cur->target!=target)
154 glBindTexture(target, id);
155 TexUnit::current().set_texture(this);
159 update_parameter(dirty_params);
165 void Texture::bind_to(unsigned i) const
167 TexUnit::activate(i);
171 const Texture *Texture::current()
173 return TexUnit::current().get_texture();
176 void Texture::unbind()
178 const Texture *cur = TexUnit::current().get_texture();
182 glBindTexture(cur->target, 0);
183 glDisable(cur->target);
184 TexUnit::current().set_texture(0);
187 void Texture::unbind_from(unsigned i)
189 TexUnit::activate(i);
194 Texture::Loader::Loader(Texture &t):
195 DataFile::ObjectLoader<Texture>(t)
197 add("min_filter", &Loader::min_filter);
198 add("mag_filter", &Loader::mag_filter);
199 add("generate_mipmap", &Loader::generate_mipmap);
202 void Texture::Loader::min_filter(TextureFilter f)
204 obj.set_min_filter(f);
207 void Texture::Loader::mag_filter(TextureFilter f)
209 obj.set_mag_filter(f);
212 void Texture::Loader::generate_mipmap(bool gm)
214 obj.set_generate_mipmap(gm);