Complete BVH building
This commit is contained in:
parent
556f50274d
commit
7d122d44b3
5 changed files with 207 additions and 17 deletions
|
|
@ -26,6 +26,21 @@ impl AABB {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn extend(self, other: Pos3) -> Self {
|
||||
Self {
|
||||
min: Pos3::new(
|
||||
Float::min(self.min.x, other.x),
|
||||
Float::min(self.min.y, other.y),
|
||||
Float::min(self.min.z, other.z),
|
||||
),
|
||||
max: Pos3::new(
|
||||
Float::max(self.max.x, other.x),
|
||||
Float::max(self.max.y, other.y),
|
||||
Float::max(self.max.z, other.z),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min(self) -> Pos3 {
|
||||
self.min
|
||||
}
|
||||
|
|
@ -43,7 +58,64 @@ impl AABB {
|
|||
&& pos.z <= self.max.z
|
||||
}
|
||||
|
||||
pub fn size(self) -> Dir3 {
|
||||
Dir3::new(
|
||||
self.max.x - self.min.x,
|
||||
self.max.y - self.min.y,
|
||||
self.max.z - self.min.z,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn intersect_ray(self, ray: Ray, min: Float, max: Float) -> Option<Float> {
|
||||
todo!()
|
||||
let t_min_x = (self.min.x() - ray.start().x()) / ray.dir().x();
|
||||
let t_min_y = (self.min.y() - ray.start().y()) / ray.dir().y();
|
||||
let t_min_z = (self.min.z() - ray.start().z()) / ray.dir().z();
|
||||
|
||||
let t_max_x = (self.max.x() - ray.start().x()) / ray.dir().x();
|
||||
let t_max_y = (self.max.y() - ray.start().y()) / ray.dir().y();
|
||||
let t_max_z = (self.max.z() - ray.start().z()) / ray.dir().z();
|
||||
|
||||
let t_min = Float::max(
|
||||
Float::max(Float::min(t_min_x, t_max_x), Float::min(t_min_y, t_max_y)),
|
||||
Float::max(Float::min(t_min_z, t_max_z), min),
|
||||
);
|
||||
let t_max = Float::min(
|
||||
Float::min(Float::max(t_min_x, t_max_x), Float::max(t_min_y, t_max_y)),
|
||||
Float::min(Float::max(t_min_z, t_max_z), max),
|
||||
);
|
||||
|
||||
if t_min < t_max {
|
||||
Some(t_min)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use super::AABB;
|
||||
use crate::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn intersection() {
|
||||
let aabb = AABB::new(Pos3::new(-1.0, -1.0, -1.0), Pos3::new(1.0, 1.0, 1.0));
|
||||
|
||||
let r = Ray::new(Pos3::new(-2.0, 0.0, 0.0), Dir3::new(1.0, 0.0, 0.0), 0.0);
|
||||
|
||||
assert_eq!(aabb.intersect_ray(r, 0.0, 10.0), Some(1.0));
|
||||
|
||||
let r = Ray::new(
|
||||
Pos3::new(-2.0, -2.0, 0.0),
|
||||
Dir3::new(1.0, 1.0, 0.0).normalize(),
|
||||
0.0,
|
||||
);
|
||||
|
||||
assert_eq!(aabb.intersect_ray(r, 0.0, 10.0), Some(FloatConsts::SQRT_2));
|
||||
|
||||
let r = Ray::new(Pos3::new(0.0, 0.0, 0.0), Dir3::new(1.0, 0.0, 0.0), 0.0);
|
||||
|
||||
assert_eq!(aabb.intersect_ray(r, 0.0, 10.0), Some(0.0));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
use crate::prelude::*;
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub trait Light<R: Rng>: Sync {
|
||||
pub trait Light<R: Rng>: Sync + Debug {
|
||||
fn emit(&self, w_in: Dir3, rng: &mut R) -> Color;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AreaLight {
|
||||
pub(crate) color: Color,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::prelude::*;
|
||||
use std::ops::{Add, Div, Mul, Neg, Sub};
|
||||
use std::ops::{Add, Div, Index, Mul, Neg, Sub};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Dir3 {
|
||||
|
|
@ -180,3 +180,16 @@ impl Neg for Dir3 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<usize> for Dir3 {
|
||||
type Output = Float;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
match index {
|
||||
0 => &self.x,
|
||||
1 => &self.y,
|
||||
2 => &self.z,
|
||||
_ => panic!("Index out of bound {}", index),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::prelude::*;
|
||||
use std::ops::{Add, Sub};
|
||||
use core::panic;
|
||||
use std::ops::{Add, Index, Sub};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Pos3 {
|
||||
|
|
@ -32,7 +33,7 @@ impl Pos3 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Sub for pos3::Pos3 {
|
||||
impl Sub for Pos3 {
|
||||
type Output = Dir3;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
|
|
@ -44,14 +45,27 @@ impl Sub for pos3::Pos3 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Add<Dir3> for pos3::Pos3 {
|
||||
type Output = pos3::Pos3;
|
||||
impl Add<Dir3> for Pos3 {
|
||||
type Output = Pos3;
|
||||
|
||||
fn add(self, rhs: Dir3) -> Self::Output {
|
||||
pos3::Pos3 {
|
||||
Pos3 {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y,
|
||||
z: self.z + rhs.z,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<usize> for Pos3 {
|
||||
type Output = Float;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
match index {
|
||||
0 => &self.x,
|
||||
1 => &self.y,
|
||||
2 => &self.z,
|
||||
_ => panic!("Index out of bound {}", index),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue