Sign in to follow this  
datahead8888

Lesson 37 Cel Shading Value Clamping Question

Recommended Posts

In Nehe tutorial #37, the cel-shading tutorial, he computes a dot product between the light's direction vector and the surface normal. Since he uses unit vectors, this dot product will be equal to the cosine of the angle between the vectors, thus having a range of -1 to 1. In order to clamp his value between 0 and 1, he does this: if (TmpShade < 0.0f) TmpShade = 0.0f; Thus any negative value is "nullified" and becomes zero. ie. if the result is -.3, -.2, or -.5 -- they all become 0. It seems to work reasonably well when you run it. My question then is - why doesn't he take the absolute value of it, or at least something that maintains the diversity of different possible negative values? TmpShade = fabs(TmpShade); I've tried it with the absolute value. It seems to work, too, but I was just wanting to understand why he wrote it the way he did. Everything's about understanding...

Share this post


Link to post
Share on other sites
Negative values means that the surface normal is pointing away from the light source which means that it is not lighted at all (light value = 0). If you take the absolute value you will start lighting surfaces lying in shadow. I.e. if your result is -1 it means that your surface normal is pointing in the opposite light direction and is not lighted at all, but your absolute value will be 1 which means that it is fully lighted.

--
Ashaman

Share this post


Link to post
Share on other sites

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