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
//! Vertex data structures.

pub use lambda_platform::gfx::assembler::{
  VertexAttribute,
  VertexElement,
};

/// Vertex data structure with position, normal, and color.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Vertex {
  pub position: [f32; 3],
  pub normal: [f32; 3],
  pub color: [f32; 3],
}

/// Construction for
#[derive(Clone, Copy, Debug)]
pub struct VertexBuilder {
  pub position: [f32; 3],
  pub normal: [f32; 3],
  pub color: [f32; 3],
}

impl VertexBuilder {
  /// Creates a new vertex builder.
  pub fn new() -> Self {
    return Self {
      position: [0.0, 0.0, 0.0],
      normal: [0.0, 0.0, 0.0],
      color: [0.0, 0.0, 0.0],
    };
  }

  /// Set the position of the vertex.
  pub fn with_position(&mut self, position: [f32; 3]) -> &mut Self {
    self.position = position;
    return self;
  }

  /// Set the normal of the vertex.
  pub fn with_normal(&mut self, normal: [f32; 3]) -> &mut Self {
    self.normal = normal;
    return self;
  }

  /// Set the color of the vertex.
  pub fn with_color(&mut self, color: [f32; 3]) -> &mut Self {
    self.color = color;
    return self;
  }

  /// Build the vertex.
  pub fn build(&self) -> Vertex {
    return Vertex {
      position: self.position,
      normal: self.normal,
      color: self.color,
    };
  }
}

#[cfg(test)]
mod test {
  #[test]
  fn vertex_building() {
    let mut vertex = super::VertexBuilder::new();

    assert_eq!(vertex.position, [0.0, 0.0, 0.0]);
    assert_eq!(vertex.normal, [0.0, 0.0, 0.0]);
    assert_eq!(vertex.color, [0.0, 0.0, 0.0]);

    let vertex = vertex
      .with_position([1.0, 2.0, 3.0])
      .with_normal([4.0, 5.0, 6.0])
      .with_color([7.0, 8.0, 9.0])
      .build();

    assert_eq!(vertex.position, [1.0, 2.0, 3.0]);
    assert_eq!(vertex.normal, [4.0, 5.0, 6.0]);
    assert_eq!(vertex.color, [7.0, 8.0, 9.0]);
  }
}