1 #include <msp/strings/format.h>
2 #include "directionallight.h"
5 #include "pointlight.h"
15 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &c, const Lighting *l):
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<6; ++i)
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);
38 shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix);
41 ShadowMap::ShadowMap(unsigned s, Renderable &c, const DirectionalLight &l, Renderable &sc):
47 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
48 ShadowMap(w, h, r, &l)
51 void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c)
53 add_light(light, s, DIRECTIONAL, c);
56 void ShadowMap::add_light(const PointLight &light, unsigned s, Renderable &c)
58 add_light(light, s, TETRAHEDRON, c);
61 void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c)
63 if(!lighting && !lights.empty())
64 throw invalid_operation("ShadowMap::add_light");
66 int index = (lighting ? lighting->find_light_index(light) : 0);
68 throw invalid_argument("ShadowMap::add_light");
70 Rect region(0, 0, s, s);
73 int next_bottom = height;
74 int next_left = region.left;
76 int top = region.bottom+region.height;
77 int right = region.left+region.width;
78 for(const ShadowedLight &l: lights)
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);
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);
89 if(next_left==region.left)
91 else if(next_left+region.width>width)
93 if(next_bottom+region.height>height)
94 throw invalid_operation("ShadowMap::add_light");
95 region.bottom = next_bottom;
99 region.left = next_left;
102 unsigned n_views = (type==TETRAHEDRON ? 4 : 1);
104 lights.emplace_back();
105 ShadowedLight &sl = lights.back();
110 sl.view_index = views.size();
111 sl.shadow_caster = &c;
113 views.resize(views.size()+n_views);
114 for(unsigned i=0; i<n_views; ++i)
116 ShadowView &view = views[sl.view_index+i];
117 view.light_index = lights.size()-1;
120 if(type==TETRAHEDRON)
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));
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));
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));
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));
148 if(!debug_name.empty())
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));
156 void ShadowMap::set_target(const Vector3 &t, float r)
162 void ShadowMap::set_darkness(float d)
165 throw invalid_argument("ShadowMap::set_darkness");
168 for(const ShadowedLight &l: lights)
169 shdata.uniform(format("shadows[%d].darkness", l.index), d);
172 void ShadowMap::set_depth_bias(float b)
175 throw invalid_argument("ShadowMap::set_depth_bias");
180 void ShadowMap::setup_frame(Renderer &renderer)
186 content.setup_frame(renderer);
187 for(const ShadowedLight &l: lights)
188 l.shadow_caster->setup_frame(renderer);
190 for(const ShadowedLight &l: lights)
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)
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);
203 vector<Matrix> shadow_matrices;
204 shadow_matrices.reserve(views.size());
205 for(ShadowView &v: views)
207 const ShadowedLight &light = lights[v.light_index];
209 if(light.type==DIRECTIONAL)
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);
216 else if(light.type==TETRAHEDRON)
218 v.camera.set_object_matrix((*light.light->get_matrix())*v.face_matrix);
219 v.camera.set_depth_clip(radius/1000.0f, radius);
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());
226 shdata.uniform_array("shd_world_matrix", shadow_matrices.size(), shadow_matrices.data());
228 for(const ShadowView &v: views)
230 const ShadowedLight &light = lights[v.light_index];
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);
238 light.shadow_caster->render(renderer, (v.face>0 ? "noclear" : ""));
242 void ShadowMap::finish_frame()
247 content.finish_frame();
251 void ShadowMap::render(Renderer &renderer, Tag tag) const
253 if(!is_enabled_for_method(tag))
254 return content.render(renderer, tag);
256 Renderer::Push _push_rend(renderer);
258 renderer.set_texture("shadow_map", &depth_buf, &sampler);
259 renderer.add_shader_data(shdata);
260 content.render(renderer, tag);
263 void ShadowMap::set_debug_name(const string &name)
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]");
277 ShadowMap *ShadowMap::Template::create(const map<string, Renderable *> &renderables) const
279 Renderable *content = get_item(renderables, content_name);
280 if(!content || !lighting)
281 throw invalid_operation("ShadowMap::Template::create");
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);
288 for(const ShadowedLight &l: lights)
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);
298 throw invalid_operation("ShadowMap::Template::create");
301 create_base(*shadow_map);
303 return shadow_map.release();
307 DataFile::Loader::ActionMap ShadowMap::Template::Loader::shared_actions;
309 ShadowMap::Template::Loader::Loader(Template &t, Collection &c):
310 DerivedObjectLoader<Template, Effect::Template::Loader>(t, c)
312 set_actions(shared_actions);
315 void ShadowMap::Template::Loader::init_actions()
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);
328 void ShadowMap::Template::Loader::light(const string &name)
331 light.light = &get_collection().get<Light>(name);
333 obj.lights.push_back(light);
336 void ShadowMap::Template::Loader::size_square(unsigned s)
342 void ShadowMap::Template::Loader::target(float x, float y, float z)
344 obj.target = Vector3(x, y, z);
348 DataFile::Loader::ActionMap ShadowMap::Template::ShadowedLight::Loader::shared_actions;
350 ShadowMap::Template::ShadowedLight::Loader::Loader(ShadowedLight &l):
351 ObjectLoader<ShadowedLight>(l)
353 set_actions(shared_actions);
356 void ShadowMap::Template::ShadowedLight::Loader::init_actions()
358 add("size", &ShadowedLight::size);
359 add("shadow_caster", &ShadowedLight::shadow_caster_name);