diff options
Diffstat (limited to 'engine/src/file_format/wavefront/mtl.rs')
-rw-r--r-- | engine/src/file_format/wavefront/mtl.rs | 115 |
1 files changed, 50 insertions, 65 deletions
diff --git a/engine/src/file_format/wavefront/mtl.rs b/engine/src/file_format/wavefront/mtl.rs index 9c6c136..ef6e894 100644 --- a/engine/src/file_format/wavefront/mtl.rs +++ b/engine/src/file_format/wavefront/mtl.rs @@ -133,64 +133,31 @@ fn statements_to_materials( match statement.keyword { Keyword::Ka => { - if statement.arguments.len() > 3 { - return Err(Error::InvalidArgumentCount { - keyword: statement.keyword.to_string(), - arg_count: statement.arguments.len(), - line_no, - }); - } - - let red = statement.get_float_arg(0, line_no)?; - let green = statement.get_float_arg(1, line_no)?; - let blue = statement.get_float_arg(2, line_no)?; + let color = get_color_from_statement(&statement, line_no)?; #[cfg(feature = "debug")] tracing::debug!("Adding ambient color"); - curr_material.material_builder = curr_material - .material_builder - .ambient(Color { red, green, blue }); + curr_material.material_builder = + curr_material.material_builder.ambient(color); } Keyword::Kd => { - if statement.arguments.len() > 3 { - return Err(Error::InvalidArgumentCount { - keyword: statement.keyword.to_string(), - arg_count: statement.arguments.len(), - line_no, - }); - } - - let red = statement.get_float_arg(0, line_no)?; - let green = statement.get_float_arg(1, line_no)?; - let blue = statement.get_float_arg(2, line_no)?; + let color = get_color_from_statement(&statement, line_no)?; #[cfg(feature = "debug")] tracing::debug!("Adding diffuse color"); - curr_material.material_builder = curr_material - .material_builder - .diffuse(Color { red, green, blue }); + curr_material.material_builder = + curr_material.material_builder.diffuse(color); } Keyword::Ks => { - if statement.arguments.len() > 3 { - return Err(Error::InvalidArgumentCount { - keyword: statement.keyword.to_string(), - arg_count: statement.arguments.len(), - line_no, - }); - } - - let red = statement.get_float_arg(0, line_no)?; - let green = statement.get_float_arg(1, line_no)?; - let blue = statement.get_float_arg(2, line_no)?; + let color = get_color_from_statement(&statement, line_no)?; #[cfg(feature = "debug")] tracing::debug!("Adding specular color"); - curr_material.material_builder = curr_material - .material_builder - .specular(Color { red, green, blue }); + curr_material.material_builder = + curr_material.material_builder.specular(color); } Keyword::MapKa => { if statement.arguments.len() > 1 { @@ -216,17 +183,7 @@ fn statements_to_materials( .ambient_map(texture_id); } Keyword::MapKd => { - if statement.arguments.len() > 1 { - return Err(Error::UnsupportedArgumentCount { - keyword: statement.keyword.to_string(), - arg_count: statement.arguments.len(), - line_no, - }); - } - - let texture_file_path = statement.get_text_arg(0, line_no)?; - - let texture = Texture::open(Path::new(texture_file_path))?; + let texture = get_map_from_texture(&statement, line_no)?; #[cfg(feature = "debug")] tracing::debug!("Adding diffuse map"); @@ -239,17 +196,7 @@ fn statements_to_materials( .diffuse_map(texture_id); } Keyword::MapKs => { - if statement.arguments.len() > 1 { - return Err(Error::UnsupportedArgumentCount { - keyword: statement.keyword.to_string(), - arg_count: statement.arguments.len(), - line_no, - }); - } - - let texture_file_path = statement.get_text_arg(0, line_no)?; - - let texture = Texture::open(Path::new(texture_file_path))?; + let texture = get_map_from_texture(&statement, line_no)?; #[cfg(feature = "debug")] tracing::debug!("Adding specular map"); @@ -261,7 +208,7 @@ fn statements_to_materials( .texture(texture) .specular_map(texture_id); } - _ => {} + Keyword::Newmtl => {} } } @@ -277,6 +224,44 @@ fn statements_to_materials( Ok(materials) } +fn get_color_from_statement( + statement: &Statement<Keyword>, + line_no: usize, +) -> Result<Color<f32>, Error> +{ + if statement.arguments.len() > 3 { + return Err(Error::InvalidArgumentCount { + keyword: statement.keyword.to_string(), + arg_count: statement.arguments.len(), + line_no, + }); + } + + let red = statement.get_float_arg(0, line_no)?; + let green = statement.get_float_arg(1, line_no)?; + let blue = statement.get_float_arg(2, line_no)?; + + Ok(Color { red, green, blue }) +} + +fn get_map_from_texture( + statement: &Statement<Keyword>, + line_no: usize, +) -> Result<Texture, Error> +{ + if statement.arguments.len() > 1 { + return Err(Error::UnsupportedArgumentCount { + keyword: statement.keyword.to_string(), + arg_count: statement.arguments.len(), + line_no, + }); + } + + let texture_file_path = statement.get_text_arg(0, line_no)?; + + Ok(Texture::open(Path::new(texture_file_path))?) +} + keyword! { #[derive(Debug, PartialEq, Eq, Clone, Copy)] enum Keyword { |