gl_utils/mesh/
cylinder.rs1use std;
2use crate::{math, vertex};
3use super::*;
4
5#[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 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 indices.push (index_offset + i);
48 indices.push (index_offset + (i + 1) % divisions);
49 indices.push (index_offset + i);
51 indices.push (index_offset + i + divisions);
52 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}