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")),
26 depth_buf.storage(DEPTH_COMPONENT32F, width, height, 1);
27 fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
28 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
31 for(unsigned i=0; i<4; ++i)
33 string base = format("shadows[%d]", i);
34 shdata.uniform(base+".enabled", 0);
35 shdata.uniform(base+".darkness", 1.0f);
36 shdata.uniform(base+".shd_world_matrix", Matrix());
37 shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
41 ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
42 ShadowMap(s, s, r, 0, c)
47 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l, Renderable &c):
48 ShadowMap(w, h, r, &l, c)
51 void ShadowMap::add_light(const DirectionalLight &light, unsigned s)
53 if(!lighting && !lights.empty())
54 throw invalid_operation("ShadowMap::add_light");
56 int index = (lighting ? lighting->find_light_index(light) : 0);
58 throw invalid_argument("ShadowMap::add_light");
60 Rect region(0, 0, s, s);
63 int next_bottom = height;
64 int next_left = region.left;
66 int top = region.bottom+region.height;
67 int right = region.left+region.width;
68 for(const ShadowedLight &l: lights)
70 int l_top = l.region.bottom+l.region.height;
71 int l_right = l.region.left+l.region.width;
72 if(l_top>region.bottom)
73 next_bottom = min(next_bottom, l_top);
75 if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
76 next_left = max(next_left, l_right);
79 if(next_left==region.left)
81 else if(next_left+region.width>width)
83 if(next_bottom+region.height>height)
84 throw invalid_operation("ShadowMap::add_light");
85 region.bottom = next_bottom;
89 region.left = next_left;
92 lights.emplace_back();
93 ShadowedLight &sl = lights.back();
98 string base = format("shadows[%d]", index);
99 shdata.uniform(base+".enabled", 1);
100 shdata.uniform(base+".darkness", darkness);
102 float xf = static_cast<float>(region.left)/width;
103 float yf = static_cast<float>(region.bottom)/height;
104 float wf = static_cast<float>(region.width)/width;
105 float hf = static_cast<float>(region.height)/height;
106 shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
109 if(!debug_name.empty())
110 sl.shadow_camera.set_debug_name(format("%s/light%d.camera", debug_name, lights.size()-1));
114 void ShadowMap::set_target(const Vector3 &t, float r)
120 void ShadowMap::set_darkness(float d)
123 throw invalid_argument("ShadowMap::set_darkness");
126 for(const ShadowedLight &l: lights)
127 shdata.uniform(format("shadows[%d].darkness", l.index), d);
130 void ShadowMap::set_depth_bias(float b)
133 throw invalid_argument("ShadowMap::set_depth_bias");
138 void ShadowMap::setup_frame(Renderer &renderer)
144 renderable.setup_frame(renderer);
145 shadow_caster.setup_frame(renderer);
147 for(ShadowedLight &l: lights)
149 l.shadow_camera.set_object_matrix(*l.light->get_matrix());
150 l.shadow_camera.set_position(target);
151 // TODO support point and spot lights with a frustum projection.
152 // Omnidirectional lights also need a cube shadow map.
153 l.shadow_camera.set_orthographic(radius*2, radius*2);
154 l.shadow_camera.set_depth_clip(-radius, radius);
156 Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/l.region.width)).scale(0.5f);
157 Matrix shadow_matrix = to_texcoord*l.shadow_camera.get_projection_matrix()*l.shadow_camera.get_view_matrix();
159 shdata.uniform(format("shadows[%d].shd_world_matrix", l.index), shadow_matrix);
162 for(ShadowedLight &l: lights)
164 Renderer::Push push(renderer);
165 renderer.set_framebuffer(&fbo);
166 renderer.set_viewport(&l.region);
167 renderer.set_scissor(&l.region);
168 renderer.set_camera(l.shadow_camera);
170 renderer.render(shadow_caster);
174 void ShadowMap::finish_frame()
179 renderable.finish_frame();
183 void ShadowMap::render(Renderer &renderer, Tag tag) const
185 if(!enabled_passes.count(tag))
186 return renderer.render(renderable, tag);
188 Renderer::Push _push_rend(renderer);
190 renderer.set_texture("shadow_map", &depth_buf, &sampler);
191 renderer.add_shader_data(shdata);
192 renderer.render(renderable, tag);
195 void ShadowMap::set_debug_name(const string &name)
198 fbo.set_debug_name(name+" [FBO]");
199 for(unsigned i=0; i<lights.size(); ++i)
200 lights[i].shadow_camera.set_debug_name(format("%s/light%d.camera", name, i));
201 depth_buf.set_debug_name(name+"/depth.tex2d");
202 shdata.set_debug_name(name+" [UBO]");