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