Ejemplo n.º 1
0
Vec4d Raytracer::shade(const RayIntersection& intersection,
                      size_t depth) const
{
  // This offset must be added to intersection points for further
  // traced rays to avoid noise in the image
  const Vec3d offset(intersection.normal() * Math::safetyEps());

  Vec4d color(0,0,0,1);
  std::shared_ptr<const Renderable> renderable = intersection.renderable();
  std::shared_ptr<const Material>   material   = renderable->material();

  for(size_t i=0;i <mScene->lights().size();++i)
  {
    const Light &light = *(mScene->lights()[i].get());

    //Shadow ray from light to hit point.
    const Vec3d L = (intersection.position() + offset) - light.position();
    const Ray shadowRay(light.position(), L);

    //Shade only if light in visible from intersection point.
    if (!mScene->anyIntersection(shadowRay,L.length()))
      color += material->shade(intersection,light);
  }

  // limit recursion depth
  if (depth >= mMaxDepth)
    return color;

  Vec3d dir = reflect(intersection.ray().direction(), intersection.normal());
  Ray reflectedRay(intersection.position() + offset, dir);
  double reflectance = material->reflectance();
  color = color * (1 - reflectance) +  reflectance * trace(reflectedRay, depth - 1) + Vec4d(0.0,0.0,0.0,1.0);
  return color;
}
Ejemplo n.º 2
0
    Vec4d Raytracer::shade(const RayIntersection &intersection,
                           size_t depth) const {
        // This offset must be added to intersection points for further
        // traced rays to avoid noise in the image
        const Vec3d offset(intersection.normal() * Math::safetyEps());

        Vec4d color(0, 0, 0, 1);
        std::shared_ptr<const Renderable> renderable = intersection.renderable();
        std::shared_ptr<const Material> material = renderable->material();

        for (size_t i = 0; i < mScene->lights().size(); ++i) {
            const Light &light = *(mScene->lights()[i].get());

            //Shadow ray from light to hit point.
            const Vec3d L = (intersection.position() + offset) - light.position();
            const Ray shadowRay(light.position(), L);

            //Shade only if light in visible from intersection point.
            if (!mScene->anyIntersection(shadowRay, L.length()))
                color += material->shade(intersection, light);
        }
        return color;
    }