pub struct Viewport {
    pub transform: Transform3D,
    pub fov: f64,
    pub origin: Vec2D,
    pub character_width_multiplier: f64,
}
Expand description

The Viewport handles printing 3D objects to a 2D View, and also acts as the scene’s camera.

Fields§

§transform: Transform3D

How the Viewport is oriented in the 3D scene

§fov: f64

The Viewport’s field of view

§origin: Vec2D

The center of the view you intend to print to. You can use View.center() as the input for this

§character_width_multiplier: f64

Most terminals don’t have perfectly square characters. The value you set here is how much the final image will be stretched in the X axis to account for this. The default value is 2.2 but it will be different in most terminals

Implementations§

source§

impl Viewport

source

pub const fn new(transform: Transform3D, fov: f64, origin: Vec2D) -> Self

Examples found in repository?
examples/spinning-cube.rs (lines 12-16)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
fn main() {
    let mut view = View::new(350, 90, ColChar::BACKGROUND);

    let mut viewport = Viewport::new(
        Transform3D::new_tr(Vec3D::new(0.0, 0.0, 5.0), Vec3D::new(-0.5, 0.3, 0.0)),
        FOV,
        view.center(),
    );

    let cube = Mesh3D::default_cube();

    fps_gameloop!(
        {
            view.clear();
            viewport.transform.rotation.y -= 0.05;
        },
        {
            view.blit(
                &viewport.render(vec![&cube], DisplayMode::Solid),
                Wrapping::Ignore,
            );
            view.display_render().unwrap();
        },
        FPS,
        |elapsed: gameloop::Duration, frame_skip| {
            println!(
                "Elapsed: {:.2?}µs | Frame skip: {}",
                elapsed.as_micros(),
                frame_skip
            );
        }
    );
}
source

pub fn perspective(&self, pos: Vec3D) -> Vec2D

Project the Vec3D on a flat plane using the Viewport’s fov and character_width_multiplier

source

pub fn transform_vertices(&self, object: &dyn ViewElement3D) -> Vec<Vec3D>

Return the object’s vertices, transformed

source

pub fn get_vertices_on_screen( &self, object: &dyn ViewElement3D ) -> (Vec<Vec2D>, Vec<f64>)

Return the screen coordinates and distance from the view for each vertex, as parallel vectors

source

pub fn project_faces( &self, objects: Vec<&dyn ViewElement3D>, sort_faces: bool, backface_culling: bool ) -> Vec<(Vec<Vec2D>, ColChar)>

Project the faces onto a 2D plane. Returns a collection of faces, each stored as a list of the points it appears at and the ColChar assigned to it

source

pub fn render( &self, objects: Vec<&dyn ViewElement3D>, display_mode: DisplayMode ) -> PixelContainer

Render the objects (implementing ViewElement3D) given the Viewport’s properties. Returns a PixelContainer which can then be blit to a View

Examples found in repository?
examples/spinning-cube.rs (line 27)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
fn main() {
    let mut view = View::new(350, 90, ColChar::BACKGROUND);

    let mut viewport = Viewport::new(
        Transform3D::new_tr(Vec3D::new(0.0, 0.0, 5.0), Vec3D::new(-0.5, 0.3, 0.0)),
        FOV,
        view.center(),
    );

    let cube = Mesh3D::default_cube();

    fps_gameloop!(
        {
            view.clear();
            viewport.transform.rotation.y -= 0.05;
        },
        {
            view.blit(
                &viewport.render(vec![&cube], DisplayMode::Solid),
                Wrapping::Ignore,
            );
            view.display_render().unwrap();
        },
        FPS,
        |elapsed: gameloop::Duration, frame_skip| {
            println!(
                "Elapsed: {:.2?}µs | Frame skip: {}",
                elapsed.as_micros(),
                frame_skip
            );
        }
    );
}

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.