15 ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l, Renderable &c):
20 sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp")),
25 depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
26 fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
27 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
29 depth_test.enabled = true;
30 depth_test.compare = LEQUAL;
33 shdata.uniform("shd_world_matrix", Matrix());
36 void ShadowMap::set_target(const Vector3 &t, float r)
42 void ShadowMap::set_darkness(float d)
45 throw invalid_argument("ShadowMap::set_darkness");
47 shdata.uniform("shadow_darkness", d);
50 void ShadowMap::set_depth_bias(float b)
53 throw invalid_argument("ShadowMap::set_depth_bias");
58 void ShadowMap::setup_frame(Renderer &renderer)
64 renderable.setup_frame(renderer);
65 shadow_caster.setup_frame(renderer);
67 shadow_camera.set_object_matrix(*light.get_matrix());
68 shadow_camera.set_position(target);
69 // TODO support point and spot lights with a frustum projection.
70 // Omnidirectional lights also need a cube shadow map.
71 shadow_camera.set_orthographic(radius*2, radius*2);
72 shadow_camera.set_depth_clip(-radius, radius);
74 shadow_matrix = shadow_camera.get_object_matrix();
75 shadow_matrix.scale(radius*2, radius*2, -radius*2);
76 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
77 shadow_matrix.invert();
79 shdata.uniform("shd_world_matrix", shadow_matrix);
81 Renderer::Push push(renderer);
82 renderer.set_framebuffer(&fbo);
83 renderer.clear(DEPTH_BUFFER_BIT);
84 renderer.set_camera(shadow_camera);
85 renderer.set_depth_test(&depth_test);
87 renderer.render(shadow_caster);
90 void ShadowMap::finish_frame()
95 renderable.finish_frame();
99 void ShadowMap::render(Renderer &renderer, Tag tag) const
101 if(!enabled_passes.count(tag))
102 return renderer.render(renderable, tag);
104 Renderer::Push _push_rend(renderer);
106 renderer.set_texture("shadow_map", &depth_buf, &sampler);
107 renderer.add_shader_data(shdata);
108 renderer.render(renderable, tag);
111 void ShadowMap::set_debug_name(const std::string &name)
114 fbo.set_debug_name(name+" [FBO]");
115 shadow_camera.set_debug_name(name+".camera");
116 depth_buf.set_debug_name(name+"/depth.tex2d");
117 shdata.set_debug_name(name+" [UBO]");