summaryrefslogtreecommitdiff
path: root/thirdparty/meshoptimizer/simplifier.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'thirdparty/meshoptimizer/simplifier.cpp')
-rw-r--r--thirdparty/meshoptimizer/simplifier.cpp45
1 files changed, 39 insertions, 6 deletions
diff --git a/thirdparty/meshoptimizer/simplifier.cpp b/thirdparty/meshoptimizer/simplifier.cpp
index bd523275ce..b195a8cb5d 100644
--- a/thirdparty/meshoptimizer/simplifier.cpp
+++ b/thirdparty/meshoptimizer/simplifier.cpp
@@ -6,6 +6,7 @@
#include <math.h>
#include <string.h>
+
#ifndef TRACE
#define TRACE 0
#endif
@@ -332,8 +333,11 @@ struct Vector3
{
float x, y, z;
};
+// -- GODOT start --
+//static void rescalePositions(Vector3* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride)
+static float rescalePositions(Vector3* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride)
+// -- GODOT end --
-static void rescalePositions(Vector3* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride)
{
size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
@@ -371,6 +375,10 @@ static void rescalePositions(Vector3* result, const float* vertex_positions_data
result[i].y = (result[i].y - minv[1]) * scale;
result[i].z = (result[i].z - minv[2]) * scale;
}
+// -- GODOT start --
+ return extent;
+// -- GODOT end --
+
}
struct Quadric
@@ -1143,7 +1151,10 @@ unsigned int* meshopt_simplifyDebugLoop = 0;
unsigned int* meshopt_simplifyDebugLoopBack = 0;
#endif
-size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error)
+// -- GODOT start --
+//size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error)
+size_t meshopt_simplify(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float *r_resulting_error)
+// -- GODOT end --
{
using namespace meshopt;
@@ -1187,7 +1198,10 @@ size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices,
#endif
Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
- rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
+// -- GODOT start --
+ //rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
+ float extent = rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
+// -- GODOT end --
Quadric* vertex_quadrics = allocator.allocate<Quadric>(vertex_count);
memset(vertex_quadrics, 0, vertex_count * sizeof(Quadric));
@@ -1198,10 +1212,13 @@ size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices,
if (result != indices)
memcpy(result, indices, index_count * sizeof(unsigned int));
+// -- GODOT start --
#if TRACE
size_t pass_count = 0;
- float worst_error = 0;
+ //float worst_error = 0;
#endif
+ float worst_error = 0;
+// -- GODOT end --
Collapse* edge_collapses = allocator.allocate<Collapse>(index_count);
unsigned int* collapse_order = allocator.allocate<unsigned int>(index_count);
@@ -1213,6 +1230,12 @@ size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices,
// target_error input is linear; we need to adjust it to match quadricError units
float error_limit = target_error * target_error;
+// -- GODOT start --
+ if (r_resulting_error) {
+ *r_resulting_error = 1.0;
+ }
+// -- GODOT end --
+
while (result_count > target_index_count)
{
size_t edge_collapse_count = pickEdgeCollapses(edge_collapses, result, result_count, remap, vertex_kind, loop);
@@ -1257,7 +1280,8 @@ size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices,
size_t new_count = remapIndexBuffer(result, result_count, collapse_remap);
assert(new_count < result_count);
-#if TRACE
+// -- GODOT start --
+//#if TRACE
float pass_error = 0.f;
for (size_t i = 0; i < edge_collapse_count; ++i)
{
@@ -1267,15 +1291,24 @@ size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices,
pass_error = c.error;
}
- pass_count++;
+ //pass_count++;
worst_error = (worst_error < pass_error) ? pass_error : worst_error;
+#if TRACE
+ pass_count++;
printf("pass %d: triangles: %d -> %d, collapses: %d/%d (goal: %d), error: %e (limit %e goal %e)\n", int(pass_count), int(result_count / 3), int(new_count / 3), int(collapses), int(edge_collapse_count), int(edge_collapse_goal), pass_error, error_limit, error_goal);
#endif
+// -- GODOT end --
result_count = new_count;
}
+// -- GODOT start --
+ if (r_resulting_error) {
+ *r_resulting_error = sqrt(worst_error) * extent;
+ }
+// -- GODOT end --
+
#if TRACE
printf("passes: %d, worst error: %e\n", int(pass_count), worst_error);
#endif