summaryrefslogtreecommitdiff
path: root/engine/src/mesh.rs
blob: f26c9c149e581bfc6d790c1655ea2f562bab40ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use engine_macros::Reflection;
use zerocopy::{Immutable, IntoBytes};

use crate::builder;
use crate::vector::Vec3;

pub mod cube;

#[derive(Debug, Clone, Default)]
pub struct Mesh
{
    vertices: Vec<Vertex>,
    indices: Option<Vec<u32>>,
}

impl Mesh
{
    #[must_use]
    pub fn new(vertices: Vec<Vertex>, indices: Option<Vec<u32>>) -> Self
    {
        Self { vertices, indices }
    }

    #[must_use]
    pub fn vertices(&self) -> &[Vertex]
    {
        &self.vertices
    }

    #[must_use]
    pub fn vertices_mut(&mut self) -> &mut [Vertex]
    {
        &mut self.vertices
    }

    #[must_use]
    pub fn indices(&self) -> Option<&[u32]>
    {
        self.indices.as_deref()
    }

    #[must_use]
    pub fn indices_mut(&mut self) -> Option<&mut [u32]>
    {
        self.indices.as_deref_mut()
    }

    /// Finds the vertex positions that are furthest in every 3D direction. Keep in mind
    /// that this can be quite time-expensive if the mesh has many vertices.
    pub fn find_furthest_vertex_positions(&self) -> DirectionPositions
    {
        let mut point_iter = self
            .vertices()
            .iter()
            .map(|vertex| Vec3::from(vertex.pos))
            .into_iter();

        let first_point = point_iter.next().unwrap();

        point_iter
            .fold(
                FurthestPosAcc {
                    up: FurthestPos::new(first_point, &Vec3::UP),
                    down: FurthestPos::new(first_point, &Vec3::DOWN),
                    left: FurthestPos::new(first_point, &Vec3::LEFT),
                    right: FurthestPos::new(first_point, &Vec3::RIGHT),
                    back: FurthestPos::new(first_point, &Vec3::BACK),
                    front: FurthestPos::new(first_point, &Vec3::FRONT),
                },
                |mut furthest_pos_acc, pos| {
                    furthest_pos_acc.up.update_if_further(pos);
                    furthest_pos_acc.down.update_if_further(pos);
                    furthest_pos_acc.left.update_if_further(pos);
                    furthest_pos_acc.right.update_if_further(pos);
                    furthest_pos_acc.back.update_if_further(pos);
                    furthest_pos_acc.front.update_if_further(pos);

                    furthest_pos_acc
                },
            )
            .into()
    }
}

builder! {
#[builder(name = VertexBuilder, derives = (Debug, Default, Clone))]
#[derive(Debug, Clone, Default, PartialEq, IntoBytes, Immutable, Reflection)]
#[non_exhaustive]
pub struct Vertex
{
    #[builder(skip_generate_fn)]
    pub pos: [f32; 3],

    #[builder(skip_generate_fn)]
    pub texture_coords: [f32; 2],

    #[builder(skip_generate_fn)]
    pub normal: [f32; 3],
}
}

impl Vertex
{
    #[must_use]
    pub fn builder() -> VertexBuilder
    {
        VertexBuilder::default()
    }
}

impl VertexBuilder
{
    pub fn pos(mut self, pos: impl Into<[f32; 3]>) -> Self
    {
        self.pos = pos.into();
        self
    }

    pub fn texture_coords(mut self, texture_coords: impl Into<[f32; 2]>) -> Self
    {
        self.texture_coords = texture_coords.into();
        self
    }

    pub fn normal(mut self, normal: impl Into<[f32; 3]>) -> Self
    {
        self.normal = normal.into();
        self
    }
}

#[derive(Debug, Clone)]
pub struct DirectionPositions
{
    pub up: Vec3<f32>,
    pub down: Vec3<f32>,
    pub left: Vec3<f32>,
    pub right: Vec3<f32>,
    pub back: Vec3<f32>,
    pub front: Vec3<f32>,
}

impl<'mesh> From<FurthestPosAcc<'mesh>> for DirectionPositions
{
    fn from(acc: FurthestPosAcc<'mesh>) -> Self
    {
        Self {
            up: acc.up.pos,
            down: acc.down.pos,
            left: acc.left.pos,
            right: acc.right.pos,
            back: acc.back.pos,
            front: acc.front.pos,
        }
    }
}

#[derive(Debug)]
struct FurthestPosAcc<'mesh>
{
    up: FurthestPos<'mesh>,
    down: FurthestPos<'mesh>,
    left: FurthestPos<'mesh>,
    right: FurthestPos<'mesh>,
    back: FurthestPos<'mesh>,
    front: FurthestPos<'mesh>,
}

#[derive(Debug)]
struct FurthestPos<'mesh>
{
    pos: Vec3<f32>,
    dot_prod: f32,
    direction: &'mesh Vec3<f32>,
}

impl<'mesh> FurthestPos<'mesh>
{
    fn new(pos: Vec3<f32>, direction: &'mesh Vec3<f32>) -> Self
    {
        Self {
            pos,
            dot_prod: direction.dot(&pos),
            direction,
        }
    }

    fn update_if_further(&mut self, point: Vec3<f32>)
    {
        let point_dot_prod = self.direction.dot(&point);

        if point_dot_prod > self.dot_prod {
            self.pos = point;
            self.dot_prod = point_dot_prod;
        }
    }
}