From: Mikko Rasa Date: Tue, 30 Jul 2024 21:36:11 +0000 (+0300) Subject: Align shadow camera to shadow map texels X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=8bd21199e78616b139cd5f7a18523181ebdfc8d4;p=libs%2Fgl.git Align shadow camera to shadow map texels This avoids swimming shadows when the target area moves smoothly. --- diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 0345ac1f..d945ea6b 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -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 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); }