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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Vertex formats and associated types and functions.
//!
//! A vertex is a type representing a point. It’s common to find vertex position, normals, colors or
//! even texture coordinates. However, you’re free to use whichever type you want.
//! Nevertheless, you’re limited to a range of types and dimensions. See `Type` and
//! `Dim` for further details.
//!
//! # `Vertex`
//!
//! ## Rules
//!
//! To be able to use a type as a vertex, you have to implement the `Vertex` trait. That trait
//! represents a mapping between your type and `VertexFormat`. A `VertexFormat` gives runtime hints
//! about your type and restricts the supported type. If you cannot map your type to `VertexFormat`,
//! that means you cannot use it as a `Vertex`.
//!
//! The rule is that your type should have a static size greater than 0 and less than or equal to 4.
//! It should also be either integral, unsigned, floating or boolean. If your type is a complex one
//! – for instance a `struct` – you have to recursively apply that rule to all its fields.
//! For instance, the tuple `(i32, bool)` implements `Vertex` by providing an implementation using
//! the ones of `i32` and `bool`.
//!
//! ## Components list
//!
//! As mentionned above, you can use tuples and structs as `Vertex`. If you look at the definition
//! of `VertexFormat`, you’ll notice that it’s a `Vec<VertexComponentFormat>`. That means simple
//! and primary types map to unit vectors – i.e. their size is 1 – but tuples and structs need
//! several `VertexComponentFormat`s to be represented, hence vectors with sizes greater than 1.
//!
//! # Generic implementation
//!
//! You have `Vertex` implementations for all the primary types that can be mapped to
//! `VertexFormat`. However, as it’s not possible to automatically implement `Vertex` for your
//! structure (yet?), a type is provided to help you design your vertex type so that you’re
//! automatically provided with a `Vertex` implementation if you use `Chain`.
//!
//! `Chain` is a special type used to represent static list of types. With that in hand, you can
//! easily create `Vertex` types and start using them without even implementing `Vertex`, as long as
//! you use `Vertex` types. Feel free to dig in the `Chain` documentation for further details.

use chain::Chain;
use std::vec::Vec;

/// A `VertexFormat` is a list of `VertexComponentFormat`s.
pub type VertexFormat = Vec<VertexComponentFormat>;

/// Retrieve the number of components in a `VertexFormat`.
pub fn vertex_format_size(vf: &VertexFormat) -> usize {
  vf.len()
}

/// A `VertexComponentFormat` gives hints about:
///
/// - the type of the component (`Type`);
/// - the dimension of the component (`Dim`).
pub struct VertexComponentFormat {
  pub component_type: Type,
  pub dim: Dim
}

/// Possible type of vertex components.
pub enum Type {
  Integral,
  Unsigned,
  Floating,
  Boolean
}

/// Possible dimension of vertex components.
pub enum Dim {
  Dim1,
  Dim2,
  Dim3,
  Dim4
}

/// A type that can be used as a `Vertex` has to implement that trait – it must provide a mapping
/// to `VertexFormat`.
///
/// If you’re not sure on how to implement that or if you want to use automatic types, feel free
/// to use the primary supported types and `Chain` or tuples.
pub trait Vertex {
  fn vertex_format() -> VertexFormat;
}

impl Vertex for () {
  fn vertex_format() -> VertexFormat {
    Vec::new()
  }
}

impl Vertex for i32 {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Integral, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [i32; 1] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Integral, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [i32; 2] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Integral, dim: Dim::Dim2 } ]
  }
}

impl Vertex for [i32; 3] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Integral, dim: Dim::Dim3 } ]
  }
}

impl Vertex for [i32; 4] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Integral, dim: Dim::Dim4 } ]
  }
}

impl Vertex for u32 {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Unsigned, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [u32; 1] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Unsigned, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [u32; 2] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Unsigned, dim: Dim::Dim2 } ]
  }
}

