Skip to content

Commit

Permalink
Add Ray/Intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidPeicho committed Dec 12, 2024
1 parent b2a4c96 commit fe6d1c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/cxx_ffi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Ensure `Intersection` always has a trivial move ctor and no destructor
unsafe impl cxx::ExternType for crate::Intersection {
type Id = cxx::type_id!("tinybvh::Intersection");
type Kind = cxx::kind::Trivial;
}
// Ensure `Ray` always has a trivial move ctor and no destructor
unsafe impl cxx::ExternType for crate::Ray {
type Id = cxx::type_id!("tinybvh::Ray");
Expand All @@ -18,6 +23,7 @@ pub(crate) mod ffi {
pub type bvhvec4;
pub type BVHNode;
pub type Ray = crate::Ray;
pub type Intersection = crate::Intersection;

// pub type Ray;

Expand Down
31 changes: 9 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod cxx_ffi;
mod errors;
mod layouts;
mod ray;
mod traversal;

pub(crate) use cxx_ffi::ffi;
pub use layouts::*;
pub use ray::*;
pub use traversal::*;

pub struct NodeId(pub u32);
Expand All @@ -19,34 +21,13 @@ impl NodeId {
}
}

#[repr(C)]
#[derive(Clone, Copy, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Intersection {
t: f32,
u: f32,
v: f32,
prim: u32,
}

#[repr(C, align(16))]
#[derive(Clone, Copy, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Ray {
origin: [f32; 3],
padding_0: u32,
dir: [f32; 3],
padding_1: u32,
r_d: [f32; 3],
padding_2: u32,
hit: Intersection,
}

//
// Tests
//

#[cfg(test)]
mod tests {
use crate::{BVHNode, Node4, BVH, BVH4};
use crate::{BVHNode, Intersector, Node4, Ray, BVH, BVH4};

const CUBE_INDICES: [u16; 36] = [
0, 1, 2, 2, 3, 0, // top
Expand Down Expand Up @@ -183,5 +164,11 @@ mod tests {
},
];
assert_eq!(bvh4.nodes(), expected);

let origin = [-1.5, 0.5, 0.0];
let mut ray = Ray::new(origin, [0.0, 0.0, -1.0]);
bvh4.intersect(&mut ray);
println!("{:?}", ray.hit);
// assert_eq!(bvh4.intersect(&mut ray), 0);
}
}
30 changes: 30 additions & 0 deletions src/ray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Intersection {
t: f32,
u: f32,
v: f32,
prim: u32,
}

#[repr(C, align(16))]
#[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Ray {
pub origin: [f32; 3],
pub padding_0: u32,
pub dir: [f32; 3],
pub padding_1: u32,
pub r_d: [f32; 3],
pub padding_2: u32,
pub hit: Intersection,
}

impl Ray {
pub fn new(origin: [f32; 3], dir: [f32; 3]) -> Self {
Self {
origin,
dir,
..Default::default()
}
}
}

0 comments on commit fe6d1c7

Please sign in to comment.