]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Detect 'insane' normals in checkMeshNormals.
authorDmitry Kostenko <codeforsmile@gmail.com>
Mon, 8 Nov 2021 22:13:50 +0000 (23:13 +0100)
committerx2048 <codeforsmile@gmail.com>
Mon, 7 Mar 2022 22:45:26 +0000 (23:45 +0100)
Detect non-zero normals which point in the opposite direction from the
face plane normal.

src/client/mesh.cpp

index c56eba2e2fb381900ad2088c9a1dc9dec9193b81..07020088910bda436554cf3a4d53b2a0872db4c1 100644 (file)
@@ -331,6 +331,9 @@ void recalculateBoundingBox(scene::IMesh *src_mesh)
 
 bool checkMeshNormals(scene::IMesh *mesh)
 {
+       // Assume correct normals if this many first faces get it right.
+       static const u16 MAX_FACES_TO_CHECK = 9;
+
        u32 buffer_count = mesh->getMeshBufferCount();
 
        for (u32 i = 0; i < buffer_count; i++) {
@@ -344,6 +347,19 @@ bool checkMeshNormals(scene::IMesh *mesh)
 
                if (!std::isfinite(length) || length < 1e-10f)
                        return false;
+
+               const u16 count = MYMIN(MAX_FACES_TO_CHECK * 3, buffer->getIndexCount());
+               for (u16 i = 0; i < count; i += 3) {
+
+                       core::plane3df plane(buffer->getPosition(buffer->getIndices()[i]),
+                                       buffer->getPosition(buffer->getIndices()[i+1]),
+                                       buffer->getPosition(buffer->getIndices()[i+2]));
+
+                       for (u16 j = 0; j < 3; j++)
+                               if (plane.Normal.dotProduct(buffer->getNormal(buffer->getIndices()[j])) < 0)
+                                       return false;
+               }
+
        }
 
        return true;