add total internal reflection

This commit is contained in:
Fabien Freling 2021-05-27 22:03:28 +02:00
parent dde1f91f01
commit 4a478b08a0
3 changed files with 18 additions and 11 deletions

View file

@ -82,6 +82,13 @@ pub const Vec3 = packed struct {
pub fn reflect(self: Vec3, n: Vec3) Vec3 {
return self.sub(n.mul_s(self.dot(n) * 2));
}
pub fn refract(self: Vec3, n: Vec3, etai_over_etat: f32) Vec3 {
const cos_theta = math.min(self.mul_s(-1).dot(n), 1.0);
const r_out_perp = self.add(n.mul_s(cos_theta)).mul_s(etai_over_etat);
const r_out_parallel = n.mul_s(-math.sqrt(math.absFloat(1.0 - r_out_perp.length_squared())));
return r_out_perp.add(r_out_parallel);
}
};
pub fn random(rng: *Random, min: f32, max: f32) Vec3 {
@ -116,13 +123,6 @@ pub fn random_in_hemisphere(rng: *Random, normal: Vec3) Vec3 {
}
}
pub fn refract(uv: Vec3, n: Vec3, etai_over_etat: f32) Vec3 {
const cos_theta = math.min(uv.mul_s(-1).dot(n), 1.0);
const r_out_perp = uv.add(n.mul_s(cos_theta)).mul_s(etai_over_etat);
const r_out_parallel = n.mul_s(-math.sqrt(math.absFloat(1.0 - r_out_perp.length_squared())));
return r_out_perp.add(r_out_parallel);
}
pub const Point3 = Vec3;
const assert = @import("std").debug.assert;