Struct ferrux_viewport::viewport::Viewport
source · [−]pub struct Viewport<S, R> { /* private fields */ }
Expand description
Entity in charge of offering the functions to draw on the screen and handle to logic of the operation.
It works using three-dimensional normalized vectors of type (x: f32, y: f32, z: f32).
The point to draw in the screen will be the one relative to given position in the x
and y
axes.
So, any point outside the (-1.0, 1.0) range will not be drawn.
The z
value works as a layer function, it will draw only the point with the highest z
on the same translated pixel.
The viewport doesn’t perform projection, that should be handled by the user before calling the functions. Viewport just draws the pixels of the highest depth relative to the given coordinates.
The axes directions are:
x
: west -> easty
: north -> southz
: far -> near
Implementations
sourceimpl<S: PixelSize, R> Viewport<S, R>
impl<S: PixelSize, R> Viewport<S, R>
sourcepub fn draw_point<'a>(&mut self, position: (f32, f32, f32), color: &'a [u8])
pub fn draw_point<'a>(&mut self, position: (f32, f32, f32), color: &'a [u8])
Commands the drawing of a point in the window. It will be rendered in the next call to Viewport::render
.
If two drawn points fall on the same pixel, the point with the lowest z
will be ignored.
Arguments
position
, coordinates of the point in(f32, f32, f32)
.color
, color of the point to draw. It should be provided as raw RGB values, alpha is included, so the expectation is a &[u8; 4] color like&[255, 0, 0, 255]
for red with 100% opacity.
Example
viewport.draw_point((0.0, 0.0, 0.0), &[255, 255, 255, 255]); // white point in the center of the screen
viewport.render()?; // renders the point in the window
Panic
Passing a color with the wrong number of members will throw a panic. It’s required to have length four (R, G, B, A);
sourcepub fn draw_line<'a>(
&mut self,
start: (f32, f32, f32),
end: (f32, f32, f32),
color: &'a [u8]
)
pub fn draw_line<'a>(
&mut self,
start: (f32, f32, f32),
end: (f32, f32, f32),
color: &'a [u8]
)
Commands the drawing of a line in the window. It will be rendered in the next call to Viewport::render
.
Arguments
start
, coordinates of the starting point of the line.end
, coordinates of the ending point of the line.color
, color of the line to draw. It should be provided as raw RGB values, alpha is included, so the expectation is a &[u8; 4] color like&[255, 0, 0, 255]
for red with 100% opacity.
Example
viewport.draw_line((-0.5, -0.5, -0.5), (0.25, 0.5, 0.0), &[255, 255, 255, 255]);
viewport.render()?; // renders the line in the window
Panic
Passing a color with the wrong number of members will throw a panic. It’s required to have length four (R, G, B, A);
sourcepub fn draw_triangle<'a>(
&mut self,
point_a: (f32, f32, f32),
point_b: (f32, f32, f32),
point_c: (f32, f32, f32),
color: &'a [u8]
)
pub fn draw_triangle<'a>(
&mut self,
point_a: (f32, f32, f32),
point_b: (f32, f32, f32),
point_c: (f32, f32, f32),
color: &'a [u8]
)
Commands the drawing of a triangle in the window. It will be rendered in the next call to Viewport::render
.
Arguments
point_a
,point_b
,point_c
. Coordinates of the points of the triangle.color
, color of the line to draw. It should be provided as raw RGB values, alpha is included, so the expectation is a &[u8; 4] color like&[255, 0, 0, 255]
for red with 100% opacity.
Example
viewport.draw_triangle((0.0, 0.0, -0.5), (-0.5, 0.5, 0.0), (0.5, 0.5, 0.0), &[255, 255, 255, 255]);
viewport.render()?; // renders the triangle in the window
Panic
Passing a color with the wrong number of members will throw a panic. It’s required to have length four (R, G, B, A);
sourcepub fn fill_triangle<'a>(
&mut self,
point_a: (f32, f32, f32),
point_b: (f32, f32, f32),
point_c: (f32, f32, f32),
color: &'a [u8]
)
pub fn fill_triangle<'a>(
&mut self,
point_a: (f32, f32, f32),
point_b: (f32, f32, f32),
point_c: (f32, f32, f32),
color: &'a [u8]
)
Commands the drawing and filling of a triangle in the window. It will be rendered in the next call to Viewport::render
.
Arguments
point_a
,point_b
,point_c
. Coordinates of the points of the triangle.color
, color of the line to draw. It should be provided as raw RGB values, alpha is included, so the expectation is a &[u8; 4] color like&[255, 0, 0, 255]
for red with 100% opacity.
Example
viewport.fill_triangle((0.0, 0.0, -0.5), (-0.5, 0.5, 0.0), (0.5, 0.5, 0.0), &[255, 255, 255, 255]);
viewport.render()?; // renders the triangle in the window
Panic
Passing a color with the wrong number of members will throw a panic. It’s required to have length four (R, G, B, A);
sourcepub fn reset_buffer(&mut self)
pub fn reset_buffer(&mut self)
Resets the buffer clearing all its current content
sourceimpl<S: PixelSize, R: Render> Viewport<S, R>
impl<S: PixelSize, R: Render> Viewport<S, R>
sourcepub fn render(&mut self) -> Result<(), ViewportError>
pub fn render(&mut self) -> Result<(), ViewportError>
Renders the content of the buffer in the Window. It doesn’t clear the buffer afterwards, to do that call Viewport::reset_buffer.
Example
viewport.fill_triangle((0.0, 0.0, -0.5), (-0.5, 0.5, 0.0), (0.5, 0.5, 0.0), &[255, 255, 255, 255]);
viewport.render()?; // renders the triangle in the window
viewport.reset_buffer(); // clears the buffer to prepare the drawing of a new frame
sourcepub fn clear_frame(&mut self) -> Result<(), ViewportError>
pub fn clear_frame(&mut self) -> Result<(), ViewportError>
Draws an empty frame without the needing of resetting the buffer. This is an optimal way of drawing an empty frame keeping the current drawing buffer without the need to save it, resetting buffer, rendering and redrawing it.
Example
viewport.fill_triangle((0.0, 0.0, -0.5), (-0.5, 0.5, 0.0), (0.5, 0.5, 0.0), &[255, 255, 255, 255]);
viewport.render()?; // renders the triangle in the window
viewport.clear_frame()?; // renders an empty frame
viewport.fill_triangle((0.25, 0.75, -0.5), (-0.25, 0.25, 0.0), (0.25, 0.25, 0.0), &[255, 0, 0, 255]);
viewport.render()?; // renders both triangles as the previous one was not deleted from the buffer
Auto Trait Implementations
impl<S, R> RefUnwindSafe for Viewport<S, R> where
R: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, R> Send for Viewport<S, R> where
R: Send,
S: Send,
impl<S, R> Sync for Viewport<S, R> where
R: Sync,
S: Sync,
impl<S, R> Unpin for Viewport<S, R> where
R: Unpin,
S: Unpin,
impl<S, R> UnwindSafe for Viewport<S, R> where
R: UnwindSafe,
S: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more