Hi,
I have a shadow-mapping shader in my deferred renderer. Because its a deferred renderer, I have to reconstruct the pixels position in the fragment shader, multiply it with a shadow-space matrix and sample the depth.
I have the following vertex shader:
#version 150
uniform mat4 im_ModelViewMatrix;
uniform mat4 im_ProjectionMatrix;
uniform mat3 im_NormalMatrix;
uniform mat4 im_ShadowMatrix[3];
uniform mat4 im_ViewSpaceMat;
in vec3 iv_Vertex;
out flat mat4 mShadowSpaceMatrix[3];
void main( void )
{
gl_Position = im_ProjectionMatrix * im_ModelViewMatrix * vec4(iv_Vertex, 1.0);
mat4 mViewSpaceInv = inverse(im_ViewSpaceMat);
mShadowSpaceMatrix[0] = im_ShadowMatrix[0] * mViewSpaceInv;
mShadowSpaceMatrix[1] = im_ShadowMatrix[1] * mViewSpaceInv;
mShadowSpaceMatrix[2] = im_ShadowMatrix[2] * mViewSpaceInv;
}
And I sample the matrices in the Fragment shader like this:
in flat mat4 mShadowSpaceMatrix[3];
...
float ShadowLookup( vec3 fragpos, float depth )
{
float index;
vec4 coords;
if( depth < 150.0 )
{
index = 0.0;
coords = mShadowSpaceMatrix[0] * vec4( fragpos, 1.0 );
}
else if( depth < 400.0 )
{
index = 1.0;
coords = mShadowSpaceMatrix[1] * vec4( fragpos, 1.0 );
}
else if( depth < 2000.0 )
{
index = 2.0;
coords = mShadowSpaceMatrix[2] * vec4( fragpos, 1.0 );
}
else
{
return 1.0;
}
coords.w = coords.z;
coords.z = index;
return ShadowCoordLookup( coords );
}
Actually this gives me wrong results, as my whole screen is black now.
However, if I change the vertex shader to this:
#version 150
uniform mat4 im_ModelViewMatrix;
uniform mat4 im_ProjectionMatrix;
uniform mat3 im_NormalMatrix;
uniform mat4 im_ShadowMatrix[3];
uniform mat4 im_ViewSpaceMat;
in vec3 iv_Vertex;
out flat mat4 mShadowSpaceMatrix0;
out flat mat4 mShadowSpaceMatrix1; //CHANGE HERE
out flat mat4 mShadowSpaceMatrix2;
void main( void )
{
gl_Position = im_ProjectionMatrix * im_ModelViewMatrix * vec4(iv_Vertex, 1.0);
mat4 mViewSpaceInv = inverse(im_ViewSpaceMat);
mShadowSpaceMatrix0 = im_ShadowMatrix[0] * mViewSpaceInv;
mShadowSpaceMatrix1 = im_ShadowMatrix[1] * mViewSpaceInv;
mShadowSpaceMatrix2 = im_ShadowMatrix[2] * mViewSpaceInv;
}
And my Fragment Shader to this:
in flat mat4 mShadowSpaceMatrix0;
in flat mat4 mShadowSpaceMatrix1; //CHANGE HERE
in flat mat4 mShadowSpaceMatrix2;
...
float ShadowLookup( vec3 fragpos, float depth )
{
float index;
vec4 coords;
if( depth < 150.0 )
{
index = 0.0;
coords = mShadowSpaceMatrix0 * vec4( fragpos, 1.0 );
}
else if( depth < 400.0 )
{
index = 1.0;
coords = mShadowSpaceMatrix1 * vec4( fragpos, 1.0 );
}
else if( depth < 2000.0 )
{
index = 2.0;
coords = mShadowSpaceMatrix2 * vec4( fragpos, 1.0 );
}
else
{
return 1.0;
}
coords.w = coords.z;
coords.z = index;
return ShadowCoordLookup( coords );
}
Then it works. Note that I just changed “in flat mat4 mShadowSpaceMatrix[3]” to “in flat mat4 mShadowSpaceMatrixN”.
Any Ideas?