Skip to content

Commit 14cc75a

Browse files
authored
shapes: draw_rectangle_ex
1 parent f5d9630 commit 14cc75a

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/shapes.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{color::Color, get_context};
44

55
use crate::quad_gl::{DrawMode, Vertex};
6-
use glam::{vec2, Vec2};
6+
use glam::{vec2, vec3, vec4, Mat4, Vec2};
77

88
/// Draws a solid triangle between points `v1`, `v2`, and `v3` with a given `color`.
99
pub fn draw_triangle(v1: Vec2, v2: Vec2, v3: Vec2, color: Color) {
@@ -74,6 +74,59 @@ pub fn draw_rectangle_lines(x: f32, y: f32, w: f32, h: f32, thickness: f32, colo
7474
context.gl.geometry(&vertices, &indices);
7575
}
7676

77+
#[derive(Debug, Clone)]
78+
pub struct DrawRectangleParams {
79+
/// Adds an offset to the position
80+
/// E.g. offset (0,0) positions the rectangle at the top left corner of the screen, while offset
81+
/// (0.5, 0.5) centers it
82+
pub offset: Vec2,
83+
84+
/// Rotation in radians
85+
pub rotation: f32,
86+
87+
pub color: Color,
88+
}
89+
90+
impl Default for DrawRectangleParams {
91+
fn default() -> Self {
92+
Self {
93+
offset: vec2(0.0, 0.0),
94+
rotation: 0.0,
95+
color: Color::from_rgba(255, 255, 255, 255),
96+
}
97+
}
98+
}
99+
100+
/// Draws a solid rectangle with its position at `[x, y]` with size `[w, h]`,
101+
/// with parameters.
102+
pub fn draw_rectangle_ex(x: f32, y: f32, w: f32, h: f32, params: DrawRectangleParams) {
103+
let context = get_context();
104+
let transform_matrix = Mat4::from_translation(vec3(x, y, 0.0))
105+
* Mat4::from_axis_angle(vec3(0.0, 0.0, 1.0), params.rotation)
106+
* Mat4::from_scale(vec3(w, h, 1.0));
107+
108+
#[rustfmt::skip]
109+
let v = [
110+
transform_matrix * vec4( 0.0 - params.offset.x, 0.0 - params.offset.y, 0.0, 1.0),
111+
transform_matrix * vec4( 0.0 - params.offset.x, 1.0 - params.offset.y, 0.0, 1.0),
112+
transform_matrix * vec4( 1.0 - params.offset.x, 1.0 - params.offset.y, 0.0, 1.0),
113+
transform_matrix * vec4( 1.0 - params.offset.x, 0.0 - params.offset.y, 0.0, 1.0),
114+
];
115+
116+
#[rustfmt::skip]
117+
let vertices = [
118+
Vertex::new(v[0].x, v[0].y, v[0].z, 0.0, 1.0, params.color),
119+
Vertex::new(v[1].x, v[1].y, v[1].z, 1.0, 0.0, params.color),
120+
Vertex::new(v[2].x, v[2].y, v[2].z, 1.0, 1.0, params.color),
121+
Vertex::new(v[3].x, v[3].y, v[3].z, 1.0, 0.0, params.color),
122+
];
123+
let indices: [u16; 6] = [0, 1, 2, 0, 2, 3];
124+
125+
context.gl.texture(None);
126+
context.gl.draw_mode(DrawMode::Triangles);
127+
context.gl.geometry(&vertices, &indices);
128+
}
129+
77130
/// Draws an outlined solid hexagon centered at `[x, y]` with a radius `size`, outline thickness
78131
/// defined by `border`, orientation defined by `vertical` (when `true`, the hexagon points along
79132
/// the `y` axis), and colors for outline given by `border_color` and fill by `fill_color`.

0 commit comments

Comments
 (0)