18 ShadowMap::ShadowMap(unsigned s, const 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() const
64 renderable.setup_frame();
67 const Vector4 &lpos = light.get_position();
71 /* XXX Not really proper way to support positional lights, but good
72 enough when the light source is far away */
73 back = Vector3(lpos)-target;
81 up = Vector3(0, 0, 1);
83 up = Vector3(0, 1, 0);
85 Vector3 right = normalize(cross(up, back));
88 columns[0] = Vector4(right, 0.0f);
89 columns[1] = Vector4(normalize(cross(back, right)), 0.0f);
90 columns[2] = Vector4(back, 0.0f);
91 columns[3] = Vector4(target, 1.0f);
92 light_matrix = Matrix::from_columns(columns);
93 view_matrix = invert(light_matrix);
95 MatrixStack::Push push_mv(MatrixStack::modelview());
96 MatrixStack::Push push_proj(MatrixStack::projection());
98 MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius);
99 MatrixStack::modelview() = view_matrix;
101 shadow_matrix = light_matrix;
102 shadow_matrix.scale(radius*2, radius*2, -radius*2);
103 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
104 shadow_matrix.invert();
106 BindRestore bind_fbo(fbo);
107 Bind bind_depth(DepthTest::lequal());
108 fbo.clear(DEPTH_BUFFER_BIT);
109 renderable.render("shadow");
112 void ShadowMap::finish_frame() const
114 renderable.finish_frame();
118 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
120 if(!enabled_passes.count(tag))
121 return renderer.render(renderable, tag);
123 Renderer::Push _push_rend(renderer);
125 unsigned unit = renderer.allocate_effect_texunit();
127 shdata.uniform("shadow", iunit);
129 Bind _bind_depth(depth_buf, unit);
131 if(const Camera *camera = renderer.get_camera())
132 /* Multiply by camera's object matrix to form a matrix that transforms
133 from eye space to shadow space. */
134 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
136 shdata.uniform("shd_eye_matrix", shadow_matrix);
138 renderer.add_shader_data(shdata);
139 renderer.render(renderable, tag);