# Combining shadows from multiple light sources

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

I've been implementing shadow mapping in my game for a while, now I got directional and point light shadows working correctly, unless I use them both at the same time. Take a look at this screenshot: http://s13.postimg.org/sjcmje2w7/point_Light.png I marked area with red arrows where weird thing happens. As you can see this area must be in shadow according to point light, but directional light lit up this area and it looks horribly. I think this problem is somehow related to biasing, but I'm not sure.

Same thing happens to directional light shadow mapping, here's how it looks: http://s11.postimg.org/jbz9qqx5v/directional_Light.png Again, I marked area where problem happens with arrows, but here it looks a lot better, when this area must be in shadow, point light lit up this area and it smoothly goes from light to dark, but still, there are some unwanted artifacts, I marked them around in white.

This is my fragment shader:

vec3 textureColor = texture2D(colorTexture, TexCoord).rgb;

//ShadowCalculation returns value between 0 and 1. 0 - fragment in shadow, 1 - fragment is not in shadow
float shadowDirectional = ShadowCalculationDirectionalLight();
float shadowPoint = ShadowCalculationPointLight();
float shadow = min(shadowPoint, shadowDirectional);

vec3 colorDirectional = PhongShade(directionalLight, normal, textureColor, shadow);
vec3 colorPoint = PhongShade(pointLight, normal, textureColor, shadow);

vec3 shadeColor = colorDirectional + colorPoint;
FragColor = vec4(shadeColor, 1.0);


And at the end of PhongShade function this is how I compute the result:

return attenuation * (diffuse + specular) * shadow + ambient

What am I doing wrong?

#### Share this post

##### Share on other sites

Why are you taking the min of both shadow terms and applying them to both light values? This is wrong (unless it's an artistic choice that you're after). You should be multiplying colorDirectional with shadowDirectional, and colorPoint by shadowPoint.

You should also only add ambient at the end as a separate "light" value, not as part of the PhongShade function, otherwise you're adding double the ambient for every new light added.

#### Share this post

##### Share on other sites

Why are you taking the min of both shadow terms and applying them to both light values? This is wrong (unless it's an artistic choice that you're after). You should be multiplying colorDirectional with shadowDirectional, and colorPoint by shadowPoint.

You should also only add ambient at the end as a separate "light" value, not as part of the PhongShade function, otherwise you're adding double the ambient for every new light added.

I used min of both shadows values because when shadows of two light sources were overlapping, they were darker, and shadows that were not overlapping was lighter. But it seems that this was one of my problems (and I think it's result is unrealistic). Now shadows looks good, thank you for helping.

Edited by Modestas

#### Share this post

##### Share on other sites

No prob. Did that also solve the artifacts you were getting on the sides of the spheres?

#### Share this post

##### Share on other sites

Yes, I'm not getting unwanted artifacts anymore. But I have another question, take a look at this screenshot: http://s16.postimg.org/rzbnd4rv9/ss14538.png Is it normal that point light shadow is brighter than directional light shadow?

#### Share this post

##### Share on other sites

Yes, I'm not getting unwanted artifacts anymore. But I have another question, take a look at this screenshot: http://s16.postimg.org/rzbnd4rv9/ss14538.png Is it normal that point light shadow is brighter than directional light shadow?

To this question and your original post.  Shadows are the lack of light received. Most people think doing phong shading and then multiplying shadows works. The shadows as pointed out should be multiplied by the N*L diffuse calculation because it is basically saying "Is the surfacing facing the light: (yes then it is receiving photons)" "If it is facing the light, is something blocking it: (If so, then it receives no photons)".  So if you perform all your lighting equation and texture lookups as if all the photons in the world hit it, it will get lit, and then some random multiplication happens to "darken" the image. It should be darkened by the fact that no light was hitting it in the lighting equation.

So this leads to your second question. Light is addative, without light every area in the world is completely black. So if your sun hits any surfaces not in shadow, those will be added quite highly with lighting (lots of photons hitting the surface). Then for every other light, more photons will hit.

So you have areas with:

Sun + Point light (which you can see very whitely)

Sun only (which is your brighter shadow because it received quite a lot of photons, just not the extra from the point light)

Point Light Only( this is your dark shadow. Only a few photons from the point light hit the surface, so its still pretty dark)

#### Share this post

##### Share on other sites

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

• ### Forum Statistics

• Total Topics
628696
• Total Posts
2984265

• 18
• 9
• 13
• 13
• 11