]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.cpp
6f4770acb4e40a73ea6270bfeabcbeeec273fcc8
[libs/gl.git] / source / core / sampler.cpp
1 #include <msp/strings/format.h>
2 #include "error.h"
3 #include "sampler.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Sampler::Sampler():
11         min_filter(NEAREST_MIPMAP_LINEAR),
12         mag_filter(LINEAR),
13         max_anisotropy(1.0f),
14         wrap_s(REPEAT),
15         wrap_t(REPEAT),
16         wrap_r(REPEAT),
17         border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)),
18         compare(false),
19         cmp_func(LEQUAL),
20         dirty_params(0)
21 { }
22
23 void Sampler::update() const
24 {
25         SamplerBackend::update(dirty_params);
26         dirty_params = 0;
27 }
28
29 void Sampler::set_min_filter(TextureFilter f)
30 {
31         min_filter = f;
32         dirty_params |= MIN_FILTER;
33 }
34
35 void Sampler::set_mag_filter(TextureFilter f)
36 {
37         mag_filter = f;
38         dirty_params |= MAG_FILTER;
39 }
40
41 void Sampler::set_filter(TextureFilter f)
42 {
43         set_min_filter(f);
44         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
45 }
46
47 void Sampler::set_max_anisotropy(float a)
48 {
49         if(a<1.0f)
50                 throw invalid_argument("Sampler::set_max_anisotropy");
51         bool supported = check_anisotropic(a>1.0f);
52         max_anisotropy = a;
53         if(supported)
54                 dirty_params |= MAX_ANISOTROPY;
55 }
56
57 void Sampler::set_wrap(TextureWrap w)
58 {
59         set_wrap_s(w);
60         set_wrap_t(w);
61         set_wrap_r(w);
62 }
63
64 void Sampler::set_wrap_s(TextureWrap w)
65 {
66         wrap_s = w;
67         dirty_params |= WRAP_S;
68 }
69
70 void Sampler::set_wrap_t(TextureWrap w)
71 {
72         wrap_t = w;
73         dirty_params |= WRAP_T;
74 }
75
76 void Sampler::set_wrap_r(TextureWrap w)
77 {
78         wrap_r = w;
79         dirty_params |= WRAP_R;
80 }
81
82 void Sampler::set_border_color(const Color &c)
83 {
84         border_color = c;
85         dirty_params |= BORDER_COLOR;
86 }
87
88 void Sampler::disable_compare()
89 {
90         compare = false;
91         dirty_params |= COMPARE;
92 }
93
94 void Sampler::set_compare(Predicate f)
95 {
96         compare = true;
97         cmp_func = f;
98         dirty_params |= COMPARE;
99 }
100
101
102 Sampler::Loader::Loader(Sampler &s):
103         DataFile::ObjectLoader<Sampler>(s)
104 {
105         add("border_color", &Loader::border_color);
106         add("compare", &Loader::compare);
107         add("filter", &Loader::filter);
108         add("mag_filter", &Loader::mag_filter);
109         add("max_anisotropy", &Loader::max_anisotropy);
110         add("min_filter", &Loader::min_filter);
111         add("wrap", &Loader::wrap);
112         add("wrap_r", &Loader::wrap_r);
113         add("wrap_s", &Loader::wrap_s);
114         add("wrap_t", &Loader::wrap_t);
115 }
116
117 void Sampler::Loader::border_color(float r, float g, float b, float a)
118 {
119         obj.set_border_color(Color(r, g, b, a));
120 }
121
122 void Sampler::Loader::compare(Predicate c)
123 {
124         obj.set_compare(c);
125 }
126
127 void Sampler::Loader::filter(TextureFilter f)
128 {
129         obj.set_filter(f);
130 }
131
132 void Sampler::Loader::mag_filter(TextureFilter f)
133 {
134         obj.set_mag_filter(f);
135 }
136
137 void Sampler::Loader::max_anisotropy(float a)
138 {
139         obj.set_max_anisotropy(a);
140 }
141
142 void Sampler::Loader::min_filter(TextureFilter f)
143 {
144         obj.set_min_filter(f);
145 }
146
147 void Sampler::Loader::wrap(TextureWrap w)
148 {
149         obj.set_wrap(w);
150 }
151
152 void Sampler::Loader::wrap_r(TextureWrap w)
153 {
154         obj.set_wrap_r(w);
155 }
156
157 void Sampler::Loader::wrap_s(TextureWrap w)
158 {
159         obj.set_wrap_s(w);
160 }
161
162 void Sampler::Loader::wrap_t(TextureWrap w)
163 {
164         obj.set_wrap_t(w);
165 }
166
167
168 bool is_mipmapped(TextureFilter filter)
169 {
170         return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
171                 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
172 }
173
174 void operator>>(const LexicalConverter &c, TextureFilter &tf)
175 {
176         if(c.get()=="NEAREST")
177                 tf = NEAREST;
178         else if(c.get()=="LINEAR")
179                 tf = LINEAR;
180         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
181                 tf = NEAREST_MIPMAP_NEAREST;
182         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
183                 tf = NEAREST_MIPMAP_LINEAR;
184         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
185                 tf = LINEAR_MIPMAP_NEAREST;
186         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
187                 tf = LINEAR_MIPMAP_LINEAR;
188         else
189                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
190 }
191
192 void operator>>(const LexicalConverter &c, TextureWrap &tw)
193 {
194         if(c.get()=="REPEAT")
195                 tw = REPEAT;
196         else if(c.get()=="CLAMP_TO_EDGE")
197                 tw = CLAMP_TO_EDGE;
198         else if(c.get()=="CLAMP_TO_BORDER")
199                 tw = CLAMP_TO_BORDER;
200         else if(c.get()=="MIRRORED_REPEAT")
201                 tw = MIRRORED_REPEAT;
202         else
203                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
204 }
205
206
207 } // namespace GL
208 } // namespace Msp