library: render: mesh: load basic material from OBJ
This commit is contained in:
parent
6ba0f328cd
commit
fe5eee0172
|
@ -70,6 +70,25 @@ impl LinearColor {
|
||||||
LinearColor { r, g, b }
|
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]
|
#[must_use]
|
||||||
/// Clamps the color's RGB components between 0.0 and 1.0.
|
/// Clamps the color's RGB components between 0.0 and 1.0.
|
||||||
///
|
///
|
||||||
|
|
|
@ -35,7 +35,7 @@ impl TryFrom<Wavefront> for Mesh {
|
||||||
fn try_from(wavefront: Wavefront) -> Result<Mesh, Self::Error> {
|
fn try_from(wavefront: Wavefront) -> Result<Mesh, Self::Error> {
|
||||||
let mut shapes = Vec::new();
|
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 {
|
for model in models {
|
||||||
let mesh = &model.mesh;
|
let mesh = &model.mesh;
|
||||||
|
@ -78,16 +78,24 @@ impl TryFrom<Wavefront> for Mesh {
|
||||||
|
|
||||||
// FIXME: handle material
|
// FIXME: handle material
|
||||||
let (material, texture): (MaterialEnum, TextureEnum) =
|
let (material, texture): (MaterialEnum, TextureEnum) =
|
||||||
if let Some(_) = mesh.material_id {
|
if let Some(mat_id) = mesh.material_id {
|
||||||
(
|
let mesh_mat = &materials[mat_id];
|
||||||
UniformMaterial::new(LightProperties::new(
|
|
||||||
LinearColor::new(1.0, 0.0, 0.0),
|
let diffuse = LinearColor::from_slice(&mesh_mat.ambient[..]);
|
||||||
LinearColor::new(0.0, 0.0, 0.0),
|
let specular = LinearColor::from_slice(&mesh_mat.ambient[..]);
|
||||||
None,
|
|
||||||
))
|
let material = UniformMaterial::new(LightProperties::new(
|
||||||
.into(),
|
diffuse.clone(),
|
||||||
UniformTexture::new(LinearColor::new(0.5, 0.5, 0.5)).into(),
|
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 {
|
} else {
|
||||||
// FIXME: should we accept this, and use a default
|
// FIXME: should we accept this, and use a default
|
||||||
// Material, or throw a LoadError
|
// Material, or throw a LoadError
|
||||||
|
|
Loading…
Reference in a new issue