I've implemented a simple shader with the following properties: - Material (ambient, diffuse and specular) - Base color which defines the color of the object - Light Color
Fragment shader: (vColor is the base color of the object)
vec3 lightDir = normalize(vec3($uLightPos.xyz - vPosInEye.xyz));
vec3 viewerPos = normalize(vec3(-vPosInEye.xyz));
vec3 specularReflection = normalize(-reflect(lightDir, vNormal));
float diffFactor = max(dot(vNormal, lightDir), 0.0);
vec4 Id = $uIdColor * diffFactor * vColor ;
vec4 Ia = $uIaColor * vColor;
vec4 Is = clamp($uIsColor * pow(max(dot(specularReflection, viewerPos), 0.0), $uIsShininess), 0.0, 1.0);
gl_FragColor = (Id + Ia + Is) + $uLightColor;
As you can see I multiply the ambient color of the material with the base color of the object. This seems to be right because I want to mix these colors together.
In the book "Open GL ES 2.0 Programming guide" this is implemented the same way. In the last line I add the color of the light.
I tried to multiply the color at first but then I got strange result: yellow * blue resulted in black (which is clear because yellow is (1.0, 1.0, 0) and blue (0.0, 0.0, 1.0) so I replaced * with +. This seems to produce the correct color (white).
So my question is: Is there a rule of thumb when I have to multiply two colors together and when to add them?