fart/main/Lighting.cc
Josh Holtrop 41b4354d52 trying to get Phong shading model working, having very large color values come back though
git-svn-id: svn://anubis/fart/trunk@72 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2009-01-30 20:53:13 +00:00

41 lines
1.4 KiB
C++

#include "Lighting.h"
#include <math.h> /* pow() */
Color Lighting::computePhong(const Material & material,
const std::vector<Light *> & 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<Light *>::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 */
result += diffuseColor
* (*it)->getDiffuseColor()
* (directionToLight % surfaceNormal);
/* calculate the specular term */
result += specularColor
* (*it)->getSpecularColor()
* pow(reflectedLightDirection % viewDirection, shininess);
}
return result;
}