15 ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l):
23 depth_buf.set_min_filter(LINEAR);
24 depth_buf.set_compare_enabled(true);
25 depth_buf.set_compare_func(LEQUAL);
26 depth_buf.set_wrap(CLAMP_TO_EDGE);
27 depth_buf.storage(DEPTH_COMPONENT, size, size, 1);
28 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
29 fbo.require_complete();
34 void ShadowMap::set_target(const Vector3 &t, float r)
40 void ShadowMap::set_darkness(float d)
43 throw invalid_argument("ShadowMap::set_darkness");
45 shdata.uniform("shadow_darkness", d);
48 void ShadowMap::set_depth_bias(float b)
51 throw invalid_argument("ShadowMap::set_depth_bias");
56 void ShadowMap::setup_frame(Renderer &renderer)
62 renderable.setup_frame(renderer);
65 camera.set_object_matrix(*light.get_matrix());
66 camera.set_position(target);
67 // TODO support point and spot lights with a frustum projection.
68 // Omnidirectional lights also need a cube shadow map.
69 camera.set_orthographic(radius*2, radius*2);
70 camera.set_depth_clip(-radius, radius);
72 shadow_matrix = camera.get_object_matrix();
73 shadow_matrix.scale(radius*2, radius*2, -radius*2);
74 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
75 shadow_matrix.invert();
77 BindRestore bind_fbo(fbo);
78 Bind bind_depth(DepthTest::lequal());
79 fbo.clear(DEPTH_BUFFER_BIT);
81 Renderer::Push push(renderer);
82 renderer.set_camera(camera);
84 renderer.render(renderable, "shadow");
87 void ShadowMap::finish_frame()
89 renderable.finish_frame();
93 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
95 if(!enabled_passes.count(tag))
96 return renderer.render(renderable, tag);
98 Renderer::Push _push_rend(renderer);
100 unsigned unit = renderer.allocate_effect_texunit();
102 shdata.uniform("shadow_map", iunit);
104 Bind _bind_depth(depth_buf, unit);
106 if(const Camera *camera = renderer.get_camera())
107 /* Multiply by camera's object matrix to form a matrix that transforms
108 from eye space to shadow space. */
109 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
111 shdata.uniform("shd_eye_matrix", shadow_matrix);
113 renderer.add_shader_data(shdata);
114 renderer.render(renderable, tag);