#include "Lighting.h" #include /* pow() */ #include using namespace std; Color Lighting::computePhong(const Material & material, const std::vector & lights, const Ray & viewRay, const Vector & surfacePoint, const Vector & surfaceNormal, const Color & ambientLight) { Color result = ambientLight; Vector viewDirection = -viewRay.getDirection(); double shininess = material.getShininess(); const Color & diffuseColor = material.getDiffuseColor(); const Color & specularColor = material.getSpecularColor(); for (std::vector::const_iterator it = lights.begin(); it != lights.end(); it++) { Vector directionToLight = (*it)->getPosition() - surfacePoint; directionToLight.normalize(); Vector reflectedLightDirection = directionToLight.reflect(surfaceNormal); /* calculate the diffuse term */ double diffuse_coef = directionToLight % surfaceNormal; if (diffuse_coef > 0.0) { result += diffuseColor * (*it)->getDiffuseColor() * diffuse_coef; } /* calculate the specular term */ double specular_coef = reflectedLightDirection % viewDirection; if (specular_coef > 0.0) { result += specularColor * (*it)->getSpecularColor() * pow(specular_coef, shininess); } } /* TODO: figure out better scaling */ if (result.r > 1.0) result.r = 1.0; if (result.g > 1.0) result.g = 1.0; if (result.b > 1.0) result.b = 1.0; return result; }