Hello!
I am having a big problem implementing VSM and I have been stucked for a couple of days.
When I use VSM instead of a basic shadow mapping, shadows behave strangely. I think the shadow map that I am generating is good, so I dont understand what is happening.
This is the code I use to generate this dual channel depth map:
for (int i = 0; i < SHADOWMAP_CASCADE_COUNT; i++)
{
//generamos los shadowmaps originales, donde se renderizara la escena
glBindFramebuffer(GL_FRAMEBUFFER, lightFrameBuffer[i]);
glBindTexture(GL_TEXTURE_2D, shadowmapTexture[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_RGBA, GL_FLOAT, 0);
glUniform1i(glGetUniformLocation(program, varName), 3); // para testear en el programa pasado, en el plane program, ni caso
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, shadowmapTexture[i], 0);
glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_FALSE);
}
-------------------------------------------------------------------------------------------------------------------------------------
This is the glsl code I use to store the depth to the texture:
float depth = v_Position.z / v_Position.w;
depth = depth * 0.5 + 0.5;
float moment1 = depth;
float moment2 = depth * depth;
float dx = dFdx(depth);
float dy = dFdy(depth);
moment2 += 0.25*(dx*dx+dy*dy);
out_Color = vec4( moment1, moment2, 0.0, 1.0);
And the code to create shadows:
float shadow(int frustaIndex) //variance shadow mapping
{
vec2 moments;
float p;
float variance;
float compare;
float d;
float pMax;
vec4 calcPos;
calcPos = shadowMapPos[frustaIndex] / shadowMapPos[frustaIndex].w;
calcPos = calcPos * 0.5 + 0.5;
compare = calcPos.z;
if(frustaIndex == 2)
{
moments = texture2D(shadowmap3, calcPos.xy).rg;
if (compare <= moments.x)
return 1.0f;
variance = moments.y - ( moments.x*moments.x );
variance = max( variance, 0.00002 );
d = compare - moments.x;
pMax = variance / (variance + d*d);
return pMax;
}
//here I do the same for the other frustums, changing the shadowmap (CSM it works because normal shadow mapping works, so the problem is not there)
}
And the result of all of this:
Hope someone can help me a bit!
Thank you!