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