-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesselate.c
330 lines (263 loc) · 9.5 KB
/
tesselate.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifdef __cplusplus
#error "Must be compiled with a C compiler"
#endif
#include "tesselate.h"
#include <malloc.h>
#include <raymath.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "defines.h"
typedef struct {
Vector3 position;
int index;
int isEar;
} bdVertexNode;
static float SignedArea3D(Vector3 a, Vector3 b, Vector3 c, Vector3 normal) {
Vector3 ab = Vector3Subtract(b, a);
Vector3 ac = Vector3Subtract(c, a);
Vector3 cross = Vector3CrossProduct(ab, ac);
return Vector3DotProduct(cross, normal);
}
static int IsPointInTriangle3D(Vector3 p, Vector3 a, Vector3 b, Vector3 c, Vector3 normal) {
float area = SignedArea3D(a, b, c, normal);
float s1 = SignedArea3D(p, a, b, normal);
float s2 = SignedArea3D(p, b, c, normal);
float s3 = SignedArea3D(p, c, a, normal);
if (ABS(area) < EPS) return 0;
float r1 = s1 / area;
float r2 = s2 / area;
float r3 = s3 / area;
return (r1 >= -EPS && r2 >= -EPS && r3 >= -EPS);
}
static int IsEar(bdVertexNode* vertices, int count, int i, Vector3 normal) {
if (count < 4) return 1;
int prev = (i - 1 + count) % count;
int next = (i + 1) % count;
Vector3 a = vertices[prev].position;
Vector3 b = vertices[i].position;
Vector3 c = vertices[next].position;
if (SignedArea3D(a, b, c, normal) <= 0) return 0;
for (int j = 0; j < count; j++) {
if (j == prev || j == i || j == next) continue;
if (IsPointInTriangle3D(vertices[j].position, a, b, c, normal)) return 0;
}
return 1;
}
static void UpdateVertexType(bdVertexNode* vertices, int count, int i, Vector3 normal) {
vertices[i].isEar = IsEar(vertices, count, i, normal);
}
static void ExtractLoopPoints(const bdLoop* loop, Vector3** points, int* point_count) {
NULL_CHECK(loop);
NULL_CHECK(points);
NULL_CHECK(point_count);
bdHalfEdge* start = loop->ledg;
bdHalfEdge* current = start;
int count = 0;
do {
count++;
current = current->nxt;
} while (current != start);
*points = malloc(sizeof(Vector3) * count);
*point_count = count;
current = start;
int i = 0;
do {
(*points)[i].x = current->vtx->vcoord[0];
(*points)[i].y = current->vtx->vcoord[1];
(*points)[i].z = current->vtx->vcoord[2];
i++;
current = current->nxt;
} while (current != start);
}
static Vector3 CalculateNormal(Vector3 v1, Vector3 v2, Vector3 v3) {
Vector3 edge1 = Vector3Subtract(v2, v1);
Vector3 edge2 = Vector3Subtract(v3, v1);
return Vector3Normalize(Vector3CrossProduct(edge1, edge2));
}
static int AddVertex(bdMesh* mesh, Vector3 position) {
if (mesh->vertex_count >= mesh->vertex_capacity) {
mesh->vertex_capacity *= 2;
mesh->vertices = realloc(mesh->vertices, sizeof(bdMeshVertex) * mesh->vertex_capacity);
}
int idx = mesh->vertex_count++;
mesh->vertices[idx].position = position;
return idx;
}
static void AddTriangle(bdMesh* mesh, Vector3 v1, Vector3 v2, Vector3 v3, Vector3 normal) {
if (mesh->triangle_count >= mesh->triangle_capacity) {
mesh->triangle_capacity *= 2;
mesh->triangles = realloc(mesh->triangles, sizeof(bdTriangle) * mesh->triangle_capacity);
}
int idx = mesh->triangle_count++;
mesh->triangles[idx].v1 = AddVertex(mesh, v1);
mesh->triangles[idx].v2 = AddVertex(mesh, v2);
mesh->triangles[idx].v3 = AddVertex(mesh, v3);
mesh->triangles[idx].face_normal = normal;
}
static void TriangulatePolygon(bdMesh* mesh, Vector3* points, int point_count, Vector3 normal) {
if (point_count < 3) return;
if (point_count == 3) {
AddTriangle(mesh, points[0], points[1], points[2], normal);
return;
}
bdVertexNode* vertices = malloc(sizeof(bdVertexNode) * point_count);
int remaining = point_count;
for (int i = 0; i < point_count; i++) {
vertices[i].position = points[i];
vertices[i].index = i;
}
for (int i = 0; i < point_count; i++) {
UpdateVertexType(vertices, point_count, i, normal);
}
while (remaining > 3) {
int earFound = 0;
for (int i = 0; i < point_count; i++) {
if (vertices[i].index == -1 || !vertices[i].isEar) continue;
int prev = i;
int curr = i;
while (vertices[prev = ((prev - 1 + point_count) % point_count)].index == -1);
while (vertices[curr = ((curr + 1) % point_count)].index == -1);
AddTriangle(mesh, vertices[prev].position, vertices[i].position,
vertices[curr].position, normal);
vertices[i].index = -1;
remaining--;
earFound = 1;
if (remaining > 3) {
UpdateVertexType(vertices, point_count, prev, normal);
UpdateVertexType(vertices, point_count, curr, normal);
}
break;
}
if (!earFound) break;
}
if (remaining == 3) {
int first = -1;
for (int i = 0; i < point_count; i++) {
if (vertices[i].index != -1) {
if (first == -1) {
first = i;
} else if (vertices[(i + 1) % point_count].index != -1) {
AddTriangle(mesh, vertices[first].position, vertices[i].position,
vertices[(i + 1) % point_count].position, normal);
break;
}
}
}
}
free(vertices);
}
static void TessellateFace(bdMesh* mesh, const bdFace* face, const bdTessellationParams* params) {
NULL_CHECK(mesh);
NULL_CHECK(face);
bdLoop* l = face->floops;
while (l) {
Vector3* points = NULL;
int point_count = 0;
ExtractLoopPoints(l, &points, &point_count);
if (point_count >= 3) {
Vector3 normal = CalculateNormal(points[0], points[1], points[2]);
TriangulatePolygon(mesh, points, point_count, normal);
}
free(points);
l = l->nextl;
}
}
bdMesh* TessellateSolid(const bdSolid* solid, const bdTessellationParams* params) {
NULL_CHECK_RET(solid, NULL);
NULL_CHECK_RET(params, NULL);
bdMesh* mesh = malloc(sizeof(bdMesh));
mesh->vertex_capacity = 1024;
mesh->triangle_capacity = 1024;
mesh->vertices = malloc(sizeof(bdMeshVertex) * mesh->vertex_capacity);
mesh->triangles = malloc(sizeof(bdTriangle) * mesh->triangle_capacity);
mesh->vertex_count = 0;
mesh->triangle_count = 0;
bdFace* f = solid->sfaces;
while (f) {
TessellateFace(mesh, f, params);
f = f->nextf;
}
return mesh;
}
Mesh ConvertToRaylibMesh(const bdMesh* mesh) {
NULL_CHECK_RET(mesh, (Mesh){0});
Mesh raylibMesh = {0};
raylibMesh.vertexCount = mesh->vertex_count * 3;
raylibMesh.triangleCount = mesh->triangle_count;
raylibMesh.vertices = RL_MALLOC(raylibMesh.vertexCount * 3 * sizeof(float));
raylibMesh.normals = RL_MALLOC(raylibMesh.vertexCount * 3 * sizeof(float));
raylibMesh.texcoords = RL_MALLOC(raylibMesh.vertexCount * 2 * sizeof(float));
raylibMesh.indices = RL_MALLOC(mesh->triangle_count * 3 * sizeof(unsigned short));
for (int i = 0; i < mesh->triangle_count; i++) {
int base = i * 9;
int texbase = i * 6;
Vector3 normal = {
mesh->triangles[i].face_normal.x,
mesh->triangles[i].face_normal.y,
mesh->triangles[i].face_normal.z
};
raylibMesh.vertices[base] = mesh->vertices[mesh->triangles[i].v1].position.x;
raylibMesh.vertices[base + 1] = mesh->vertices[mesh->triangles[i].v1].position.y;
raylibMesh.vertices[base + 2] = mesh->vertices[mesh->triangles[i].v1].position.z;
raylibMesh.normals[base] = normal.x;
raylibMesh.normals[base + 1] = normal.y;
raylibMesh.normals[base + 2] = normal.z;
raylibMesh.texcoords[texbase] = 1.0f;
raylibMesh.texcoords[texbase + 1] = 0.0f;
raylibMesh.vertices[base + 3] = mesh->vertices[mesh->triangles[i].v2].position.x;
raylibMesh.vertices[base + 4] = mesh->vertices[mesh->triangles[i].v2].position.y;
raylibMesh.vertices[base + 5] = mesh->vertices[mesh->triangles[i].v2].position.z;
raylibMesh.normals[base + 3] = normal.x;
raylibMesh.normals[base + 4] = normal.y;
raylibMesh.normals[base + 5] = normal.z;
raylibMesh.texcoords[texbase + 2] = 0.0f;
raylibMesh.texcoords[texbase + 3] = 1.0f;
raylibMesh.vertices[base + 6] = mesh->vertices[mesh->triangles[i].v3].position.x;
raylibMesh.vertices[base + 7] = mesh->vertices[mesh->triangles[i].v3].position.y;
raylibMesh.vertices[base + 8] = mesh->vertices[mesh->triangles[i].v3].position.z;
raylibMesh.normals[base + 6] = normal.x;
raylibMesh.normals[base + 7] = normal.y;
raylibMesh.normals[base + 8] = normal.z;
raylibMesh.texcoords[texbase + 4] = 0.0f;
raylibMesh.texcoords[texbase + 5] = 0.0f;
raylibMesh.indices[i * 3] = i * 3;
raylibMesh.indices[i * 3 + 1] = i * 3 + 1;
raylibMesh.indices[i * 3 + 2] = i * 3 + 2;
}
return raylibMesh;
}
void FreeMesh(bdMesh* mesh) {
if (mesh) {
free(mesh->vertices);
free(mesh->triangles);
free(mesh);
}
}
int ExportMeshToStl(const bdMesh* mesh, const char* filepath) {
NULL_CHECK_RET(mesh, ERROR)
NULL_CHECK_RET(filepath, ERROR)
FILE* file = fopen(filepath, "wb");
NULL_CHECK_RET(file, ERROR);
char header[80] = {0};
uint32_t triangle_count = mesh->triangle_count;
fwrite(header, sizeof(header), 1, file);
fwrite(&triangle_count, sizeof(uint32_t), 1, file);
for (int i = 0; i < mesh->triangle_count; i++) {
Vector3 v1 = mesh->vertices[mesh->triangles[i].v1].position;
Vector3 v2 = mesh->vertices[mesh->triangles[i].v2].position;
Vector3 v3 = mesh->vertices[mesh->triangles[i].v3].position;
Vector3 normal = CalculateNormal(v1, v2, v3);
float buffer[12] = {
normal.x, normal.y, normal.z,
v1.x, v1.y, v1.z,
v2.x, v2.y, v2.z,
v3.x, v3.y, v3.z
};
uint16_t attribute_byte_count = 0;
fwrite(buffer, sizeof(float), 12, file);
fwrite(&attribute_byte_count, sizeof(uint16_t), 1, file);
}
fclose(file);
return SUCCESS;
}