1 #include <msp/strings/format.h>
2 #include "directionallight.h"
14 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l, Renderable &c):
20 sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
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);
27 for(unsigned i=0; i<4; ++i)
29 string base = format("shadows[%d]", i);
30 shdata.uniform(base+".enabled", 0);
31 shdata.uniform(base+".darkness", 1.0f);
32 shdata.uniform(base+".shd_world_matrix", Matrix());
33 shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
37 ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
38 ShadowMap(s, s, r, 0, c)
43 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l, Renderable &c):
44 ShadowMap(w, h, r, &l, c)
47 void ShadowMap::add_light(const DirectionalLight &light, unsigned s)
49 if(!lighting && !lights.empty())
50 throw invalid_operation("ShadowMap::add_light");
52 int index = (lighting ? lighting->find_light_index(light) : 0);
54 throw invalid_argument("ShadowMap::add_light");
56 Rect region(0, 0, s, s);
59 int next_bottom = height;
60 int next_left = region.left;
62 int top = region.bottom+region.height;
63 int right = region.left+region.width;
64 for(const ShadowedLight &l: lights)
66 int l_top = l.region.bottom+l.region.height;
67 int l_right = l.region.left+l.region.width;
68 if(l_top>region.bottom)
69 next_bottom = min(next_bottom, l_top);
71 if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
72 next_left = max(next_left, l_right);
75 if(next_left==region.left)
77 else if(next_left+region.width>width)
79 if(next_bottom+region.height>height)
80 throw invalid_operation("ShadowMap::add_light");
81 region.bottom = next_bottom;
85 region.left = next_left;
88 lights.emplace_back();
89 ShadowedLight &sl = lights.back();
94 string base = format("shadows[%d]", index);
95 shdata.uniform(base+".enabled", 1);
96 shdata.uniform(base+".darkness", darkness);
98 float xf = static_cast<float>(region.left)/width;
99 float yf = static_cast<float>(region.bottom)/height;
100 float wf = static_cast<float>(region.width)/width;
101 float hf = static_cast<float>(region.height)/height;
102 shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
105 if(!debug_name.empty())
106 sl.shadow_camera.set_debug_name(format("%s/light%d.camera", debug_name, lights.size()-1));
110 void ShadowMap::set_target(const Vector3 &t, float r)
116 void ShadowMap::set_darkness(float d)
119 throw invalid_argument("ShadowMap::set_darkness");
122 for(const ShadowedLight &l: lights)
123 shdata.uniform(format("shadows[%d].darkness", l.index), d);
126 void ShadowMap::set_depth_bias(float b)
129 throw invalid_argument("ShadowMap::set_depth_bias");
134 void ShadowMap::setup_frame(Renderer &renderer)
140 renderable.setup_frame(renderer);
141 shadow_caster.setup_frame(renderer);
143 for(ShadowedLight &l: lights)
145 l.shadow_camera.set_object_matrix(*l.light->get_matrix());
146 l.shadow_camera.set_position(target);
147 // TODO support point and spot lights with a frustum projection.
148 // Omnidirectional lights also need a cube shadow map.
149 l.shadow_camera.set_orthographic(radius*2, radius*2);
150 l.shadow_camera.set_depth_clip(-radius, radius);
152 Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/l.region.width)).scale(0.5f);
153 Matrix shadow_matrix = to_texcoord*l.shadow_camera.get_projection_matrix()*l.shadow_camera.get_view_matrix();
155 shdata.uniform(format("shadows[%d].shd_world_matrix", l.index), shadow_matrix);
158 for(ShadowedLight &l: lights)
160 Renderer::Push push(renderer);
161 renderer.set_framebuffer(&fbo);
162 renderer.set_viewport(&l.region);
163 renderer.set_scissor(&l.region);
164 renderer.set_camera(l.shadow_camera);
166 renderer.render(shadow_caster);
170 void ShadowMap::finish_frame()
175 renderable.finish_frame();
179 void ShadowMap::render(Renderer &renderer, Tag tag) const
181 if(!enabled_passes.count(tag))
182 return renderer.render(renderable, tag);
184 Renderer::Push _push_rend(renderer);
186 renderer.set_texture("shadow_map", &depth_buf, &sampler);
187 renderer.add_shader_data(shdata);
188 renderer.render(renderable, tag);
191 void ShadowMap::set_debug_name(const string &name)
194 fbo.set_debug_name(name+" [FBO]");
195 for(unsigned i=0; i<lights.size(); ++i)
196 lights[i].shadow_camera.set_debug_name(format("%s/light%d.camera", name, i));
197 depth_buf.set_debug_name(name+"/depth.tex");
198 shdata.set_debug_name(name+" [UBO]");