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
use na;
use na::{Pnt3, Pnt2, Vec3, BaseFloat};
use super::{TriMesh, IndexBuffer};
use super::utils;
use math::Scalar;
pub fn cylinder<N>(diameter: N, height: N, nsubdiv: u32) -> TriMesh<Pnt3<N>>
where N: Scalar {
let mut cylinder = unit_cylinder(nsubdiv);
cylinder.scale_by(&Vec3::new(diameter, height, diameter));
cylinder
}
pub fn unit_cylinder<N>(nsubdiv: u32) -> TriMesh<Pnt3<N>>
where N: Scalar {
let two_pi: N = BaseFloat::two_pi();
let invsubdiv = na::one::<N>() / na::cast(nsubdiv as f64);
let dtheta = two_pi * invsubdiv;
let mut coords = Vec::new();
let mut indices = Vec::new();
let mut normals: Vec<Vec3<N>>;
utils::push_circle(na::cast(0.5), nsubdiv, dtheta, na::cast(-0.5), &mut coords);
normals = coords.iter().map(|p| p.as_vec().clone()).collect();
utils::push_circle(na::cast(0.5), nsubdiv, dtheta, na::cast(0.5), &mut coords);
utils::push_ring_indices(0, nsubdiv, nsubdiv, &mut indices);
utils::push_filled_circle_indices(0, nsubdiv, &mut indices);
utils::push_filled_circle_indices(nsubdiv, nsubdiv, &mut indices);
let len = indices.len();
let bottom_start_id = len - (nsubdiv as usize - 2);
utils::reverse_clockwising(&mut indices[bottom_start_id ..]);
let mut indices = utils::split_index_buffer(&indices[..]);
let mut uvs = Vec::with_capacity(coords.len());
let mut curr_u = na::zero::<N>();
for _ in 0 .. nsubdiv {
uvs.push(Pnt2::new(curr_u.clone(), na::zero()));
curr_u = curr_u + invsubdiv;
}
curr_u = na::zero();
for _ in 0 .. nsubdiv {
uvs.push(Pnt2::new(curr_u.clone(), na::one()));
curr_u = curr_u + invsubdiv;
}
for n in normals.iter_mut() {
n.x = n.x * na::cast(2.0);
n.y = na::zero();
n.z = n.z * na::cast(2.0);
}
normals.push(Vec3::y());
normals.push(-Vec3::y());
let nlen = normals.len() as u32;
let top_start_id = len - 2 * (nsubdiv as usize - 2);
for i in indices[.. top_start_id].iter_mut() {
if i.x.y >= nsubdiv {
i.x.y = i.x.y - nsubdiv;
}
if i.y.y >= nsubdiv {
i.y.y = i.y.y - nsubdiv;
}
if i.z.y >= nsubdiv {
i.z.y = i.z.y - nsubdiv;
}
}
for i in indices[top_start_id .. bottom_start_id].iter_mut() {
i.x.y = nlen - 2;
i.y.y = nlen - 2;
i.z.y = nlen - 2;
}
for i in indices[bottom_start_id ..].iter_mut() {
i.x.y = nlen - 1;
i.y.y = nlen - 1;
i.z.y = nlen - 1;
}
TriMesh::new(coords, Some(normals), Some(uvs), Some(IndexBuffer::Split(indices)))
}