I'm currently learning about how to generate omnidirectional shadowmap for point light from the following resource:
https://learnopengl.com/Advanced-Lighting/Shadows/Point-Shadows
The author is talking about layered rendering where instead of doing 6 passes for each cubemap face we use one pass and multiply the number of fragment shader by 6 for each mesh by using the geometry shader stage for layered rendering.
He achieves this with the following vertex, geometry, and fragment shaders respectively:
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
void main()
{
gl_Position = model * vec4(aPos, 1.0);
}
#version 330 core
layout (triangles) in;
layout (triangle_strip, max_vertices=18) out;
uniform mat4 shadowMatrices[6];
out vec4 FragPos; // FragPos from GS (output per emitvertex)
void main()
{
for(int face = 0; face < 6; ++face)
{
gl_Layer = face; // built-in variable that specifies to which face we render.
for(int i = 0; i < 3; ++i) // for each triangle vertex
{
FragPos = gl_in[i].gl_Position;
gl_Position = shadowMatrices[face] * FragPos;
EmitVertex();
}
EndPrimitive();
}
}
#version 330 core
in vec4 FragPos;
uniform vec3 lightPos;
uniform float far_plane;
void main()
{
// get distance between fragment and light source
float lightDistance = length(FragPos.xyz - lightPos);
// map to [0;1] range by dividing by far_plane
lightDistance = lightDistance / far_plane;
// write this as modified depth
gl_FragDepth = lightDistance;
}
What I'm having hard time to understand is why bother with keeping FragPos
between the geometry shader and the fragment shader?
I know he uses it to calculate a linear distance to the light position but why can't we just get linear z coordinate like so:
#version 330 core
uniform float u_Near;
uniform float u_Far;
float linearizeDepth(float depth)
{
float z = depth * 2.0 - 1.0; // back to NDC
return (2.0 * u_Near * u_Far) / (u_Far + u_Near - z * (u_Far - u_Near));
}
void main()
{
float depth = linearizeDepth(gl_FragCoord.z);
gl_FragDepth = depth;
}
Also I would like to understand how one could use the attenuation variables of a point light (constant quadratic linear) to calculate the far z plane of the projection matrix for the shadow mapping.