In Computer Graphics Phong is the basic lighting model consist of 3 components ambient, diffuse and specular.
- Ambient lighting: even when it is dark there is usually still some light somewhere in the world (the moon, a distant light) so objects are almost never completely dark. To simulate this we use an ambient lighting constant that always gives the object some color.
- Diffuse lighting: simulates the directional impact a light object has on an object. This is the most visually significant component of the lighting model. The more a part of an object faces the light source, the brighter it becomes.
- Specular lighting: simulates the bright spot of a light that appears on shiny objects. Specular highlights are more inclined to the color of the light than the color of the object.
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 anormal;
out vec3 fragPos;
out vec3 normal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
void main(){
fragPos = vec3(model * vec4(aPos,1.0f));
normal = mat3(transpos(inverse(model))) * aNormal;
gl_Position = proj * view * vec4(aPos,1.0);
}out vec4 FragColor;
in vec3 fragPos;
in vec3 normal;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 viewPos;
void main(){
// ambient
float ambientStrength = 0.1f;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(normal);
vec3 lightDir = normalize(lightPos - fragPos);
vec3 diff = max(dot(lightDir,norm),0.0f);
vec3 diffuse = diff * lightColor;
// specular
float specularStrength = 0.5f;
vec3 viewDir = normalize(viewPos - fragPos);
vec3 reflectDir = reflect(-lightDir,norm);
vec3 spec = pow(max(dot(viewDir,reflectDir),0.0f),32);
vec3 specular = spec * specularStrength * lightColor;
vec3 result = (ambient + diffuse + specular) * objectColor;
FragColor = vec4(result,1.0f);
}