The previous formulas assumed that we had white light. The color of the light source will of course affect the color of light reflected from a surface. Remember, the color of an object is a function of the incident wavelengths of light that it reflects.
To ad colored lights to our illumination model, we simply need to multiply each illumination component by the color of the light.
if (N · L) > 0 d_a.r = ka* object_color.r + kd(N · L) * object_color.r * light_color.r d_a.g = ka* object_color.g + kd(N · L) * object_color.g * light_color.g d_a.b = ka* object_color.b + kd(N · L) * object_color.b * light_color.b
For our specular illumination, the color of the highlight will now be the color of the light, so the formula gets changed as follows:
if (N · L > 0) { n_h = (MAX(0, (N · H) )spec_exp spec.r = obj_ks[obj]*n_h * light_color.r; spec.g = obj_ks[obj]*n_h * light_color.g; spec.b = obj_ks[obj]*n_h * light_color.b; } final_color.r = MIN(1.0, (d_a.r + (light_color.r - d_a.r)*spec.r)) final_color.g = MIN(1.0, (d_a.g + (light_color.g - d_a.g)*spec.g)) final_color.b = MIN(1.0, (d_a.b + (light_color.b - d_a.b)*spec.b))
Multiple lights are very easy to implement. All you need to do is to add the illumination from each light source, making sure that you do not allow the final color values to exceed 1.0 .
if ( cos(gamma) > cos(beta) ) light_intensity = light_intensity * cosn(beta) else light_intensity=0