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