+ string base = format("shadows[%d]", i);
+ shdata.uniform(base+".enabled", 0);
+ shdata.uniform(base+".darkness", 1.0f);
+ shdata.uniform(base+".shd_world_matrix", Matrix());
+ shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
+ }
+}
+
+ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
+ ShadowMap(s, s, r, 0, c)
+{
+ add_light(l, s);
+}
+
+ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l, Renderable &c):
+ ShadowMap(w, h, r, &l, c)
+{ }
+
+void ShadowMap::add_light(const DirectionalLight &light, unsigned s)
+{
+ 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;