Group Group Group Group Group Group Group Group Group

Ch 4 confusion over specular calculation

They say that getting computer graphics right is all about having the correct number of sign errors. I was scratching my head over the specular calculation for a while since it didn’t seem to match the diagram with the L, N, R, and V vectors. It turns out the calculation is correct but it does reverse 2 of the vectors from how they appear in the diagram, which is why it works. Also, it is probably the teeniest bit more efficient than the way I expected it. In particular, you have…

float3 reflection = reflect(lightDirection, normalDirection);
float3 cameraPosition = normalize(in.worldPosition - fragmentUniforms.cameraPosition); 
float specularIntensity = pow(saturate(dot(reflection, cameraPosition)), materialShininess);
specularColor += light.specularColor * materialSpecularColor * specularIntensity;”

This has L facing away from the surface point. Also, this has V facing towards the surface point. The diagram has these reversed. This had me quite confused because just looking at the reflect call in isolation (which computes L - 2(N.L)*N) I was not seeing something that would make sense.

Since both are reversed, it turns out the result is correct. However, to make it match the diagram I would have coded it as…

float3 reflection = reflect(-lightDirection, normalDirection); // negate light direction
float3 cameraPosition = normalize(fragmentUniforms.cameraPosition - in.worldPosition); // reverse subtraction
float specularIntensity = pow(saturate(dot(reflection, cameraPosition)), materialShininess);
specularColor += light.specularColor * materialSpecularColor * specularIntensity;”

Love the book so far though.

~chuck

Yes - it depends on how you are looking at it at the time. I did a little diagram here: Chapter 5, cone direction of the cone direction where there is a similar confusion.

I’m glad you’re enjoying the book :smiley:

1 Like

Thanks. On the positive side, I taught myself how to use the shader debugger (amazing), which allowed me to get to the bottom of my confusion much more quickly.

The shader debugger is truly amazing. If anybody else reads this, the sooner you get into it the better. Chapter 15 goes into it more.

1 Like