• Advertisement
Sign in to follow this  

Ortho Shadow Problem.

This topic is 2035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

"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:
[CODE]
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);
[/CODE]
[img]http://i50.tinypic.com/2z6tibo.png[/img]

But with Orthographic mode:
(Also a debug window at right top corner that shows depth texture.)


[CODE]
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);[/CODE]


[img]http://i50.tinypic.com/350kwib.png[/img]


Also my shaders:
Fragment:

[CODE]
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;
}[/CODE]


And Vertex Shader:
[CODE]
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;
}
[/CODE]

Any ideas?? [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
Or need more info?? [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] Edited by FreOzgur

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
No there is no problem [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] ( -1 into ortho is possible.)
But I solved.. with a new shader code [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]


[CODE]
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;
}
[/CODE]

Now working perfectly.. Edited by FreOzgur

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement