diff --git a/main/Lighting.cc b/main/Lighting.cc deleted file mode 100644 index 73cee51..0000000 --- a/main/Lighting.cc +++ /dev/null @@ -1,58 +0,0 @@ - -#include "Lighting.h" -#include /* pow() */ -#include -using namespace std; - -Color Lighting::computePhong(const refptr material, - const std::vector< refptr > & 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 >::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; -} diff --git a/main/Lighting.h b/main/Lighting.h deleted file mode 100644 index 30e7de1..0000000 --- a/main/Lighting.h +++ /dev/null @@ -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 - -class Lighting -{ - public: - static Color computePhong(const refptr material, - const std::vector< refptr > & lights, - const Ray & viewRay, - const Vector & surfacePoint, - const Vector & surfaceNormal, - const Color & ambientLight); - - protected: -}; - -#endif - diff --git a/main/Scene.cc b/main/Scene.cc index efdefa8..37cffb5 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -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, + 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 >::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) { diff --git a/main/Scene.h b/main/Scene.h index fa15fb5..efcd514 100644 --- a/main/Scene.h +++ b/main/Scene.h @@ -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, + const Ray & viewRay, + const Vector & surfacePoint, + const Vector & surfaceNormal); /* In Scene-load.cc */ void load(const char * filename);