1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:47:35 +00:00

3DFileViewer: Calculate face-normal from vertex-normals of the triangle

This change calculates the face-normal of the triangle by adding
the three vertex-normals and then normalizing. This results in an
average of the three vertex-normals.
This commit is contained in:
Pedro Pereira 2021-11-05 01:35:06 +00:00 committed by Andreas Kling
parent 5fd58a2663
commit ed41f48ea4
2 changed files with 26 additions and 4 deletions

View file

@ -58,10 +58,30 @@ void Mesh::draw(float uv_scale)
m_vertex_list.at(triangle.c).y,
m_vertex_list.at(triangle.c).z);
// Compute the triangle normal
const FloatVector3 vec_ab = vertex_b - vertex_a;
const FloatVector3 vec_ac = vertex_c - vertex_a;
const FloatVector3 normal = vec_ab.cross(vec_ac).normalized();
FloatVector3 normal;
if (has_normals()) {
const FloatVector3 normal_a(
m_normal_list.at(triangle.normal_index0).x,
m_normal_list.at(triangle.normal_index0).y,
m_normal_list.at(triangle.normal_index0).z);
const FloatVector3 normal_b(
m_normal_list.at(triangle.normal_index1).x,
m_normal_list.at(triangle.normal_index1).y,
m_normal_list.at(triangle.normal_index1).z);
const FloatVector3 normal_c(
m_normal_list.at(triangle.normal_index2).x,
m_normal_list.at(triangle.normal_index2).y,
m_normal_list.at(triangle.normal_index2).z);
normal = (normal_a + normal_b + normal_c).normalized();
} else {
// Compute the triangle normal
const FloatVector3 vec_ab = vertex_b - vertex_a;
const FloatVector3 vec_ac = vertex_c - vertex_a;
normal = vec_ab.cross(vec_ac).normalized();
}
// Compute lighting with a Lambertian color model
const auto light_intensity = max(light_direction.dot(normal), 0.f);