]> git.tdb.fi Git - libs/gl.git/commitdiff
Align shadow camera to shadow map texels
authorMikko Rasa <tdb@tdb.fi>
Tue, 30 Jul 2024 21:36:11 +0000 (00:36 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 30 Jul 2024 21:41:34 +0000 (00:41 +0300)
This avoids swimming shadows when the target area moves smoothly.

source/effects/shadowmap.cpp

index 0345ac1fac7c6e694cdf2689a9fd5333344769d3..d945ea6b35ae1962c56f2e0908633336593da8d5 100644 (file)
@@ -209,7 +209,10 @@ void ShadowMap::setup_frame(Renderer &renderer)
                if(light.type==DIRECTIONAL)
                {
                        v.camera.set_object_matrix(*light.light->get_matrix());
-                       v.camera.set_position(target);
+                       Vector4 pos = v.camera.get_view_matrix()*compose(target, 0.0f);
+                       LinAl::Vector<float, 2> texel_size = { radius*2.0f/light.region.width, radius*2.0f/light.region.height };
+                       pos = { round(pos.x/texel_size.x)*texel_size.x, round(pos.y/texel_size.y)*texel_size.y, pos.z, 0.0f };
+                       v.camera.set_position((v.camera.get_object_matrix()*pos).slice<3>(0));
                        v.camera.set_orthographic(radius*2, radius*2);
                        v.camera.set_depth_clip(-radius, radius);
                }