@@ -299,7 +299,7 @@ namespace spartan::geometry_processing
299
299
indices->push_back (baseIndex + i);
300
300
}
301
301
302
- // Build bottom cap
302
+ // build bottom cap
303
303
baseIndex = (int )vertices->size ();
304
304
y = -0 .5f * height;
305
305
@@ -310,13 +310,13 @@ namespace spartan::geometry_processing
310
310
const float u = x / height + 0 .5f ;
311
311
const float v = z / height + 0 .5f ;
312
312
313
- normal = Vector3 (0 , -1 , 0 );
314
- tangent = Vector3 (1 , 0 , 0 );
313
+ normal = Vector3 (0 , -1 , 0 );
314
+ tangent = Vector3 (1 , 0 , 0 );
315
315
vertices->emplace_back (Vector3 (x, y, z), Vector2 (u, v), normal , tangent);
316
316
}
317
317
318
- normal = Vector3 (0 , -1 , 0 );
319
- tangent = Vector3 (1 , 0 , 0 );
318
+ normal = Vector3 (0 , -1 , 0 );
319
+ tangent = Vector3 (1 , 0 , 0 );
320
320
vertices->emplace_back (Vector3 (0 , y, 0 ), Vector2 (0 .5f , 0 .5f ), normal , tangent);
321
321
322
322
centerIndex = (int )vertices->size () - 1 ;
@@ -339,9 +339,13 @@ namespace spartan::geometry_processing
339
339
float error = 0 .1f ;
340
340
size_t index_count = indices.size ();
341
341
size_t current_triangle_count = indices.size () / 3 ;
342
-
342
+
343
+ if (current_triangle_count >= triangle_target)
344
+ return ;
345
+
343
346
// loop until the current triangle count is less than or equal to the target triangle count
344
347
std::vector<uint32_t > indices_simplified (index_count);
348
+ uint32_t iteration_count = 0 ;
345
349
while (current_triangle_count > triangle_target)
346
350
{
347
351
float threshold = 1 .0f - reduction;
@@ -359,9 +363,14 @@ namespace spartan::geometry_processing
359
363
);
360
364
361
365
indices.assign (indices_simplified.begin (), indices_simplified.begin () + index_count);
362
- current_triangle_count = index_count / 3 ;
363
- reduction += 0 .1f ;
364
- error += 0 .1f ;
366
+ current_triangle_count = index_count / 3 ;
367
+ reduction = fmodf (reduction + 0 .1f , 1 .0f );
368
+ error = fmodf (error + 0 .1f , 1 .0f );
369
+
370
+ // break if meshopt_simplify gives up
371
+ iteration_count++;
372
+ if (iteration_count > 10 )
373
+ break ;
365
374
}
366
375
}
367
376
@@ -392,7 +401,7 @@ namespace spartan::geometry_processing
392
401
393
402
// optimization #4: create a simplified version of the model
394
403
{
395
- auto get_vertex_target = [](size_t vertex_count )
404
+ auto get_triangle_target = [](size_t triangle_count )
396
405
{
397
406
std::tuple<float , size_t > agressivness_table[] =
398
407
{
@@ -402,17 +411,17 @@ namespace spartan::geometry_processing
402
411
{ 0 .9f , 2000 } // gentle
403
412
};
404
413
405
- for (const auto & [reduction_percentage, vertex_threshold ] : agressivness_table)
414
+ for (const auto & [reduction_percentage, triangle_threshold ] : agressivness_table)
406
415
{
407
- if (vertex_count > vertex_threshold )
416
+ if (triangle_count > triangle_threshold )
408
417
{
409
- return static_cast <size_t >(vertex_count * reduction_percentage);
418
+ return static_cast <size_t >(triangle_count * reduction_percentage);
410
419
}
411
420
}
412
- return vertex_count ; // native
421
+ return triangle_count ; // native
413
422
};
414
423
415
- simplify (indices, vertices, get_vertex_target (vertex_count ));
424
+ simplify (indices, vertices, get_triangle_target (indices. size () / 3 ));
416
425
}
417
426
}
418
427
}
0 commit comments