From fe5eee01721e8c7d0f68900a0de5e6cb5ffb6c54 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Thu, 26 Mar 2020 23:16:06 +0100 Subject: [PATCH] library: render: mesh: load basic material from OBJ --- pathtracer/src/core/color.rs | 19 +++++++++++++++++++ pathtracer/src/render/mesh.rs | 30 +++++++++++++++++++----------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/pathtracer/src/core/color.rs b/pathtracer/src/core/color.rs index 5a90730..b2406ae 100644 --- a/pathtracer/src/core/color.rs +++ b/pathtracer/src/core/color.rs @@ -70,6 +70,25 @@ impl LinearColor { LinearColor { r, g, b } } + /// Creates a new `Color` from a slice. + /// + /// Panics if slice has less than 3 elements. + /// + /// # Examples + /// + /// ``` + /// # use pathtracer::core::LinearColor; + /// # + /// let color = LinearColor::new(&[1.0, 0.0, 0.0]); // bright red! + /// ``` + pub fn from_slice(s: &[f32]) -> Self { + LinearColor { + r: s[0], + g: s[1], + b: s[2], + } + } + #[must_use] /// Clamps the color's RGB components between 0.0 and 1.0. /// diff --git a/pathtracer/src/render/mesh.rs b/pathtracer/src/render/mesh.rs index c201858..7c3748b 100644 --- a/pathtracer/src/render/mesh.rs +++ b/pathtracer/src/render/mesh.rs @@ -35,7 +35,7 @@ impl TryFrom for Mesh { fn try_from(wavefront: Wavefront) -> Result { let mut shapes = Vec::new(); - let (models, _materials) = load_obj(&wavefront.obj_file)?; + let (models, materials) = load_obj(&wavefront.obj_file)?; for model in models { let mesh = &model.mesh; @@ -78,16 +78,24 @@ impl TryFrom for Mesh { // FIXME: handle material let (material, texture): (MaterialEnum, TextureEnum) = - if let Some(_) = mesh.material_id { - ( - UniformMaterial::new(LightProperties::new( - LinearColor::new(1.0, 0.0, 0.0), - LinearColor::new(0.0, 0.0, 0.0), - None, - )) - .into(), - UniformTexture::new(LinearColor::new(0.5, 0.5, 0.5)).into(), - ) + if let Some(mat_id) = mesh.material_id { + let mesh_mat = &materials[mat_id]; + + let diffuse = LinearColor::from_slice(&mesh_mat.ambient[..]); + let specular = LinearColor::from_slice(&mesh_mat.ambient[..]); + + let material = UniformMaterial::new(LightProperties::new( + diffuse.clone(), + specular, + // FIXME: material.dissolve is supposed to be "the alpha term" + // Needs translation to our ReflTransEnum + None, + )); + + // we only handle uniform textures + let texture = UniformTexture::new(diffuse); + + (material.into(), texture.into()) } else { // FIXME: should we accept this, and use a default // Material, or throw a LoadError