Compare commits

..

5 Commits

5 changed files with 38 additions and 13 deletions

View File

@ -75,7 +75,6 @@ impl AABB {
pub fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> bool { pub fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> bool {
self.hit_simd(r, t_min, t_max) self.hit_simd(r, t_min, t_max)
//self.hit_naive(r, t_min, t_max)
} }
pub fn hit_naive(&self, r: Ray, t_min: f32, t_max: f32) -> bool { pub fn hit_naive(&self, r: Ray, t_min: f32, t_max: f32) -> bool {

View File

@ -194,13 +194,15 @@ where
} }
} }
} else { } else {
let hr = self.intersect_bvh(r, node.left_child, t_min, t_max); let r1 = self.intersect_bvh(r, node.left_child, t_min, t_max);
if hr.is_some() { let r2 = self.intersect_bvh(r, node.left_child + 1, t_min, t_max);
return hr; // Merge results, if both hit, take the one closest to the ray origin (smallest t
} // value).
let hr = self.intersect_bvh(r, node.left_child + 1, t_min, t_max); match (&r1, &r2) {
if hr.is_some() { (Some(_), None) => return r1,
return hr; (None, Some(_)) => return r2,
(None, None) => (),
(Some(rp1), Some(rp2)) => return if rp1.t < rp2.t { r1 } else { r2 },
} }
} }
None None
@ -282,7 +284,7 @@ fn ray_triangle_intersect_moller_trumbore(r: Ray, tri: &Triangle) -> Option<RayT
if t > EPSILON { if t > EPSILON {
return Some(RayTriangleResult { return Some(RayTriangleResult {
t, t,
p: r.origin + r.direction * t, p: r.point_at_parameter(t),
}); });
} }
None None

View File

@ -104,8 +104,8 @@ impl KDTree {
}), }),
_ => panic!("Unreachable"), _ => panic!("Unreachable"),
}; };
info!("left_half {:?}", left_half); //info!("left_half {:?}", left_half);
info!("right_half {:?}", right_half); //info!("right_half {:?}", right_half);
if left_half.is_empty() { if left_half.is_empty() {
return KDTree::Leaf(Box::new(HitableList::new(right_half))); return KDTree::Leaf(Box::new(HitableList::new(right_half)));
}; };

View File

