Struct gemini_engine::elements3d::view3d::Viewport
source · 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: Transform3DHow the Viewport is oriented in the 3D scene
fov: f64The Viewport’s field of view
origin: Vec2DThe center of the view you intend to print to. You can use View.center() as the input for this
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.2 but it will be different in most terminals
Implementations§
source§impl Viewport
impl Viewport
sourcepub const fn new(transform: Transform3D, fov: f64, origin: Vec2D) -> Self
pub const fn new(transform: Transform3D, fov: f64, origin: Vec2D) -> Self
Examples found in repository?
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
);
}
);
}sourcepub fn perspective(&self, pos: Vec3D) -> Vec2D
pub fn perspective(&self, pos: Vec3D) -> Vec2D
Project the Vec3D on a flat plane using the Viewport’s fov and character_width_multiplier
sourcepub fn transform_vertices(&self, object: &dyn ViewElement3D) -> Vec<Vec3D>
pub fn transform_vertices(&self, object: &dyn ViewElement3D) -> Vec<Vec3D>
Return the object’s vertices, transformed
sourcepub fn get_vertices_on_screen(
&self,
object: &dyn ViewElement3D
) -> (Vec<Vec2D>, Vec<f64>)
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
sourcepub fn project_faces(
&self,
objects: Vec<&dyn ViewElement3D>,
sort_faces: bool,
backface_culling: bool
) -> Vec<(Vec<Vec2D>, ColChar)>
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
sourcepub fn render(
&self,
objects: Vec<&dyn ViewElement3D>,
display_mode: DisplayMode
) -> PixelContainer
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?
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
);
}
);
}