]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Make desertpillars compatible with the modernization changes
[libs/gl.git] / source / texture.cpp
1 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
2 #include <msp/gl/extensions/sgis_generate_mipmap.h>
3 #include <msp/strings/format.h>
4 #include "error.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(TexUnit::current().get_texture()!=this)
68         {
69                 TexUnit *unit = TexUnit::find_unit(this);
70                 if(!unit)
71                 {
72                         dirty_params |= mask;
73                         return;
74                 }
75
76                 unit->bind();
77         }
78
79         if(mask&MIN_FILTER)
80                 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
81         if(mask&MAG_FILTER)
82                 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
83         if(mask&MAX_ANISOTROPY)
84                 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
85         if(mask&WRAP_S)
86                 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
87         if(mask&WRAP_T)
88                 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
89         if(mask&WRAP_R)
90                 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
91         if(mask&GENERATE_MIPMAP)
92                 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap);
93         if(mask&COMPARE)
94                 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
95         if(mask&COMPARE_FUNC)
96                 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
97 }
98
99 void Texture::set_min_filter(TextureFilter f)
100 {
101         min_filter = f;
102         update_parameter(MIN_FILTER);
103 }
104
105 void Texture::set_mag_filter(TextureFilter f)
106 {
107         mag_filter = f;
108         update_parameter(MAG_FILTER);
109 }
110
111 void Texture::set_filter(TextureFilter f)
112 {
113         set_min_filter(f);
114         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
115 }
116
117 void Texture::set_max_anisotropy(float a)
118 {
119         if(a<1.0f)
120                 throw invalid_argument("Texture::set_max_anisotropy");
121         else if(a>1.0f)
122                 static Require _req(EXT_texture_filter_anisotropic);
123         max_anisotropy = a;
124         update_parameter(MAX_ANISOTROPY);
125 }
126
127 void Texture::set_wrap(TextureWrap w)
128 {
129         set_wrap_s(w);
130         set_wrap_t(w);
131         set_wrap_r(w);
132 }
133
134 void Texture::set_wrap_s(TextureWrap w)
135 {
136         wrap_s = w;
137         update_parameter(WRAP_S);
138 }
139
140 void Texture::set_wrap_t(TextureWrap w)
141 {
142         wrap_t = w;
143         update_parameter(WRAP_T);
144 }
145
146 void Texture::set_wrap_r(TextureWrap w)
147 {
148         wrap_r = w;
149         update_parameter(WRAP_R);
150 }
151
152 void Texture::set_generate_mipmap(bool gm)
153 {
154         if(gm)
155                 static Require _req(SGIS_generate_mipmap);
156         gen_mipmap = gm;
157         update_parameter(GENERATE_MIPMAP);
158 }
159
160 void Texture::set_compare_enabled(bool c)
161 {
162         compare = c;
163         update_parameter(COMPARE);
164 }
165
166 void Texture::set_compare_func(Predicate f)
167 {
168         cmp_func = f;
169         update_parameter(COMPARE_FUNC);
170 }
171
172 void Texture::bind_to(unsigned i) const
173 {
174         TexUnit &unit = TexUnit::get_unit(i);
175         const Texture *cur = unit.get_texture();
176         if(unit.set_texture(this))
177         {
178                 unit.bind();
179                 if(cur && cur->target!=target)
180                         glDisable(cur->target);
181                 if(!cur || cur->target!=target)
182                         glEnable(target);
183                 glBindTexture(target, id);
184
185                 if(dirty_params)
186                 {
187                         update_parameter(dirty_params);
188                         dirty_params = 0;
189                 }
190         }
191 }
192
193 const Texture *Texture::current(unsigned i)
194 {
195         return TexUnit::get_unit(i).get_texture();
196 }
197
198 void Texture::unbind_from(unsigned i)
199 {
200         TexUnit &unit = TexUnit::get_unit(i);
201         const Texture *cur = unit.get_texture();
202         if(unit.set_texture(0))
203         {
204                 unit.bind();
205                 glBindTexture(cur->target, 0);
206                 glDisable(cur->target);
207         }
208 }
209
210
211 Texture::Loader::Loader(Texture &t):
212         DataFile::ObjectLoader<Texture>(t)
213 {
214         add("filter", &Loader::filter);
215         add("max_anisotropy", &Loader::max_anisotropy);
216         add("generate_mipmap", &Loader::generate_mipmap);
217         add("mag_filter", &Loader::mag_filter);
218         add("min_filter", &Loader::min_filter);
219         add("wrap",       &Loader::wrap);
220         add("wrap_r",     &Loader::wrap_r);
221         add("wrap_s",     &Loader::wrap_s);
222         add("wrap_t",     &Loader::wrap_t);
223 }
224
225 void Texture::Loader::filter(TextureFilter f)
226 {
227         obj.set_filter(f);
228 }
229
230 void Texture::Loader::generate_mipmap(bool gm)
231 {
232         obj.set_generate_mipmap(gm);
233 }
234
235 void Texture::Loader::mag_filter(TextureFilter f)
236 {
237         obj.set_mag_filter(f);
238 }
239
240 void Texture::Loader::max_anisotropy(float a)
241 {
242         obj.set_max_anisotropy(a);
243 }
244
245 void Texture::Loader::min_filter(TextureFilter f)
246 {
247         obj.set_min_filter(f);
248 }
249
250 void Texture::Loader::wrap(TextureWrap w)
251 {
252         obj.set_wrap(w);
253 }
254
255 void Texture::Loader::wrap_r(TextureWrap w)
256 {
257         obj.set_wrap_r(w);
258 }
259
260 void Texture::Loader::wrap_s(TextureWrap w)
261 {
262         obj.set_wrap_s(w);
263 }
264
265 void Texture::Loader::wrap_t(TextureWrap w)
266 {
267         obj.set_wrap_t(w);
268 }
269
270 } // namespace GL
271 } // namespace Msp