Skip to content

Commit 7ba90aa

Browse files
committed
[add] mesh and vertex builders alongside tests.
1 parent 4b8328d commit 7ba90aa

2 files changed

Lines changed: 124 additions & 2 deletions

File tree

lambda/src/core/render/mesh.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
11
use super::vertex::Vertex;
22

3+
#[derive(Clone, Debug)]
34
pub struct Mesh {
4-
pub vertices: Vec<Vertex>,
5-
pub indices: Vec<u32>,
5+
vertices: Vec<Vertex>,
6+
indices: Vec<usize>,
7+
}
8+
9+
#[derive(Clone, Debug)]
10+
pub struct MeshBuilder {
11+
size: usize,
12+
vertices: Vec<Vertex>,
13+
indices: Vec<usize>,
14+
}
15+
16+
impl MeshBuilder {
17+
pub fn new() -> Self {
18+
return Self {
19+
size: 0,
20+
vertices: vec![],
21+
indices: vec![],
22+
};
23+
}
24+
25+
pub fn with_capacity(&mut self, size: usize) -> &mut Self {
26+
self.size = size;
27+
return self;
28+
}
29+
30+
pub fn with_vertex(&mut self, vertex: Vertex) -> &mut Self {
31+
self.vertices.push(vertex);
32+
return self;
33+
}
34+
35+
pub fn build(&self) -> Mesh {
36+
return Mesh {
37+
vertices: self.vertices.clone(),
38+
indices: self.indices.clone(),
39+
};
40+
}
41+
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
#[test]
46+
fn mesh_building() {
47+
let mut mesh = super::MeshBuilder::new();
48+
49+
assert_eq!(mesh.vertices.len(), 0);
50+
assert_eq!(mesh.indices.len(), 0);
51+
52+
let mut mesh = mesh
53+
.with_capacity(10)
54+
.with_vertex(crate::core::render::vertex::VertexBuilder::new().build())
55+
.build();
56+
57+
assert_eq!(mesh.vertices.len(), 1);
58+
assert_eq!(mesh.indices.len(), 0);
59+
assert_eq!(mesh.vertices[0].position, [0.0, 0.0, 0.0]);
60+
assert_eq!(mesh.vertices[0].normal, [0.0, 0.0, 0.0]);
61+
assert_eq!(mesh.vertices[0].color, [0.0, 0.0, 0.0]);
62+
}
663
}

lambda/src/core/render/vertex.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,68 @@ pub struct Vertex {
44
pub normal: [f32; 3],
55
pub color: [f32; 3],
66
}
7+
8+
/// Construction for
9+
#[derive(Clone, Copy, Debug)]
10+
pub struct VertexBuilder {
11+
pub position: [f32; 3],
12+
pub normal: [f32; 3],
13+
pub color: [f32; 3],
14+
}
15+
16+
impl VertexBuilder {
17+
pub fn new() -> Self {
18+
return Self {
19+
position: [0.0, 0.0, 0.0],
20+
normal: [0.0, 0.0, 0.0],
21+
color: [0.0, 0.0, 0.0],
22+
};
23+
}
24+
25+
/// Set the position of the vertex.
26+
pub fn with_position(&mut self, position: [f32; 3]) -> &mut Self {
27+
self.position = position;
28+
return self;
29+
}
30+
31+
/// Set the normal of the vertex.
32+
pub fn with_normal(&mut self, normal: [f32; 3]) -> &mut Self {
33+
self.normal = normal;
34+
return self;
35+
}
36+
37+
pub fn with_color(&mut self, color: [f32; 3]) -> &mut Self {
38+
self.color = color;
39+
return self;
40+
}
41+
42+
pub fn build(&self) -> Vertex {
43+
return Vertex {
44+
position: self.position,
45+
normal: self.normal,
46+
color: self.color,
47+
};
48+
}
49+
}
50+
51+
#[cfg(test)]
52+
mod test {
53+
#[test]
54+
fn vertex_building() {
55+
let mut vertex = super::VertexBuilder::new();
56+
57+
assert_eq!(vertex.position, [0.0, 0.0, 0.0]);
58+
assert_eq!(vertex.normal, [0.0, 0.0, 0.0]);
59+
assert_eq!(vertex.color, [0.0, 0.0, 0.0]);
60+
61+
let mut vertex = vertex
62+
.with_position([1.0, 2.0, 3.0])
63+
.with_normal([4.0, 5.0, 6.0])
64+
.with_color([7.0, 8.0, 9.0])
65+
.build();
66+
67+
assert_eq!(vertex.position, [1.0, 2.0, 3.0]);
68+
assert_eq!(vertex.normal, [4.0, 5.0, 6.0]);
69+
assert_eq!(vertex.color, [7.0, 8.0, 9.0]);
70+
}
71+
}

0 commit comments

Comments
 (0)