Tiny OBJ loader, inspired by Syoyo's excellent tinyobjloader. Aims to be a simple and lightweight option for loading OBJ files, simply returns two vecs containing loaded models and materials. All models are made of triangles, any quad or polygon faces in an OBJ file will be converted to triangles.
Rust doc can be found here.
Add the crate as a dependency in your Cargo.toml and you're all set!
In this simple example we load the classic Cornell Box model that only defines positions and print out its attributes.
```rust extern crate tobj;
use std::path::Path; use tobj;
let cornellbox = tobj::loadobj(&Path::new("cornellbox.obj")); assert!(cornellbox.isok()); let (models, materials) = cornellbox.unwrap();
println!("# of models: {}", models.len()); println!("# of materials: {}", materials.len()); for (i, m) in models.iter().enumerate() { let mesh = &m.mesh; println!("model[{}].name = {}", i, m.name); println!("model[{}].mesh.materialid = {:?}", i, mesh.materialid);
println!("Size of model[{}].indices: {}", i, mesh.indices.len());
for f in 0..(mesh.indices.len() / 3) {
println!(" idx[{}] = {}, {}, {}.", f, mesh.indices[3 * f],
mesh.indices[3 * f + 1], mesh.indices[3 * f + 2]);
}
// Normals and texture coordinates are also loaded, but not printed in this example
println!("model[{}].vertices: {}", i, mesh.positions.len() / 3);
assert!(mesh.positions.len() % 3 == 0);
for v in 0..(mesh.positions.len() / 3) {
println!(" v[{}] = ({}, {}, {})", v, mesh.positions[3 * v],
mesh.positions[3 * v + 1], mesh.positions[3 * v + 2]);
}
}
for (i, m) in materials.iter().enumerate() { println!("material[{}].name = {}", i, m.name); println!(" material.Ka = ({}, {}, {})", m.ambient[0], m.ambient[1], m.ambient[2]); println!(" material.Kd = ({}, {}, {})", m.diffuse[0], m.diffuse[1], m.diffuse[2]); println!(" material.Ks = ({}, {}, {})", m.specular[0], m.specular[1], m.specular[2]); println!(" material.Ns = {}", m.shininess); println!(" material.d = {}", m.dissolve); println!(" material.mapKa = {}", m.ambienttexture); println!(" material.mapKd = {}", m.diffusetexture); println!(" material.mapKs = {}", m.speculartexture); println!(" material.mapNs = {}", m.normaltexture); println!(" material.mapd = {}", m.dissolvetexture); for (k, v) in &m.unknown_param { println!(" material.{} = {}", k, v); } } ```
For an example of integration with glium to make a simple OBJ viewer, check out tobj viewer. A sample image from the viewer is shown below, the Rust logo model was made by Nylithius on BlenderArtists. The Rungholt model can be found on Morgan McGuire's meshes page and was originally built by kescha.
The Rungholt model is reasonably large (6.7M triangles, 12.3M vertices) and is loaded in 8.765s (+/- .56s) using a peak of ~1GB of memory on a Windows 8 machine with an i7-4790k and 16GB of 1600Mhz DDR3 RAM on tobj 0.0.5 and rustc 1.1.0-nightly 97d4e76c2. Future work will focus on improving performance and memory usage.