1use super::generators::{IndexedPolygon, SharedVertex};
2use super::{MapVertex, Quad};
3use std::ops::Range;
4use {Normal, Position, Vertex};
5
6#[derive(Clone)]
8pub struct Cube {
9 range: Range<usize>,
10}
11
12impl Cube {
13 pub fn new() -> Self {
15 Cube { range: 0..6 }
16 }
17
18 fn vert(&self, idx: usize) -> Position {
19 let x = if idx & 4 == 4 { 1. } else { -1. };
20 let y = if idx & 2 == 2 { 1. } else { -1. };
21 let z = if idx & 1 == 1 { 1. } else { -1. };
22 [x, y, z].into()
23 }
24
25 fn face_indexed(&self, idx: usize) -> (Normal, Quad<usize>) {
26 match idx {
27 0 => ([1., 0., 0.].into(), Quad::new(0b110, 0b111, 0b101, 0b100)),
28 1 => ([-1., 0., 0.].into(), Quad::new(0b000, 0b001, 0b011, 0b010)),
29 2 => ([0., 1., 0.].into(), Quad::new(0b011, 0b111, 0b110, 0b010)),
30 3 => ([0., -1., 0.].into(), Quad::new(0b100, 0b101, 0b001, 0b000)),
31 4 => ([0., 0., 1.].into(), Quad::new(0b101, 0b111, 0b011, 0b001)),
32 5 => ([0., 0., -1.].into(), Quad::new(0b000, 0b010, 0b110, 0b100)),
33 idx => panic!("{} face is higher then 6", idx),
34 }
35 }
36
37 fn face(&self, idx: usize) -> Quad<Vertex> {
38 let (no, quad) = self.face_indexed(idx);
39 quad.map_vertex(|i| Vertex {
40 pos: self.vert(i),
41 normal: no,
42 })
43 }
44}
45
46impl Iterator for Cube {
47 type Item = Quad<Vertex>;
48
49 fn next(&mut self) -> Option<Quad<Vertex>> {
50 self.range.next().map(|idx| self.face(idx))
51 }
52
53 fn size_hint(&self) -> (usize, Option<usize>) {
54 self.range.size_hint()
55 }
56}
57
58impl SharedVertex<Vertex> for Cube {
59 fn shared_vertex(&self, idx: usize) -> Vertex {
60 let (no, quad) = self.face_indexed(idx / 4);
61 let vid = match idx % 4 {
62 0 => quad.x,
63 1 => quad.y,
64 2 => quad.z,
65 3 => quad.w,
66 _ => unreachable!(),
67 };
68 Vertex {
69 pos: self.vert(vid),
70 normal: no,
71 }
72 }
73
74 fn shared_vertex_count(&self) -> usize {
75 24
76 }
77}
78
79impl IndexedPolygon<Quad<usize>> for Cube {
80 fn indexed_polygon(&self, idx: usize) -> Quad<usize> {
81 Quad::new(idx * 4 + 0, idx * 4 + 1, idx * 4 + 2, idx * 4 + 3)
82 }
83
84 fn indexed_polygon_count(&self) -> usize {
85 6
86 }
87}