|
|
@@ -145,6 +145,7 @@ void MeshLoader::doPostLoad()
|
|
|
}
|
|
|
|
|
|
createAllNormals();
|
|
|
+ fixNormals();
|
|
|
if(texCoords.size() > 0)
|
|
|
{
|
|
|
createVertTangents();
|
|
|
@@ -266,4 +267,39 @@ void MeshLoader::createVertTangents()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//==============================================================================
|
|
|
+void MeshLoader::fixNormals()
|
|
|
+{
|
|
|
+ const F32 positionsDistanceThresh = getEpsilon<F32>() * getEpsilon<F32>();
|
|
|
+ const F32 normalsDotThresh = cos(NORMALS_ANGLE_MERGE);
|
|
|
+
|
|
|
+ for(U i = 1; i < vertCoords.size(); i++)
|
|
|
+ {
|
|
|
+ const Vec3& crntPos = vertCoords[i];
|
|
|
+ Vec3& crntNormal = vertNormals[i];
|
|
|
+
|
|
|
+ // Check the previous
|
|
|
+ for(U j = 0; j < i; j++)
|
|
|
+ {
|
|
|
+ const Vec3& otherPos = vertCoords[j];
|
|
|
+ Vec3& otherNormal = vertNormals[j];
|
|
|
+
|
|
|
+ F32 distanceSq = crntPos.getDistanceSquared(otherPos);
|
|
|
+
|
|
|
+ if(distanceSq <= positionsDistanceThresh)
|
|
|
+ {
|
|
|
+ F32 dot = crntNormal.dot(otherNormal);
|
|
|
+
|
|
|
+ if(dot <= normalsDotThresh)
|
|
|
+ {
|
|
|
+ Vec3 newNormal = (crntNormal + otherNormal) * 0.5;
|
|
|
+ newNormal.normalize();
|
|
|
+
|
|
|
+ newNormal = otherNormal = newNormal;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
} // end namespace anki
|