Jump to content

  • Log In with Google      Sign In   
  • Create Account

Ortho Shadow Problem.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 OzgurEra   Members   -  Reputation: 731

Like
0Likes
Like

Posted 22 June 2012 - 11:11 AM

"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);
Posted Image

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


Posted Image


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?? Posted Image
Or need more info?? Posted Image

Edited by FreOzgur, 22 June 2012 - 11:54 AM.


Sponsor:

#2 dpadam450   Members   -  Reputation: 949

Like
0Likes
Like

Posted 22 June 2012 - 02:08 PM

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.

#3 OzgurEra   Members   -  Reputation: 731

Like
0Likes
Like

Posted 22 June 2012 - 04:13 PM

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


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..

Edited by FreOzgur, 22 June 2012 - 04:13 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS