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 -> east
  • y: north -> south
  • z: far -> near

Implementations

Returns the width of the current window

Returns the height of the current window

Returns the depth of the current window

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);

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);

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);

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);

Resets the buffer clearing all its current content

Changes the size of the rendered window. Doing it will reset the buffer, clearing the current content.

Arguments
  • width. New width of the window.
  • height. New height of the window.

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

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.