//----------------------------------------------------------------------------
// William Baxter III's Ray Tracer
//
//     Project for Comp 238, Raster Graphics
//     University of North Carolina at Chapel Hill
//     
// $Id:$
//----------------------------------------------------------------------------

#include "wb3Plane.hpp"

const wb3Artifact* wb3Plane::Intersect(const Ray3f& r, float* dist, 
                                       const wb3Artifact *ignore) const
{
  float ndotv = m_N * r.V();
  const float eps = 1e-4f;
  if (this!=ignore && (ndotv < -eps || ndotv > eps))
  {
    *dist = - (m_fD + m_N * r.P())/ndotv;
    return this;
  }
  else return 0; // ray parallel or nearly-parallel to plane
}

//----------------------------------------------------------------------------
Vec3f& wb3Plane::GetColorComponent(
    Vec3f& color, wb3Component::ComponentType type, const Vec3f& Pw) const
{
  // In general this will iterate over all textures in the textureset of
  // the given type and add each of their contributions to the Materials.
  color = GetMaterial()->GetColorComponent(type);
  // Color becomes the base color
  
  const wb3TextureList *tl = GetTextureList(type);
  if (!tl) return color;

  // [Texture set stuff...]
  // iterator
  using namespace std;

  Vec3f contrib;
  Vec2f UV(Pw.x, Pw.z);
  list<wb3Texture*>::const_iterator it;
  for(it = tl->begin(); it != tl->end(); ++it)
  {
    (*it)->GetValue(contrib, UV);
    color &= contrib;
  }
  return color;
}
//----------------------------------------------------------------------------
float wb3Plane::GetScalarComponent(
    wb3Component::ComponentType type, const Vec3f& Pw) const
{
  // In general this will iterate over all textures in the textureset of
  // the given type and add each of their contributions to the Materials.
  // Except for bump
  float attrib = GetMaterial()->GetScalarComponent(type);

  // [Texture set stuff...]

  return attrib;
}
//----------------------------------------------------------------------------


