19 ShadowMap::ShadowMap(unsigned s, const Renderable &r, const Light &l):
27 depth_buf.set_min_filter(LINEAR);
28 depth_buf.set_compare_enabled(true);
29 depth_buf.set_compare_func(LEQUAL);
30 depth_buf.set_wrap(CLAMP_TO_EDGE);
31 depth_buf.storage(DEPTH_COMPONENT, size, size);
32 fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
33 fbo.require_complete();
38 void ShadowMap::set_target(const Vector3 &t, float r)
44 void ShadowMap::set_darkness(float d)
47 throw invalid_argument("ShadowMap::set_darkness");
49 shdata.uniform("shadow_darkness", d);
52 void ShadowMap::set_depth_bias(float b)
55 throw invalid_argument("ShadowMap::set_depth_bias");
60 void ShadowMap::setup_frame() const
65 renderable.setup_frame();
68 const Vector4 &lpos = light.get_position();
72 /* XXX Not really proper way to support positional lights, but good
73 enough when the light source is far away */
74 back = Vector3(lpos)-target;
82 up = Vector3(0, 0, 1);
84 up = Vector3(0, 1, 0);
86 Vector3 right = normalize(cross(up, back));
89 columns[0] = Vector4(right, 0.0f);
90 columns[1] = Vector4(normalize(cross(back, right)), 0.0f);
91 columns[2] = Vector4(back, 0.0f);
92 columns[3] = Vector4(target, 1.0f);
93 light_matrix = Matrix::from_columns(columns);
94 view_matrix = invert(light_matrix);
96 MatrixStack::Push push_mv(MatrixStack::modelview());
97 MatrixStack::Push push_proj(MatrixStack::projection());
99 MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius);
100 MatrixStack::modelview() = view_matrix;
102 shadow_matrix = light_matrix;
103 shadow_matrix.scale(radius*2, radius*2, -radius*2);
104 shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
105 shadow_matrix.invert();
107 BindRestore bind_fbo(fbo);
108 Bind bind_depth(DepthTest::lequal());
109 fbo.clear(DEPTH_BUFFER_BIT);
110 renderable.render("shadow");
113 void ShadowMap::finish_frame() const
115 renderable.finish_frame();
119 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
121 if(!enabled_passes.count(tag))
122 return renderer.render(renderable, tag);
124 Renderer::Push _push_rend(renderer);
126 unsigned unit = renderer.allocate_effect_texunit();
128 shdata.uniform("shadow", iunit);
129 shdata.uniform("shadow_unit", iunit);
131 Bind _bind_depth(depth_buf, unit);
132 TexGen tg_s, tg_t, tg_r;
133 tg_s.set_plane(Vector4(shadow_matrix(0, 0), shadow_matrix(0, 1), shadow_matrix(0, 2), shadow_matrix(0, 3)));
134 tg_t.set_plane(Vector4(shadow_matrix(1, 0), shadow_matrix(1, 1), shadow_matrix(1, 2), shadow_matrix(1, 3)));
135 tg_r.set_plane(Vector4(shadow_matrix(2, 0), shadow_matrix(2, 1), shadow_matrix(2, 2), shadow_matrix(2, 3)));
136 tg_s.bind_to(unit, SCOORD);
137 tg_t.bind_to(unit, TCOORD);
138 tg_r.bind_to(unit, RCOORD);
140 if(const Camera *camera = renderer.get_camera())
141 /* Multiply by camera's object matrix to form a matrix that transforms
142 from eye space to shadow space. */
143 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
145 shdata.uniform("shd_eye_matrix", shadow_matrix);
147 renderer.add_shader_data(shdata);
148 renderer.render(renderable, tag);
150 TexGen::unbind_from(unit, SCOORD);
151 TexGen::unbind_from(unit, TCOORD);
152 TexGen::unbind_from(unit, RCOORD);