use core::f32::consts::FRAC_PI_4;
use glam::{Mat4, Vec2, Vec3, Vec4};
const RAY_DEPTH: f32 = 0.5;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Projection {
Perspective {
fov_y: f32,
near: f32,
},
Orthographic {
half_height: f32,
near: f32,
far: f32,
},
}
impl Default for Projection {
fn default() -> Self {
Self::Perspective {
fov_y: FRAC_PI_4,
near: 0.1,
}
}
}
impl Projection {
#[must_use]
pub fn matrix(self, aspect: f32) -> Mat4 {
assert!(
aspect.is_finite() && aspect > 0.0,
"aspect ratio must be finite and positive, got {aspect}"
);
match self {
Self::Perspective { fov_y, near } => {
assert!(near > 0.0, "the near plane must be positive, got {near}");
assert!(
fov_y > 0.0 && fov_y < core::f32::consts::PI,
"the field of view must be in (0, π), got {fov_y}"
);
glam::camera::rh::proj::directx::perspective_infinite_reverse(fov_y, aspect, near)
}
Self::Orthographic {
half_height,
near,
far,
} => {
assert!(
half_height > 0.0,
"the orthographic half-height must be positive, got {half_height}"
);
assert!(far > near, "far {far} must be beyond near {near}");
let half_width = half_height * aspect;
glam::camera::rh::proj::directx::orthographic(
-half_width,
half_width,
-half_height,
half_height,
far,
near,
)
}
}
}
#[must_use]
pub const fn is_orthographic(self) -> bool {
matches!(self, Self::Orthographic { .. })
}
#[must_use]
pub fn pixels_per_unit(self, height: f32) -> f32 {
match self {
Self::Perspective { fov_y, .. } => {
assert!(
fov_y > 0.0 && fov_y < core::f32::consts::PI,
"the field of view must be in (0, π), got {fov_y}"
);
0.5 * height / (0.5 * fov_y).tan()
}
Self::Orthographic { half_height, .. } => {
assert!(
half_height > 0.0,
"the orthographic half-height must be positive, got {half_height}"
);
0.5 * height / half_height
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Camera {
pub eye: Vec3,
pub target: Vec3,
pub up: Vec3,
pub projection: Projection,
}
impl Default for Camera {
fn default() -> Self {
Self {
eye: Vec3::new(0.0, 0.0, 2.0),
target: Vec3::ZERO,
up: Vec3::Y,
projection: Projection::default(),
}
}
}
impl Camera {
#[must_use]
pub fn view(&self) -> Mat4 {
let direction = self.target - self.eye;
assert!(
direction.length_squared() > 0.0 && direction.is_finite(),
"the camera must look somewhere: eye {:?} and target {:?}",
self.eye,
self.target
);
assert!(
self.up.length_squared() > 0.0 && self.up.is_finite(),
"the camera's up vector must be non-zero and finite, got {:?}",
self.up
);
assert!(
direction
.normalize()
.cross(self.up.normalize())
.length_squared()
> f32::EPSILON,
"the camera's up vector {:?} is parallel to its view direction {direction:?}; there \
is no basis to build",
self.up
);
glam::camera::rh::view::look_at_mat4(self.eye, self.target, self.up.normalize())
}
#[must_use]
pub fn view_projection(&self, aspect: f32) -> Mat4 {
self.projection.matrix(aspect) * self.view()
}
#[must_use]
pub fn with_projection(mut self, projection: Projection) -> Self {
self.projection = projection;
self
}
#[must_use]
pub fn depth_of(&self, point: Vec3, aspect: f32) -> Option<f32> {
let clip = self.view_projection(aspect) * point.extend(1.0);
(clip.w > f32::MIN_POSITIVE).then(|| clip.z / clip.w)
}
#[must_use]
pub fn ray_through(&self, at: Vec2, extent: (u32, u32)) -> ViewRay {
let (width, height) = extent;
assert!(
width > 0 && height > 0,
"a viewport with a zero side has no pixel to shoot a ray through, got {width}x{height}"
);
let size = Vec2::new(width as f32, height as f32);
let ndc = Vec2::new(at.x / size.x * 2.0 - 1.0, 1.0 - at.y / size.y * 2.0);
let inverse = self.view_projection(size.x / size.y).inverse();
let unproject = |depth: f32| {
let point = inverse * Vec4::new(ndc.x, ndc.y, depth, 1.0);
point.truncate() / point.w
};
let origin = unproject(crcbl_hal::depth::NEAR);
ViewRay {
origin,
direction: (unproject(RAY_DEPTH) - origin).normalize(),
}
}
#[must_use]
pub fn pixel_of(&self, point: Vec3, extent: (u32, u32)) -> Option<Vec2> {
let (width, height) = extent;
assert!(
width > 0 && height > 0,
"a viewport with a zero side has no pixel to land on, got {width}x{height}"
);
let size = Vec2::new(width as f32, height as f32);
let clip = self.view_projection(size.x / size.y) * point.extend(1.0);
if clip.w <= f32::MIN_POSITIVE {
return None;
}
let ndc = Vec2::new(clip.x, clip.y) / clip.w;
Some(Vec2::new(
(ndc.x + 1.0) * 0.5 * size.x,
(1.0 - ndc.y) * 0.5 * size.y,
))
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ViewRay {
pub origin: Vec3,
pub direction: Vec3,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DirectionalLight {
pub direction: Vec3,
pub color: Vec3,
pub ambient: Vec3,
}
impl Default for DirectionalLight {
fn default() -> Self {
Self {
direction: Vec3::new(0.4, 0.8, 0.6).normalize(),
color: Vec3::new(1.60, 1.55, 1.45),
ambient: Vec3::splat(0.08),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Fog {
pub density: f32,
pub falloff: f32,
pub reference_height: f32,
pub color: Vec3,
pub sun_scattering: f32,
pub light_scattering: f32,
pub anisotropy: f32,
}
impl Fog {
pub const NONE: Self = Self {
density: 0.0,
falloff: 0.0,
reference_height: 0.0,
color: Vec3::ZERO,
sun_scattering: 0.0,
light_scattering: 0.0,
anisotropy: 0.0,
};
}
impl Default for Fog {
fn default() -> Self {
Self::NONE
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Sky {
pub zenith: Vec3,
pub horizon: Vec3,
pub ground: Vec3,
}
impl Sky {
pub const NONE: Self = Self {
zenith: Vec3::ZERO,
horizon: Vec3::ZERO,
ground: Vec3::ZERO,
};
pub(crate) fn gradient(self) -> crcbl_shaders::sky::SkyGradient {
crcbl_shaders::sky::SkyGradient {
zenith: self.zenith.to_array(),
horizon: self.horizon.to_array(),
ground: self.ground.to_array(),
}
}
}
impl Default for Sky {
fn default() -> Self {
Self::NONE
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Atmosphere {
pub sun_direction: Vec3,
pub sun_illuminance: Vec3,
pub altitude_km: f32,
pub km_per_unit: f32,
}
impl Atmosphere {
pub const NOON: Self = Self {
sun_direction: Vec3::Y,
sun_illuminance: Vec3::ONE,
altitude_km: 0.0,
km_per_unit: crcbl_shaders::atmosphere::KM_PER_METRE,
};
#[must_use]
pub fn parameters(self) -> crcbl_shaders::atmosphere::Atmosphere {
let direction = self.sun_direction.normalize_or(Vec3::Y);
crcbl_shaders::atmosphere::Atmosphere {
sun_direction: direction.to_array(),
sun_illuminance: self.sun_illuminance.to_array(),
altitude_km: self.altitude_km,
km_per_unit: self.km_per_unit,
}
}
}
impl Default for Atmosphere {
fn default() -> Self {
Self::NOON
}
}
#[cfg(test)]
mod tests {
use super::*;
const ASPECT: f32 = 16.0 / 9.0;
fn standard_z(near: f32, far: f32) -> Mat4 {
glam::camera::rh::proj::directx::perspective(FRAC_PI_4, ASPECT, near, far)
}
fn reversed() -> Camera {
Camera {
eye: Vec3::ZERO,
target: -Vec3::Z,
up: Vec3::Y,
projection: Projection::Perspective {
fov_y: FRAC_PI_4,
near: 0.1,
},
}
}
#[test]
fn perspective_depth_runs_from_one_at_the_near_plane_to_zero_at_infinity() {
let camera = reversed();
let near = camera
.depth_of(Vec3::new(0.0, 0.0, -0.1), ASPECT)
.expect("in front of the eye");
assert!(
(near - crcbl_hal::depth::NEAR).abs() < 1e-5,
"a point on the near plane must have depth {}, got {near}",
crcbl_hal::depth::NEAR
);
let far = camera
.depth_of(Vec3::new(0.0, 0.0, -1.0e9), ASPECT)
.expect("in front of the eye");
assert!(
far.abs() < 1e-6,
"a point at effectively infinity must approach {}, got {far}",
crcbl_hal::depth::FAR
);
let mut previous = f32::INFINITY;
for distance in [0.1f32, 0.5, 1.0, 10.0, 100.0, 1000.0, 100_000.0] {
let depth = camera
.depth_of(Vec3::new(0.0, 0.0, -distance), ASPECT)
.expect("in front of the eye");
assert!(
depth < previous,
"depth must decrease with distance under reversed-Z: {distance} m gave \
{depth}, which is not below {previous}"
);
assert!((0.0..=1.0).contains(&depth), "{distance} m gave {depth}");
previous = depth;
}
}
#[test]
fn reversed_z_resolves_a_centimetre_at_300_metres_and_standard_z_does_not() {
let camera = reversed();
let near_surface = Vec3::new(0.0, 0.0, -300.0);
let far_surface = Vec3::new(0.0, 0.0, -300.01);
let reversed_near = camera.depth_of(near_surface, ASPECT).expect("in front");
let reversed_far = camera.depth_of(far_surface, ASPECT).expect("in front");
assert_ne!(
reversed_near, reversed_far,
"reversed-Z must resolve 1 cm at 300 m; both quantised to {reversed_near}"
);
assert!(
reversed_near > reversed_far,
"the nearer surface must hold the larger value under reversed-Z"
);
let conventional = standard_z(0.1, 10_000.0);
let depth = |point: Vec3| {
let clip = conventional * point.extend(1.0);
clip.z / clip.w
};
let (standard_near, standard_far) = (depth(near_surface), depth(far_surface));
assert_eq!(
standard_near, standard_far,
"the control is only meaningful if conventional depth genuinely \
collapses here; it gave {standard_near} and {standard_far}, which \
differ — re-derive the distances rather than relaxing the test"
);
}
#[test]
fn the_projection_agrees_with_the_seams_depth_defaults() {
use crcbl_hal::{ClearValue, CompareOp, DepthStencilState, Format};
let state = DepthStencilState::default();
assert_eq!(state.depth_compare, CompareOp::Greater);
assert_eq!(state.format, Format::D32Float);
assert_eq!(ClearValue::default().depth, crcbl_hal::depth::CLEAR);
let camera = reversed();
for distance in [0.11f32, 1.0, 50.0, 5_000.0] {
let depth = camera
.depth_of(Vec3::new(0.0, 0.0, -distance), ASPECT)
.expect("in front");
assert!(
depth > ClearValue::default().depth,
"geometry at {distance} m must beat the cleared far plane, got {depth}"
);
}
}
#[test]
fn switching_to_orthographic_changes_only_the_projection() {
let perspective = Camera::default();
let ortho = perspective.with_projection(Projection::Orthographic {
half_height: 1.0,
near: 0.1,
far: 100.0,
});
assert_eq!(perspective.eye, ortho.eye);
assert_eq!(perspective.target, ortho.target);
assert_eq!(perspective.up, ortho.up);
assert_eq!(
perspective.view(),
ortho.view(),
"the view matrix is a property of where the camera is, not of how it projects"
);
assert!(!perspective.projection.is_orthographic());
assert!(ortho.projection.is_orthographic());
assert_ne!(
perspective.view_projection(ASPECT),
ortho.view_projection(ASPECT)
);
}
#[test]
fn orthographic_depth_makes_a_larger_z_draw_on_top() {
let camera = Camera {
eye: Vec3::new(0.0, 0.0, 100.0),
target: Vec3::ZERO,
up: Vec3::Y,
projection: Projection::Orthographic {
half_height: 10.0,
near: 0.1,
far: 200.0,
},
};
let mut previous = f32::NEG_INFINITY;
for z_index in [0.0f32, 1.0, 2.0, 5.0, 20.0] {
let depth = camera
.depth_of(Vec3::new(0.0, 0.0, z_index), ASPECT)
.expect("orthographic w is always 1");
assert!(
(0.0..=1.0).contains(&depth),
"z-index {z_index} projected outside the depth range: {depth}"
);
assert!(
depth > previous,
"z-index {z_index} gave depth {depth}, which does not beat {previous}; \
a larger z must draw on top under the seam's `Greater` test"
);
previous = depth;
}
}
#[test]
fn orthographic_depth_is_linear_in_z() {
let camera = Camera {
eye: Vec3::new(0.0, 0.0, 100.0),
target: Vec3::ZERO,
up: Vec3::Y,
projection: Projection::Orthographic {
half_height: 10.0,
near: 0.0,
far: 100.0,
},
};
let depth = |z: f32| {
camera
.depth_of(Vec3::new(0.0, 0.0, z), ASPECT)
.expect("orthographic w is always 1")
};
let step = depth(1.0) - depth(0.0);
for z in [1.0f32, 2.0, 10.0, 50.0] {
let measured = depth(z + 1.0) - depth(z);
assert!(
(measured - step).abs() < 1e-6,
"the depth step at z={z} is {measured}, not the constant {step}"
);
}
assert!(step > 0.0, "successive layers must increase in depth");
}
#[test]
fn a_wider_viewport_shows_more_world_and_does_not_stretch_it() {
for projection in [
Projection::default(),
Projection::Orthographic {
half_height: 1.0,
near: 0.1,
far: 10.0,
},
] {
let square = projection.matrix(1.0);
let wide = projection.matrix(2.0);
assert!(
(square.y_axis.y - wide.y_axis.y).abs() < 1e-6,
"{projection:?}"
);
assert!(wide.x_axis.x < square.x_axis.x, "{projection:?}");
}
}
#[test]
#[should_panic(expected = "aspect ratio")]
fn a_zero_aspect_ratio_is_refused() {
let _ = Projection::default().matrix(0.0);
}
#[test]
#[should_panic(expected = "near plane")]
fn a_zero_near_plane_is_refused() {
let _ = Projection::Perspective {
fov_y: FRAC_PI_4,
near: 0.0,
}
.matrix(ASPECT);
}
#[test]
#[should_panic(expected = "must be beyond near")]
fn an_inverted_orthographic_range_is_refused() {
let _ = Projection::Orthographic {
half_height: 1.0,
near: 10.0,
far: 1.0,
}
.matrix(ASPECT);
}
#[test]
fn the_default_light_is_normalised_and_has_headroom() {
let light = DirectionalLight::default();
assert!((light.direction.length() - 1.0).abs() < 1e-6);
assert!(
light.ambient.min_element() > 0.0,
"an unlit face must not be black"
);
assert!(
light.ambient.max_element() < 0.5,
"ambient must not flatten the shading"
);
assert!(
light.color.max_element() > 1.0 / 0.9,
"the key light must have HDR headroom, got {:?}",
light.color
);
let to_camera = Camera::default().eye.normalize();
assert!(
light.direction.dot(to_camera) > 0.0,
"a key light pointing away from every visible face renders an \
unlit cube: light {:?} against camera {to_camera:?}",
light.direction
);
}
#[test]
fn a_point_behind_the_eye_has_no_depth() {
let camera = reversed();
assert_eq!(camera.depth_of(Vec3::new(0.0, 0.0, 10.0), ASPECT), None);
assert_eq!(camera.depth_of(camera.eye, ASPECT), None);
assert!(camera.depth_of(Vec3::new(0.0, 0.0, -1.0), ASPECT).is_some());
}
#[test]
fn an_orthographic_camera_projects_everything() {
let camera = reversed().with_projection(Projection::Orthographic {
half_height: 2.0,
near: 0.1,
far: 100.0,
});
assert!(
camera.depth_of(Vec3::new(0.0, 0.0, 10.0), ASPECT).is_some(),
"an orthographic camera has no eye plane to be behind"
);
}
#[test]
#[should_panic(expected = "parallel to its view direction")]
fn an_up_vector_along_the_view_direction_panics() {
let camera = Camera {
eye: Vec3::new(0.0, 0.0, 2.0),
target: Vec3::ZERO,
up: Vec3::Z,
projection: Projection::default(),
};
let _ = camera.view();
}
#[test]
#[should_panic(expected = "up vector must be non-zero")]
fn a_zero_up_vector_panics() {
let camera = Camera {
up: Vec3::ZERO,
..Camera::default()
};
let _ = camera.view();
}
#[test]
#[should_panic(expected = "must look somewhere")]
fn a_camera_looking_at_its_own_eye_panics() {
let camera = Camera {
target: Vec3::new(0.0, 0.0, 2.0),
..Camera::default()
};
let _ = camera.view();
}
#[test]
fn every_projection_inverts_to_the_sparsity_the_screen_space_shaders_read() {
const DROPPED: [(usize, usize); 8] = [
(0, 1),
(0, 2),
(1, 0),
(1, 2),
(2, 0),
(2, 1),
(3, 0),
(3, 1),
];
let projections = [
(Projection::default(), ASPECT),
(
Projection::Perspective {
fov_y: FRAC_PI_4,
near: 0.1,
},
1264.0 / 1370.0,
),
(
Projection::Perspective {
fov_y: 0.3,
near: 5.0,
},
0.5,
),
(
Projection::Perspective {
fov_y: 2.9,
near: 0.001,
},
3.7,
),
(
Projection::Orthographic {
half_height: 10.0,
near: 0.1,
far: 1000.0,
},
ASPECT,
),
(
Projection::Orthographic {
half_height: 0.5,
near: 1.0,
far: 2.0,
},
0.25,
),
];
for (projection, aspect) in projections {
let inverse = projection.matrix(aspect).inverse();
let columns = inverse.to_cols_array();
let at = |row: usize, column: usize| columns[column * 4 + row];
for (row, column) in DROPPED {
assert_eq!(
at(row, column),
0.0,
"{projection:?} at aspect {aspect} inverts with a non-zero at \
({row}, {column}), which `unproject` in the screen-space shaders drops"
);
}
for ndc in [
glam::Vec2::ZERO,
glam::Vec2::new(-1.0, -1.0),
glam::Vec2::new(1.0, 1.0),
glam::Vec2::new(-0.731, 0.269),
] {
for depth in [0.0, crcbl_hal::depth::NEAR, 0.5, 1.0e-4] {
let full = inverse * glam::Vec4::new(ndc.x, ndc.y, depth, 1.0);
let reduced = glam::Vec4::new(
at(0, 0) * ndc.x + at(0, 3),
at(1, 1) * ndc.y + at(1, 3),
at(2, 2) * depth + at(2, 3),
at(3, 2) * depth + at(3, 3),
);
assert_eq!(
full.to_array().map(f32::to_bits),
reduced.to_array().map(f32::to_bits),
"{projection:?} at aspect {aspect}: the shaders' reduced unprojection \
of {ndc:?} at depth {depth} is not the full product {full:?}"
);
}
}
}
}
#[test]
fn the_screen_space_unprojection_is_the_full_product_for_that_sparsity() {
let sparse = Mat4::from_cols_array(&[
2.5, 0.0, 0.0, 0.0, 0.0, -3.25, 0.0, 0.0, 0.0, 0.0, 0.75, -1.5, 0.125, -0.375, 4.5, 1.25,
]);
let columns = sparse.to_cols_array();
let at = |row: usize, column: usize| columns[column * 4 + row];
for ndc in [
glam::Vec2::new(-0.731, 0.269),
glam::Vec2::new(0.5, -0.125),
glam::Vec2::new(1.0, -1.0),
] {
for depth in [crcbl_hal::depth::NEAR, 0.375, 1.0e-4] {
let full = sparse * glam::Vec4::new(ndc.x, ndc.y, depth, 1.0);
let reduced = glam::Vec4::new(
at(0, 0) * ndc.x + at(0, 3),
at(1, 1) * ndc.y + at(1, 3),
at(2, 2) * depth + at(2, 3),
at(3, 2) * depth + at(3, 3),
);
assert_eq!(
full.to_array().map(f32::to_bits),
reduced.to_array().map(f32::to_bits),
"the shaders' reduced unprojection of {ndc:?} at depth {depth} is not the \
full product {full:?}"
);
}
}
}
const VIEWPORT: (u32, u32) = (800, 600);
fn centre() -> Vec2 {
Vec2::new(VIEWPORT.0 as f32, VIEWPORT.1 as f32) * 0.5
}
fn perspective() -> Camera {
Camera {
eye: Vec3::new(3.0, 4.0, 5.0),
target: Vec3::new(-1.0, 0.5, 0.0),
up: Vec3::Y,
projection: Projection::Perspective {
fov_y: FRAC_PI_4,
near: 0.25,
},
}
}
#[test]
fn a_pixels_ray_projects_back_to_that_pixel() {
for at in [
Vec2::new(0.5, 0.5),
centre(),
Vec2::new(11.5, 523.5),
Vec2::new(799.5, 0.5),
Vec2::new(640.5, 480.5),
] {
let ray = perspective().ray_through(at, VIEWPORT);
for distance in [0.5_f32, 7.0, 250.0] {
let pixel = perspective()
.pixel_of(ray.origin + ray.direction * distance, VIEWPORT)
.expect("a point along the ray is in front of the eye");
assert!(
(pixel - at).length() < 1e-2,
"the ray through {at:?} reached {pixel:?} {distance} m along itself",
);
}
}
}
#[test]
fn the_top_left_pixel_is_up_and_to_the_left_in_the_world() {
let camera = Camera {
eye: Vec3::ZERO,
target: -Vec3::Z,
up: Vec3::Y,
projection: Projection::Perspective {
fov_y: FRAC_PI_4,
near: 0.1,
},
};
let top_left = camera.ray_through(Vec2::new(0.5, 0.5), VIEWPORT);
assert!(
top_left.direction.y > 0.0,
"the top of the window must look up, got {:?}",
top_left.direction,
);
assert!(
top_left.direction.x < 0.0,
"the left of the window must look left, got {:?}",
top_left.direction,
);
let bottom_right = camera.ray_through(Vec2::new(799.5, 599.5), VIEWPORT);
assert!(bottom_right.direction.y < 0.0 && bottom_right.direction.x > 0.0);
}
#[test]
fn a_point_behind_the_eye_lands_on_no_pixel() {
let camera = perspective();
let behind = camera.eye + (camera.eye - camera.target);
assert_eq!(camera.pixel_of(behind, VIEWPORT), None);
assert!(camera.pixel_of(camera.target, VIEWPORT).is_some());
}
#[test]
fn the_ray_begins_at_the_near_plane_depth() {
for camera in [
perspective(),
Camera {
projection: Projection::Orthographic {
half_height: 3.0,
near: 0.5,
far: 40.0,
},
..perspective()
},
] {
let ray = camera.ray_through(Vec2::new(210.5, 99.5), VIEWPORT);
let depth = camera
.depth_of(ray.origin, VIEWPORT.0 as f32 / VIEWPORT.1 as f32)
.expect("the near plane is in front of the eye");
assert!(
(depth - crcbl_hal::depth::NEAR).abs() < 1e-4,
"the ray started at depth {depth}, not the near plane",
);
}
}
#[test]
fn an_orthographic_camera_shoots_parallel_rays_and_a_perspective_one_does_not() {
let orthographic = Camera {
projection: Projection::Orthographic {
half_height: 3.0,
near: 0.5,
far: 40.0,
},
..perspective()
};
let left = Vec2::new(40.5, 300.5);
let right = Vec2::new(760.5, 300.5);
let spread = |camera: &Camera| {
camera
.ray_through(left, VIEWPORT)
.direction
.dot(camera.ray_through(right, VIEWPORT).direction)
};
assert!(
(spread(&orthographic) - 1.0).abs() < 1e-5,
"orthographic rays must be parallel, got a dot of {}",
spread(&orthographic),
);
assert!(
spread(&perspective()) < 0.95,
"perspective rays must diverge, got a dot of {}",
spread(&perspective()),
);
assert!(
orthographic
.ray_through(left, VIEWPORT)
.origin
.distance(orthographic.ray_through(right, VIEWPORT).origin)
> 1.0
);
}
#[test]
fn the_direction_is_unit_length() {
let ray = perspective().ray_through(Vec2::new(1.5, 599.5), VIEWPORT);
assert!((ray.direction.length() - 1.0).abs() < 1e-6);
}
#[test]
#[should_panic(expected = "zero side")]
fn a_viewport_with_a_zero_side_is_refused() {
let _ = perspective().ray_through(centre(), (800, 0));
}
}