gl_utils/mesh/
grid.rs

1use crate::vertex;
2use super::*;
3
4/// (vertex count, index count)
5#[inline]
6pub fn lines3d_vertex_index_counts (dims : u16) -> (u32, u32) {
7  let dims = dims as u32;
8  ( (dims+1) * (dims+1),
9    4 * (dims * dims + dims) )
10}
11
12impl Lines3d {
13  /// Produces vertices and indices for a 3D lines list arranged in a square grid
14  /// in the X/Y plane of `dims` by `dims` dimensions.
15  ///
16  /// The number of vertices will be `(dims + 1)^2`.
17  ///
18  /// The number of indices will be `4*(dims^2 + dims)`
19  ///
20  /// # Panics
21  ///
22  /// Panics if `dims` is zero
23  pub fn grid (index_offset : u32, dims : u16) -> Self {
24    assert!(0 < dims);
25    let (num_vertices, num_indices) = lines3d_vertex_index_counts (dims);
26    let dims = dims as u32;
27    let mut vertices
28      = Vec::<vertex::Vert3dInstanced>::with_capacity (num_vertices as usize);
29    for i in 0..(dims+1) {
30      for j in 0..(dims+1) {
31        let x = i as f32 - 0.5 * dims as f32;
32        let y = j as f32 - 0.5 * dims as f32;
33        vertices.push (vertex::Vert3dInstanced { inst_position: [x, y, 0.0] })
34      }
35    }
36    debug_assert_eq!(vertices.len(), num_vertices as usize);
37
38    let mut indices  = Vec::<u32>::with_capacity (num_indices as usize);
39    for i in 0..dims {
40      for j in 0..dims {
41        let base_index = index_offset + (i * (dims+1) + j);
42        indices.push (base_index);
43        indices.push (base_index + (dims+1));
44        indices.push (base_index);
45        indices.push (base_index + 1);
46      }
47    }
48    let top_base_index   = index_offset + (dims+1) * dims;
49    let right_base_index = index_offset + dims;
50    for k in 0..dims {
51      indices.push (top_base_index   + k);
52      indices.push (top_base_index   + k + 1);
53      indices.push (right_base_index + k * (dims+1));
54      indices.push (right_base_index + (k + 1) * (dims+1));
55    }
56    debug_assert_eq!(indices.len(), num_indices as usize);
57
58    Lines3d { vertices, indices }
59  }
60}