pub struct Camera {
pub eye: Vec3,
pub target: Vec3,
pub up: Vec3,
pub projection: Projection,
}Expand description
A camera: where it is, what it looks at, and how it projects.
Positions are plain Vec3 in camera-relative render space, not
WorldPos. That is deliberate and is
the WorldPos rule recorded in docs/notes/backends.md: every simulation
position is a WorldPos, and plain Vec3 is only ever camera-relative
render space. Converting one to the other is the renderer’s job at P2, when
something other than a hardcoded cube has a position at all.
Fields§
§eye: Vec3Eye position in render space.
target: Vec3Point the camera looks at.
up: Vec3World up. Must not be parallel to target - eye.
projection: ProjectionHow the world flattens into clip space.
Implementations§
Source§impl Camera
impl Camera
Sourcepub fn view(&self) -> Mat4
pub fn view(&self) -> Mat4
The view matrix: world → right-handed Y-up view space.
§Panics
If the camera is degenerate: eye and target coincide, up is zero,
or up is parallel to the view direction. Each of those makes
look_at divide by a zero-length vector and hand back a matrix of
NaNs — which propagates through the uniform block into the vertex
shader and shows up as an empty screen with no error anywhere, the same
failure Projection::matrix asserts against.
Sourcepub fn view_projection(&self, aspect: f32) -> Mat4
pub fn view_projection(&self, aspect: f32) -> Mat4
Sourcepub fn with_projection(self, projection: Projection) -> Self
pub fn with_projection(self, projection: Projection) -> Self
The same camera with a different projection — milestone 5, entire.
Sourcepub fn depth_of(&self, point: Vec3, aspect: f32) -> Option<f32>
pub fn depth_of(&self, point: Vec3, aspect: f32) -> Option<f32>
The depth value a point in render space projects to.
The perspective divide, done on the CPU. Exists because it is what makes
the depth convention testable without a GPU: this is the number that
ends up in the D32_SFLOAT buffer, and reversed-Z is a claim about how
it is distributed.
Returns None for a point on or behind the eye plane, where the divide
is undefined.
w > 0, not w.abs() > 0: under a right-handed perspective projection
w is the distance in front of the eye, so a point behind it has a
negative w and dividing by it yields a positive-looking depth for
geometry that is not in the frustum at all. Under
Projection::Orthographic w is always 1, so every point has a
depth and this returns one — which is correct, an orthographic camera
has no eye plane to be behind.
§Panics
As Projection::matrix and Camera::view.
Sourcepub fn ray_through(&self, at: Vec2, extent: (u32, u32)) -> ViewRay
pub fn ray_through(&self, at: Vec2, extent: (u32, u32)) -> ViewRay
The world-space ray through the pixel at at, in a viewport of
extent pixels.
The inverse of depth_of, and the reason it is here
rather than in the caller: both are the perspective divide done on the
CPU, and a screen-to-ray written against a matrix somebody else built is
the classic place a convention gets applied twice or not at all.
at is in the same pixels a ShellEvent reports: (0, 0) is the
top-left corner of the window and y grows downward, which is
crcbl_ui::draw_list’s convention too. A whole pixel is its top-left
corner, so a caller that wants the ray through a pixel’s centre adds
half a pixel to both — what matters for picking, and what a test
projecting a point back must account for.
The ray begins on the near plane rather than at
eye, because that is where the visible world starts and
because Projection::Orthographic has no single eye point at all. The
direction is unit length, so a hit’s parametric distance is a distance
in metres.
use crcbl_render::{Camera, Projection};
use glam::Vec2;
let camera = Camera::default();
let extent = (800, 600);
// The centre of the viewport looks where the camera looks.
let ray = camera.ray_through(Vec2::new(400.0, 300.0), extent);
let forward = (camera.target - camera.eye).normalize();
assert!((ray.direction - forward).length() < 1e-5);§Panics
If extent has a zero side — there is no pixel to shoot through — and
otherwise as Projection::matrix and Camera::view.
Sourcepub fn pixel_of(&self, point: Vec3, extent: (u32, u32)) -> Option<Vec2>
pub fn pixel_of(&self, point: Vec3, extent: (u32, u32)) -> Option<Vec2>
The pixel a point in render space lands on, in a viewport of extent
pixels — ray_through run the other way.
In the same pixels ray_through takes: (0, 0) is the top-left corner
and y grows downward, and the answer is a position rather than a whole
pixel, so the ray through it passes through point. What a marker drawn
over the scene — a health bar, a label, an editor’s handles — is placed
with.
Not clamped to the viewport: a point off to the side answers with a pixel
outside it. None for a point on or behind the eye plane, on
depth_of’s terms, where it lands on no pixel at all.
use crcbl_render::Camera;
use glam::Vec2;
let camera = Camera::default();
let at = camera.pixel_of(camera.target, (800, 600)).expect("in front");
assert!((at - Vec2::new(400.0, 300.0)).length() < 1e-3);§Panics
As ray_through.