Factored out interpolating over a triangle's surface into its own function.

We'll be using this in the RectangleLight sampling code soon.
This commit is contained in:
Nathan Vegdahl 2017-08-17 11:23:47 -07:00
parent 5a03a46ac7
commit b1bd419779
2 changed files with 17 additions and 10 deletions

View File

@ -128,6 +128,22 @@ pub fn intersect_ray(ray: &Ray, tri: (Point, Point, Point)) -> Option<(f32, f32,
Some((t, b0, b1, b2)) Some((t, b0, b1, b2))
} }
/// Calculates a point on a triangle's surface at the given barycentric
/// coordinates.
///
/// Returns the point and the error magnitude of the point.
pub fn surface_point(tri: (Point, Point, Point), bary: (f32, f32, f32)) -> (Point, f32) {
let pos = ((tri.0.into_vector() * bary.0) + (tri.1.into_vector() * bary.1) +
(tri.2.into_vector() * bary.2))
.into_point();
let pos_err = (((tri.0.into_vector().abs() * bary.0) + (tri.1.into_vector().abs() * bary.1) +
(tri.2.into_vector().abs() * bary.2)) * fp_gamma(7)).co
.h_max();
(pos, pos_err)
}
fn max_abs_3(a: f32, b: f32, c: f32) -> f32 { fn max_abs_3(a: f32, b: f32, c: f32) -> f32 {
let a = a.abs(); let a = a.abs();
let b = b.abs(); let b = b.abs();

View File

@ -5,7 +5,6 @@ use mem_arena::MemArena;
use accel::BVH4; use accel::BVH4;
use bbox::BBox; use bbox::BBox;
use boundable::Boundable; use boundable::Boundable;
use fp_utils::fp_gamma;
use lerp::lerp_slice; use lerp::lerp_slice;
use math::{Point, Normal, Matrix4x4, dot, cross}; use math::{Point, Normal, Matrix4x4, dot, cross};
use ray::{Ray, AccelRay}; use ray::{Ray, AccelRay};
@ -193,15 +192,7 @@ impl<'a> Surface for TriangleMesh<'a> {
r.mark_done(); r.mark_done();
} else { } else {
// Calculate intersection point and error magnitudes // Calculate intersection point and error magnitudes
let pos = ((tri.0.into_vector() * b0) + (tri.1.into_vector() * b1) + let (pos, pos_err) = triangle::surface_point(tri, (b0, b1, b2));
(tri.2.into_vector() * b2))
.into_point();
let pos_err = (((tri.0.into_vector().abs() * b0) +
(tri.1.into_vector().abs() * b1) +
(tri.2.into_vector().abs() * b2)) *
fp_gamma(7)).co
.h_max();
// Calculate geometric surface normal // Calculate geometric surface normal
let geo_normal = cross(tri.0 - tri.1, tri.0 - tri.2).into_normal(); let geo_normal = cross(tri.0 - tri.1, tri.0 - tri.2).into_normal();