moved Lighting::computePhong() into Scene, removed Lighting module

git-svn-id: svn://anubis/fart/trunk@184 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-03-04 00:16:32 +00:00
parent e5cae862eb
commit 7d0b4c5646
4 changed files with 60 additions and 93 deletions

View File

@ -1,58 +0,0 @@
#include "Lighting.h"
#include <math.h> /* pow() */
#include <iostream>
using namespace std;
Color Lighting::computePhong(const refptr<Material> material,
const std::vector< refptr<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< refptr<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;
}

View File

@ -1,26 +0,0 @@
#ifndef LIGHTING_H
#define LIGHTING_H LIGHTING_H
#include "Light.h"
#include "util/Ray.h"
#include "util/Color.h"
#include "util/Material.h"
#include "util/refptr.h"
#include <vector>
class Lighting
{
public:
static Color computePhong(const refptr<Material> material,
const std::vector< refptr<Light> > & lights,
const Ray & viewRay,
const Vector & surfacePoint,
const Vector & surfaceNormal,
const Color & ambientLight);
protected:
};
#endif

View File

@ -13,7 +13,6 @@
#include "util/Color.h"
#include "shapes/Shape.h"
#include "Light.h"
#include "Lighting.h"
using namespace std;
@ -159,12 +158,10 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
Vector surfacePoint = ray[hit.dist];
Vector surfaceNormal = hit.shape->getNormalAt(surfacePoint);
color = Lighting::computePhong(material,
m_lights,
ray,
surfacePoint,
surfaceNormal,
m_ambient_light);
color = computePhong(material,
ray,
surfacePoint,
surfaceNormal);
if (depth > 0 && factor > SCENE_FACTOR_THRESHOLD)
{
@ -175,7 +172,7 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
Vector reflected_direction =
(-ray.getDirection()).reflect(surfaceNormal);
Ray newRay(surfacePoint, reflected_direction);
Vector jitter_surface_point = newRay[0.001];
Vector jitter_surface_point = newRay[0.0001];
Ray jitterNewRay(jitter_surface_point, reflected_direction);
Color c = traceRayRecurse(jitterNewRay,
depth - 1,
@ -187,7 +184,7 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
if (factor * transparency > SCENE_FACTOR_THRESHOLD)
{
color *= (1.0 - transparency);
Vector jitter_surface_point = ray[hit.dist + 0.001];
Vector jitter_surface_point = ray[hit.dist + 0.0001];
Ray newRay(jitter_surface_point, ray.getDirection());
Color c = traceRayRecurse(newRay,
depth - 1,
@ -232,6 +229,56 @@ Scene::ShapeDistance Scene::getRayClosestHit(const Ray & ray)
return hit;
}
Color Scene::computePhong(const refptr<Material> material,
const Ray & viewRay,
const Vector & surfacePoint,
const Vector & surfaceNormal)
{
Color result = m_ambient_light * material->getAmbientColor();
Vector viewDirection = -viewRay.getDirection();
double shininess = material->getShininess();
const Color & diffuseColor = material->getDiffuseColor();
const Color & specularColor = material->getSpecularColor();
for (std::vector< refptr<Light> >::const_iterator it = m_lights.begin();
it != m_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;
}
bool operator<(const Scene::ShapeDistance & sd1,
const Scene::ShapeDistance & sd2)
{

View File

@ -52,6 +52,10 @@ class Scene
Color traceRay(const Ray & ray);
Color traceRayRecurse(const Ray & ray, int depth, double factor);
ShapeDistance getRayClosestHit(const Ray & ray);
Color computePhong(const refptr<Material> material,
const Ray & viewRay,
const Vector & surfacePoint,
const Vector & surfaceNormal);
/* In Scene-load.cc */
void load(const char * filename);