@@ -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