Published Jun 05, 2019
[
 
]
Colors are digitally represented using a red, green and blue component commonly abbreviated as RGB.
glm::vec3 coral(1.0f, 0.5f, 0.31f);
The colors we see in real life are not the colors the objects actually have, but are the colors reflected from the object; the colors that are not absorbed (rejected) by the objects are the colors we perceive of them.
When we define a light source in OpenGL we want to give this light source a color. If we would then multiply the light source’s color with an object’s color value, the resulting color is the reflected color of the object (and thus its perceived color)
glm::vec3 lightColor(1.0f, 1.0f, 1.0f);
glm::vec3 toyColor(1.0f, 0.5f, 0.31f);
glm::vec3 result = lightColor * toyColor; // = (1.0f, 0.5f, 0.31f);
Fragment Shader deals with the color.
#version 330 core
out vec4 FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
void main()
{
FragColor = vec4(lightColor * objectColor, 1.0);
}
The major building blocks of the Phong model consist of 3 components:
Adding ambient lighting to the scene is really easy. We take the light’s color, multiply it with a small constant ambient factor, multiply this with the object’s color and use it as the fragment’s color:
void main()
{
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
vec3 result = ambient * objectColor;
FragColor = vec4(result, 1.0);
}
Diffuse lighting gives the object more brightness the closer its fragments are aligned to the light rays from a light source.