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));
33 shdata.uniform(base+".bias", 0.0f);
37 shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix);
40 ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
46 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
47 ShadowMap(w, h, r, &l)
50 void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c)
52 add_light(light, s, DIRECTIONAL, c);
55 void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c)
57 if(!lighting && !lights.empty())
58 throw invalid_operation("ShadowMap::add_light");
60 int index = (lighting ? lighting->find_light_index(light) : 0);
62 throw invalid_argument("ShadowMap::add_light");
64 Rect region(0, 0, s, s);
67 int next_bottom = height;
68 int next_left = region.left;
70 int top = region.bottom+region.height;
71 int right = region.left+region.width;
72 for(const ShadowedLight &l: lights)
74 int l_top = l.region.bottom+l.region.height;
75 int l_right = l.region.left+l.region.width;
76 if(l_top>region.bottom)
77 next_bottom = min(next_bottom, l_top);
79 if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
80 next_left = max(next_left, l_right);
83 if(next_left==region.left)
85 else if(next_left+region.width>width)
87 if(next_bottom+region.height>height)
88 throw invalid_operation("ShadowMap::add_light");
89 region.bottom = next_bottom;
93 region.left = next_left;
96 lights.emplace_back();
97 ShadowedLight &sl = lights.back();
102 sl.view_index = views.size();
103 sl.shadow_caster = &c;
105 views.emplace_back();
106 ShadowView &view = views[sl.view_index];
107 view.light_index = lights.size()-1;
110 string base = format("shadows[%d]", index);
111 shdata.uniform(base+".type", static_cast<int>(type));
112 shdata.uniform(base+".darkness", darkness);
113 shdata.uniform(base+".matrix_index", static_cast<int>(sl.view_index));
115 float xf = static_cast<float>(region.left)/width;
116 float yf = static_cast<float>(region.bottom)/height;
117 float wf = static_cast<float>(region.width)/width;
118 float hf = static_cast<float>(region.height)/height;
119 shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
122 if(!debug_name.empty())
124 for(unsigned i=sl.view_index; i<views.size(); ++i)
125 views[i].camera.set_debug_name(format("%s/view%d.camera", debug_name, i));
130 void ShadowMap::set_target(const Vector3 &t, float r)
136 void ShadowMap::set_darkness(float d)
139 throw invalid_argument("ShadowMap::set_darkness");
142 for(const ShadowedLight &l: lights)
143 shdata.uniform(format("shadows[%d].darkness", l.index), d);
146 void ShadowMap::set_depth_bias(float b)
149 throw invalid_argument("ShadowMap::set_depth_bias");
154 void ShadowMap::setup_frame(Renderer &renderer)
160 renderable.setup_frame(renderer);
161 for(const ShadowedLight &l: lights)
162 l.shadow_caster->setup_frame(renderer);
164 for(const ShadowedLight &l: lights)
166 string base = format("shadows[%d]", l.index);
167 if(l.type==DIRECTIONAL)
168 shdata.uniform(base+".bias", depth_bias/l.region.width);
171 vector<Matrix> shadow_matrices;
172 shadow_matrices.reserve(views.size());
173 for(ShadowView &v: views)
175 const ShadowedLight &light = lights[v.light_index];
177 if(light.type==DIRECTIONAL)
179 v.camera.set_object_matrix(*light.light->get_matrix());
180 v.camera.set_position(target);
181 v.camera.set_orthographic(radius*2, radius*2);
182 v.camera.set_depth_clip(-radius, radius);
185 Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f)).scale(0.5f);
186 shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix());
189 shdata.uniform_array("shd_world_matrix", shadow_matrices.size(), shadow_matrices.data());
191 for(const ShadowView &v: views)
193 const ShadowedLight &light = lights[v.light_index];
195 Renderer::Push push(renderer);
196 renderer.set_framebuffer(&fbo);
197 renderer.set_viewport(&light.region);
198 renderer.set_scissor(&light.region);
199 renderer.set_camera(v.camera);
201 renderer.render(*light.shadow_caster, (v.face>0 ? "noclear" : ""));
205 void ShadowMap::finish_frame()
210 renderable.finish_frame();
214 void ShadowMap::render(Renderer &renderer, Tag tag) const
216 if(!enabled_passes.count(tag))
217 return renderer.render(renderable, tag);
219 Renderer::Push _push_rend(renderer);
221 renderer.set_texture("shadow_map", &depth_buf, &sampler);
222 renderer.add_shader_data(shdata);
223 renderer.render(renderable, tag);
226 void ShadowMap::set_debug_name(const string &name)
229 fbo.set_debug_name(name+" [FBO]");
230 for(unsigned i=0; i<views.size(); ++i)
231 views[i].camera.set_debug_name(format("%s/view%d.camera", name, i));
232 depth_buf.set_debug_name(name+"/depth.tex");
233 shdata.set_debug_name(name+" [UBO]");