Browse Source

Merge pull request #51921 from mortarroad/3.x-fix-convex-hull-winding

[3.x] Fix winding of new convex hull implementation.
Camille Mohr-Daurat 4 years ago
parent
commit
16642e2838
1 changed files with 10 additions and 1 deletions
  1. 10 1
      core/math/convex_hull.cpp

+ 10 - 1
core/math/convex_hull.cpp

@@ -2286,9 +2286,18 @@ Error ConvexHullComputer::convex_hull(const Vector3 *p_points, int32_t p_point_c
 			e = e->get_next_edge_of_face();
 			e = e->get_next_edge_of_face();
 		} while (e != e_start);
 		} while (e != e_start);
 
 
+		// reverse indices: Godot wants clockwise, but this is counter-clockwise
+		if (face.indices.size() > 2) {
+			// reverse all but the first index.
+			int *indices = face.indices.ptrw();
+			for (int c = 0; c < (face.indices.size() - 1) / 2; c++) {
+				SWAP(indices[c + 1], indices[face.indices.size() - 1 - c]);
+			}
+		}
+
 		// compute normal
 		// compute normal
 		if (face.indices.size() >= 3) {
 		if (face.indices.size() >= 3) {
-			face.plane = Plane(r_mesh.vertices[face.indices[0]], r_mesh.vertices[face.indices[2]], r_mesh.vertices[face.indices[1]]);
+			face.plane = Plane(r_mesh.vertices[face.indices[0]], r_mesh.vertices[face.indices[1]], r_mesh.vertices[face.indices[2]]);
 		} else {
 		} else {
 			WARN_PRINT("Too few vertices per face.");
 			WARN_PRINT("Too few vertices per face.");
 		}
 		}