lambda/render/
vertex.rs

1//! Vertex data structures.
2
3pub use lambda_platform::gfx::assembler::{
4  VertexAttribute,
5  VertexElement,
6};
7
8/// Vertex data structure with position, normal, and color.
9#[repr(C)]
10#[derive(Clone, Copy, Debug)]
11pub struct Vertex {
12  pub position: [f32; 3],
13  pub normal: [f32; 3],
14  pub color: [f32; 3],
15}
16
17/// Construction for
18#[derive(Clone, Copy, Debug)]
19pub struct VertexBuilder {
20  pub position: [f32; 3],
21  pub normal: [f32; 3],
22  pub color: [f32; 3],
23}
24
25impl VertexBuilder {
26  /// Creates a new vertex builder.
27  pub fn new() -> Self {
28    return Self {
29      position: [0.0, 0.0, 0.0],
30      normal: [0.0, 0.0, 0.0],
31      color: [0.0, 0.0, 0.0],
32    };
33  }
34
35  /// Set the position of the vertex.
36  pub fn with_position(&mut self, position: [f32; 3]) -> &mut Self {
37    self.position = position;
38    return self;
39  }
40
41  /// Set the normal of the vertex.
42  pub fn with_normal(&mut self, normal: [f32; 3]) -> &mut Self {
43    self.normal = normal;
44    return self;
45  }
46
47  /// Set the color of the vertex.
48  pub fn with_color(&mut self, color: [f32; 3]) -> &mut Self {
49    self.color = color;
50    return self;
51  }
52
53  /// Build the vertex.
54  pub fn build(&self) -> Vertex {
55    return Vertex {
56      position: self.position,
57      normal: self.normal,
58      color: self.color,
59    };
60  }
61}
62
63#[cfg(test)]
64mod test {
65  #[test]
66  fn vertex_building() {
67    let mut vertex = super::VertexBuilder::new();
68
69    assert_eq!(vertex.position, [0.0, 0.0, 0.0]);
70    assert_eq!(vertex.normal, [0.0, 0.0, 0.0]);
71    assert_eq!(vertex.color, [0.0, 0.0, 0.0]);
72
73    let vertex = vertex
74      .with_position([1.0, 2.0, 3.0])
75      .with_normal([4.0, 5.0, 6.0])
76      .with_color([7.0, 8.0, 9.0])
77      .build();
78
79    assert_eq!(vertex.position, [1.0, 2.0, 3.0]);
80    assert_eq!(vertex.normal, [4.0, 5.0, 6.0]);
81    assert_eq!(vertex.color, [7.0, 8.0, 9.0]);
82  }
83}