I made a shadow shader for my project.
When I use Perspective projection when rendering Depth buffer(Shadow map), there is no problem..
But when I simply switch to the Ortho Projection, every fragment become black. (I mean under shadow..)
I didn't understand why.
Here is the screenshots: (Sorry for the textures )
With Perspective:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (float)(displayWidth) / (float)(displayHeight), 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(position_x, position_y, position_z, lookAt_x, lookAt_y, lookAt_z, 0.0, 1.0, 0.0);
But with Orthographic mode:
(Also a debug window at right top corner that shows depth texture.)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10,-10, 10, -1.0, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(position_x, position_y, position_z, lookAt_x, lookAt_y, lookAt_z, 0.0, 1.0, 0.0);
Also my shaders:
Fragment:
uniform sampler2DShadow ShadowMap;
uniform sampler2D textureSampler;
uniform vec4 LightColor;
varying vec4 ShadowCoord;
varying vec2 TexCoord0;
varying vec3 WorldNormal;
varying vec3 LightDir;
float xPixelOffset = 1.0 / (1366.0 * 1.0);
float yPixelOffset = 1.0 / (768.0 * 1.0);
float lookup(vec2 offSet)
{
return shadow2DProj(ShadowMap, ShadowCoord + vec4(offSet.x * xPixelOffset * ShadowCoord.w, offSet.y * yPixelOffset * ShadowCoord.w, 0.005, 0.0) ).w;
}
void main()
{
vec3 FinalDiffuse = vec3(0.0, 0.0, 0.0);
vec3 AmbientLight = vec3(0.0, 0.1, 0.1);
vec4 TexColor = vec4(texture2D(textureSampler, TexCoord0));
float shadow;
float attenuation;
float LenSq = length(LightDir);
vec3 N = normalize(WorldNormal);
vec3 L = normalize(LightDir);
float lambertTerm = max(dot(N, L), 0.0);
attenuation = min((LightColor.w * LightColor.w) / (LenSq * LenSq), 1.0);
FinalDiffuse += LightColor * lambertTerm;// * attenuation;
if (ShadowCoord.w > 1.0)
{
float x,y;
for (y = -1.5 ; y <=1.5 ; y+=1.0)
for (x = -1.5 ; x <=1.5 ; x+=1.0)
shadow += lookup(vec2(x,y));
shadow /= 16.0 ;
}
FinalDiffuse *= shadow;
vec4 outColor = vec4(vec3(TexColor.xyz * (FinalDiffuse + AmbientLight.xyz)) , TexColor.a);
gl_FragColor = outColor;
}
And Vertex Shader:
varying vec3 WorldNormal;
varying vec4 ShadowCoord;
varying vec2 TexCoord0;
varying vec3 LightDir;
uniform vec4 LightPosition;
void main()
{
ShadowCoord = gl_TextureMatrix[7] * gl_Vertex;
gl_Position = ftransform();//gl_ModelViewProjectionMatrix * gl_Vertex;
vec4 WorldPos = gl_ModelViewMatrix * gl_Vertex;
WorldNormal = gl_NormalMatrix * gl_Normal;
LightDir = vec3(LightPosition - WorldPos);
TexCoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
gl_FrontColor = gl_Color;
}
Any ideas??
Or need more info??