From 52d11df35ca9846085b910e2e40434b48847d9d8 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 16 Apr 2022 15:22:40 +0800 Subject: Add dielectric reflectiveness (using Schlick's Approximation). --- material_dielectric.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'material_dielectric.cpp') diff --git a/material_dielectric.cpp b/material_dielectric.cpp index 3aaf259..ed8e4eb 100644 --- a/material_dielectric.cpp +++ b/material_dielectric.cpp @@ -9,13 +9,24 @@ bool material_dielectric::scatter(ray3d &r, const object &hit_obj, double hit_t, assert(hit_obj.is_on(hit_p)); auto n = hit_obj.normal_vector(hit_p); auto ri_ = ri_inv; - if (dot(r.direction(), n) > 0) { + auto cos1 = dot(r.direction(), n); // -cos(a1) + if (cos1 > 0) { // the ray is started from the object's inner, // use normal vector and ri on the surface's inner side n = -n; ri_ = 1.0 / ri_; + } else { + cos1 = -cos1; + } + vec3d r2; + // determine reflection or refraction using Schlick's Approximation. + if (reflectance(cos1, ri_) > ruvg.range01_scalar()) { + // reflect + r2 = n.reflect(r.direction()); + } else { + // refract + r2 = n.refract(r.direction(), ri_); } - const auto r2 = n.refract(r.direction(), ri_); // refracted vector r.direction(r2.unit_vec()); r.source(hit_p); return true; -- cgit v1.2.3