Ortho Shadow Problem.

Started by
1 comment, last by FreOzgur 11 years, 10 months ago
"Sorry for my bad English..."

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 :D )
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);

2z6tibo.png

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);



350kwib.png


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?? sad.png
Or need more info?? sad.png
Advertisement
First you put -1 into your ortho which is impossible and I'd try that first. The result of being able to draw something behind your eyeball sounds random to me.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

No there is no problem smile.png ( -1 into ortho is possible.)
But I solved.. with a new shader code smile.png



uniform sampler2D ShadowMap;
uniform sampler2D textureSampler;
uniform vec4 LightColor;
varying vec4 ShadowCoord;
varying vec2 TexCoord0;
varying vec3 WorldNormal;
varying vec3 LightDir;
vec4 DividedShadowCoord;
float xPixelOffset = 1.0 / (1366.0 * 1.0);
float yPixelOffset = 1.0 / (768.0 * 1.0);
float lookup(vec2 offSet)
{
return texture2D(ShadowMap, DividedShadowCoord + vec4(offSet.x * xPixelOffset , offSet.y * yPixelOffset, 0.0, 0.0) ).z;
}
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;
DividedShadowCoord = ShadowCoord;
DividedShadowCoord = DividedShadowCoord / DividedShadowCoord.w;
DividedShadowCoord.z += 0.00005;
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 (DividedShadowCoord.w > 0.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)) < DividedShadowCoord.z ? 0.0 : 1.0 ;
shadow /= 16.0 ;
}
FinalDiffuse *= shadow;
vec4 outColor = vec4(vec3(/*TexColor.xyz **/ (FinalDiffuse + AmbientLight.xyz)) , TexColor.a);
gl_FragColor = outColor;
}


Now working perfectly..

This topic is closed to new replies.

Advertisement