//----------------------------------------------------------------------------
// William Baxter III's Ray Tracer
//
//     Project for Comp 238, Raster Graphics
//     University of North Carolina at Chapel Hill
//     
// $Id:$
//----------------------------------------------------------------------------

#include <math.h>
#include "wb3DirLight.hpp"
#include "wb3Material.hpp"
#include "wb3Artifact.hpp"
#include "wb3Scene.hpp"

//----------------------------------------------------------------------------
wb3DirectionalLight::wb3DirectionalLight(const Vec3f& dirn)
  : wb3Light()
{
  SetDirection(dirn);
}
//----------------------------------------------------------------------------
wb3DirectionalLight::wb3DirectionalLight(const Vec3f& color,
                                         const Vec3f& dirn)
  : wb3Light(color)
{
  SetDirection(dirn);
}
//----------------------------------------------------------------------------


// Computes the local portion of the illumination for a point
void wb3DirectionalLight::Contribute(
  const wb3Scene *scene, const wb3Artifact *hit,
  const Ray3f& I, 
  const Vec3f &isect, 
  const Vec3f &N, Vec3f& color) const
{

  float NdotL = -(N * GetDirection());
  if (NdotL > 0) { // NDOTL SHOULD BE NORMALIZED ALREADY
    // Make sure nothing else is in the way
    Ray3f shadowRay(isect, -GetDirection());
    float dist;
    if (!GetShadowing() || !scene->Intersect(shadowRay, &dist, hit))
    {
      // Diffuse component
      // NdotL /= toLightDist; // shouldn't have to normalize NdotL
      Vec3f hitCol;
      color += NdotL * GetColor() & hit->GetDiffuse(hitCol, isect);

      // Specular component
      Vec3f R = I.V() + 2.0f * (-I.V() * N) * N; // reflected direction
      NdotL = float(pow(-GetDirection() * R, 
                        hit->GetSpecularity(isect)));
      color += NdotL * GetColor() & hit->GetSpecular(hitCol,isect);
    }
  }
}
