1 #include <msp/strings/format.h>
2 #include "directionallight.h"
14 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l):
19 sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
21 depth_buf.storage(DEPTH_COMPONENT32F, width, height, 1);
22 fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
23 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
26 for(unsigned i=0; i<4; ++i)
28 string base = format("shadows[%d]", i);
29 shdata.uniform(base+".type", 0);
30 shdata.uniform(base+".darkness", 1.0f);
31 shdata.uniform(base+".matrix_index", 0);
32 shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
36 shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix);
39 ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
45 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
46 ShadowMap(w, h, r, &l)
49 void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c)
51 add_light(light, s, DIRECTIONAL, c);
54 void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c)
56 if(!lighting && !lights.empty())
57 throw invalid_operation("ShadowMap::add_light");
59 int index = (lighting ? lighting->find_light_index(light) : 0);
61 throw invalid_argument("ShadowMap::add_light");
63 Rect region(0, 0, s, s);
66 int next_bottom = height;
67 int next_left = region.left;
69 int top = region.bottom+region.height;
70 int right = region.left+region.width;
71 for(const ShadowedLight &l: lights)
73 int l_top = l.region.bottom+l.region.height;
74 int l_right = l.region.left+l.region.width;
75 if(l_top>region.bottom)
76 next_bottom = min(next_bottom, l_top);
78 if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
79 next_left = max(next_left, l_right);
82 if(next_left==region.left)
84 else if(next_left+region.width>width)
86 if(next_bottom+region.height>height)
87 throw invalid_operation("ShadowMap::add_light");
88 region.bottom = next_bottom;
92 region.left = next_left;
95 lights.emplace_back();
96 ShadowedLight &sl = lights.back();
101 sl.view_index = views.size();
102 sl.shadow_caster = &c;
104 views.emplace_back();
105 ShadowView &view = views[sl.view_index];
106 view.light_index = lights.size()-1;
109 string base = format("shadows[%d]", index);
110 shdata.uniform(base+".type", static_cast<int>(type));
111 shdata.uniform(base+".darkness", darkness);
112 shdata.uniform(base+".matrix_index", static_cast<int>(sl.view_index));
114 float xf = static_cast<float>(region.left)/width;
115 float yf = static_cast<float>(region.bottom)/height;
116 float wf = static_cast<float>(region.width)/width;
117 float hf = static_cast<float>(region.height)/height;
118 shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
121 if(!debug_name.empty())
123 for(unsigned i=sl.view_index; i<views.size(); ++i)
124 views[i].camera.set_debug_name(format("%s/view%d.camera", debug_name, i));
129 void ShadowMap::set_target(const Vector3 &t, float r)
135 void ShadowMap::set_darkness(float d)
138 throw invalid_argument("ShadowMap::set_darkness");
141 for(const ShadowedLight &l: lights)
142 shdata.uniform(format("shadows[%d].darkness", l.index), d);
145 void ShadowMap::set_depth_bias(float b)
148 throw invalid_argument("ShadowMap::set_depth_bias");
153 void ShadowMap::setup_frame(Renderer &renderer)
159 renderable.setup_frame(renderer);
160 for(const ShadowedLight &l: lights)
161 l.shadow_caster->setup_frame(renderer);
163 vector<Matrix> shadow_matrices;
164 shadow_matrices.reserve(views.size());
165 for(ShadowView &v: views)
167 const ShadowedLight &light = lights[v.light_index];
169 if(light.type==DIRECTIONAL)
171 v.camera.set_object_matrix(*light.light->get_matrix());
172 v.camera.set_position(target);
173 v.camera.set_orthographic(radius*2, radius*2);
174 v.camera.set_depth_clip(-radius, radius);
177 Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/light.region.width)).scale(0.5f);
178 shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix());
181 shdata.uniform_array("shd_world_matrix", shadow_matrices.size(), shadow_matrices.data());
183 for(const ShadowView &v: views)
185 const ShadowedLight &light = lights[v.light_index];
187 Renderer::Push push(renderer);
188 renderer.set_framebuffer(&fbo);
189 renderer.set_viewport(&light.region);
190 renderer.set_scissor(&light.region);
191 renderer.set_camera(v.camera);
193 renderer.render(*light.shadow_caster, (v.face>0 ? "noclear" : ""));
197 void ShadowMap::finish_frame()
202 renderable.finish_frame();
206 void ShadowMap::render(Renderer &renderer, Tag tag) const
208 if(!enabled_passes.count(tag))
209 return renderer.render(renderable, tag);
211 Renderer::Push _push_rend(renderer);
213 renderer.set_texture("shadow_map", &depth_buf, &sampler);
214 renderer.add_shader_data(shdata);
215 renderer.render(renderable, tag);
218 void ShadowMap::set_debug_name(const string &name)
221 fbo.set_debug_name(name+" [FBO]");
222 for(unsigned i=0; i<views.size(); ++i)
223 views[i].camera.set_debug_name(format("%s/view%d.camera", name, i));
224 depth_buf.set_debug_name(name+"/depth.tex");
225 shdata.set_debug_name(name+" [UBO]");