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:
parent
e5cae862eb
commit
7d0b4c5646
@ -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;
|
|
||||||
}
|
|
@ -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
|
|
||||||
|
|
@ -13,7 +13,6 @@
|
|||||||
#include "util/Color.h"
|
#include "util/Color.h"
|
||||||
#include "shapes/Shape.h"
|
#include "shapes/Shape.h"
|
||||||
#include "Light.h"
|
#include "Light.h"
|
||||||
#include "Lighting.h"
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -159,12 +158,10 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
|
|||||||
Vector surfacePoint = ray[hit.dist];
|
Vector surfacePoint = ray[hit.dist];
|
||||||
Vector surfaceNormal = hit.shape->getNormalAt(surfacePoint);
|
Vector surfaceNormal = hit.shape->getNormalAt(surfacePoint);
|
||||||
|
|
||||||
color = Lighting::computePhong(material,
|
color = computePhong(material,
|
||||||
m_lights,
|
ray,
|
||||||
ray,
|
surfacePoint,
|
||||||
surfacePoint,
|
surfaceNormal);
|
||||||
surfaceNormal,
|
|
||||||
m_ambient_light);
|
|
||||||
|
|
||||||
if (depth > 0 && factor > SCENE_FACTOR_THRESHOLD)
|
if (depth > 0 && factor > SCENE_FACTOR_THRESHOLD)
|
||||||
{
|
{
|
||||||
@ -175,7 +172,7 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
|
|||||||
Vector reflected_direction =
|
Vector reflected_direction =
|
||||||
(-ray.getDirection()).reflect(surfaceNormal);
|
(-ray.getDirection()).reflect(surfaceNormal);
|
||||||
Ray newRay(surfacePoint, reflected_direction);
|
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);
|
Ray jitterNewRay(jitter_surface_point, reflected_direction);
|
||||||
Color c = traceRayRecurse(jitterNewRay,
|
Color c = traceRayRecurse(jitterNewRay,
|
||||||
depth - 1,
|
depth - 1,
|
||||||
@ -187,7 +184,7 @@ Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
|
|||||||
if (factor * transparency > SCENE_FACTOR_THRESHOLD)
|
if (factor * transparency > SCENE_FACTOR_THRESHOLD)
|
||||||
{
|
{
|
||||||
color *= (1.0 - transparency);
|
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());
|
Ray newRay(jitter_surface_point, ray.getDirection());
|
||||||
Color c = traceRayRecurse(newRay,
|
Color c = traceRayRecurse(newRay,
|
||||||
depth - 1,
|
depth - 1,
|
||||||
@ -232,6 +229,56 @@ Scene::ShapeDistance Scene::getRayClosestHit(const Ray & ray)
|
|||||||
return hit;
|
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,
|
bool operator<(const Scene::ShapeDistance & sd1,
|
||||||
const Scene::ShapeDistance & sd2)
|
const Scene::ShapeDistance & sd2)
|
||||||
{
|
{
|
||||||
|
@ -52,6 +52,10 @@ class Scene
|
|||||||
Color traceRay(const Ray & ray);
|
Color traceRay(const Ray & ray);
|
||||||
Color traceRayRecurse(const Ray & ray, int depth, double factor);
|
Color traceRayRecurse(const Ray & ray, int depth, double factor);
|
||||||
ShapeDistance getRayClosestHit(const Ray & ray);
|
ShapeDistance getRayClosestHit(const Ray & ray);
|
||||||
|
Color computePhong(const refptr<Material> material,
|
||||||
|
const Ray & viewRay,
|
||||||
|
const Vector & surfacePoint,
|
||||||
|
const Vector & surfaceNormal);
|
||||||
|
|
||||||
/* In Scene-load.cc */
|
/* In Scene-load.cc */
|
||||||
void load(const char * filename);
|
void load(const char * filename);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user