bevy/test-data/generate-obj.rs
alteous dc8fe97715 Add closures interface
Former-commit-id: 76f4a864fa3b1fbde9d5ac2eab7c5d0639d147ef
2017-08-29 19:16:39 +01:00

46 lines
1.5 KiB
Rust

extern crate gltf;
use std::io::Write;
fn main() {
let path = "test-data/Avocado.gltf";
let gltf = gltf::Import::from_path(path).sync().unwrap();
let mesh = gltf.meshes().nth(0).unwrap();
let primitive = mesh.primitives().nth(0).unwrap();
let positions: Vec<[f32; 3]> = primitive.positions().unwrap().collect();
let normals: Vec<[f32; 3]> = primitive.normals().unwrap().collect();
let mut tex_coords: Vec<[f32; 2]> = vec![];
let mut indices: Vec<u16> = vec![];
match primitive.tex_coords(0).unwrap() {
gltf::mesh::TexCoords::F32(iter) => tex_coords.extend(iter),
_ => unreachable!(),
}
match primitive.indices().unwrap() {
gltf::mesh::Indices::U16(iter) => indices.extend(iter),
_ => unreachable!(),
}
let file = std::fs::File::create("Avocado.obj").unwrap();
let mut writer = std::io::BufWriter::new(file);
for position in &positions {
writeln!(writer, "v {} {} {}", position[0], position[1], position[2]);
}
for normal in &normals {
writeln!(writer, "vn {} {} {}", normal[0], normal[1], normal[2]);
}
for tex_coord in &tex_coords {
writeln!(writer, "vt {} {}", tex_coord[0], tex_coord[1]);
}
let mut i = indices.iter();
while let (Some(v0), Some(v1), Some(v2)) = (i.next(), i.next(), i.next()) {
writeln!(
writer,
"f {}/{}/{} {}/{}/{} {}/{}/{}",
v0, v0, v0,
v1, v1, v1,
v2, v2, v2,
);
}
}