Skip to content

Commit 7e3a7e8

Browse files
qingyouzhaoiche033
authored andcommitted
Check normal presence before trying to read asMesh normals (#654)
* In AssimpLoader::Implementation::CreateSubMesh check normal presence before reading * RecalculateNormals if submesh has no normals --------- Signed-off-by: Qingyou Zhao <[email protected]> (cherry picked from commit a50d8d3)
1 parent 959f899 commit 7e3a7e8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

graphics/src/AssimpLoader.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -667,18 +667,20 @@ SubMesh AssimpLoader::Implementation::CreateSubMesh(
667667
{
668668
// Add the vertex
669669
math::Vector3d vertex;
670-
math::Vector3d normal;
671670
vertex.X(_assimpMesh->mVertices[vertexIdx].x);
672671
vertex.Y(_assimpMesh->mVertices[vertexIdx].y);
673672
vertex.Z(_assimpMesh->mVertices[vertexIdx].z);
674-
normal.X(_assimpMesh->mNormals[vertexIdx].x);
675-
normal.Y(_assimpMesh->mNormals[vertexIdx].y);
676-
normal.Z(_assimpMesh->mNormals[vertexIdx].z);
677673
vertex = _transform * vertex;
678-
normal = rot * normal;
679-
normal.Normalize();
680674
subMesh.AddVertex(vertex);
681-
subMesh.AddNormal(normal);
675+
if (_assimpMesh->HasNormals()) {
676+
math::Vector3d normal;
677+
normal.X(_assimpMesh->mNormals[vertexIdx].x);
678+
normal.Y(_assimpMesh->mNormals[vertexIdx].y);
679+
normal.Z(_assimpMesh->mNormals[vertexIdx].z);
680+
normal = rot * normal;
681+
normal.Normalize();
682+
subMesh.AddNormal(normal);
683+
}
682684
// Iterate over sets of texture coordinates
683685
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i)
684686
{
@@ -698,6 +700,9 @@ SubMesh AssimpLoader::Implementation::CreateSubMesh(
698700
subMesh.AddIndex(face.mIndices[2]);
699701
}
700702
subMesh.SetMaterialIndex(_assimpMesh->mMaterialIndex);
703+
if (subMesh.NormalCount() == 0u){
704+
subMesh.RecalculateNormals();
705+
}
701706
return subMesh;
702707
}
703708

0 commit comments

Comments
 (0)