10 |
|
#include "cvmesh.h" |
11 |
|
#include "otypes.h" |
12 |
|
#include "face.h" |
13 |
+ |
#include "tmesh.h" |
14 |
|
|
15 |
|
/* |
16 |
|
* We need to divide faces into triangles and record auxiliary information |
24 |
|
int fl; /* flags of what we're storing */ |
25 |
|
OBJECT obj; /* mesh triangle ID */ |
26 |
|
FVECT vn[3]; /* normals */ |
27 |
< |
FLOAT vc[3][2]; /* uv coords. */ |
27 |
> |
RREAL vc[3][2]; /* uv coords. */ |
28 |
|
} TRIDATA; |
29 |
|
|
30 |
|
#define tdsize(fl) ((fl)&MT_UV ? sizeof(TRIDATA) : \ |
31 |
< |
(fl)&MT_N ? sizeof(TRIDATA)-2*sizeof(FLOAT) : \ |
31 |
> |
(fl)&MT_N ? sizeof(TRIDATA)-6*sizeof(RREAL) : \ |
32 |
|
sizeof(int)+sizeof(OBJECT)) |
33 |
|
|
34 |
|
#define OMARGIN (10*FTINY) /* margin around global cube */ |
37 |
|
|
38 |
|
FVECT meshbounds[2]; /* mesh bounding box */ |
39 |
|
|
40 |
+ |
static void add2bounds(FVECT vp, RREAL vc[2]); |
41 |
+ |
static OBJECT cvmeshtri(OBJECT obj); |
42 |
+ |
static OCTREE cvmeshoct(OCTREE ot); |
43 |
|
|
44 |
+ |
|
45 |
+ |
|
46 |
|
MESH * |
47 |
< |
cvinit(nm) /* initialize empty mesh */ |
48 |
< |
char *nm; |
47 |
> |
cvinit( /* initialize empty mesh */ |
48 |
> |
char *nm |
49 |
> |
) |
50 |
|
{ |
51 |
|
/* free old mesh, first */ |
52 |
|
if (ourmesh != NULL) { |
76 |
|
|
77 |
|
|
78 |
|
int |
79 |
< |
cvpoly(mo, n, vp, vn, vc) /* convert a polygon to extended triangles */ |
80 |
< |
OBJECT mo; |
81 |
< |
int n; |
82 |
< |
FVECT *vp; |
83 |
< |
FVECT *vn; |
84 |
< |
FLOAT (*vc)[2]; |
79 |
> |
cvpoly( /* convert a polygon to extended triangles */ |
80 |
> |
OBJECT mo, |
81 |
> |
int n, |
82 |
> |
FVECT *vp, |
83 |
> |
FVECT *vn, |
84 |
> |
RREAL (*vc)[2] |
85 |
> |
) |
86 |
|
{ |
87 |
|
int tcnt = 0; |
88 |
|
int flags; |
89 |
< |
FLOAT *tn[3], *tc[3]; |
89 |
> |
RREAL *tn[3], *tc[3]; |
90 |
|
int *ord; |
91 |
|
int i, j; |
92 |
|
|
139 |
|
|
140 |
|
|
141 |
|
static void |
142 |
< |
add2bounds(vp, vc) /* add point and uv coordinate to bounds */ |
143 |
< |
FVECT vp; |
144 |
< |
FLOAT vc[2]; |
142 |
> |
add2bounds( /* add point and uv coordinate to bounds */ |
143 |
> |
FVECT vp, |
144 |
> |
RREAL vc[2] |
145 |
> |
) |
146 |
|
{ |
147 |
|
register int j; |
148 |
|
|
164 |
|
|
165 |
|
|
166 |
|
int /* create an extended triangle */ |
167 |
< |
cvtri(mo, vp1, vp2, vp3, vn1, vn2, vn3, vc1, vc2, vc3) |
168 |
< |
OBJECT mo; |
169 |
< |
FVECT vp1, vp2, vp3; |
170 |
< |
FVECT vn1, vn2, vn3; |
171 |
< |
FLOAT vc1[2], vc2[2], vc3[2]; |
167 |
> |
cvtri( |
168 |
> |
OBJECT mo, |
169 |
> |
FVECT vp1, |
170 |
> |
FVECT vp2, |
171 |
> |
FVECT vp3, |
172 |
> |
FVECT vn1, |
173 |
> |
FVECT vn2, |
174 |
> |
FVECT vn3, |
175 |
> |
RREAL vc1[2], |
176 |
> |
RREAL vc2[2], |
177 |
> |
RREAL vc3[2] |
178 |
> |
) |
179 |
|
{ |
180 |
|
static OBJECT fobj = OVOID; |
181 |
|
char buf[32]; |
185 |
|
OBJREC *fop; |
186 |
|
int j; |
187 |
|
|
188 |
< |
flags = MT_V; |
189 |
< |
if (vn1 != NULL && vn2 != NULL && vn3 != NULL) |
190 |
< |
flags |= MT_N; |
188 |
> |
flags = MT_V; /* check what we have */ |
189 |
> |
if (vn1 != NULL && vn2 != NULL && vn3 != NULL) { |
190 |
> |
RREAL *rp; |
191 |
> |
switch (flat_tri(vp1, vp2, vp3, vn1, vn2, vn3)) { |
192 |
> |
case ISBENT: |
193 |
> |
flags |= MT_N; |
194 |
> |
/* fall through */ |
195 |
> |
case ISFLAT: |
196 |
> |
break; |
197 |
> |
case RVBENT: |
198 |
> |
flags |= MT_N; |
199 |
> |
rp = vn1; vn1 = vn3; vn3 = rp; |
200 |
> |
/* fall through */ |
201 |
> |
case RVFLAT: |
202 |
> |
rp = vp1; vp1 = vp3; vp3 = rp; |
203 |
> |
rp = vc1; vc1 = vc3; vc3 = rp; |
204 |
> |
break; |
205 |
> |
case DEGEN: |
206 |
> |
error(WARNING, "degenerate triangle"); |
207 |
> |
return(0); |
208 |
> |
default: |
209 |
> |
error(INTERNAL, "bad return from flat_tri()"); |
210 |
> |
} |
211 |
> |
} |
212 |
|
if (vc1 != NULL && vc2 != NULL && vc3 != NULL) |
213 |
|
flags |= MT_UV; |
214 |
|
if (fobj == OVOID) { /* create new triangle object */ |
218 |
|
fop = objptr(fobj); |
219 |
|
fop->omod = mo; |
220 |
|
fop->otype = OBJ_FACE; |
221 |
< |
sprintf(buf, "t%d", fobj); |
221 |
> |
sprintf(buf, "t%ld", fobj); |
222 |
|
fop->oname = savqstr(buf); |
223 |
|
fop->oargs.nfargs = 9; |
224 |
< |
fop->oargs.farg = (FLOAT *)malloc(9*sizeof(FLOAT)); |
224 |
> |
fop->oargs.farg = (RREAL *)malloc(9*sizeof(RREAL)); |
225 |
|
if (fop->oargs.farg == NULL) |
226 |
|
goto nomem; |
227 |
|
} else { /* else reuse failed one */ |
277 |
|
|
278 |
|
|
279 |
|
static OBJECT |
280 |
< |
cvmeshtri(obj) /* add an extended triangle to our mesh */ |
281 |
< |
OBJECT obj; |
280 |
> |
cvmeshtri( /* add an extended triangle to our mesh */ |
281 |
> |
OBJECT obj |
282 |
> |
) |
283 |
|
{ |
284 |
|
OBJREC *o = objptr(obj); |
285 |
|
TRIDATA *ts; |
315 |
|
|
316 |
|
|
317 |
|
void |
318 |
< |
cvmeshbounds() /* set mesh boundaries */ |
318 |
> |
cvmeshbounds(void) /* set mesh boundaries */ |
319 |
|
{ |
320 |
|
int i; |
321 |
|
|
323 |
|
return; |
324 |
|
/* fix coordinate bounds */ |
325 |
|
for (i = 0; i < 3; i++) { |
326 |
< |
if (meshbounds[0][i] >= meshbounds[1][i]) |
326 |
> |
if (meshbounds[0][i] > meshbounds[1][i]) |
327 |
|
error(USER, "no polygons in mesh"); |
328 |
|
meshbounds[0][i] -= OMARGIN; |
329 |
|
meshbounds[1][i] += OMARGIN; |
339 |
|
ourmesh->uvlim[1][0] = ourmesh->uvlim[1][1] = 0.; |
340 |
|
} else { |
341 |
|
for (i = 0; i < 2; i++) { |
342 |
< |
double marg; |
343 |
< |
marg = 1e-6*(ourmesh->uvlim[1][i] - |
344 |
< |
ourmesh->uvlim[0][i]); |
342 |
> |
double marg; /* expand past endpoints */ |
343 |
> |
marg = (2./(1L<<(8*sizeof(uint16)))) * |
344 |
> |
(ourmesh->uvlim[1][i] - |
345 |
> |
ourmesh->uvlim[0][i]) + FTINY; |
346 |
|
ourmesh->uvlim[0][i] -= marg; |
347 |
|
ourmesh->uvlim[1][i] += marg; |
348 |
|
} |
352 |
|
|
353 |
|
|
354 |
|
static OCTREE |
355 |
< |
cvmeshoct(ot) /* convert triangles in subtree */ |
356 |
< |
OCTREE ot; |
355 |
> |
cvmeshoct( /* convert triangles in subtree */ |
356 |
> |
OCTREE ot |
357 |
> |
) |
358 |
|
{ |
359 |
|
int i; |
360 |
|
|
378 |
|
|
379 |
|
|
380 |
|
MESH * |
381 |
< |
cvmesh() /* convert mesh and octree leaf nodes */ |
381 |
> |
cvmesh(void) /* convert mesh and octree leaf nodes */ |
382 |
|
{ |
383 |
|
if (ourmesh == NULL) |
384 |
|
return(NULL); |