A big commit

This commit is contained in:
Thomas Forgione
2018-04-11 17:03:32 +02:00
parent 6cb76e9986
commit 211f7fc5c5
7 changed files with 129 additions and 30 deletions
+26 -1
View File
@@ -4,7 +4,7 @@ pub mod obj;
pub mod mtl;
use std::fmt;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::{BufReader, Error};
use math::vector::{Vector2, Vector3};
@@ -19,6 +19,9 @@ pub enum Element {
/// An empty element (correspond to an empty line for example).
None,
/// Declares the use of a material file.
MaterialLib(String),
/// Changes the material used for the next faces.
UseMaterial(String),
@@ -48,6 +51,9 @@ pub enum Element {
/// An unknown material instruction that will be copied into the mtl file.
UnknownMaterialInstruction(String),
/// Declares multiple faces.
Faces(Vec<Face>),
}
#[derive(Debug)]
@@ -225,6 +231,16 @@ impl<LP: LineParser> Parser for LP {
Element::TextureCoordinate(v) => model.texture_coordinates.push(v),
Element::Normal(v) => model.normals.push(v),
Element::MaterialLib(ref material_path) => {
// Append material_path to path
let mut path = PathBuf::from(path);
path.pop();
path.push(material_path);
if let Err(err) = parse_into_model(path.to_str().unwrap(), model) {
return Err(err);
}
},
Element::Face(f) => {
if let Err(ModelError::IndexError(index, size)) = model.add_face(f) {
return Err(ParserError::IndexOutOfBound(path.to_owned(), num, index, size));
@@ -274,7 +290,16 @@ impl<LP: LineParser> Parser for LP {
}
current_material.unwrap().add_unknown_instruction(s);
},
Element::Faces(faces) => {
for f in faces {
if let Err(ModelError::IndexError(index, size)) = model.add_face(f) {
return Err(ParserError::IndexOutOfBound(path.to_owned(), num, index, size));
}
}
}
}
}
+11 -5
View File
@@ -153,7 +153,6 @@ impl ObjParser {
if let Some(texture_coordinate_string) = texture_coordinate_string {
if texture_coordinate_string.len() != 0 {
let texture_coordinate_index = texture_coordinate_string.parse::<usize>();
if texture_coordinate_index.is_err() {
return Err(ParserError::ParseNumberError(
path.to_owned(),line_number, texture_coordinate_string.to_owned()
@@ -185,10 +184,15 @@ impl ObjParser {
}
if face_vertices.len() != 3 {
Err(ParserError::TooManyVertices(path.to_owned(), line_number, face_vertices.len()))
} else {
if face_vertices.len() == 3 {
Ok(Element::Face(Face::new(face_vertices[0], face_vertices[1], face_vertices[2])))
} else if face_vertices.len() == 4 {
Ok(Element::Faces(vec![
Face::new(face_vertices[0], face_vertices[1], face_vertices[2]),
Face::new(face_vertices[0], face_vertices[2], face_vertices[3]),
]))
} else {
Err(ParserError::TooManyVertices(path.to_owned(), line_number, face_vertices.len()))
}
@@ -203,12 +207,14 @@ impl LineParser for ObjParser {
if let Some(first) = split.nth(0) {
match first {
"mtllib" => Ok(Element::MaterialLib(split.nth(0).unwrap().to_owned())),
"v" => self.parse_vertex(line_number, line, path),
"vt" => self.parse_texture_coordinate(line_number, line, path),
"vn" => self.parse_normal(line_number, line, path),
"usemtl" => Ok(Element::UseMaterial(split.nth(0).unwrap().to_owned())),
"f" => self.parse_face(line_number, line, path),
"#" | "g" | "s" | "o" | "mtllib" => Ok(Element::None),
"#" | "g" | "s" | "o" => Ok(Element::None),
key if key.len() != 0 => Err(ParserError::UnexpectedKeyword(path.to_owned(), line_number, key.to_owned())),
_ => Ok(Element::None),
}