//----------------------------------------------------------------------------
// William Baxter III's Ray Tracer
//
//     Project for Comp 238, Raster Graphics
//     University of North Carolina at Chapel Hill
//     
// $Id:$
//----------------------------------------------------------------------------

#include "wb3Scene.hpp"

static const Vec3f ONES(1.0f,1.0f,1.0f);
wb3Material ms_SceneMaterial(Vec3f::ZERO, // ambient
                             Vec3f::ZERO, // diffuse
                             Vec3f::ZERO, // specular
                             ONES,        // reflectivity
                             ONES,        // refractivity
                             0,           // spec exponent
                             1.0);        // index of refraction

wb3Scene::wb3Scene(float BGColor[3])
{
  if (BGColor) {
    m_BGColor.x = BGColor[0];
    m_BGColor.y = BGColor[1];
    m_BGColor.z = BGColor[2];
  } else {
    m_BGColor = Vec3f::ZERO;
  }
  m_pMaterial = &ms_SceneMaterial;
  m_pCam = new Camera;
}

//----------------------------------------------------------------------------

void wb3Scene::Shade(const wb3Scene* scene, 
                     const wb3Artifact* from,
                     const wb3Artifact* hit,
                     const Ray3f& r, const Vec3f &isect, 
                     Vec3f& color, Vec3f& attenuation, int hits) const
{
  float t;
  // hit is the object most recently hit
  const wb3Artifact *newhit = Intersect(r, &t, hit);
  if (newhit) {
    const wb3Artifact *to;
    if (newhit == from) // we must be exiting the thing we were just in
    {
      to = scene; // bad assumption in a world of nested objects !!!
    }
    else
    {
      to = newhit;
    }
    newhit->Shade(scene, from, to, r, r[t], color, attenuation, hits);
  }
  else
    color = GetBackground() & attenuation;
}


