-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathng_mesh_simplify.h
93 lines (66 loc) · 2.51 KB
/
ng_mesh_simplify.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef HAS_SIMD_MESH_SIMPLIFY_H_BEEN_INCUDED
#define HAS_SIMD_MESH_SIMPLIFY_H_BEEN_INCUDED
//
// Release under unlicense, public domain
//
#include <stdlib.h>
#include <glm/glm.hpp>
// ----------------------------------------------------------------------------
// Change these definitions to suit your project
#if 0
using vec4 = float[4];
#else
using vec4 = glm::vec4;
#endif
// ----------------------------------------------------------------------------
// The algorithm with alloc chunks of memory
// override these to point to your engine's allocators, etc
#define ng_alloc malloc
#define ng_free free
// ----------------------------------------------------------------------------
struct MeshSimplificationOptions
{
// Each iteration involves selecting a fraction of the edges at random as possible
// candidates for collapsing. There is likely a sweet spot here trading off against number
// of edges processed vs number of invalid collapses generated due to collisions
// (the more edges that are processed the higher the chance of collisions happening)
float edgeFraction = 0.125f;
// Stop simplfying after a given number of iterations
int maxIterations = 10;
// And/or stop simplifying when we've reached a percentage of the input triangles
float targetPercentage = 0.05f;
// The maximum allowed error when collapsing an edge (error is calculated as 1.0/qef_error)
float maxError = 1.f;
// Useful for controlling how uniform the mesh is (or isn't)
float maxEdgeSize = 0.5f;
// If the mesh has sharp edges this can used to prevent collapses which would otherwise be used
float minAngleCosine = 0.8f;
};
// ----------------------------------------------------------------------------
struct MeshVertex
{
vec4 xyz, normal, colour;
};
// ----------------------------------------------------------------------------
struct MeshTriangle
{
int indices_[3];
};
// ----------------------------------------------------------------------------
class MeshBuffer
{
public:
static void initialiseVertexArray();
MeshVertex* vertices = nullptr;
int numVertices = 0;
MeshTriangle* triangles = nullptr;
int numTriangles = 0;
};
// ----------------------------------------------------------------------------
// The MeshBuffer instance will be edited in place
void ngMeshSimplifier(
MeshBuffer* mesh,
const vec4& worldSpaceOffset,
const MeshSimplificationOptions& options);
// ----------------------------------------------------------------------------
#endif // HAS_SIMD_MESH_SIMPLIFY_H_BEEN_INCUDED