Skip to main content

zintl_render/
mesh.rs

1use zintl_render_math::{PhysicalPixelsPoint, PhysicalPixelsRect};
2
3/// Vertex in device pixels.
4/// note: Texture bounds are not normalized.
5#[derive(Clone, Debug)]
6pub struct Vertex {
7    pub position: PhysicalPixelsPoint,
8    pub tex_coords: PhysicalPixelsPoint,
9}
10
11/// Mesh in device pixels
12#[repr(C)]
13#[derive(Clone, Debug, Default)]
14pub struct Mesh {
15    pub vertices: Vec<Vertex>,
16    pub indices: Vec<u32>,
17    pub texture_id: Option<usize>,
18    pub children: Vec<Mesh>,
19}
20
21impl Mesh {
22    pub fn from_children(children: Vec<Mesh>) -> Self {
23        Self {
24            vertices: Vec::new(),
25            indices: Vec::new(),
26            texture_id: None,
27            children,
28        }
29    }
30
31    pub fn from_device_rect(
32        rect: PhysicalPixelsRect,
33        texture_id: Option<usize>,
34        tex_bounds: PhysicalPixelsRect,
35    ) -> Self {
36        let vertices = vec![
37            Vertex {
38                position: rect.min,
39                tex_coords: PhysicalPixelsPoint::new(tex_bounds.min.x, tex_bounds.min.y),
40            },
41            Vertex {
42                position: PhysicalPixelsPoint::new(rect.max.x, rect.min.y),
43                tex_coords: PhysicalPixelsPoint::new(tex_bounds.max.x, tex_bounds.min.y),
44            },
45            Vertex {
46                position: PhysicalPixelsPoint::new(rect.max.x, rect.max.y),
47                tex_coords: PhysicalPixelsPoint::new(tex_bounds.max.x, tex_bounds.max.y),
48            },
49            Vertex {
50                position: PhysicalPixelsPoint::new(rect.min.x, rect.max.y),
51                tex_coords: PhysicalPixelsPoint::new(tex_bounds.min.x, tex_bounds.max.y),
52            },
53        ];
54        let indices = vec![0, 1, 2, 0, 2, 3];
55        Self {
56            vertices,
57            indices,
58            texture_id,
59            children: Vec::new(),
60        }
61    }
62}