16 ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l):
20 sampler(resources.get<Sampler>("_linear_clamp_shadow.samp")),
25 depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
26 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
27 fbo.require_complete();
32 void ShadowMap::set_target(const Vector3 &t, float r)
38 void ShadowMap::set_darkness(float d)
41 throw invalid_argument("ShadowMap::set_darkness");
43 shdata.uniform("shadow_darkness", d);
46 void ShadowMap::set_depth_bias(float b)
49 throw invalid_argument("ShadowMap::set_depth_bias");
54 void ShadowMap::setup_frame(Renderer &renderer)
60 renderable.setup_frame(renderer);
63 camera.set_object_matrix(*light.get_matrix());
64 camera.set_position(target);
65 // TODO support point and spot lights with a frustum projection.
66 // Omnidirectional lights also need a cube shadow map.
67 camera.set_orthographic(radius*2, radius*2);
68 camera.set_depth_clip(-radius, radius);
70 shadow_matrix = camera.get_object_matrix();
71 shadow_matrix.scale(radius*2, radius*2, -radius*2);
72 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
73 shadow_matrix.invert();
75 BindRestore bind_fbo(fbo);
76 Bind bind_depth(DepthTest::lequal());
77 fbo.clear(DEPTH_BUFFER_BIT);
79 Renderer::Push push(renderer);
80 renderer.set_camera(camera);
82 renderer.render(renderable, "shadow");
85 void ShadowMap::finish_frame()
87 renderable.finish_frame();
91 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
93 if(!enabled_passes.count(tag))
94 return renderer.render(renderable, tag);
96 Renderer::Push _push_rend(renderer);
98 unsigned unit = renderer.allocate_effect_texunit();
100 shdata.uniform("shadow_map", iunit);
102 Bind _bind_sampler(sampler, unit);
103 Bind _bind_depth(depth_buf, unit);
105 if(const Camera *camera = renderer.get_camera())
106 /* Multiply by camera's object matrix to form a matrix that transforms
107 from eye space to shadow space. */
108 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
110 shdata.uniform("shd_eye_matrix", shadow_matrix);
112 renderer.add_shader_data(shdata);
113 renderer.render(renderable, tag);