I'm trying to render a scene using multiple point light casting shadows. Currently, If I have more than 1 point light, shadows are wrong.
I'm drawing to FBO with a depth cubemap attached to it and in geometry shader for every point light I emit vertices to each face of the cubemap.
//Geometry shader
out vec4 v_FragPos;
void main()
{
for (int pl = 0; pl < u_PointLightsSize; ++pl)
{
for (int face = 0; face < 6; ++face)
{
gl_Layer = face;
for (int i = 0; i < 3; ++i)
{
v_FragPos = gl_in[i].gl_Position;
gl_Position = u_PointLights[pl].ViewProj[face] * v_FragPos;
EmitVertex();
}
EndPrimitive();
}
}
}
u_PointLights is an array of PointLight structs
struct PointLight
{
mat4 ViewProj[6];
vec3 Position;
vec3 Ambient;
vec3 Diffuse;
vec3 Specular;
float Distance;
};
And my fragment shader looks like this
in vec4 v_FragPos;
const float g_FarPlane = 10000.f;
void main()
{
float minDistance = g_FarPlane;
for (int i = 0; i < u_PointLightsSize; ++i)
{
float distance = length(v_FragPos.xyz - u_PointLights[i].Position);
if (distance < minDistance)
minDistance = distance;
}
minDistance /= g_FarPlane;
gl_FragDepth = minDistance;
}
Can anyone tell what I'm doing wrong? If I use one point light, shadows are fine.