From 740abd1609b39e5d21f5769f935969bd92ffdebd Mon Sep 17 00:00:00 2001 From: justin Date: Tue, 31 Aug 2021 10:46:44 +0100 Subject: [PATCH] Implement correctly shadows on geometry, with arbitrary near plane cap --- shader/lighting/cap.geom | 4 ++-- shader/lighting/lineShadow.geom | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/shader/lighting/cap.geom b/shader/lighting/cap.geom index 32e9274be..e1aed2f5a 100644 --- a/shader/lighting/cap.geom +++ b/shader/lighting/cap.geom @@ -8,9 +8,9 @@ vec4 projNear (vec4 pos) // note we project to a specific height // this is quite brittle, not ideal vec3 dir = pos.xyz - lightPos ; - float a = (150 - pos.z) / dir.z ; + float a = (140 - pos.z) / dir.z ; vec2 xy = (pos.xyz + a * dir).xy ; - return vec4 ( xy, 150 , 1) ; + return vec4 ( xy, 140 , 1) ; } void main() { diff --git a/shader/lighting/lineShadow.geom b/shader/lighting/lineShadow.geom index a6c56a243..f10775003 100644 --- a/shader/lighting/lineShadow.geom +++ b/shader/lighting/lineShadow.geom @@ -4,6 +4,24 @@ layout (triangle_strip, max_vertices = 4) out; layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ; uniform vec3 lightPos; vec4 shift (vec4 p) { return (vec4 (p.xyz + (100000*(p.xyz-lightPos)), 1));} +// copied from lighting/cap.geom +vec4 projNear (vec4 pos) +{ + // note we project to a specific height + // this is quite brittle, not ideal + vec3 dir = pos.xyz - lightPos ; + float a = (140 - pos.z) / dir.z ; + vec2 xy = (pos.xyz + a * dir).xy ; + return vec4 ( xy, 140 , 1) ; +} +vec4 shiftNear (vec4 pos) +{ vec4 sp = shift(pos); + if (sp.z > 140) + { return projNear(pos) ; } + else + { return sp ; } +} + vec4 f (vec4 p) {return (theMat * p);} void main() { @@ -18,8 +36,8 @@ void main() if ( dot(n0 , lightDir) * dot(n1 , lightDir) < 0 ) { vec4 p1 = gl_in[1].gl_Position; - vec4 p2 = shift(p0); - vec4 p3 = shift(p1); + vec4 p2 = shiftNear(p0); + vec4 p3 = shiftNear(p1); if ( dot(n0 , lightDir) > 0) { gl_Position = f(p0); EmitVertex();