Skip to main content

Camera

Struct Camera 

Source
pub struct Camera {
    pub transform: CamTransform,
}
Expand description

A 3D camera with perspective or orthographic projection.

Wraps a CamTransform that holds position, rotation, FOV, clip distances, and pre-computed view/projection matrices. After mutating any transform property, call pre_update to recalculate.

§Conventions

  • Y-up world coordinate system.
  • -Z forward — the camera looks down its local -Z axis by default.
  • Euler angles in degrees, applied in XYZ order.

§Movement methods

DirectionMethodAxis
Forward (in look direction)fly_forwCamera-local -Z
Backwardfly_backCamera-local +Z
Left (strafe)fly_leftCamera-local -X
Right (strafe)fly_rightCamera-local +X
Upfly_upWorld +Y
Downfly_downWorld -Y

§Rotation methods

MethodEffectCommon name
spin_xPitch (look up/down)Tilt forward/backward
spin_yYaw (look left/right)Turn head side-to-side
spin_zRoll (rotate view)Tilt horizon

§Getters and setters

PropertyGetterSetter
Field of view (degrees)fovset_fov, add_fov
Orthographic scaleortho_scaleset_ortho_scale, add_ortho_scale
Projection modeprojset_proj
Clip distancesclipset_clip, set_clip_near, set_clip_far
Viewport sizeset_size
View/proj matricespre_update

§Example

use optic_core::{CamProj, Size2D};
use optic_render::Camera;

let mut cam = Camera::new(Size2D::from(1920, 1080), CamProj::Persp);
cam.fly_forw(10.0);       // move in look direction
cam.spin_y(-90.0);        // yaw left 90°
cam.pre_update();          // recalculate matrices

Fields§

§transform: CamTransform

Implementations§

Source§

impl Camera

Source

pub fn match_canvas_size(canvas: &Canvas, proj: CamProj) -> Self

Creates a camera sized to match the given canvas dimensions.

Source

pub fn new(size: Size2D, proj: CamProj) -> Self

Creates a camera at (0, 0, 5) with 75° FOV, default clip distances, and the given projection type.

Source

pub fn pre_update(&mut self)

Recalculates the view and projection matrices from the current transform state.

Call this once per frame after all movement (fly_forw, etc.) and rotation (spin_y, etc.) have been applied. The matrices are consumed by the rendering pipeline — without this call the camera will continue using stale matrices from the previous frame.

Source

pub fn fov(&self) -> f32

Returns the vertical field of view in degrees.

Source

pub fn ortho_scale(&self) -> f32

Returns the orthographic scale factor.

Source

pub fn proj(&self) -> CamProj

Returns the current projection type.

Source

pub fn clip(&self) -> ClipDist

Returns the near/far clip distances.

Source

pub fn set_clip(&mut self, clip: ClipDist)

Sets both near and far clip distances at once.

Source

pub fn set_clip_near(&mut self, near: f32)

Sets the near clip plane distance.

Source

pub fn set_clip_far(&mut self, far: f32)

Sets the far clip plane distance.

Source

pub fn set_size(&mut self, size: Size2D)

Sets the canvas/viewport size (used for aspect ratio calculation).

Source

pub fn set_proj(&mut self, proj: CamProj)

Switches between perspective and orthographic projection.

Source

pub fn set_fov(&mut self, fov: f32)

Sets the vertical field of view in degrees (clamped to ≥ 0.01).

Source

pub fn add_fov(&mut self, value: f32)

Adds value to the FOV (clamped to ≥ 0.01).

Source

pub fn set_ortho_scale(&mut self, value: f32)

Sets the orthographic scale factor.

Source

pub fn add_ortho_scale(&mut self, value: f32)

Adds value to the orthographic scale factor.

Source

pub fn fly_forw(&mut self, speed: f32)

Moves the camera forward (in the direction it faces).

The forward direction is derived from the camera’s current rotation (stored as front in CamTransform). For the default orientation this moves along world -Z.

§When to use

Call this each frame with speed * delta_time for smooth first-person movement. Call pre_update after all movement and rotation for the frame.

Source

pub fn fly_back(&mut self, speed: f32)

Moves the camera backward (opposite the direction it faces).

The inverse of fly_forw. Equivalent to fly_forw(-speed).

Source

pub fn fly_left(&mut self, speed: f32)

Moves the camera left (strafe), perpendicular to the forward direction.

The strafe direction is computed as front × world_up, producing a vector orthogonal to both the look direction and the world Y axis. This keeps the horizon level even when pitching up or down.

Source

pub fn fly_right(&mut self, speed: f32)

Moves the camera right (strafe), perpendicular to the forward direction.

The inverse of fly_left. Equivalent to fly_left(-speed).

Source

pub fn fly_up(&mut self, speed: f32)

Moves the camera straight up (world Y axis).

Unlike fly_forw / fly_left, this always moves along the world Y axis regardless of the camera’s current pitch or roll.

Source

pub fn fly_down(&mut self, speed: f32)

Moves the camera straight down (world Y axis).

The inverse of fly_up. Equivalent to fly_up(-speed).

Source

pub fn spin_x(&mut self, speed: f32)

Pitches the camera up or down (rotation around the local X axis).

Positive values tilt the view downward (looking toward the ground); negative values tilt upward (looking toward the sky).

§When to use

Combine with spin_y for full free-look control (e.g. mouse-look in a first-person game).

Source

pub fn spin_y(&mut self, speed: f32)

Yaws the camera left or right (rotation around the local Y axis).

Positive values turn right; negative values turn left. This is the primary rotation for first-person horizontal look-around.

Source

pub fn spin_z(&mut self, speed: f32)

Rolls the camera (rotation around the local Z axis).

Tilts the horizon. Rarely used in first-person games; useful for cinematic cameras or flight simulators.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>