(triangles: &[Triangle])
| 849 | } |
| 850 | |
| 851 | fn build_bvh(triangles: &[Triangle]) -> Bvh { |
| 852 | let items: Vec<BvhItem> = triangles |
| 853 | .iter() |
| 854 | .enumerate() |
| 855 | .map(|(i, t)| { |
| 856 | let bmin = t.v0.min(t.v1).min(t.v2); |
| 857 | let bmax = t.v0.max(t.v1).max(t.v2); |
| 858 | BvhItem { |
| 859 | bounds_min: bmin, |
| 860 | bounds_max: bmax, |
| 861 | centroid: (bmin + bmax) * 0.5, |
| 862 | triangle_index: i as u32, |
| 863 | } |
| 864 | }) |
| 865 | .collect(); |
| 866 | |
| 867 | let mut items_mut = items; |
| 868 | let mut nodes: Vec<BvhNode> = Vec::new(); |
| 869 | let mut order: Vec<u32> = Vec::with_capacity(triangles.len()); |
| 870 | nodes.push(BvhNode { |
| 871 | bounds_min: Vec3::ZERO, |
| 872 | bounds_max: Vec3::ZERO, |
| 873 | first_triangle: 0, |
| 874 | tri_count: 0, |
| 875 | }); |
| 876 | build_bvh_recursive(&mut items_mut, 0, &mut nodes, &mut order, 0); |
| 877 | Bvh { |
| 878 | nodes, |
| 879 | triangle_indices: order, |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | fn intersect_bvh(ray: &Ray, scene: &Scene, bvh: &Bvh) -> Option<Hit> { |
| 884 | let mut closest: Option<Hit> = None; |
no test coverage detected