Skip to main content

mln_engine/render/
static_data.rs

1use mln_style::constants;
2
3use crate::draw::quads::RasterLayoutVertex;
4use crate::draw::segment::{MAX_SEGMENT_VERTICES, SegmentBase};
5
6/// The most quads a segment draws: as many as its 16-bit indices reach.
7const SEGMENT_QUADS: usize = MAX_SEGMENT_VERTICES / 4;
8
9/// The vertices and indices of the geometry the layers share.
10pub struct RenderStaticData;
11
12impl RenderStaticData {
13    /// The tile's four corners, as position-only vertices (`a_pos`).
14    #[must_use]
15    pub fn tile_vertices() -> Vec<[i16; 2]> {
16        let extent = constants::EXTENT as i16;
17        vec![[0, 0], [extent, 0], [0, extent], [extent, extent]]
18    }
19
20    /// The two triangles of a quad of four vertices: top left, top right,
21    /// bottom left, bottom right.
22    #[must_use]
23    pub fn quad_triangle_indices() -> Vec<u16> {
24        vec![0, 1, 2, 1, 2, 3]
25    }
26
27    /// The quad of a whole tile that samples a whole image.
28    #[must_use]
29    pub fn raster_vertices() -> Vec<RasterLayoutVertex> {
30        use geometry::Point;
31
32        let e = constants::EXTENT as i16;
33        let t = constants::EXTENT as u16;
34        vec![
35            RasterLayoutVertex::new(Point::new(0, 0), Point::new(0, 0)),
36            RasterLayoutVertex::new(Point::new(e, 0), Point::new(t, 0)),
37            RasterLayoutVertex::new(Point::new(0, e), Point::new(0, t)),
38            RasterLayoutVertex::new(Point::new(e, e), Point::new(t, t)),
39        ]
40    }
41
42    /// The segment that draws [`raster_vertices`](Self::raster_vertices) with
43    /// [`quad_triangle_indices`](Self::quad_triangle_indices).
44    #[must_use]
45    pub fn raster_segments() -> Vec<SegmentBase> {
46        vec![SegmentBase::with_lengths(0, 0, 4, 6)]
47    }
48
49    #[must_use]
50    pub(crate) fn quad_indices() -> Vec<u16> {
51        // ┌──────┐
52        // │ 0  1 │ Counter-Clockwise winding order.
53        // │      │ Triangle 1: 0 => 2 => 1
54        // │ 2  3 │ Triangle 2: 1 => 2 => 3
55        // └──────┘
56        vec![0, 2, 1, 1, 2, 3]
57    }
58
59    /// The triangles of the quads that a segment of `quads_segments` draws,
60    /// each quad four vertices after the last's, as `quad_indices` draws the
61    /// quad.
62    #[must_use]
63    pub fn quads_indices() -> Vec<u16> {
64        let quad = Self::quad_indices();
65        (0..SEGMENT_QUADS as u16).flat_map(|wall| quad.iter().map(move |&index| 4 * wall + index)).collect()
66    }
67
68    /// The segments that draw `quads` quads that are in the vertex buffer,
69    /// one after the other, from the indices of `quads_indices`.
70    #[must_use]
71    pub fn quads_segments(quads: usize) -> Vec<SegmentBase> {
72        (0..quads)
73            .step_by(SEGMENT_QUADS)
74            .map(|first| {
75                let count = SEGMENT_QUADS.min(quads - first);
76                SegmentBase::with_lengths(4 * first, 0, 4 * count, 6 * count)
77            })
78            .collect()
79    }
80
81    /// The segment that draws [`tile_vertices`](Self::tile_vertices) with
82    /// [`quad_triangle_indices`](Self::quad_triangle_indices).
83    #[must_use]
84    pub fn tile_triangle_segments() -> Vec<SegmentBase> {
85        vec![SegmentBase::with_lengths(0, 0, 4, 6)]
86    }
87}