16 ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l, Renderable &c):
20 sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
25 ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l):
29 sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
34 void ShadowMap::init(unsigned s)
41 depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
42 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
43 fbo.require_complete();
46 shdata.uniform("shd_world_matrix", Matrix());
49 void ShadowMap::set_target(const Vector3 &t, float r)
55 void ShadowMap::set_darkness(float d)
58 throw invalid_argument("ShadowMap::set_darkness");
60 shdata.uniform("shadow_darkness", d);
63 void ShadowMap::set_depth_bias(float b)
66 throw invalid_argument("ShadowMap::set_depth_bias");
71 void ShadowMap::setup_frame(Renderer &renderer)
77 renderable.setup_frame(renderer);
78 shadow_caster.setup_frame(renderer);
80 shadow_camera.set_object_matrix(*light.get_matrix());
81 shadow_camera.set_position(target);
82 // TODO support point and spot lights with a frustum projection.
83 // Omnidirectional lights also need a cube shadow map.
84 shadow_camera.set_orthographic(radius*2, radius*2);
85 shadow_camera.set_depth_clip(-radius, radius);
87 shadow_matrix = shadow_camera.get_object_matrix();
88 shadow_matrix.scale(radius*2, radius*2, -radius*2);
89 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
90 shadow_matrix.invert();
92 shdata.uniform("shd_world_matrix", shadow_matrix);
94 BindRestore bind_fbo(fbo);
95 Bind bind_depth(DepthTest::lequal());
96 fbo.clear(DEPTH_BUFFER_BIT);
98 Renderer::Push push(renderer);
99 renderer.set_camera(shadow_camera);
101 renderer.render(shadow_caster);
104 void ShadowMap::finish_frame()
109 renderable.finish_frame();
113 void ShadowMap::render(Renderer &renderer, Tag tag) const
115 if(!enabled_passes.count(tag))
116 return renderer.render(renderable, tag);
118 Renderer::Push _push_rend(renderer);
120 renderer.set_texture("shadow_map", &depth_buf, &sampler);
121 renderer.add_shader_data(shdata);
122 renderer.render(renderable, tag);