I am trying to implement Phong lighting. In some tutorials specular lighting is added to ambient and diffuse lighting and then total lighting is multiplied by texture color. I also saw a tutorial where specular lighting was added separately after the addition of ambient and diffuse lights was multiplied with texture color.
Here is a fragment shader with both options present and with screenshots.
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
in vec3 normals;
in vec3 fragPosition;
//texture samplers
uniform sampler2D texture1;
uniform vec3 ambientLight;
uniform vec3 lightPosition;
uniform vec3 lightColor;
uniform vec3 viewPosition;
uniform float specularStrength;
uniform float shineDamp;
void main()
{
vec3 norm = normalize(normals);
vec3 lightDir = normalize(lightPosition - fragPosition);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 viewDir = normalize(viewPosition - fragPosition);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shineDamp);
vec3 specular = specularStrength * spec * lightColor;
// 1. Specular is added to ambient and diffuse lights and this result is multiplied with texture
//FragColor = vec4((ambientLight + diffuse + specular), 1.0f) * texture(texture1, TexCoord);
// 2. Specular is added separately to result of multiplication of ambient + diffuse and texture
//FragColor = vec4((ambientLight + diffuse), 1.0f) * texture(texture1, TexCoord) + vec4(specular, 1.0);
}
In these screenshots shineDump value was 32.0f and specularStrength was 0.5f.
Which one looks correct? In my opinion, the 2nd option looks correct compared to the 1st one but a lot of tutorials use the formula from the 1st option.