-
Notifications
You must be signed in to change notification settings - Fork 45
/
print_mesh.rs
121 lines (108 loc) · 4.16 KB
/
print_mesh.rs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
fn main() {
let obj_file = std::env::args()
.nth(1)
.expect("A .obj file to print is required");
let (models, materials) =
tobj::load_obj(obj_file, &tobj::LoadOptions::default()).expect("Failed to OBJ load file");
// Note: If you don't mind missing the materials, you can generate a default.
let materials = materials.expect("Failed to load MTL file");
println!("Number of models = {}", models.len());
println!("Number of materials = {}", materials.len());
for (i, m) in models.iter().enumerate() {
let mesh = &m.mesh;
println!();
println!("model[{}].name = \'{}\'", i, m.name);
println!("model[{}].mesh.material_id = {:?}", i, mesh.material_id);
println!(
"model[{}].face_count = {}",
i,
mesh.face_arities.len()
);
let mut next_face = 0;
for face in 0..mesh.face_arities.len() {
let end = next_face + mesh.face_arities[face] as usize;
let face_indices = &mesh.indices[next_face..end];
println!(" face[{}].indices = {:?}", face, face_indices);
if !mesh.texcoord_indices.is_empty() {
let texcoord_face_indices = &mesh.texcoord_indices[next_face..end];
println!(
" face[{}].texcoord_indices = {:?}",
face, texcoord_face_indices
);
}
if !mesh.normal_indices.is_empty() {
let normal_face_indices = &mesh.normal_indices[next_face..end];
println!(
" face[{}].normal_indices = {:?}",
face, normal_face_indices
);
}
next_face = end;
}
// Normals and texture coordinates are also loaded, but not printed in
// this example.
println!(
"model[{}].positions = {}",
i,
mesh.positions.len() / 3
);
assert!(mesh.positions.len() % 3 == 0);
for vtx in 0..mesh.positions.len() / 3 {
println!(
" position[{}] = ({}, {}, {})",
vtx,
mesh.positions[3 * vtx],
mesh.positions[3 * vtx + 1],
mesh.positions[3 * vtx + 2]
);
}
}
for (i, m) in materials.iter().enumerate() {
println!("material[{}].name = \'{}\'", i, m.name);
if let Some(ambient) = m.ambient {
println!(
" material.Ka = ({}, {}, {})",
ambient[0], ambient[1], ambient[2]
);
}
if let Some(diffuse) = m.diffuse {
println!(
" material.Kd = ({}, {}, {})",
diffuse[0], diffuse[1], diffuse[2]
);
}
if let Some(specular) = m.specular {
println!(
" material.Ks = ({}, {}, {})",
specular[0], specular[1], specular[2]
);
}
if let Some(shininess) = m.shininess {
println!(" material.Ns = {}", shininess);
}
if let Some(dissolve) = m.dissolve {
println!(" material.d = {}", dissolve);
}
if let Some(ambient_texture) = &m.ambient_texture {
println!(" material.map_Ka = {}", ambient_texture);
}
if let Some(diffuse_texture) = &m.diffuse_texture {
println!(" material.map_Kd = {}", diffuse_texture);
}
if let Some(specular_texture) = &m.specular_texture {
println!(" material.map_Ks = {}", specular_texture);
}
if let Some(shininess_texture) = &m.shininess_texture {
println!(" material.map_Ns = {}", shininess_texture);
}
if let Some(normal_texture) = &m.normal_texture {
println!(" material.map_Bump = {}", normal_texture);
}
if let Some(dissolve_texture) = &m.dissolve_texture {
println!(" material.map_d = {}", dissolve_texture);
}
for (k, v) in &m.unknown_param {
println!(" material.{} = {}", k, v);
}
}
}