gl_utils/mesh/
cylinder.rs

1use std;
2use crate::{math, vertex};
3use super::*;
4
5/// (vertex count, index count)
6#[inline]
7pub fn lines3d_vertex_index_counts (divisions : u16) -> (u32, u32) {
8  ( 2 * divisions as u32, 6 * divisions as u32)
9}
10
11impl Lines3d {
12  /// Unit cylinder-- total height is 2.0 centered at 0.0 in the vertical (Z)
13  /// axis, radius 1.0 in the X/Y plane.
14  ///
15  /// # Panics
16  ///
17  /// Divisions must be 2 or greater.
18  pub fn cylinder (index_offset : u32, divisions : u16) -> Self {
19    use std::f32::consts::PI;
20    assert!(2 <= divisions);
21    let (num_vertices, num_indices) = lines3d_vertex_index_counts (divisions);
22    let divisions      = divisions as u32;
23    let division_angle = 2.0 * (PI / divisions as f32);
24    let vertices = {
25      let mut vertices
26        = Vec::<vertex::Vert3dInstanced>::with_capacity (num_vertices as usize);
27      let mut top
28        = Vec::<vertex::Vert3dInstanced>::with_capacity (num_vertices as usize / 2);
29      let mut bottom
30        = Vec::<vertex::Vert3dInstanced>::with_capacity (num_vertices as usize / 2);
31      for i in 0..divisions {
32        let i = i as f32;
33        let v = *math::Rotation3::from_angle_z (math::Rad (i * division_angle))
34          * math::Vector3::unit_y();
35        top.push    (vertex::Vert3dInstanced { inst_position: [v.x, v.y,  1.0] });
36        bottom.push (vertex::Vert3dInstanced { inst_position: [v.x, v.y, -1.0] });
37      }
38      vertices.append (&mut top);
39      vertices.append (&mut bottom);
40      vertices
41    };
42    debug_assert_eq!(vertices.len(), num_vertices as usize);
43
44    let mut indices  = Vec::<u32>::with_capacity (num_indices as usize);
45    for i in 0..divisions {
46      // connect to next vertex in the loop
47      indices.push (index_offset + i);
48      indices.push (index_offset + (i + 1) % divisions);
49      // connect to lower loop
50      indices.push (index_offset + i);
51      indices.push (index_offset + i + divisions);
52      // connect lower loop to next vertex
53      indices.push (index_offset + divisions + i);
54      indices.push (index_offset + divisions + (i + 1) % divisions);
55    }
56    debug_assert_eq!(indices.len(), num_indices as usize);
57
58    Lines3d { vertices, indices }
59  }
60}