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