pub struct Viewport {
pub camera_transform: Transform3D,
pub fov: f64,
pub canvas_centre: Vec2D,
pub objects: Vec<Mesh3D>,
pub display_mode: DisplayMode,
pub character_width_multiplier: f64,
pub clipping_distace: f64,
}Expand description
The Viewport handles drawing 3D objects to a 2D Canvas, and also acts as the scene’s camera.
Fields§
§camera_transform: Transform3DThis transform is applied to every vertex in the scene. Transform3D::look_at_lh works best for this
fov: f64The Viewport’s field of view, in degrees
canvas_centre: Vec2DThe centre of the view you intend to draw to. View.centre() returns exactly what you need for this
objects: Vec<Mesh3D>The objects to be drawn on the screen
display_mode: DisplayModeThe style in which the objects should be rendered. Read DisplayMode for more info
character_width_multiplier: f64Most 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.0 but it will be different in most terminals
clipping_distace: f64Any face with vertices closer to the viewport than this value will be clipped
Implementations§
Source§impl Viewport
impl Viewport
Sourcepub const fn new(
camera_transform: Transform3D,
fov: f64,
canvas_centre: Vec2D,
) -> Self
pub const fn new( camera_transform: Transform3D, fov: f64, canvas_centre: Vec2D, ) -> Self
Create a new Viewport
Examples found in repository?
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 42 43
fn main() {
let mut view = View::new(100, 45, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -3.0, 6.0), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.7, Vec3D::new(1.0, 1.0, 1.0)),
],
};
viewport.objects.push(Mesh3D::torus(1.8, 1.0, 32, 16));
fps_gameloop!(
{
let donut_tr = &mut viewport.objects[0].transform;
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_y(-0.03));
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_x(0.03));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS
);
}More examples
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 42 43 44 45 46 47 48 49 50 51
fn main() {
let mut view = View::new(100, 50, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -1.5, 4.3), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.objects.push(Mesh3D::default_cube());
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.6, Vec3D::new(0.5, 1.0, 1.0)),
],
};
fps_gameloop!(
{
viewport.objects[0].transform = viewport.objects[0]
.transform
.mul_mat4(&Transform3D::from_rotation_y(-0.05));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS,
|elapsed: Duration, frame_skip| {
println!(
"Elapsed: {:.2?}µs | Frame skip: {}",
elapsed.as_micros(),
frame_skip
);
}
);
}