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