From ed58d2ec7a669e441dcb98e963afffe5804acfce Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 11 Oct 2010 15:26:41 +0000 Subject: [PATCH] added Vector::refract() git-svn-id: svn://anubis/fart/trunk@372 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- util/Vector.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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;