]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Support for anisotropic texture filtering
[libs/gl.git] / source / texture.cpp
1 #include <msp/strings/format.h>
2 #include "error.h"
3 #include "ext_texture_filter_anisotropic.h"
4 #include "sgis_generate_mipmap.h"
5 #include "texture.h"
6 #include "texunit.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 void operator>>(const LexicalConverter &c, TextureFilter &tf)
14 {
15         if(c.get()=="NEAREST")
16                 tf = NEAREST;
17         else if(c.get()=="LINEAR")
18                 tf = LINEAR;
19         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
20                 tf = NEAREST_MIPMAP_NEAREST;
21         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
22                 tf = NEAREST_MIPMAP_LINEAR;
23         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
24                 tf = LINEAR_MIPMAP_NEAREST;
25         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
26                 tf = LINEAR_MIPMAP_LINEAR;
27         else
28                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
29 }
30
31
32 void operator>>(const LexicalConverter &c, TextureWrap &tw)
33 {
34         if(c.get()=="REPEAT")
35                 tw = REPEAT;
36         else if(c.get()=="CLAMP_TO_EDGE")
37                 tw = CLAMP_TO_EDGE;
38         else if(c.get()=="MIRRORED_REPEAT")
39                 tw = MIRRORED_REPEAT;
40         else
41                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
42 }
43
44
45 Texture::Texture(GLenum t):
46         target(t),
47         min_filter(NEAREST_MIPMAP_LINEAR),
48         mag_filter(LINEAR),
49         wrap_s(REPEAT),
50         wrap_t(REPEAT),
51         wrap_r(REPEAT),
52         gen_mipmap(false),
53         compare(false),
54         cmp_func(LEQUAL),
55         dirty_params(0)
56 {
57         glGenTextures(1, &id);
58 }
59
60 Texture::~Texture()
61 {
62         glDeleteTextures(1, &id);
63 }
64
65 void Texture::update_parameter(int mask) const
66 {
67         if(current()==this)
68         {
69                 if(mask&MIN_FILTER)
70                         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
71                 if(mask&MAG_FILTER)
72                         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
73                 if(mask&MAX_ANISOTROPY)
74                         glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
75                 if(mask&WRAP_S)
76                         glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
77                 if(mask&WRAP_T)
78                         glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
79                 if(mask&WRAP_R)
80                         glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
81                 if(mask&GENERATE_MIPMAP)
82                         glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
83                 if(mask&COMPARE)
84                         glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
85                 if(mask&COMPARE_FUNC)
86                         glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
87         }
88         else
89                 dirty_params |= mask;
90 }
91
92 void Texture::set_min_filter(TextureFilter f)
93 {
94         min_filter = f;
95         update_parameter(MIN_FILTER);
96 }
97
98 void Texture::set_mag_filter(TextureFilter f)
99 {
100         mag_filter = f;
101         update_parameter(MAG_FILTER);
102 }
103
104 void Texture::set_max_anisotropy(float a)
105 {
106         if(a<1.0f)
107                 throw invalid_argument("Texture::set_max_anisotropy");
108         else if(a>1.0f)
109                 static Require _req(EXT_texture_filter_anisotropic);
110         max_anisotropy = a;
111         update_parameter(MAX_ANISOTROPY);
112 }
113
114 void Texture::set_wrap(TextureWrap w)
115 {
116         set_wrap_s(w);
117         set_wrap_t(w);
118         set_wrap_r(w);
119 }
120
121 void Texture::set_wrap_s(TextureWrap w)
122 {
123         wrap_s = w;
124         update_parameter(WRAP_S);
125 }
126
127 void Texture::set_wrap_t(TextureWrap w)
128 {
129         wrap_t = w;
130         update_parameter(WRAP_T);
131 }
132
133 void Texture::set_wrap_r(TextureWrap w)
134 {
135         wrap_r = w;
136         update_parameter(WRAP_R);
137 }
138
139 void Texture::set_generate_mipmap(bool gm)
140 {
141         if(gm)
142                 static Require _req(SGIS_generate_mipmap);
143         gen_mipmap = gm;
144         update_parameter(GENERATE_MIPMAP);
145 }
146
147 void Texture::set_compare_enabled(bool c)
148 {
149         compare = c;
150         update_parameter(COMPARE);
151 }
152
153 void Texture::set_compare_func(Predicate f)
154 {
155         cmp_func = f;
156         update_parameter(COMPARE_FUNC);
157 }
158
159 void Texture::bind() const
160 {
161         const Texture *cur = TexUnit::current().get_texture();
162         if(cur!=this)
163         {
164                 if(cur && cur->target!=target)
165                         glDisable(cur->target);
166                 if(!cur || cur->target!=target)
167                         glEnable(target);
168
169                 glBindTexture(target, id);
170                 TexUnit::current().set_texture(this);
171
172                 if(dirty_params)
173                 {
174                         update_parameter(dirty_params);
175                         dirty_params = 0;
176                 }
177         }
178 }
179
180 void Texture::bind_to(unsigned i) const
181 {
182         TexUnit::activate(i);
183         bind();
184 }
185
186 const Texture *Texture::current()
187 {
188         return TexUnit::current().get_texture();
189 }
190
191 void Texture::unbind()
192 {
193         const Texture *cur = TexUnit::current().get_texture();
194         if(!cur)
195                 return;
196
197         glBindTexture(cur->target, 0);
198         glDisable(cur->target);
199         TexUnit::current().set_texture(0);
200 }
201
202 void Texture::unbind_from(unsigned i)
203 {
204         TexUnit::activate(i);
205         unbind();
206 }
207
208
209 Texture::Loader::Loader(Texture &t):
210         DataFile::ObjectLoader<Texture>(t)
211 {
212         add("max_anisotropy", &Loader::max_anisotropy);
213         add("generate_mipmap", &Loader::generate_mipmap);
214         add("mag_filter", &Loader::mag_filter);
215         add("min_filter", &Loader::min_filter);
216         add("wrap",       &Loader::wrap);
217         add("wrap_r",     &Loader::wrap_r);
218         add("wrap_s",     &Loader::wrap_s);
219         add("wrap_t",     &Loader::wrap_t);
220 }
221
222 void Texture::Loader::generate_mipmap(bool gm)
223 {
224         obj.set_generate_mipmap(gm);
225 }
226
227 void Texture::Loader::mag_filter(TextureFilter f)
228 {
229         obj.set_mag_filter(f);
230 }
231
232 void Texture::Loader::max_anisotropy(float a)
233 {
234         obj.set_max_anisotropy(a);
235 }
236
237 void Texture::Loader::min_filter(TextureFilter f)
238 {
239         obj.set_min_filter(f);
240 }
241
242 void Texture::Loader::wrap(TextureWrap w)
243 {
244         obj.set_wrap(w);
245 }
246
247 void Texture::Loader::wrap_r(TextureWrap w)
248 {
249         obj.set_wrap_r(w);
250 }
251
252 void Texture::Loader::wrap_s(TextureWrap w)
253 {
254         obj.set_wrap_s(w);
255 }
256
257 void Texture::Loader::wrap_t(TextureWrap w)
258 {
259         obj.set_wrap_t(w);
260 }
261
262 } // namespace GL
263 } // namespace Msp