Function tobj::load_obj_buf
source · pub fn load_obj_buf<B, ML>(
reader: &mut B,
load_options: &LoadOptions,
material_loader: ML
) -> LoadResult
Expand description
Load the various meshes in an OBJ
buffer.
This could e.g. be a network stream, a text file already in memory etc.
§Arguments
You must pass a material_loader
function, which will return a material
given a name.
A trivial material loader may just look at the file name and then call
load_mtl_buf
with the in-memory MTL file source.
Alternatively it could pass an MTL
file in memory to load_mtl_buf
to
parse materials from some buffer.
load_options
– Governs on-the-fly processing of the mesh during loading. SeeLoadOptions
for more information.
§Example
The test for load_obj_buf
includes the OBJ and MTL files as strings
and uses a Cursor
to provide a BufRead
interface on the buffer.
use std::{env, fs::File, io::BufReader};
let dir = env::current_dir().unwrap();
let mut cornell_box_obj = dir.clone();
cornell_box_obj.push("obj/cornell_box.obj");
let mut cornell_box_file = BufReader::new(File::open(cornell_box_obj.as_path()).unwrap());
let mut cornell_box_mtl1 = dir.clone();
cornell_box_mtl1.push("obj/cornell_box.mtl");
let mut cornell_box_mtl2 = dir.clone();
cornell_box_mtl2.push("obj/cornell_box2.mtl");
let m = tobj::load_obj_buf(
&mut cornell_box_file,
&tobj::LoadOptions {
triangulate: true,
single_index: true,
..Default::default()
},
|p| match p.file_name().unwrap().to_str().unwrap() {
"cornell_box.mtl" => {
let f = File::open(cornell_box_mtl1.as_path()).unwrap();
tobj::load_mtl_buf(&mut BufReader::new(f))
}
"cornell_box2.mtl" => {
let f = File::open(cornell_box_mtl2.as_path()).unwrap();
tobj::load_mtl_buf(&mut BufReader::new(f))
}
_ => unreachable!(),
},
);