# Lesson 37 Cel Shading Value Clamping Question

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

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

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013598
×