-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.cpp
More file actions
234 lines (188 loc) · 5.84 KB
/
patch.cpp
File metadata and controls
234 lines (188 loc) · 5.84 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//############################################################################
//## ##
//## PATCH.CPP ##
//## ##
//## Converts a Quake3 Bezier patch into a hard coded polygon mesh. ##
//## ##
//## OpenSourced 12/5/2000 by John W. Ratcliff ##
//## ##
//## No warranty expressed or implied. ##
//## ##
//## Part of the Q3BSP project, which converts a Quake 3 BSP file into a ##
//## polygon mesh. ##
//############################################################################
//## ##
//## Contact John W. Ratcliff at jratcliff@verant.com ##
//############################################################################
#include "patch.h"
#define LEVEL_WIDTH(lvl) ((1 << (lvl+1)) + 1)
#define MAXMESHLEVEL 5
#define MINDIST (0.4f*0.4f)
PatchSurface::PatchSurface(const LightMapVertex *cp,
int npoints,
int controlx,
int controly)
{
int sizex,sizey;
FindSize(controlx,controly,cp,sizex,sizey);
int size = sizex*sizey;
mPoints = new LightMapVertex[size];
int stepx = (sizex-1) / (controlx-1);
int stepy = (sizey-1) / (controly-1);
const LightMapVertex *control = cp;
for (int y=0; y<sizey; y+=stepy)
{
for (int x=0; x<sizex; x+=stepx)
{
int p = y*sizex+x;
mPoints[p] = *control++;
}
}
FillPatch(controlx,controly,sizex,sizey,mPoints);
mCount = (sizex-1)*(sizey-1)*6;
if ( 1 )
{
mIndices = new unsigned short[mCount];
unsigned short *foo = mIndices;
for (int y=0; y < sizey-1; ++y)
{
for (int x = 0; x < sizex-1; ++x)
{
*foo++ = y*sizex+x;
*foo++ = (y+1)*sizex+x;
*foo++ = y*sizex+x+1;
*foo++ = y*sizex+x+1;
*foo++ = (y+1)*sizex+x;
*foo++ = (y+1)*sizex+x+1;
}
}
}
}
PatchSurface::~PatchSurface(void)
{
delete mIndices;
delete mPoints;
}
bool PatchSurface::FindSize(int controlx,int controly,const LightMapVertex *cp,int &sizex,int &sizey) const
{
/* Find non-coincident pairs in u direction */
bool found=false;
const LightMapVertex *a=0;
const LightMapVertex *b=0;
for (int v=0; v <controly; v++)
{
for (int u=0; u < controlx; u+=2)
{
a = &cp[v * controlx + u];
b = &cp[v * controlx + u + 2];
if ( a->mPos.x != b->mPos.x ||
a->mPos.y != b->mPos.y ||
a->mPos.z != b->mPos.z )
{
found = true;
break;
}
}
if (found) break;
}
if (!found)
{
printf("Bad mesh control points\n");
return false;
}
/* Find subdivision level in u */
int levelx = FindLevel(a[0].mPos,a[1].mPos,b[0].mPos);
sizex = (LEVEL_WIDTH(levelx) - 1) * ((controlx-1) / 2) + 1;
for (int u=0; u <controlx; u++)
{
for (int v=0; v < controly; v+=2)
{
a = &cp[v * controlx + u];
b = &cp[ ((v+2) * controlx) + u ];
if ( a->mPos.x != b->mPos.x ||
a->mPos.y != b->mPos.y ||
a->mPos.z != b->mPos.z )
{
found = true;
break;
}
}
if (found) break;
}
if (!found)
{
printf("Bad mesh control points\n");
return false;
}
/* Find subdivision level in u */
int levely = FindLevel(a[0].mPos,a[1].mPos,b[0].mPos);
sizey = (LEVEL_WIDTH(levely) - 1) * ((controly-1) / 2) + 1;
return true;
}
int PatchSurface::FindLevel(const Vector3d<float> &cv0,
const Vector3d<float> &cv1,
const Vector3d<float> &cv2) const
{
int level;
Vector3d<float> a,b,dist;
Vector3d<float> v0 = cv0; // init control points.
Vector3d<float> v1 = cv1;
Vector3d<float> v2 = cv2;
/* Subdivide on the left until tolerance is reached */
for (level=0; level <MAXMESHLEVEL-1; level++)
{
/* Subdivide on the left */
a.Lerp(v0,v1,0.5f);
b.Lerp(v1,v2,0.5f);
v2.Lerp(a,b,0.5f);
/* Find distance moved */
dist = v2-v1;
float dist2 = dist.x*dist.x + dist.y*dist.y + dist.z*dist.z;
if ( dist2 < MINDIST ) break;
/* Insert new middle vertex */
v1 = a;
}
return level;
}
void PatchSurface::FillPatch(int controlx,int controly,int sizex,int sizey,LightMapVertex *p)
{
int stepx = (sizex-1) / (controlx-1);
for (int u = 0; u < sizex; u += stepx)
{
FillCurve(controly, sizey,sizex,p+u);
}
for (int v = 0; v < sizey; v++)
{
FillCurve(controlx, sizex, 1, p + v * sizex);
}
}
void PatchSurface::FillCurve(int numcp, int size, int stride,LightMapVertex *p)
{
int step, halfstep, i, mid;
LightMapVertex a,b;
step = (size-1) / (numcp-1);
while (step > 0)
{
halfstep = step / 2;
for (i=0; i < size-1; i += step*2)
{
mid = (i+step)*stride;
a.Lerp(p[i*stride],p[mid],0.5f);
b.Lerp(p[mid],p[(i+step*2)*stride],0.5f);
p[mid].Lerp(a,b,0.5f);
// vec_avg(p[i*stride], p[mid], a);
// vec_avg(p[mid], p[(i+step*2)*stride], b);
// vec_avg(a, b, p[mid]);
if (halfstep > 0)
{
p[(i+halfstep)*stride] = a;
p[(i+3*halfstep)*stride] = b;
}
}
step /= 2;
}
}