Sign in to follow this  

need help with shadow mapping

This topic is 1836 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

Hi,

 

I'm trying to do shadow mapping. Unfortunately, I can't get the shadow to be displayed. I checked with gDEbugger, the shadow map is correct, but I'm not sure if I calculate the shadows properly.

 

here's the project:

https://docs.google.com/open?id=0BzHTUfIQ-XD8WjNldzEyZXhjTU0

 

I also attached an image of the shadow map depth texture.

 

any idea what is wrong?

 

best regards,

Yours3lf

Share this post


Link to post
Share on other sites

ok, I think I got it.

I just needed to transform the clip space shadow coords to ndc, then to tex coords.

 

#version 330 core

layout(binding=0) uniform sampler2D texture0;
uniform vec3 light_pos, spot_dir;
uniform float radius, spot_exponent, spot_cos_cutoff;

in vec3 normal;
in vec3 vs_pos;
in vec4 ls_pos;

out vec4 color;

void main()
{
	vec3 n = normal;
	vec3 l = light_pos - vs_pos;
	float d = length(l);
	l = normalize(l);
	float att = (1 + d / radius);
	att = 1.0 / ( att * att );
	float n_dot_l = dot(n, l);
	
	if(n_dot_l > 0)
	{		
		vec3 h = 0.5 * (l + normalize(-vs_pos));
		float n_dot_h = max(dot(n, h), 0);
		
		float spot_effect = dot(-l, spot_dir);
		if(spot_effect > spot_cos_cutoff)
		{
			spot_effect = pow( spot_effect, spot_exponent );
			att = spot_effect / att + 1;
		}
		att -= 1;
		
		vec4 shadow_coord = ls_pos / ls_pos.w; //convert to ndc
		shadow_coord = shadow_coord * 0.5 + 0.5; //scale, bias to [0...1]
		
		float depth = texture( texture0, shadow_coord.xy ).x;
		
		att *= depth < shadow_coord.z - 0.005 ? 0 : 1; //add bias to get rid of shadow acne
		
		color += vec4(vec3((0.1 + n_dot_l + pow(n_dot_h, 20)) * att), 1);
	}
	
	color.xyz = pow(color.xyz, vec3(1/2.2));
    //color = vec4(1);
}

Share this post


Link to post
Share on other sites

This topic is 1836 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this