]> git.tdb.fi Git - libs/gl.git/blobdiff - source/effects/shadowmap.cpp
Fix the return value of ConstantFolder::apply
[libs/gl.git] / source / effects / shadowmap.cpp
index 41a97f806b1da20480374f98fe99ac2e1071ed7a..639a352562bc062768e4303b89505afccd65f757 100644 (file)
@@ -13,15 +13,31 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l, Renderable &c):
+       Effect(r),
+       light(l),
+       shadow_caster(c),
+       sampler(resources.get<Sampler>("_linear_clamp_shadow.samp"))
+{
+       init(s);
+}
+
 ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l):
        Effect(r),
-       size(s),
        light(l),
-       sampler(resources.get<Sampler>("_linear_clamp_shadow.samp")),
-       radius(1),
-       depth_bias(4),
-       rendered(false)
+       shadow_caster(r),
+       sampler(resources.get<Sampler>("_linear_clamp_shadow.samp"))
 {
+       init(s);
+}
+
+void ShadowMap::init(unsigned s)
+{
+       size = s;
+       radius = 1;
+       depth_bias = 4;
+       rendered = false;
+
        depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
        fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
        fbo.require_complete();
@@ -58,16 +74,16 @@ void ShadowMap::setup_frame(Renderer &renderer)
 
        rendered = true;
        renderable.setup_frame(renderer);
+       shadow_caster.setup_frame(renderer);
 
-       Camera camera;
-       camera.set_object_matrix(*light.get_matrix());
-       camera.set_position(target);
+       shadow_camera.set_object_matrix(*light.get_matrix());
+       shadow_camera.set_position(target);
        // TODO support point and spot lights with a frustum projection.
        // Omnidirectional lights also need a cube shadow map.
-       camera.set_orthographic(radius*2, radius*2);
-       camera.set_depth_clip(-radius, radius);
+       shadow_camera.set_orthographic(radius*2, radius*2);
+       shadow_camera.set_depth_clip(-radius, radius);
 
-       shadow_matrix = camera.get_object_matrix();
+       shadow_matrix = shadow_camera.get_object_matrix();
        shadow_matrix.scale(radius*2, radius*2, -radius*2);
        shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
        shadow_matrix.invert();
@@ -77,9 +93,9 @@ void ShadowMap::setup_frame(Renderer &renderer)
        fbo.clear(DEPTH_BUFFER_BIT);
 
        Renderer::Push push(renderer);
-       renderer.set_camera(camera);
+       renderer.set_camera(shadow_camera);
 
-       renderer.render(renderable, "shadow");
+       renderer.render(shadow_caster);
 }
 
 void ShadowMap::finish_frame()