Files
loop/shader/lighting/lineShadow.geom
T

92 lines
2.8 KiB
GLSL

#version 450 core
layout(lines_adjacency) in;
layout(triangle_strip, max_vertices = 4) out;
layout(std140, binding = 0) uniform TheMat { mat4 theMat; };
layout(location=0) uniform vec3 lightPos;
layout(location=1) uniform float radiusUniform;
// vec4 shift (vec4 p) { return (vec4 (p.xyz +
// (lumRad.a*normalize(p.xyz-lightPos)), 1));}
float closestPointOnLineParam3 (vec3 a, vec3 b, vec3 p) {
return dot(p - a,b-a) / dot(b-a,b-a);
}
vec3 closestPointOnSeg3 (vec3 a,vec3 b, vec3 p) {
float x = closestPointOnLineParam3(a,b,p);
if (x < 0) {
return a;
} else{ if (x > 1) { return b; }
{ return a + (x * (b- a));}
}
}
//vec3 closepoint =
vec4 shift(vec4 p) { return (vec4(p.xyz + (100000 * (p.xyz - lightPos)), 1)); }
vec4 shiftBy(float x, vec4 p) {
return (vec4(lightPos + (x * normalize(p.xyz - lightPos)), 1));
}
// vec4 shift (vec4 p) { return (vec4 (lightPos +
// (lumRad.a*normalize(p.xyz-lightPos)), 1));}
// copied from lighting/cap.geom, should not be changed on its own
vec4 projNear(vec4 pos) {
// note we project to a specific height
// this is quite brittle, not ideal
vec3 dir = pos.xyz - lightPos;
float a = (100 - pos.z) / dir.z;
vec2 xy = (pos.xyz + a * dir).xy;
return vec4(xy, 100, 1);
}
vec4 shiftNear(vec4 pos)
//{ vec4 sp = shiftBy(radiusUniform,pos);
{
vec4 sp = shift(pos);
if (sp.z > 100) {
return projNear(pos);
} else {
return sp;
}
}
vec4 f(vec4 p) { return (theMat * p); }
void main() {
vec4 p0 = gl_in[0].gl_Position;
vec4 p1 = gl_in[1].gl_Position;
vec3 closepoint = closestPointOnSeg3(p0.xyz,p1.xyz,lightPos);
float ru2 = radiusUniform * radiusUniform;
vec4 mid = 0.5 * (p0 + p1);
vec3 n0a = gl_in[2].gl_Position.xyz;
vec3 n1a = gl_in[3].gl_Position.xyz;
vec3 n0 = cross(p1.xyz - p0.xyz, n0a - p0.xyz);
vec3 n1 = cross(p0.xyz - p1.xyz, n1a - p1.xyz);
// vec3 n2 = n0 + n1; // assumes the summands are normalized
vec3 lightDir = p0.xyz - lightPos.xyz;
vec3 lightDir2 = p1.xyz - lightPos.xyz;
// first test if the edge is part of the silhouette
// that is, if the normals of the faces connected by the edge point are in
// "different directions" wrt the light direction
if (dot(n0, lightDir) * dot(n1, lightDir) <= 0 &&
(dot(lightDir, lightDir) < ru2 || dot(lightDir2, lightDir2) < ru2))
// using <= rather than < seems to get rid of overlapping shadow
// artefacts
{
//vec4 p2 = shiftNear(p0);
//vec4 p3 = shiftNear(p1);
vec4 p2 = shiftNear(p0);
vec4 p3 = shiftNear(p1);
gl_Position = f(p0);
EmitVertex();
if (dot(n0, lightDir) > 0) {
gl_Position = f(p2);
EmitVertex();
gl_Position = f(p1);
EmitVertex();
} else {
gl_Position = f(p1);
EmitVertex();
gl_Position = f(p2);
EmitVertex();
}
gl_Position = f(p3);
EmitVertex();
EndPrimitive();
} else {
}
}