Added -s mtl option

This commit is contained in:
2018-06-20 17:24:33 +02:00
parent a487822790
commit 2b9a1064a7
6 changed files with 49 additions and 20 deletions
+14 -3
View File
@@ -46,7 +46,8 @@ pub enum Element {
///
/// First string is the name of the map.
/// Second string is the path to the image file.
Texture(String, String),
/// Vector3 is the size of the texture.
Texture(String, String, Vector3<f32>),
/// Change the main color of the current material.
Diffuse(Vector3<f32>),
@@ -107,6 +108,12 @@ pub enum ParserError {
/// usize is the line.
NoMaterialExist(String, usize),
/// No path after a map
NoPathAfterMap(String, usize),
/// Unexpected token
UnexpectedToken(String, usize, String),
/// Something weird happened
OtherError(String),
}
@@ -175,6 +182,10 @@ impl fmt::Display for ParserError {
write!(f, "Redecralation of material in {}:{}:\n\t{} already exists", p, n, l),
ParserError::NoMaterialExist(ref p, l) =>
write!(f, "Missing material in {}:{}\n\tNo material were defined, can't set attribute", p, l),
ParserError::NoPathAfterMap(ref p, l) =>
write!(f, "Missing path for map in {}:{}", p, l),
ParserError::UnexpectedToken(ref p, l, ref t) =>
write!(f, "Unexpected token {} in {}:{}", t, p, l),
ParserError::OtherError(ref p) =>
write!(f, "Something weird happened in {}...", p),
}
@@ -265,7 +276,7 @@ impl<LP: LineParser> Parser for LP {
}
},
Element::Texture(texture_name, texture_path) => {
Element::Texture(texture_name, texture_path, size) => {
if current_material_name.is_none() {
return Err(ParserError::NoMaterialExist(path.to_owned(), num));
}
@@ -279,7 +290,7 @@ impl<LP: LineParser> Parser for LP {
}
let current_material = current_material.unwrap();
current_material.textures.insert(texture_name, texture_path);
current_material.textures.insert(texture_name, (texture_path, size));
},
Element::Diffuse(v) => {
+19 -3
View File
@@ -54,9 +54,25 @@ impl LineParser for MtlParser {
map if map.starts_with("map_") => {
let mut full_path = root.clone();
full_path.push(split.nth(0).unwrap().to_owned());
Ok(Element::Texture(first.to_owned(), full_path.to_str().unwrap().to_owned()))
let split = split.collect::<Vec<_>>();
if split.len() == 0 {
Err(ParserError::NoPathAfterMap(path.to_owned(), line_number))
} else if split.len() == 1 {
let mut full_path = root.clone();
full_path.push(split[0].to_owned());
Ok(Element::Texture(first.to_owned(), full_path.to_str().unwrap().to_owned(), Vector3::new(1.0, 1.0, 1.0)))
} else if split[0] == "-s" {
let size = Vector3::new(
if let Ok(f) = split[1].parse::<f32>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[1].to_owned())); },
if let Ok(f) = split[2].parse::<f32>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[2].to_owned())); },
if let Ok(f) = split[3].parse::<f32>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[3].to_owned())); },
);
let mut full_path = root.clone();
full_path.push(split[4].to_owned());
Ok(Element::Texture(first.to_owned(), full_path.to_str().unwrap().to_owned(), size))
} else {
Err(ParserError::UnexpectedKeyword(path.to_owned(), line_number, split[2].to_owned()))
}
},