-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatch.c
291 lines (234 loc) · 7.79 KB
/
patch.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
// patch.c - subdivision mesh algorithm (based on old-school 'ROAM' algorithm)
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "math.h"
#include "patch.h"
// a comment for testing github commit stuff
#define MAX_PATCHNODES (1 << 24) // maximum binary-tree triangles
// I hate this hacky shizzles
struct tri;
//
typedef struct tri
{
struct tri *child[2], *neigh[3]; // children and neighbor triangles
} tri_t;
//
// binary tree data
tri_t triangles[MAX_PATCHNODES];
unsigned int num_triangles = 0;
// meshified patch
pface_t patch_faces[MAX_PATCHFACES];
unsigned int num_patchfaces = 0;
//
tri_t *new_tri()
{
tri_t *t;
if(num_triangles == MAX_PATCHNODES)
return 0;
t = &triangles[num_triangles++];
t->child[0] = 0;
t->child[1] = 0;
t->neigh[0] = 0;
t->neigh[1] = 0;
t->neigh[2] = 0;
return t;
}
//
// update neighbor relationships between a triangle and its neighbors,
// along with maintaining the neighbor's of their children
int patch_settriangleneighbors(tri_t *t)
{
t->child[0]->neigh[0] = t->child[1];
t->child[0]->neigh[2] = t->neigh[0];
t->child[1]->neigh[1] = t->child[0];
t->child[1]->neigh[2] = t->neigh[1];
if(t->neigh[0]) // if any of left-neighbor's neighbors are self, set to our left child
{
if(t->neigh[0]->neigh[2] == t)
t->neigh[0]->neigh[2] = t->child[0];
else if(t->neigh[0]->neigh[0] == t)
t->neigh[0]->neigh[0] = t->child[0];
else if(t->neigh[0]->neigh[1] == t)
t->neigh[0]->neigh[1] = t->child[0];
}
if(t->neigh[1]) // if any of right-neighbor's neighbors are self, set to our right child
{
if(t->neigh[1]->neigh[2] == t)
t->neigh[1]->neigh[2] = t->child[1];
else if(t->neigh[1]->neigh[1] == t)
t->neigh[1]->neigh[1] = t->child[1];
else if(t->neigh[1]->neigh[0] == t)
t->neigh[1]->neigh[0] = t->child[1];
}
if(t->neigh[2]) // base neighbor
{
if(t->neigh[2]->child[0]) // set neighbors for baseneighbor's children
{
t->neigh[2]->child[0]->neigh[1] = t->child[1];
t->neigh[2]->child[1]->neigh[0] = t->child[0];
t->child[0]->neigh[1] = t->neigh[2]->child[1];
t->child[1]->neigh[0] = t->neigh[2]->child[0];
}
else // baseneighbor has no children
return 1;
}
else
{
t->child[0]->neigh[1] = 0;
t->child[1]->neigh[0] = 0;
}
return 0;
}
//
// divide a triangle at its hypoteneuse along with its baseneighbor to prevent cracks
void patch_splittriangle(tri_t *t)
{
if(t->child[0])
return;
if(t->neigh[2])
if(t->neigh[2]->neigh[2] != t) // if baseneighbor's baseneighbor isn't us, we need to split them first
patch_splittriangle(t->neigh[2]);
// split triangle by creating child triangles
if(!(t->child[0] = new_tri()) || !(t->child[1] = new_tri()))
{
print("patch_splittriangle: binary tree node overflow!");
return;
}
// update neighbor assignments
if(patch_settriangleneighbors(t))
patch_splittriangle(t->neigh[2]); // forcesplit our base neighbor to prevent cracks, if it's not already split
}
//
// quick macro for accessing heightmap via XY coordinate
#define hmapz(x, y) heights[((unsigned int)x + (unsigned int)y * width)]
//
float hmap(float x, float y, int width, int height, float *heights)
{
unsigned int lx, ly, hx, hy;
float a, b, c, d, fx, fy;
lx = (unsigned int)x;
ly = (unsigned int)y;
hx = lx + 1;
hy = ly + 1;
if(hx == width)
hx -= 1;
if(hy == height)
hy -= 1;
fx = x - lx;
fy = y - ly;
a = hmapz(lx, ly);
b = hmapz(hx, ly);
c = hmapz(lx, hy);
d = hmapz(hx, hy);
return FLERP(FLERP(a, b, fx), FLERP(c, d, fx), fy);
}
//
// recursively subdivide triangles until we are down to single pixels
// or until the triangle conforms to the heightmap within a tolerance
void patch_recursedivide(tri_t *t, float *heights, int width, int height, float thresh, vec3 left, vec3 apex, vec3 right, float level)
{
vec3 pt, napex, delta = (vec3){ fabs(left.x - right.x), fabs(left.y - right.y), 0 };
float z = 0;
// make sure we're not down smaller than heightmap grid
if(((delta.x && delta.x <= 1) || (delta.y && delta.y <= 1) || (!delta.x && !delta.y)))
return;
napex = vmix(left, right, 0.5);
pt = napex; // middle of base
z += fabs(pt.z - hmap(pt.x, pt.y, width, height, heights));
pt = vmix(apex, napex, 0.5); // center of triangle
z += fabs(pt.z - hmap(pt.x, pt.y, width, height, heights));
pt = vmix(right, left, 0.25); // 1/4 of base
z += fabs(pt.z - hmap(pt.x, pt.y, width, height, heights));
pt = vmix(right, left, 0.75); // 3/4 of base
z += fabs(pt.z - hmap(pt.x, pt.y, width, height, heights));
z *= 0.125;
// split if it has no children and deviates from heightmap enough
if(!t->child[0] && (level <= 3 || z >= thresh * level))
patch_splittriangle(t);
// recurse down through children, if we have any to deal with
if(t->child[0])
{
// get center of hypoteneuse
pt = vmix(left, right, 0.5);
// use heightmap's z as childrens' apex vertex z
pt.z = hmap(pt.x, pt.y, width, height, heights);
level = sqrt(level * level + 1);
patch_recursedivide(t->child[0], heights, width, height, thresh, apex, pt, left, level);
patch_recursedivide(t->child[1], heights, width, height, thresh, right, pt, apex, level);
}
}
//
// recursively subdivide triangles until we are down to single pixels
// or until the triangle conforms to the heightmap within a tolerance
void patch_recursemesh(tri_t *t, float *heights, int width, int height, vec3 left, vec3 apex, vec3 right)
{
pface_t pf;
vec3 pt;
// calculate hypoteneuse center point by averaging endpoints together
pt = vscale(vadd(left, right), 0.5);
// get actual heightmap value for this horizontal position
pt.z = hmap(pt.x, pt.y, width, height, heights);
// recurse subdivide
if(t->child[0])
{
patch_recursemesh(t->child[0], heights, width, height, apex, pt, left);
patch_recursemesh(t->child[1], heights, width, height, right, pt, apex);
}
else // leaf triangle, output its geometry
{
if(num_patchfaces == MAX_PATCHFACES)
{
print("patch_recursemesh: triangle face overflow!");
return;
}
pf.verts[0] = left;
pf.verts[1] = apex;
pf.verts[2] = right;
patch_faces[num_patchfaces++] = pf;
}
}
//
// generate a mesh that conforms to a floating-point heightmap
// using subdividing triangles similar to ROAM terrain algorithm
void patch_meshheightmap(float *heights, int width, int height, float hscale, float vscale, float thresh)
{
int x;
pface_t *p;
vec3 pts[] = // outer edge of heightmap, origin triangles
{
{ 0, 0, hmap(0, 0, width, height, heights) },
{ width - 1, 0, hmap(width - 1, 0, width, height, heights) },
{ width - 1, height - 1, hmap(width - 1, height - 1, width, height, heights) },
{ 0, height - 1, hmap(0, height - 1, width, height, heights) }
};
// initialize root triangles
num_triangles = 2;
triangles[0].neigh[2] = &triangles[1]; // set as eachothers' base neighbors
triangles[1].neigh[2] = &triangles[0];
// recursively subdivide to conform triangles to heightmap within a tolerance
patch_recursedivide(&triangles[0], heights, width, height, thresh, pts[0], pts[1], pts[2], 1.0f);
patch_recursedivide(&triangles[1], heights, width, height, thresh, pts[2], pts[3], pts[0], 1.0f);
print("...generated %d binary tree triangles", num_triangles);
// recursively subdivide to iterate through the triangle tree, outputting triangle faces
patch_recursemesh(&triangles[0], heights, width, height, pts[0], pts[1], pts[2]);
patch_recursemesh(&triangles[1], heights, width, height, pts[2], pts[3], pts[0]);
print("...generated %d mesh triangle faces", num_patchfaces);
// scale mesh vertices to user settings
for(x = 0; x < num_patchfaces; x++)
{
p = &patch_faces[x];
p->verts[0].x *= hscale;
p->verts[0].y *= hscale;
p->verts[0].z *= vscale;
p->verts[1].x *= hscale;
p->verts[1].y *= hscale;
p->verts[1].z *= vscale;
p->verts[2].x *= hscale;
p->verts[2].y *= hscale;
p->verts[2].z *= vscale;
}
print("...scaled triangles");
}
//