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