@ -410,8 +410,11 @@ fn progress(
let ray_diff = current_stat.rays - last_stat.rays; let ray_diff = current_stat.rays - last_stat.rays;
let now = time::Instant::now(); let now = time::Instant::now();
let start_diff = now - start_time; let start_diff = now - start_time;
let percent = 100. * current_stat.pixels as f32 / pixel_total as f32; let ratio = current_stat.pixels as f32 / pixel_total as f32;
let eta = 100. * start_diff.as_secs_f32() / percent; let percent = ratio * 100.;
let elapsed = start_diff.as_secs_f32();
let total = elapsed * (1. / ratio);
let eta = total - elapsed;
format!( format!(
"{:7} / {:7}pixels ({:2.0}%) {:7}pixels/s {:7}rays/s eta {:.0}s", "{:7} / {:7}pixels ({:2.0}%) {:7}pixels/s {:7}rays/s eta {:.0}s",
human.format(current_stat.pixels as f64), human.format(current_stat.pixels as f64),

View File

@ -16,6 +16,7 @@ pub struct Vec3 {
} }
// Return a `Vec3` with the lowest of each component in v1 or v2. // Return a `Vec3` with the lowest of each component in v1 or v2.
#[inline]
pub fn min(v1: Vec3, v2: Vec3) -> Vec3 { pub fn min(v1: Vec3, v2: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: v1.x.min(v2.x), x: v1.x.min(v2.x),
@ -24,6 +25,7 @@ pub fn min(v1: Vec3, v2: Vec3) -> Vec3 {
} }
} }
// Return a `Vec3` with the greatest of each component in v1 or v2. // Return a `Vec3` with the greatest of each component in v1 or v2.
#[inline]
pub fn max(v1: Vec3, v2: Vec3) -> Vec3 { pub fn max(v1: Vec3, v2: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: v1.x.max(v2.x), x: v1.x.max(v2.x),
@ -32,6 +34,7 @@ pub fn max(v1: Vec3, v2: Vec3) -> Vec3 {
} }
} }
#[inline]
pub fn cross(v1: Vec3, v2: Vec3) -> Vec3 { pub fn cross(v1: Vec3, v2: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: v1.y * v2.z - v1.z * v2.y, x: v1.y * v2.z - v1.z * v2.y,
@ -40,6 +43,7 @@ pub fn cross(v1: Vec3, v2: Vec3) -> Vec3 {
} }
} }
#[inline]
pub fn dot(v1: Vec3, v2: Vec3) -> f32 { pub fn dot(v1: Vec3, v2: Vec3) -> f32 {
v1.x * v2.x + v1.y * v2.y + v1.z * v2.z v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
} }
@ -49,26 +53,32 @@ impl Vec3 {
Vec3 { x, y, z } Vec3 { x, y, z }
} }
#[inline]
pub fn min(self) -> f32 { pub fn min(self) -> f32 {
self.x.min(self.y).min(self.z) self.x.min(self.y).min(self.z)
} }
#[inline]
pub fn max(self) -> f32 { pub fn max(self) -> f32 {
self.x.max(self.y).max(self.z) self.x.max(self.y).max(self.z)
} }
#[inline]
pub fn length(self) -> f32 { pub fn length(self) -> f32 {
self.squared_length().sqrt() self.squared_length().sqrt()
} }
#[inline]
pub fn squared_length(self) -> f32 { pub fn squared_length(self) -> f32 {
self.x * self.x + self.y * self.y + self.z * self.z self.x * self.x + self.y * self.y + self.z * self.z
} }
#[inline]
pub fn unit_vector(self) -> Vec3 { pub fn unit_vector(self) -> Vec3 {
self / self.length() self / self.length()
} }
#[inline]
pub fn make_unit_vector(&mut self) { pub fn make_unit_vector(&mut self) {
*self = self.unit_vector(); *self = self.unit_vector();
} }
@ -111,6 +121,7 @@ impl str::FromStr for Vec3 {
impl Add<f32> for Vec3 { impl Add<f32> for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn add(self, r: f32) -> Vec3 { fn add(self, r: f32) -> Vec3 {
Vec3 { Vec3 {
x: self.x + r, x: self.x + r,
@ -123,6 +134,7 @@ impl Add<f32> for Vec3 {
impl Add for Vec3 { impl Add for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn add(self, r: Vec3) -> Vec3 { fn add(self, r: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: self.x + r.x, x: self.x + r.x,
@ -135,6 +147,7 @@ impl Add for Vec3 {
impl Div<Vec3> for Vec3 { impl Div<Vec3> for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn div(self, r: Vec3) -> Vec3 { fn div(self, r: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: self.x / r.x, x: self.x / r.x,
@ -147,6 +160,7 @@ impl Div<Vec3> for Vec3 {
impl Div<Vec3> for f32 { impl Div<Vec3> for f32 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn div(self, r: Vec3) -> Vec3 { fn div(self, r: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: self / r.x, x: self / r.x,
@ -159,6 +173,7 @@ impl Div<Vec3> for f32 {
impl Div<f32> for Vec3 { impl Div<f32> for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn div(self, r: f32) -> Vec3 { fn div(self, r: f32) -> Vec3 {
Vec3 { Vec3 {
x: self.x / r, x: self.x / r,
@ -170,6 +185,7 @@ impl Div<f32> for Vec3 {
impl Index<usize> for Vec3 { impl Index<usize> for Vec3 {
type Output = f32; type Output = f32;
#[inline]
fn index(&self, idx: usize) -> &f32 { fn index(&self, idx: usize) -> &f32 {
match idx { match idx {
0 => &self.x, 0 => &self.x,
@ -183,6 +199,7 @@ impl Index<usize> for Vec3 {
impl Mul for Vec3 { impl Mul for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn mul(self, r: Vec3) -> Vec3 { fn mul(self, r: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: self.x * r.x, x: self.x * r.x,
@ -195,6 +212,7 @@ impl Mul for Vec3 {
impl Mul<Vec3> for f32 { impl Mul<Vec3> for f32 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn mul(self, v: Vec3) -> Vec3 { fn mul(self, v: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: v.x * self, x: v.x * self,
@ -207,6 +225,7 @@ impl Mul<Vec3> for f32 {
impl Mul<f32> for Vec3 { impl Mul<f32> for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn mul(self, r: f32) -> Vec3 { fn mul(self, r: f32) -> Vec3 {
Vec3 { Vec3 {
x: self.x * r, x: self.x * r,
@ -219,6 +238,7 @@ impl Mul<f32> for Vec3 {
impl Neg for Vec3 { impl Neg for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn neg(self) -> Vec3 { fn neg(self) -> Vec3 {
-1. * self -1. * self
} }
@ -227,6 +247,7 @@ impl Neg for Vec3 {
impl Sub for Vec3 { impl Sub for Vec3 {
type Output = Vec3; type Output = Vec3;
#[inline]
fn sub(self, r: Vec3) -> Vec3 { fn sub(self, r: Vec3) -> Vec3 {
Vec3 { Vec3 {
x: self.x - r.x, x: self.x - r.x,