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
use crate::d3::Vertex3D;

#[derive(Debug, Copy, Clone)]
pub struct Box {
    pub width: f32,
    pub height: f32,
    pub depth: f32,
    pub width_segments: u32,
    pub height_segments: u32,
    pub depth_segments: u32,
}

impl Box {
    pub fn new(
        width: f32,
        height: f32,
        depth: f32,
        width_segments: u32,
        height_segments: u32,
        depth_segments: u32,
    ) -> Self {
        Self {
            width,
            height,
            depth,
            width_segments,
            height_segments,
            depth_segments,
        }
    }
}

impl Default for Box {
    fn default() -> Self {
        Self {
            width: 1.,
            height: 1.,
            depth: 1.,
            width_segments: 1,
            height_segments: 1,
            depth_segments: 1,
        }
    }
}

impl crate::Geometry<Vertex3D> for Box {
    type Vertices = std::vec::IntoIter<Vertex3D>;
    type Indices = std::vec::IntoIter<u32>;

    fn vertices(&self) -> Self::Vertices {
        let mut vertices = Vec::new();

        build_plane(
            &mut vertices,
            2,
            1,
            0,
            -1.,
            -1.,
            self.depth,
            self.height,
            self.width,
            self.depth_segments,
            self.height_segments,
        );
        build_plane(
            &mut vertices,
            2,
            1,
            0,
            1.,
            -1.,
            self.depth,
            self.height,
            -self.width,
            self.depth_segments,
            self.height_segments,
        );
        build_plane(
            &mut vertices,
            0,
            2,
            1,
            1.,
            1.,
            self.width,
            self.depth,
            self.height,
            self.width_segments,
            self.depth_segments,
        );
        build_plane(
            &mut vertices,
            0,
            2,
            1,
            1.,
            -1.,
            self.width,
            self.depth,
            -self.height,
            self.width_segments,
            self.depth_segments,
        );
        build_plane(
            &mut vertices,
            0,
            1,
            2,
            1.,
            -1.,
            self.width,
            self.height,
            self.depth,
            self.width_segments,
            self.height_segments,
        );
        build_plane(
            &mut vertices,
            0,
            1,
            2,
            -1.,
            -1.,
            self.width,
            self.height,
            -self.depth,
            self.width_segments,
            self.height_segments,
        );

        vertices.into_iter()
    }

    fn indices(&self) -> Self::Indices {
        let faces = [
            (self.depth_segments, self.height_segments),
            (self.depth_segments, self.height_segments),
            (self.width_segments, self.depth_segments),
            (self.width_segments, self.depth_segments),
            (self.width_segments, self.height_segments),
            (self.width_segments, self.height_segments),
        ];

        let mut index_start = 0;
        let mut indices = Vec::new();
        for (grid_x, grid_y) in faces.iter().copied() {
            let grid_x1 = grid_x + 1;
            for iy in 0..grid_y {
                for ix in 0..grid_x {
                    let a = ix + grid_x1 * iy;
                    let b = ix + grid_x1 * (iy + 1);
                    let c = (ix + 1) + grid_x1 * (iy + 1);
                    let d = (ix + 1) + grid_x1 * iy;

                    indices.push(index_start + a);
                    indices.push(index_start + b);
                    indices.push(index_start + d);

                    indices.push(index_start + b);
                    indices.push(index_start + c);
                    indices.push(index_start + d);
                }
            }
            index_start += grid_x * grid_y * 4;
        }

        indices.into_iter()
    }
}

fn build_plane(
    vertices: &mut Vec<Vertex3D>,
    u: usize,
    v: usize,
    w: usize,
    u_dir: f32,
    v_dir: f32,
    width: f32,
    height: f32,
    depth: f32,
    grid_x: u32,
    grid_y: u32,
) {
    let segment_width = width / grid_x as f32;
    let segment_height = height / grid_y as f32;

    let width_half = width / 2.;
    let height_half = height / 2.;
    let depth_half = depth / 2.;

    let grid_x1 = grid_x + 1;
    let grid_y1 = grid_y + 1;

    for iy in 0..grid_y1 {
        let iy = iy as f32;
        let y = iy * segment_height - height_half;
        for ix in 0..grid_x1 {
            let ix = ix as f32;
            let x = ix * segment_width - width_half;

            let mut position = [0f32; 3];
            position[u] = x * u_dir;
            position[v] = y * v_dir;
            position[w] = depth_half;

            let mut normal = [0f32; 3];
            normal[u] = 0.;
            normal[v] = 0.;
            normal[w] = if depth > 0. { 1. } else { -1. };

            vertices.push(Vertex3D {
                position,
                normal,
                uv: [ix / grid_x as f32, iy / grid_y as f32],
                color: [1., 1., 1., 1.],
            })
        }
    }
}