39 lines
937 B
Rust

use crate::vec3::Vec3;
#[derive(Copy, Clone, Debug, Default)]
pub struct Ray {
pub origin: Vec3,
pub direction: Vec3,
pub time: f32,
// Precache 1/direction, a single ray intersects multiple AABB's, and divides are more
// expensive than multiplies.
pub inv_direction: Vec3,
pub sign: [usize; 3],
}
impl Ray {
pub fn new<V>(origin: V, direction: V, time: f32) -> Ray
where
V: Into<Vec3>,
{
let direction: Vec3 = direction.into();
let origin = origin.into();
let inv = 1. / direction;
Ray {
origin,
direction,
time,
inv_direction: inv,
sign: [
(inv.x < 0.) as usize,
(inv.y < 0.) as usize,
(inv.z < 0.) as usize,
],
}
}
pub fn point_at_parameter(self, t: f32) -> Vec3 {
self.origin + self.direction * t
}
}