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
use crate::core::*;
pub struct Mesh {
pub position_buffer: VertexBuffer,
pub normal_buffer: Option<VertexBuffer>,
pub tangent_buffer: Option<VertexBuffer>,
pub uv_buffer: Option<VertexBuffer>,
pub color_buffer: Option<VertexBuffer>,
pub index_buffer: Option<ElementBuffer>,
pub name: String,
}
impl Mesh {
pub fn new(context: &Context, cpu_mesh: &CPUMesh) -> ThreeDResult<Self> {
cpu_mesh.validate()?;
let position_buffer = VertexBuffer::new_with_static(context, &cpu_mesh.positions)?;
let normal_buffer = if let Some(ref normals) = cpu_mesh.normals {
Some(VertexBuffer::new_with_static(context, normals)?)
} else {
None
};
let tangent_buffer = if let Some(ref tangents) = cpu_mesh.tangents {
Some(VertexBuffer::new_with_static(context, tangents)?)
} else {
None
};
let index_buffer = if let Some(ref indices) = cpu_mesh.indices {
Some(match indices {
Indices::U8(ind) => ElementBuffer::new_with(context, ind)?,
Indices::U16(ind) => ElementBuffer::new_with(context, ind)?,
Indices::U32(ind) => ElementBuffer::new_with(context, ind)?,
})
} else {
None
};
let uv_buffer = if let Some(ref uvs) = cpu_mesh.uvs {
Some(VertexBuffer::new_with_static(context, uvs)?)
} else {
None
};
let color_buffer = if let Some(ref colors) = cpu_mesh.colors {
Some(VertexBuffer::new_with_static(context, colors)?)
} else {
None
};
Ok(Self {
position_buffer,
normal_buffer,
tangent_buffer,
index_buffer,
uv_buffer,
color_buffer,
name: cpu_mesh.name.clone(),
})
}
}