test(renderer): relocate frustum tests into src/tests
This commit is contained in:
parent
bb4e591a09
commit
748e783f1e
|
|
@ -62,40 +62,5 @@ impl Frustum {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
#[path = "tests/frustum.rs"]
|
||||||
use super::Frustum;
|
mod tests;
|
||||||
use glam::Vec3;
|
|
||||||
|
|
||||||
/// Builds a frustum for a camera at the origin looking down the -Z axis, matching the engine's right-handed Vulkan-clip projection.
|
|
||||||
fn forward_facing_frustum() -> Frustum {
|
|
||||||
let proj = glam::camera::rh::proj::vulkan::perspective(60f32.to_radians(), 1.0, 0.1, 100.0);
|
|
||||||
let view = glam::camera::rh::view::look_at_mat4(Vec3::ZERO, Vec3::NEG_Z, Vec3::Y);
|
|
||||||
Frustum::from_view_proj(proj * view)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn box_in_front_is_visible() {
|
|
||||||
let frustum = forward_facing_frustum();
|
|
||||||
assert!(frustum.intersects_aabb(Vec3::new(-1.0, -1.0, -6.0), Vec3::new(1.0, 1.0, -4.0)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn box_behind_camera_is_culled() {
|
|
||||||
// A box entirely behind the camera. This is the case that fails if the near plane is extracted with the OpenGL `r3 + r2` formula instead of `r2`.
|
|
||||||
let frustum = forward_facing_frustum();
|
|
||||||
assert!(!frustum.intersects_aabb(Vec3::new(-1.0, -1.0, 4.0), Vec3::new(1.0, 1.0, 6.0)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn box_far_to_the_side_is_culled() {
|
|
||||||
// Well outside the horizontal field of view at an otherwise valid depth.
|
|
||||||
let frustum = forward_facing_frustum();
|
|
||||||
assert!(!frustum.intersects_aabb(Vec3::new(50.0, -1.0, -5.0), Vec3::new(52.0, 1.0, -4.0)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn huge_box_straddling_origin_is_visible() {
|
|
||||||
let frustum = forward_facing_frustum();
|
|
||||||
assert!(frustum.intersects_aabb(Vec3::splat(-100.0), Vec3::splat(100.0)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
39
crates/renderer/src/tests/frustum.rs
Normal file
39
crates/renderer/src/tests/frustum.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
//! Unit tests for view-frustum culling in [`crate::frustum`].
|
||||||
|
|
||||||
|
use super::Frustum;
|
||||||
|
use glam::Vec3;
|
||||||
|
|
||||||
|
/// Builds a frustum for a camera at the origin looking down the -Z axis, matching the engine's right-handed Vulkan-clip projection.
|
||||||
|
fn forward_facing_frustum() -> Frustum {
|
||||||
|
let proj = glam::camera::rh::proj::vulkan::perspective(60f32.to_radians(), 1.0, 0.1, 100.0);
|
||||||
|
let view = glam::camera::rh::view::look_at_mat4(Vec3::ZERO, Vec3::NEG_Z, Vec3::Y);
|
||||||
|
Frustum::from_view_proj(proj * view)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn box_in_front_is_visible() {
|
||||||
|
let frustum = forward_facing_frustum();
|
||||||
|
assert!(frustum.intersects_aabb(Vec3::new(-1.0, -1.0, -6.0), Vec3::new(1.0, 1.0, -4.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn box_behind_camera_is_culled() {
|
||||||
|
// A box entirely behind the camera. This is the case that fails if the near plane is extracted with the OpenGL `r3 + r2` formula instead of `r2`.
|
||||||
|
let frustum = forward_facing_frustum();
|
||||||
|
assert!(!frustum.intersects_aabb(Vec3::new(-1.0, -1.0, 4.0), Vec3::new(1.0, 1.0, 6.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn box_far_to_the_side_is_culled() {
|
||||||
|
// Well outside the horizontal field of view at an otherwise valid depth.
|
||||||
|
let frustum = forward_facing_frustum();
|
||||||
|
assert!(!frustum.intersects_aabb(Vec3::new(50.0, -1.0, -5.0), Vec3::new(52.0, 1.0, -4.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn huge_box_straddling_origin_is_visible() {
|
||||||
|
let frustum = forward_facing_frustum();
|
||||||
|
assert!(frustum.intersects_aabb(Vec3::splat(-100.0), Vec3::splat(100.0)));
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue