diff --git a/src/raycolor.cpp b/src/raycolor.cpp index 0d8bd6a..ed76da5 100644 --- a/src/raycolor.cpp +++ b/src/raycolor.cpp @@ -3,6 +3,8 @@ #include "blinn_phong_shading.h" #include "reflect.h" +const double FUDGE_FACTOR = 1e-6; + // Hint: for the mirror reflections, you can use raycolor recursively. That is what 'num_recursive_calls' is for. // The maximum number of recursive calls should be 10. In other words, to get the color of a mirror reflection, // stop after 10 bounces. @@ -17,7 +19,8 @@ bool raycolor( { int hit_id; double t; - Eigen::Vector3d n; + Eigen::Vector3d n, reflect_rgb; + Ray reflect_ray; /* Compute viewing array. */ if (!first_hit(ray, min_t, objects, hit_id, t, n)) { @@ -27,5 +30,16 @@ bool raycolor( /* Evaluate shading model and set pixel to that color. */ rgb = blinn_phong_shading(ray, hit_id, t, n, objects, lights); + + if (num_recursive_calls >= 10) { + return true; + } + + /* Reflections */ + reflect_ray.origin = ray.origin + t * ray.direction; + reflect_ray.direction = reflect(ray.direction, n); + raycolor(reflect_ray, FUDGE_FACTOR, objects, lights, num_recursive_calls + 1, reflect_rgb); + rgb += (objects[hit_id]->material->km.array() * reflect_rgb.array()).matrix(); + return true; } diff --git a/src/reflect.cpp b/src/reflect.cpp index 14dd412..9c23305 100644 --- a/src/reflect.cpp +++ b/src/reflect.cpp @@ -2,6 +2,5 @@ Eigen::Vector3d reflect(const Eigen::Vector3d & in, const Eigen::Vector3d & n) { - // Replace with your code here: - return Eigen::Vector3d(0,0,0); + return in - 2 * in.dot(n) * n; }