impl Vertex for [u32; 3] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Unsigned, dim: Dim::Dim3 } ]
  }
}

impl Vertex for [u32; 4] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Unsigned, dim: Dim::Dim4 } ]
  }
}

impl Vertex for f32 {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Floating, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [f32; 1] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Floating, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [f32; 2] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Floating, dim: Dim::Dim2 } ]
  }
}

impl Vertex for [f32; 3] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Floating, dim: Dim::Dim3 } ]
  }
}

impl Vertex for [f32; 4] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Floating, dim: Dim::Dim4 } ]
  }
}

impl Vertex for bool {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Boolean, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [bool; 1] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Boolean, dim: Dim::Dim1 } ]
  }
}

impl Vertex for [bool; 2] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Boolean, dim: Dim::Dim2 } ]
  }
}

impl Vertex for [bool; 3] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Boolean, dim: Dim::Dim3 } ]
  }
}

impl Vertex for [bool; 4] {
  fn vertex_format() -> VertexFormat {
    vec![ VertexComponentFormat { component_type: Type::Boolean, dim: Dim::Dim4 } ]
  }
}

impl<A, B> Vertex for Chain<A, B> where A: Vertex, B: Vertex {
  fn vertex_format() -> VertexFormat {
    let mut t = A::vertex_format();
    t.extend(B::vertex_format());
    t
  }
}

impl<A, B> Vertex for (A, B) where A: Vertex, B: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, B>::vertex_format()
  }
}

impl<A, B, C> Vertex for (A, B, C) where A: Vertex, B: Vertex, C: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, C>>::vertex_format()
  }
}

impl<A, B, C, D> Vertex for (A, B, C, D) where A: Vertex, B: Vertex, C: Vertex, D: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, Chain<C, D>>>::vertex_format()
  }
}

impl<A, B, C, D, E> Vertex for (A, B, C, D, E) where A: Vertex, B: Vertex, C: Vertex, D: Vertex, E: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, Chain<C, Chain<D, E>>>>::vertex_format()
  }
}

impl<A, B, C, D, E, F> Vertex for (A, B, C, D, E, F) where A: Vertex, B: Vertex, C: Vertex, D: Vertex, E: Vertex, F: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, Chain<C, Chain<D, Chain<E, F>>>>>::vertex_format()
  }
}

impl<A, B, C, D, E, F, G> Vertex for (A, B, C, D, E, F, G) where A: Vertex, B: Vertex, C: Vertex, D: Vertex, E: Vertex, F: Vertex, G: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, Chain<C, Chain<D, Chain<E, Chain<F, G>>>>>>::vertex_format()
  }
}

impl<A, B, C, D, E, F, G, H> Vertex for (A, B, C, D, E, F, G, H) where A: Vertex, B: Vertex, C: Vertex, D: Vertex, E: Vertex, F: Vertex, G: Vertex, H: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, Chain<C, Chain<D, Chain<E, Chain<F, Chain<G, H>>>>>>>::vertex_format()
  }
}

impl<A, B, C, D, E, F, G, H, I> Vertex for (A, B, C, D, E, F, G, H, I) where A: Vertex, B: Vertex, C: Vertex, D: Vertex, E: Vertex, F: Vertex, G: Vertex, H: Vertex, I: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, Chain<C, Chain<D, Chain<E, Chain<F, Chain<G, Chain<H, I>>>>>>>>::vertex_format()
  }
}

impl<A, B, C, D, E, F, G, H, I, J> Vertex for (A, B, C, D, E, F, G, H, I, J) where A: Vertex, B: Vertex, C: Vertex, D: Vertex, E: Vertex, F: Vertex, G: Vertex, H: Vertex, I: Vertex, J: Vertex {
  fn vertex_format() -> VertexFormat {
    Chain::<A, Chain<B, Chain<C, Chain<D, Chain<E, Chain<F, Chain<G, Chain<H, Chain<I, J>>>>>>>>>::vertex_format()
  }
}