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);
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 const Vector4 &lpos = light.get_position();
66 /* XXX Not really proper way to support positional lights, but good
67 enough when the light source is far away */
68 camera.set_look_direction(lpos.w*target-lpos.slice<3>(0));
70 camera.set_up_direction((abs(camera.get_look_direction().z)<0.99) ? Vector3(0, 0, 1) : Vector3(0, 1, 0));
71 camera.set_position(target);
72 camera.set_orthographic(radius*2, radius*2);
73 camera.set_depth_clip(-radius, radius);
75 shadow_matrix = camera.get_object_matrix();
76 shadow_matrix.scale(radius*2, radius*2, -radius*2);
77 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
78 shadow_matrix.invert();
80 BindRestore bind_fbo(fbo);
81 Bind bind_depth(DepthTest::lequal());
82 fbo.clear(DEPTH_BUFFER_BIT);
84 Renderer::Push push(renderer);
85 renderer.set_camera(camera);
87 renderer.render(renderable, "shadow");
90 void ShadowMap::finish_frame()
92 renderable.finish_frame();
96 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
98 if(!enabled_passes.count(tag))
99 return renderer.render(renderable, tag);
101 Renderer::Push _push_rend(renderer);
103 unsigned unit = renderer.allocate_effect_texunit();
105 shdata.uniform("shadow", iunit);
107 Bind _bind_depth(depth_buf, unit);
109 if(const Camera *camera = renderer.get_camera())
110 /* Multiply by camera's object matrix to form a matrix that transforms
111 from eye space to shadow space. */
112 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
114 shdata.uniform("shd_eye_matrix", shadow_matrix);
116 renderer.add_shader_data(shdata);
117 renderer.render(renderable, tag);