Chapter 10: Light direction

Hi Caroline,
could I respectfully suggest that there is some inconsistency in the calculation of lightDirection in the phongLight(…) function

For directional lights, the calculation you use is:

float3 lightDirection = normalize(-light.position)

which I consider to be correct; the light is travelling from the “position” of the light; hence the minus. Later this permits the following calculation:

float diffuseIntensity = saturate(-dot(lightDirection, pointNormal));

where the normal points out of the fragment but the light into it, requiring a minus sign to get the cosine of the angle between them. All of this makes sense.

However, when you go on to consider Point or Spot lights, you reverse the lightDirection with the following calculation which gives the direction we should look to to find the light, not the direction the light is travelling in:

float3 lightDirection = normalize(light.position - worldPosition);

and forcing you to calculate diffuseIntensity (inconsistently) thus:

float diffuseIntensity = saturate(dot(lightDirection, pointNormal));

I think you need to change to the following and reverse a number of other signs to make the code consistent with the text.

float3 lightDirection = normalize(worldPosition - light.position);

regards
Don

1 Like