library: render: mesh: handle empty normals in OBJ

This commit is contained in:
Antoine Martin 2020-03-26 22:48:32 +01:00 committed by Bruno BELANYI
parent 0368edbd74
commit 6ba0f328cd

View file

@ -10,9 +10,9 @@ use tobj::{self, load_obj};
use super::Object;
use crate::{
core::{LightProperties, LinearColor},
material::UniformMaterial,
shape::InterpolatedTriangle,
texture::UniformTexture,
material::{MaterialEnum, UniformMaterial},
shape::{InterpolatedTriangle, ShapeEnum, Triangle},
texture::{TextureEnum, UniformTexture},
Point, Vector,
};
@ -54,7 +54,9 @@ impl TryFrom<Wavefront> for Mesh {
let pos_b = Point::from_slice(&mesh.positions[(b * 3)..(b * 3 + 2)]);
let pos_c = Point::from_slice(&mesh.positions[(c * 3)..(c * 3 + 2)]);
// FIXME: normals could be empty
let triangle: ShapeEnum = if mesh.normals.is_empty() {
Triangle::new(pos_a, pos_b, pos_c).into()
} else {
let norm_a = Unit::new_normalize(Vector::new(
mesh.normals[a * 3],
mesh.normals[a * 3 + 1],
@ -71,15 +73,13 @@ impl TryFrom<Wavefront> for Mesh {
mesh.normals[c * 3 + 2],
));
let t = InterpolatedTriangle::new(pos_a, pos_b, pos_c, norm_a, norm_b, norm_c);
InterpolatedTriangle::new(pos_a, pos_b, pos_c, norm_a, norm_b, norm_c).into()
};
// FIXME: handle material
let (material, texture): (MaterialEnum, TextureEnum) =
if let Some(_) = mesh.material_id {
} else {
// FIXME: should we accept this, and use a default
// Material, or throw a LoadError
shapes.push(Object::new(
t.into(),
(
UniformMaterial::new(LightProperties::new(
LinearColor::new(1.0, 0.0, 0.0),
LinearColor::new(0.0, 0.0, 0.0),
@ -87,8 +87,22 @@ impl TryFrom<Wavefront> for Mesh {
))
.into(),
UniformTexture::new(LinearColor::new(0.5, 0.5, 0.5)).into(),
));
}
)
} else {
// FIXME: should we accept this, and use a default
// Material, or throw a LoadError
(
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(),
)
};
shapes.push(Object::new(triangle, material, texture));
}
}