paddle/display/gpu/
gpu_triangle.rs

1//! Triangles ready to be drawn by GPU, after tesselation and all CPU-side transformations have finished
2
3use crate::graphics::AbstractTriangle;
4use crate::Scalar;
5use std::cmp::Ordering;
6
7#[derive(Clone)]
8/// A triangle to draw to the GPU
9pub struct GpuTriangle {
10    /// The plane the triangle falls on
11    pub z: f32,
12    /// The indexes in the vertex list that the GpuTriangle uses
13    pub indices: [u32; 3],
14}
15
16impl GpuTriangle {
17    /// Create a new untextured GPU Triangle
18    pub fn new(offset: u32, indices: [u32; 3], z: impl Scalar) -> GpuTriangle {
19        GpuTriangle {
20            z: z.float(),
21            indices: [
22                indices[0] + offset,
23                indices[1] + offset,
24                indices[2] + offset,
25            ],
26        }
27    }
28    pub fn from_abstract(t: &AbstractTriangle, offset: u32, z: f32) -> Self {
29        Self {
30            z,
31            indices: [
32                t.indices[0] + offset,
33                t.indices[1] + offset,
34                t.indices[2] + offset,
35            ],
36        }
37    }
38}
39
40// For sorting by z-order
41impl PartialEq for GpuTriangle {
42    fn eq(&self, other: &GpuTriangle) -> bool {
43        self.z.eq(&other.z)
44    }
45}
46
47impl Eq for GpuTriangle {}
48
49impl PartialOrd for GpuTriangle {
50    fn partial_cmp(&self, other: &GpuTriangle) -> Option<Ordering> {
51        Some(self.cmp(other))
52    }
53}
54
55impl Ord for GpuTriangle {
56    fn cmp(&self, other: &GpuTriangle) -> Ordering {
57        self.z.partial_cmp(&other.z).unwrap()
58    }
59}