JoeyDeVries / LearnOpenGL

Code repository of all OpenGL chapters from the book and its accompanying website https://learnopengl.com
https://learnopengl.com
Other
11.06k stars 2.8k forks source link

Error in shader of point light #26

Closed galek closed 8 years ago

galek commented 8 years ago

Original: vec3 lightDir = normalize(light.position - FragPos);

But it's incorrect(see in any samples): Must be vec3 lightDir = normalize(FragPos-light.position);

But I don't like visual result

andrewparlane commented 8 years ago

I disagree, the original is correct.

We are looking for the angle between the fragPos and the light, and the fragPos and the normal.

[image: Inline images 1]

So the vector lightDir is the vector from the fragPos to the lightPos. Then lightDir DOT normal = |lightDir| |normal| cos theta.

since we normalize lightDir, and the normal is normalized both of their lengths are 1. So: lightDir dot normal = cos theta.

If we used the method you are suggesting, it would look more like:

[image: Inline images 2]

Which gives you the wrong theta.

Hope that helps.

On 22 January 2016 at 00:55, Nick Galko notifications@github.com wrote:

Original: vec3 lightDir = normalize(light.position - FragPos);

But it's incorrect(see in any samples): Must be vec3 lightDir = normalize(FragPos-light.position);

But I don't like visual result

— Reply to this email directly or view it on GitHub https://github.com/JoeyDeVries/LearnOpenGL/issues/26.

galek commented 8 years ago

ok, thanks for description