+ set_darkness(1.0f);
+ for(unsigned i=0; i<6; ++i)
+ {
+ string base = format("shadows[%d]", i);
+ shdata.uniform(base+".type", 0);
+ shdata.uniform(base+".darkness", 1.0f);
+ shdata.uniform(base+".matrix_index", 0);
+ shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
+ shdata.uniform(base+".bias", 0.0f, 1.0f);
+ }
+
+ Matrix dummy_matrix;
+ shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix);
+}
+
+ShadowMap::ShadowMap(unsigned s, Renderable &c, const DirectionalLight &l, Renderable &sc):
+ ShadowMap(s, s, c, 0)
+{
+ add_light(l, s, sc);
+}
+
+ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
+ ShadowMap(w, h, r, &l)
+{ }
+
+void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c)
+{
+ add_light(light, s, DIRECTIONAL, c);
+}
+
+void ShadowMap::add_light(const PointLight &light, unsigned s, Renderable &c)
+{
+ add_light(light, s, TETRAHEDRON, c);
+}
+
+void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c)
+{
+ if(!lighting && !lights.empty())
+ throw invalid_operation("ShadowMap::add_light");
+
+ int index = (lighting ? lighting->find_light_index(light) : 0);
+ if(index<0)
+ throw invalid_argument("ShadowMap::add_light");
+
+ Rect region(0, 0, s, s);
+ while(1)
+ {
+ int next_bottom = height;
+ int next_left = region.left;
+
+ int top = region.bottom+region.height;
+ int right = region.left+region.width;
+ for(const ShadowedLight &l: lights)
+ {
+ int l_top = l.region.bottom+l.region.height;
+ int l_right = l.region.left+l.region.width;
+ if(l_top>region.bottom)
+ next_bottom = min(next_bottom, l_top);
+
+ if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
+ next_left = max(next_left, l_right);
+ }
+
+ if(next_left==region.left)
+ break;
+ else if(next_left+region.width>width)
+ {
+ if(next_bottom+region.height>height)
+ throw invalid_operation("ShadowMap::add_light");
+ region.bottom = next_bottom;
+ region.left = 0;
+ }
+ else
+ region.left = next_left;
+ }
+
+ unsigned n_views = (type==TETRAHEDRON ? 4 : 1);
+
+ lights.emplace_back();
+ ShadowedLight &sl = lights.back();
+ sl.light = &light;
+ sl.index = index;
+ sl.region = region;
+ sl.type = type;
+ sl.view_index = views.size();
+ sl.shadow_caster = &c;
+
+ views.resize(views.size()+n_views);
+ for(unsigned i=0; i<n_views; ++i)
+ {
+ ShadowView &view = views[sl.view_index+i];
+ view.light_index = lights.size()-1;
+ view.face = i;
+
+ if(type==TETRAHEDRON)
+ {
+ if(i>0)
+ view.face_matrix = Matrix().rotate(Geometry::Angle<float>::from_turns((i-0.5f)/3.0f), Vector3(0, 0, 1))
+ .rotate(Geometry::Angle<float>::straight()-Geometry::acos<float>(1.0f/3.0f), Vector3(1, 0, 0));
+
+ float triangle_h = sqrt(0.75f)*2.0f;
+ float bottom = 0.5f/sqrt(0.75f);
+ float distance = (sqrt(2.0f/3.0f)-sqrt(3.0f/8.0f))*2.0f;
+ view.camera.set_field_of_view(Geometry::atan<float>(triangle_h/distance)*2.0f);
+ view.camera.set_aspect_ratio(1.0f/triangle_h);
+ view.camera.set_frustum_axis(0.0f, 1.0f-bottom/triangle_h);
+ view.camera.set_frustum_rotation(-Geometry::Angle<float>::from_turns(i/4.0f));
+ }
+ }
+
+ string base = format("shadows[%d]", index);
+ shdata.uniform(base+".type", static_cast<int>(type));
+ shdata.uniform(base+".darkness", darkness);
+ shdata.uniform(base+".matrix_index", static_cast<int>(sl.view_index));
+
+ float xf = static_cast<float>(region.left)/width;
+ float yf = static_cast<float>(region.bottom)/height;
+ float wf = static_cast<float>(region.width)/width;
+ float hf = static_cast<float>(region.height)/height;
+ shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
+
+#ifdef DEBUG
+ if(!debug_name.empty())
+ {
+ for(unsigned i=sl.view_index; i<views.size(); ++i)
+ views[i].camera.set_debug_name(format("%s/view%d.camera", debug_name, i));
+ }
+#endif