Struct draw2d::camera::OrthoCamera[][src]

pub struct OrthoCamera { /* fields omitted */ }

Implementations

Build a new camera with a given viewport height and aspect ratio.

Params

  • viewport_height defines the height of the view rectangle in world space.
  • aspect_ratio is the ratio of the desired viewport’s width/height.

Get the camera’s full transformation matrix. This can be passed to a shader for transformations.

The camera’s bounds in world-space.

Example

let ortho = OrthoCamera::with_viewport(1.0, 2.0);
let bounds = ortho.bounds();

assert_relative_eq!(bounds.left, -1.0);
assert_relative_eq!(bounds.right, 1.0);
assert_relative_eq!(bounds.top, 0.5);
assert_relative_eq!(bounds.bottom, -0.5);

Set the camera’s position in world-space.

Example

let mut ortho = OrthoCamera::with_viewport(2.0, 1.0);
ortho.set_world_position(&na::Point2::new(30.0, -0.5));

assert_relative_eq!(
  ortho.world_position(),
  na::Point2::new(30.0, -0.5)
);

let bounds = ortho.bounds();
assert_relative_eq!(bounds.left, -1.0 + 30.0);
assert_relative_eq!(bounds.right, 1.0 + 30.0);
assert_relative_eq!(bounds.top, 1.0 - 0.5);
assert_relative_eq!(bounds.bottom, -1.0 - 0.5);

Get the camera’s position in world space.

Example

let pos = OrthoCamera::with_viewport(1.0, 1.0).world_position();

assert_relative_eq!(pos, na::Point2::new(0.0, 0.0));

Resize the viewport’s width such that the viewing rectangle has the desired aspect ratio.

Example

let mut ortho = OrthoCamera::with_viewport(1.0, 1.0);
ortho.set_aspect_ratio(2.0);

let bounds = ortho.bounds();
assert_relative_eq!(bounds.left, -1.0);
assert_relative_eq!(bounds.right, 1.0);
assert_relative_eq!(bounds.top, 0.5);
assert_relative_eq!(bounds.bottom, -0.5);

The camera viewport’s aspect ratio.

Get the height of the viewport.

Get the width of the viewport.

Set the viewport’s height to a new value.

Automatically resizes the viewport’s width to maintain the current aspect ratio.

Example

// a camera which is 3x as wide as it is tall
let mut ortho = OrthoCamera::with_viewport(2.0, 3.0);

assert_relative_eq!(ortho.aspect_ratio(), 3.0);
assert_relative_eq!(ortho.viewport_height(), 2.0);
assert_relative_eq!(ortho.viewport_width(), 6.0);

ortho.set_viewport_height(3.3);

assert_relative_eq!(ortho.aspect_ratio(), 3.0);
assert_relative_eq!(ortho.viewport_height(), 3.3);
assert_relative_eq!(ortho.viewport_width(), 9.9);

Unproject a vector from normalized device coordinates (NDC) to view space.

Vectors are just a direction and a magnitude, so this transformation does not apply the camera’s translation in world space.

Example

// a camera which is 3x as wide as it is tall
let mut ortho = OrthoCamera::with_viewport(2.0, 3.0);

// the camera's world position is ignored when unprojecting a vector
ortho.set_world_position(&na::Point2::new(100.0, -34523.0));

// Vulkan ndc coords have Y ranging from -1 at the top of the screen,
// to 1 at the bottom of the screen.
let top_right_ndc = na::Vector2::new(1.0, -1.0);

// The unprojected vector should point to the top right of the viewport
// rectangle, but is not influenced by the camera's world position.
let unprojected = ortho.unproject_vec(&top_right_ndc);
assert_relative_eq!(unprojected, na::Vector2::new(3.0, 1.0));

Unproject a point from normalized device coordinates (NDC) to world space.

Points are logically a specific location in space. As such, the point’s coordinates will be transformed b ythe camera’s location in world space.

e.g. this method returns where the ndc point would actually be located in world coordinates.

Example

// a camera which is 3x as wide as it is tall
let mut ortho = OrthoCamera::with_viewport(2.0, 3.0);

// the camera's world position is ignored when unprojecting a vector
ortho.set_world_position(&na::Point2::new(100.0, -34523.0));

// Vulkan ndc coords have Y ranging from -1 at the top of the screen,
// to 1 at the bottom of the screen.
let bottom_left_ndc = na::Point2::new(-1.0, 1.0);

// The unprojected point should account for both the camera's viewing
// rectangle, and the camera's world position.
let unprojected = ortho.unproject_point(&bottom_left_ndc);
assert_relative_eq!(
    unprojected,
    na::Point2::new(-3.0, -1.0) + ortho.world_position().coords
);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

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

Should always be Self

Performance hack: Clone doesn’t get inlined for Copy types in debug mode, so make it inline anyway.

Tests if Self the same as the type T Read more

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.