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);
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::set_texture_unit(unsigned u)
64 shdata.uniform("shadow", i);
65 shdata.uniform("shadow_unit", i);
68 void ShadowMap::setup_frame() const
73 renderable.setup_frame();
76 Vector4 lpos = light.get_position();
80 /* XXX Not really proper way to support positional lights, but good
81 enough when the light source is far away */
87 float l = sqrt(lpos.x*lpos.x+lpos.y*lpos.y+lpos.z*lpos.z);
93 if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y))
95 float d = sqrt(lpos.x*lpos.x+lpos.z*lpos.z);
98 matrix[8] = -lpos.x/d;
99 matrix[1] = -lpos.x*lpos.y/d;
101 matrix[9] = -lpos.z*lpos.y/d;
105 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y);
106 matrix[0] = -lpos.y/d;
107 matrix[4] = lpos.x/d;
109 matrix[1] = -lpos.x*lpos.z/d;
110 matrix[5] = -lpos.y*lpos.z/d;
118 matrix[12] = -(target.x*matrix[0]+target.y*matrix[4]+target.z*matrix[8]);
119 matrix[13] = -(target.x*matrix[1]+target.y*matrix[5]+target.z*matrix[9]);
120 matrix[14] = -(target.x*matrix[2]+target.y*matrix[6]+target.z*matrix[10]);
126 light_matrix = matrix;
128 MatrixStack::Push push_mv(MatrixStack::modelview());
129 MatrixStack::Push push_proj(MatrixStack::projection());
131 MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius);
132 MatrixStack::modelview() = light_matrix;
134 Bind bind_fbo(fbo, true);
135 Bind bind_depth(DepthTest::lequal());
136 fbo.clear(DEPTH_BUFFER_BIT);
137 renderable.render("shadow");
140 void ShadowMap::finish_frame() const
142 renderable.finish_frame();
146 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
148 if(!enabled_passes.count(tag))
149 return renderer.render(renderable, tag);
151 const double *matrix = light_matrix.data();
153 // Has side effect of changing the current unit
154 depth_buf.bind_to(unit);
155 float diam = radius*2;
156 TexGen tg_s, tg_t, tg_r;
157 tg_s.set_plane(Vector4(matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5f));
158 tg_t.set_plane(Vector4(matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5f));
159 tg_r.set_plane(Vector4(-matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5f-matrix[14]/diam-depth_bias/size));
160 tg_s.bind_to(SCOORD);
161 tg_t.bind_to(TCOORD);
162 tg_r.bind_to(RCOORD);
163 TexUnit::activate(0);
165 Renderer::Push _push_rend(renderer);
166 renderer.add_shader_data(shdata);
167 renderer.render(renderable, tag);
169 Texture::unbind_from(unit);
170 TexGen::unbind_from(SCOORD);
171 TexGen::unbind_from(TCOORD);
172 TexGen::unbind_from(RCOORD);
173 TexUnit::activate(0);