18 ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l):
26 depth_buf.set_min_filter(LINEAR);
27 depth_buf.set_compare_enabled(true);
28 depth_buf.set_compare_func(LEQUAL);
29 depth_buf.set_wrap(CLAMP_TO_EDGE);
30 depth_buf.storage(DEPTH_COMPONENT, size, size);
31 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
32 fbo.require_complete();
37 void ShadowMap::set_target(const Vector3 &t, float r)
43 void ShadowMap::set_darkness(float d)
46 throw invalid_argument("ShadowMap::set_darkness");
48 shdata.uniform("shadow_darkness", d);
51 void ShadowMap::set_depth_bias(float b)
54 throw invalid_argument("ShadowMap::set_depth_bias");
59 void ShadowMap::setup_frame(Renderer &renderer)
65 renderable.setup_frame(renderer);
68 const Vector4 &lpos = light.get_position();
69 /* XXX Not really proper way to support positional lights, but good
70 enough when the light source is far away */
71 camera.set_look_direction(lpos.w*target-lpos.slice<3>(0));
73 camera.set_up_direction((abs(camera.get_look_direction().z)<0.99) ? Vector3(0, 0, 1) : Vector3(0, 1, 0));
74 camera.set_position(target);
75 camera.set_orthographic(radius*2, radius*2);
76 camera.set_depth_clip(-radius, radius);
78 shadow_matrix = camera.get_object_matrix();
79 shadow_matrix.scale(radius*2, radius*2, -radius*2);
80 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
81 shadow_matrix.invert();
83 BindRestore bind_fbo(fbo);
84 Bind bind_depth(DepthTest::lequal());
85 fbo.clear(DEPTH_BUFFER_BIT);
87 Renderer::Push push(renderer);
88 renderer.set_camera(camera);
90 renderer.render(renderable, "shadow");
93 void ShadowMap::finish_frame()
95 renderable.finish_frame();
99 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
101 if(!enabled_passes.count(tag))
102 return renderer.render(renderable, tag);
104 Renderer::Push _push_rend(renderer);
106 unsigned unit = renderer.allocate_effect_texunit();
108 shdata.uniform("shadow", iunit);
110 Bind _bind_depth(depth_buf, unit);
112 if(const Camera *camera = renderer.get_camera())
113 /* Multiply by camera's object matrix to form a matrix that transforms
114 from eye space to shadow space. */
115 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
117 shdata.uniform("shd_eye_matrix", shadow_matrix);
119 renderer.add_shader_data(shdata);
120 renderer.render(renderable, tag);