diff --git a/util/Vector.h b/util/Vector.h index fe15fa8..b723ac0 100644 --- a/util/Vector.h +++ b/util/Vector.h @@ -63,6 +63,23 @@ class Vector return (*this) - target * (2 * dot(target)); } + /* + * from http://www.flipcode.com/archives/ + * Reflections_and_Refraction_in_Raytracing.shtml + * target: normal vector of surface + * n1: refraction index of object we're coming from + * n2: refraction index of object we're going into + */ + Vector refract(const Vector & target, double n1, double n2) const + { + const double n = n1 / n2; + const double cosI = dot(target); + const double sinT2 = n * n * (1.0 - cosI * cosI); + if (sinT2 > 1.0) + return Vector(0.0, 0.0, 0.0); + return (*this) * n - target * (n + sqrt(1.0 - sinT2)); + } + Vector getPerpendicular() const { Vector t = *this;