fart/main/Lighting.cc
2009-02-10 01:09:07 +00:00

59 lines
1.8 KiB
C++

#include "Lighting.h"
#include <math.h> /* pow() */
#include <iostream>
using namespace std;
Color Lighting::computePhong(const refptr<Material> material,
const std::vector<Light *> & lights,
const Ray & viewRay,
const Vector & surfacePoint,
const Vector & surfaceNormal,
const Color & ambientLight)
{
Color result = ambientLight * material->getAmbientColor();
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 */
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;
}