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