[go: up one dir, main page]

obj 0.7.0

A package for loading Wavefront .obj files
Documentation
//   Copyright 2017 GFX Developers
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#[cfg(feature = "genmesh")]
extern crate genmesh;

pub use mtl::{Material, Mtl};
pub use obj::{GenPolygon, Group, IndexTuple, Obj, Object, SimplePolygon};

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, Result as IoResult};
use std::path::Path;
use std::rc::Rc;

mod obj;
mod mtl;

pub fn load<P: GenPolygon>(path: &Path) -> IoResult<Obj<Rc<Material>, P>> {
    let f = File::open(path)?;
    let obj = Obj::load(&mut BufReader::new(f));
    let mut materials = HashMap::new();

    for m in &obj.materials {
        let p = path.with_file_name(m);
        let file = File::open(&p)?;
        let mtl = Mtl::load(&mut BufReader::new(file));
        for m in mtl.materials {
            materials.insert(m.name.clone(), Rc::new(m));
        }
    }

    Ok(obj.map(|Group { name, index, material, indices }| {
        Group {
            name,
            index,
            material: material.and_then(|m| materials.get(&m).cloned()),
            indices,
        }
    }))
}