Skip to main content

Yes, you should always normalize light computation vectors.

dot(N, L) = cos(angle(N, L)) * length(N) * length(L), so, if vectors are normalized max(0.0, dot(N,L)) will get you values in the range of [0,1] which is exacly what you want.

0 being light perpendicular to surface normal, 1 being light pointing exaclyt at the same direction of normal (thus giving most contribution). Negative values means light is hit the back of surface, thus the need o max(0.0, dot()).

A few things worth noting:

1. Linear and Gamma Space

Monitors use a non-linear color space (gamma), so you should be aware of this or youyour light will appear darker. Textures that come from programs are already written in gamma space and you should convert them to linear space to compute lighting and after lighting done you should convert final result to gamma again.

Now you either use aan sRGB texture and framebuffer extension to do this automatically for you or you need to do this isin your shader.

So, for instance, you'd have to do:

vec3 finalCol = do_all_lighting_and_shading();  
return vec4(pow(finalCol, 1.0 / 2.2), pixelAlpha); 

For more information on linear and gamma space check this link: http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html

The Importance of Being Linear, and this is how to add sRGB extension to do gamma/linear conversion automatically for you: http://www.g-truc.net/post-0263.html Using sRGB color space with OpenGL.

2. A word on Specular

I see you're using Reflect vector to compute specular. A better approach is to use Half vector. Check this: https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model

Yes, you should always normalize light computation vectors.

dot(N, L) = cos(angle(N, L)) * length(N) * length(L), so, if vectors are normalized max(0.0, dot(N,L)) will get you values in the range of [0,1] which is exacly what you want.

0 being light perpendicular to surface normal, 1 being light pointing exaclyt at the same direction of normal (thus giving most contribution). Negative values means light is hit the back of surface, thus the need o max(0.0, dot()).

A few things worth noting:

1. Linear and Gamma Space

Monitors use a non-linear color space (gamma), so you should be aware of this or you light will appear darker. Textures that come from programs are already written in gamma space and you should convert them to linear space to compute lighting and after lighting done you should convert final result to gamma again.

Now you either use a sRGB texture and framebuffer extension to do this automatically for you or you need to do this is your shader.

So, for instance, you'd have to do:

vec3 finalCol = do_all_lighting_and_shading();  
return vec4(pow(finalCol, 1.0 / 2.2), pixelAlpha); 

For more information on linear and gamma space check this link: http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html

and this is how add sRGB extension to do gamma/linear conversion automatically for you: http://www.g-truc.net/post-0263.html

2. A word on Specular

I see you're using Reflect vector to compute specular. A better approach is to use Half vector. Check this: https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model

Yes, you should always normalize light computation vectors.

dot(N, L) = cos(angle(N, L)) * length(N) * length(L), so, if vectors are normalized max(0.0, dot(N,L)) will get you values in the range of [0,1] which is exacly what you want.

0 being light perpendicular to surface normal, 1 being light pointing exaclyt at the same direction of normal (thus giving most contribution). Negative values means light is hit the back of surface, thus the need o max(0.0, dot()).

A few things worth noting:

1. Linear and Gamma Space

Monitors use a non-linear color space (gamma), so you should be aware of this or your light will appear darker. Textures that come from programs are already written in gamma space and you should convert them to linear space to compute lighting and after lighting done you should convert final result to gamma again.

Now you either use an sRGB texture and framebuffer extension to do this automatically for you or you need to do this in your shader.

So, for instance, you'd have to do:

vec3 finalCol = do_all_lighting_and_shading();  
return vec4(pow(finalCol, 1.0 / 2.2), pixelAlpha); 

For more information on linear and gamma space check this link: The Importance of Being Linear, and this is how to add sRGB extension to do gamma/linear conversion automatically for you: Using sRGB color space with OpenGL.

2. A word on Specular

I see you're using Reflect vector to compute specular. A better approach is to use Half vector. Check this: https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model

Source Link
Felipe Lira
  • 321
  • 1
  • 11

Yes, you should always normalize light computation vectors.

dot(N, L) = cos(angle(N, L)) * length(N) * length(L), so, if vectors are normalized max(0.0, dot(N,L)) will get you values in the range of [0,1] which is exacly what you want.

0 being light perpendicular to surface normal, 1 being light pointing exaclyt at the same direction of normal (thus giving most contribution). Negative values means light is hit the back of surface, thus the need o max(0.0, dot()).

A few things worth noting:

1. Linear and Gamma Space

Monitors use a non-linear color space (gamma), so you should be aware of this or you light will appear darker. Textures that come from programs are already written in gamma space and you should convert them to linear space to compute lighting and after lighting done you should convert final result to gamma again.

Now you either use a sRGB texture and framebuffer extension to do this automatically for you or you need to do this is your shader.

So, for instance, you'd have to do:

vec3 finalCol = do_all_lighting_and_shading();  
return vec4(pow(finalCol, 1.0 / 2.2), pixelAlpha); 

For more information on linear and gamma space check this link: http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html

and this is how add sRGB extension to do gamma/linear conversion automatically for you: http://www.g-truc.net/post-0263.html

2. A word on Specular

I see you're using Reflect vector to compute specular. A better approach is to use Half vector. Check this: https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model