]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / effects / shadowmap.cpp
1 #include <msp/strings/format.h>
2 #include "directionallight.h"
3 #include "error.h"
4 #include "lighting.h"
5 #include "pointlight.h"
6 #include "renderer.h"
7 #include "resources.h"
8 #include "shadowmap.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &c, const Lighting *l):
16         Effect(c),
17         width(w),
18         height(h),
19         lighting(l),
20         sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
21 {
22         depth_buf.storage(DEPTH_COMPONENT32F, width, height, 1);
23         fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
24         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
25
26         set_darkness(1.0f);
27         for(unsigned i=0; i<6; ++i)
28         {
29                 string base = format("shadows[%d]", i);
30                 shdata.uniform(base+".type", 0);
31                 shdata.uniform(base+".darkness", 1.0f);
32                 shdata.uniform(base+".matrix_index", 0);
33                 shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
34                 shdata.uniform(base+".bias", 0.0f, 1.0f);
35         }
36
37         Matrix dummy_matrix;
38         shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix);
39 }
40
41 ShadowMap::ShadowMap(unsigned s, Renderable &c, const DirectionalLight &l, Renderable &sc):
42         ShadowMap(s, s, c, 0)
43 {
44         add_light(l, s, sc);
45 }
46
47 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
48         ShadowMap(w, h, r, &l)
49 { }
50
51 void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c)
52 {
53         add_light(light, s, DIRECTIONAL, c);
54 }
55
56 void ShadowMap::add_light(const PointLight &light, unsigned s, Renderable &c)
57 {
58         add_light(light, s, TETRAHEDRON, c);
59 }
60
61 void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c)
62 {
63         if(!lighting && !lights.empty())
64                 throw invalid_operation("ShadowMap::add_light");
65
66         int index = (lighting ? lighting->find_light_index(light) : 0);
67         if(index<0)
68                 throw invalid_argument("ShadowMap::add_light");
69
70         Rect region(0, 0, s, s);
71         while(1)
72         {
73                 int next_bottom = height;
74                 int next_left = region.left;
75
76                 int top = region.bottom+region.height;
77                 int right = region.left+region.width;
78                 for(const ShadowedLight &l: lights)
79                 {
80                         int l_top = l.region.bottom+l.region.height;
81                         int l_right = l.region.left+l.region.width;
82                         if(l_top>region.bottom)
83                                 next_bottom = min(next_bottom, l_top);
84
85                         if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
86                                 next_left = max(next_left, l_right);
87                 }
88
89                 if(next_left==region.left)
90                         break;
91                 else if(next_left+region.width>width)
92                 {
93                         if(next_bottom+region.height>height)
94                                 throw invalid_operation("ShadowMap::add_light");
95                         region.bottom = next_bottom;
96                         region.left = 0;
97                 }
98                 else
99                         region.left = next_left;
100         }
101
102         unsigned n_views = (type==TETRAHEDRON ? 4 : 1);
103
104         lights.emplace_back();
105         ShadowedLight &sl = lights.back();
106         sl.light = &light;
107         sl.index = index;
108         sl.region = region;
109         sl.type = type;
110         sl.view_index = views.size();
111         sl.shadow_caster = &c;
112
113         views.resize(views.size()+n_views);
114         for(unsigned i=0; i<n_views; ++i)
115         {
116                 ShadowView &view = views[sl.view_index+i];
117                 view.light_index = lights.size()-1;
118                 view.face = i;
119
120                 if(type==TETRAHEDRON)
121                 {
122                         if(i>0)
123                                 view.face_matrix = Matrix().rotate(Geometry::Angle<float>::from_turns((i-0.5f)/3.0f), Vector3(0, 0, 1))
124                                         .rotate(Geometry::Angle<float>::straight()-Geometry::acos<float>(1.0f/3.0f), Vector3(1, 0, 0));
125
126                         float triangle_h = sqrt(0.75f)*2.0f;
127                         float bottom = 0.5f/sqrt(0.75f);
128                         float distance = (sqrt(2.0f/3.0f)-sqrt(3.0f/8.0f))*2.0f;
129                         view.camera.set_field_of_view(Geometry::atan<float>(triangle_h/distance)*2.0f);
130                         view.camera.set_aspect_ratio(1.0f/triangle_h);
131                         view.camera.set_frustum_axis(0.0f, 1.0f-bottom/triangle_h);
132                         view.camera.set_frustum_rotation(-Geometry::Angle<float>::from_turns(i/4.0f));
133                 }
134         }
135
136         string base = format("shadows[%d]", index);
137         shdata.uniform(base+".type", static_cast<int>(type));
138         shdata.uniform(base+".darkness", darkness);
139         shdata.uniform(base+".matrix_index", static_cast<int>(sl.view_index));
140
141         float xf = static_cast<float>(region.left)/width;
142         float yf = static_cast<float>(region.bottom)/height;
143         float wf = static_cast<float>(region.width)/width;
144         float hf = static_cast<float>(region.height)/height;
145         shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
146
147 #ifdef DEBUG
148         if(!debug_name.empty())
149         {
150                 for(unsigned i=sl.view_index; i<views.size(); ++i)
151                         views[i].camera.set_debug_name(format("%s/view%d.camera", debug_name, i));
152         }
153 #endif
154 }
155
156 void ShadowMap::set_target(const Vector3 &t, float r)
157 {
158         target = t;
159         radius = r;
160 }
161
162 void ShadowMap::set_darkness(float d)
163 {
164         if(d<0.0f || d>1.0f)
165                 throw invalid_argument("ShadowMap::set_darkness");
166
167         darkness = d;
168         for(const ShadowedLight &l: lights)
169                 shdata.uniform(format("shadows[%d].darkness", l.index), d);
170 }
171
172 void ShadowMap::set_depth_bias(float b)
173 {
174         if(b<0.0f)
175                 throw invalid_argument("ShadowMap::set_depth_bias");
176
177         depth_bias = b;
178 }
179
180 void ShadowMap::setup_frame(Renderer &renderer)
181 {
182         if(rendered)
183                 return;
184
185         rendered = true;
186         content.setup_frame(renderer);
187         for(const ShadowedLight &l: lights)
188                 l.shadow_caster->setup_frame(renderer);
189
190         for(const ShadowedLight &l: lights)
191         {
192                 string base = format("shadows[%d]", l.index);
193                 if(l.type==DIRECTIONAL)
194                         shdata.uniform(base+".bias", depth_bias/l.region.width, 0.0f);
195                 else if(l.type==TETRAHEDRON)
196                 {
197                         float distance = (sqrt(2.0f/3.0f)-sqrt(3.0f/8.0f))*2.0f;
198                         float bias = depth_bias*2.0f/(distance*l.region.width);
199                         shdata.uniform(base+".bias", views[l.view_index].camera.get_projection_matrix()(2, 2), 1.0f-bias);
200                 }
201         }
202
203         vector<Matrix> shadow_matrices;
204         shadow_matrices.reserve(views.size());
205         for(ShadowView &v: views)
206         {
207                 const ShadowedLight &light = lights[v.light_index];
208
209                 if(light.type==DIRECTIONAL)
210                 {
211                         v.camera.set_object_matrix(*light.light->get_matrix());
212                         v.camera.set_position(target);
213                         v.camera.set_orthographic(radius*2, radius*2);
214                         v.camera.set_depth_clip(-radius, radius);
215                 }
216                 else if(light.type==TETRAHEDRON)
217                 {
218                         v.camera.set_object_matrix((*light.light->get_matrix())*v.face_matrix);
219                         v.camera.set_depth_clip(radius/1000.0f, radius);
220                 }
221
222                 Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.0f)).scale(Vector3(0.5f, 0.5f, 1.0f));
223                 shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix());
224         }
225
226         shdata.uniform_array("shd_world_matrix", shadow_matrices.size(), shadow_matrices.data());
227
228         for(const ShadowView &v: views)
229         {
230                 const ShadowedLight &light = lights[v.light_index];
231
232                 Renderer::Push push(renderer);
233                 renderer.set_framebuffer(&fbo);
234                 renderer.set_viewport(&light.region);
235                 renderer.set_scissor(&light.region);
236                 renderer.set_camera(v.camera);
237
238                 light.shadow_caster->render(renderer, (v.face>0 ? "noclear" : ""));
239         }
240 }
241
242 void ShadowMap::finish_frame()
243 {
244         if(rendered)
245         {
246                 rendered = false;
247                 content.finish_frame();
248         }
249 }
250
251 void ShadowMap::render(Renderer &renderer, Tag tag) const
252 {
253         if(!is_enabled_for_method(tag))
254                 return content.render(renderer, tag);
255
256         Renderer::Push _push_rend(renderer);
257
258         renderer.set_texture("shadow_map", &depth_buf, &sampler);
259         renderer.add_shader_data(shdata);
260         content.render(renderer, tag);
261 }
262
263 void ShadowMap::set_debug_name(const string &name)
264 {
265 #ifdef DEBUG
266         fbo.set_debug_name(name+" [FBO]");
267         for(unsigned i=0; i<views.size(); ++i)
268                 views[i].camera.set_debug_name(format("%s/view%d.camera", name, i));
269         depth_buf.set_debug_name(name+"/depth.tex");
270         shdata.set_debug_name(name+" [UBO]");
271 #else
272         (void)name;
273 #endif
274 }
275
276
277 ShadowMap *ShadowMap::Template::create(const map<string, Renderable *> &renderables) const
278 {
279         Renderable *content = get_item(renderables, content_name);
280         if(!content || !lighting)
281                 throw invalid_operation("ShadowMap::Template::create");
282
283         RefPtr<ShadowMap> shadow_map = new ShadowMap(width, height, *content, *lighting);
284         shadow_map->set_target(target, radius);
285         shadow_map->set_depth_bias(depth_bias);
286         shadow_map->set_darkness(darkness);
287
288         for(const ShadowedLight &l: lights)
289         {
290                 Renderable *shadow_caster = get_item(renderables, l.shadow_caster_name);
291                 if(!l.light || !shadow_caster)
292                         throw invalid_operation("ShadowMap::Template::create");
293                 if(const DirectionalLight *dir_light = dynamic_cast<const DirectionalLight *>(l.light))
294                         shadow_map->add_light(*dir_light, l.size, *shadow_caster);
295                 else if(const PointLight *point_light = dynamic_cast<const PointLight *>(l.light))
296                         shadow_map->add_light(*point_light, l.size, *shadow_caster);
297                 else
298                         throw invalid_operation("ShadowMap::Template::create");
299         }
300
301         create_base(*shadow_map);
302
303         return shadow_map.release();
304 }
305
306
307 DataFile::Loader::ActionMap ShadowMap::Template::Loader::shared_actions;
308
309 ShadowMap::Template::Loader::Loader(Template &t, Collection &c):
310         DerivedObjectLoader<Template, Effect::Template::Loader>(t, c)
311 {
312         set_actions(shared_actions);
313 }
314
315 void ShadowMap::Template::Loader::init_actions()
316 {
317         Effect::Template::Loader::init_actions();
318         add("darkness", &Template::darkness);
319         add("depth_bias", &Template::depth_bias);
320         add("light", &Loader::light);
321         add("lighting", &Template::lighting);
322         add("radius", &Template::radius);
323         add("size", &Loader::size_square);
324         add("size", &Template::width, &Template::height);
325         add("target", &Loader::target);
326 }
327
328 void ShadowMap::Template::Loader::light(const string &name)
329 {
330         ShadowedLight light;
331         light.light = &get_collection().get<Light>(name);
332         load_sub(light);
333         obj.lights.push_back(light);
334 }
335
336 void ShadowMap::Template::Loader::size_square(unsigned s)
337 {
338         obj.width = s;
339         obj.height = s;
340 }
341
342 void ShadowMap::Template::Loader::target(float x, float y, float z)
343 {
344         obj.target = Vector3(x, y, z);
345 }
346
347
348 DataFile::Loader::ActionMap ShadowMap::Template::ShadowedLight::Loader::shared_actions;
349
350 ShadowMap::Template::ShadowedLight::Loader::Loader(ShadowedLight &l):
351         ObjectLoader<ShadowedLight>(l)
352 {
353         set_actions(shared_actions);
354 }
355
356 void ShadowMap::Template::ShadowedLight::Loader::init_actions()
357 {
358         add("size", &ShadowedLight::size);
359         add("shadow_caster", &ShadowedLight::shadow_caster_name);
360 }
361
362 } // namespace GL
363 } // namespace Msp