[][src]Struct truck_platform::Camera

pub struct Camera {
    pub matrix: Matrix4,
    // some fields omitted
}

Camera

A Scene holds only one Camera.

Fields

matrix: Matrix4

camera matrix

This matrix must be in the Euclidean momentum group, the semi-direct product of O(3) and R^3.

Implementations

impl Camera[src]

pub fn position(&self) -> Point3[src]

Returns the position of camera, the forth column of the camera matrix.

Examples

use truck_platform::*;
use truck_base::cgmath64::*;
let mut camera = Camera::default();
camera.matrix = Matrix4::from_translation(Vector3::new(1.0, 2.0, 3.0));
assert_eq!(camera.position(), Point3::new(1.0, 2.0, 3.0));

pub fn eye_direction(&self) -> Vector3[src]

Returns the eye direction of camera. the inverse of the z-axis of the camera matrix.

Examples

use std::f64::consts::PI;
use truck_platform::*;
use truck_base::{cgmath64::*, tolerance::Tolerance};
let mut camera = Camera::default();
camera.matrix = Matrix4::from_axis_angle(
    Vector3::new(1.0, 1.0, 1.0).normalize(),
    Rad(2.0 * PI / 3.0),
);
assert!(camera.eye_direction().near(&-Vector3::unit_x()));

pub fn head_direction(&self) -> Vector3[src]

Returns the direction of the head vector, the y-axis of the camera matrix.

Examples

use std::f64::consts::PI;
use truck_platform::*;
use truck_base::{cgmath64::*, tolerance::Tolerance};
let mut camera = Camera::default();
camera.matrix = Matrix4::from_axis_angle(
    Vector3::new(1.0, 1.0, 1.0).normalize(),
    Rad(2.0 * PI / 3.0),
);
assert!(camera.head_direction().near(&Vector3::unit_z()));

pub fn projection_type(&self) -> ProjectionType[src]

Returns the projection type of the camera.

Examples

use truck_platform::*;
// the projection type of the default camera is perspective.
assert_eq!(Camera::default().projection_type(), ProjectionType::Perspective);

pub fn perspective_camera<R: Into<Rad<f64>>>(
    matrix: Matrix4,
    field_of_view: R,
    near_clip: f64,
    far_clip: f64
) -> Camera
[src]

Creates a perspective camera.

Arguments

  • matrix: camera matrix
  • field_of_view: FOV, based on the vertical direction of the screen.
  • near_clip: distance to the nearest face of the view volume
  • far_clip: distance to the farthest face of the view volume

Examples

use std::f64::consts::PI;
use truck_base::{cgmath64::*, tolerance::Tolerance};
use truck_platform::*;
let matrix = Matrix4::look_at(
    Point3::new(1.0, 1.0, 1.0),
    Point3::origin(),
    Vector3::new(0.0, 1.0, 0.0),
);
let camera = Camera::perspective_camera(
    // depends on the difference of the style with cgmath,
    // the matrix must be inverted
    matrix.invert().unwrap(),
    Rad(PI / 4.0),
    0.1,
    1.0,
);
assert!(camera.eye_direction().near(&-Vector3::new(1.0, 1.0, 1.0).normalize()));
assert_eq!(camera.projection_type(), ProjectionType::Perspective);

pub fn parallel_camera(
    matrix: Matrix4,
    screen_size: f64,
    near_clip: f64,
    far_clip: f64
) -> Camera
[src]

Creates a parallel camera.

Arguments

  • matrix: camera matrix
  • screen_size: screen size, based on the vertical direction of the screen.`
  • near_clip: distance to the nearest face of the view volume
  • far_clip: distance to the farthest face of the view volume

Examples

use truck_base::{cgmath64::*, tolerance::Tolerance};
use truck_platform::*;
let matrix = Matrix4::look_at(
    Point3::new(1.0, 1.0, 1.0),
    Point3::origin(),
    Vector3::new(0.0, 1.0, 0.0),
);
let camera = Camera::parallel_camera(
    // depends on the difference of the style with cgmath,
    // the matrix must be inverted
    matrix.invert().unwrap(),
    1.0,
    0.1,
    1.0,
);
assert!(camera.head_direction().near(&Vector3::new(-0.5, 1.0, -0.5).normalize()));
assert_eq!(camera.projection_type(), ProjectionType::Parallel);

pub fn projection(&self, as_rat: f64) -> Matrix4[src]

Returns the projection matrix into the normalized view volume.

Arguments

as_rat: the aspect ratio, x-resolution / y-resulution.

Examples

// perspective camera
use std::f64::consts::PI;
use truck_base::{cgmath64::*, tolerance::*};
use truck_platform::*;
 
let fov = PI / 4.0;
let as_rat = 1.2;
let matrix = Matrix4::look_at(
    Point3::new(1.0, 1.0, 1.0),
    Point3::origin(),
    Vector3::new(0.0, 1.0, 0.0),
);
let camera = Camera::perspective_camera(
    matrix.invert().unwrap(),
    Rad(fov),
    0.1,
    10.0,
);
 
// calculation by the ray-tracing
let pt = Point3::new(-1.5, -1.4, -2.5);
let vec = pt - camera.position();
let far = 1.0 / (fov / 2.0).tan();
let dir = camera.eye_direction();
let y_axis = camera.head_direction();
let x_axis = dir.cross(y_axis);
let proj_length = dir.dot(vec);
let h = (vec - proj_length * dir) * far / proj_length;
let u = h.dot(x_axis) / as_rat;
let v = h.dot(y_axis);
 
// check the answer
let uv = camera.projection(as_rat).transform_point(pt);
assert!(f64::near(&u, &uv[0]), "{} {}", u, uv[0]);
assert!(f64::near(&v, &uv[1]));
// parallel camera
use truck_base::{cgmath64::*, tolerance::*};
use truck_platform::*;
 
let size = 3.0;
let as_rat = 1.2;
let matrix = Matrix4::look_at(
    Point3::new(1.0, 1.0, 1.0),
    Point3::origin(),
    Vector3::new(0.0, 1.0, 0.0),
);
let camera = Camera::parallel_camera(
    matrix.invert().unwrap(),
    size,
    0.1,
    10.0,
);
 
// calculation by the ray-tracing
let pt = Point3::new(-1.5, -1.4, -2.5);
let vec = pt - camera.position();
let dir = camera.eye_direction();
let y_axis = camera.head_direction();
let x_axis = dir.cross(y_axis);
let h = vec - vec.dot(dir) * dir;
let u = h.dot(x_axis) / (size / 2.0) / as_rat;
let v = h.dot(y_axis) / (size / 2.0);
 
// check the answer
let uv = camera.projection(as_rat).transform_point(pt);
assert!(f64::near(&u, &uv[0]), "{} {}", u, uv[0]);
assert!(f64::near(&v, &uv[1]));

pub fn buffer(&self, as_rat: f64, device: &Device) -> BufferHandler[src]

Creates a UNIFORM buffer of camera.

The bind group provides Scene holds this uniform buffer.

Shader Example

layout(set = 0, binding = 0) uniform Camera {
    mat4 camera_matrix;     // the camera matrix
    mat4 camera_projection; // the projection into the normalized view volume
};

Trait Implementations

impl Clone for Camera[src]

impl Debug for Camera[src]

impl Default for Camera[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.