Skip to main content

Camera

Struct Camera 

Source
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: Vec3

Eye position in render space.

§target: Vec3

Point the camera looks at.

§up: Vec3

World up. Must not be parallel to target - eye.

§projection: Projection

How the world flattens into clip space.

Implementations§

Source§

impl Camera

Source

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.

Source

pub fn view_projection(&self, aspect: f32) -> Mat4

The combined world → clip matrix for an aspect viewport.

§Panics

As Projection::matrix.

Source

pub fn with_projection(self, projection: Projection) -> Self

The same camera with a different projection — milestone 5, entire.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Camera

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Camera

Source§

impl Debug for Camera

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Camera

Source§

fn default() -> Self

Two metres back along +Z, looking at the origin, Y up, default perspective.

Source§

impl PartialEq for Camera

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Camera

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HalThreadSafe for T
where T: Send + Sync,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.