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 */ |
70 |
|
|
71 |
|
|
72 |
|
int |
73 |
< |
cvpoly(n, vp, vn, vc) /* convert a polygon to extended triangles */ |
73 |
> |
cvpoly(mo, n, vp, vn, vc) /* convert a polygon to extended triangles */ |
74 |
> |
OBJECT mo; |
75 |
|
int n; |
76 |
|
FVECT *vp; |
77 |
|
FVECT *vn; |
78 |
< |
FLOAT (*vc)[2]; |
78 |
> |
RREAL (*vc)[2]; |
79 |
|
{ |
80 |
|
int tcnt = 0; |
81 |
|
int flags; |
82 |
< |
FLOAT *tn[3], *tc[3]; |
82 |
> |
RREAL *tn[3], *tc[3]; |
83 |
|
int *ord; |
84 |
|
int i, j; |
85 |
|
|
99 |
|
tc[0] = tc[1] = tc[2] = NULL; |
100 |
|
} |
101 |
|
if (n == 3) /* output single triangle */ |
102 |
< |
return(cvtri(vp[0], vp[1], vp[2], |
102 |
> |
return(cvtri(mo, vp[0], vp[1], vp[2], |
103 |
|
tn[0], tn[1], tn[2], |
104 |
|
tc[0], tc[1], tc[2])); |
105 |
|
|
116 |
|
if (flags & MT_UV) |
117 |
|
for (i = 3; i--; ) |
118 |
|
tc[i] = vc[ord[i]]; |
119 |
< |
i = cvtri(vp[ord[0]], vp[ord[1]], vp[ord[2]], |
119 |
> |
tcnt += cvtri(mo, vp[ord[0]], vp[ord[1]], vp[ord[2]], |
120 |
|
tn[0], tn[1], tn[2], |
121 |
|
tc[0], tc[1], tc[2]); |
120 |
– |
if (i < 0) |
121 |
– |
return(i); |
122 |
– |
tcnt += i; |
122 |
|
/* remove vertex and rotate */ |
123 |
|
n--; |
124 |
|
j = ord[0]; |
134 |
|
static void |
135 |
|
add2bounds(vp, vc) /* add point and uv coordinate to bounds */ |
136 |
|
FVECT vp; |
137 |
< |
FLOAT vc[2]; |
137 |
> |
RREAL vc[2]; |
138 |
|
{ |
139 |
|
register int j; |
140 |
|
|
156 |
|
|
157 |
|
|
158 |
|
int /* create an extended triangle */ |
159 |
< |
cvtri(vp1, vp2, vp3, vn1, vn2, vn3, vc1, vc2, vc3) |
159 |
> |
cvtri(mo, vp1, vp2, vp3, vn1, vn2, vn3, vc1, vc2, vc3) |
160 |
> |
OBJECT mo; |
161 |
|
FVECT vp1, vp2, vp3; |
162 |
|
FVECT vn1, vn2, vn3; |
163 |
< |
FLOAT vc1[2], vc2[2], vc3[2]; |
163 |
> |
RREAL vc1[2], vc2[2], vc3[2]; |
164 |
|
{ |
165 |
< |
char buf[32]; |
166 |
< |
int flags; |
167 |
< |
TRIDATA *ts; |
168 |
< |
OBJECT fobj; |
169 |
< |
FACE *f; |
170 |
< |
OBJREC *fop; |
171 |
< |
int j; |
165 |
> |
static OBJECT fobj = OVOID; |
166 |
> |
char buf[32]; |
167 |
> |
int flags; |
168 |
> |
TRIDATA *ts; |
169 |
> |
FACE *f; |
170 |
> |
OBJREC *fop; |
171 |
> |
int j; |
172 |
|
|
173 |
< |
flags = MT_V; |
174 |
< |
if (vn1 != NULL && vn2 != NULL && vn3 != NULL) |
175 |
< |
flags |= MT_N; |
173 |
> |
flags = MT_V; /* check what we have */ |
174 |
> |
if (vn1 != NULL && vn2 != NULL && vn3 != NULL) { |
175 |
> |
RREAL *rp; |
176 |
> |
switch (flat_tri(vp1, vp2, vp3, vn1, vn2, vn3)) { |
177 |
> |
case ISBENT: |
178 |
> |
flags |= MT_N; |
179 |
> |
/* fall through */ |
180 |
> |
case ISFLAT: |
181 |
> |
break; |
182 |
> |
case RVBENT: |
183 |
> |
flags |= MT_N; |
184 |
> |
rp = vn1; vn1 = vn3; vn3 = rp; |
185 |
> |
/* fall through */ |
186 |
> |
case RVFLAT: |
187 |
> |
rp = vp1; vp1 = vp3; vp3 = rp; |
188 |
> |
rp = vc1; vc1 = vc3; vc3 = rp; |
189 |
> |
break; |
190 |
> |
case DEGEN: |
191 |
> |
error(WARNING, "degenerate triangle"); |
192 |
> |
return(0); |
193 |
> |
default: |
194 |
> |
error(INTERNAL, "bad return from flat_tri()"); |
195 |
> |
} |
196 |
> |
} |
197 |
|
if (vc1 != NULL && vc2 != NULL && vc3 != NULL) |
198 |
|
flags |= MT_UV; |
199 |
< |
/* create extended triangle */ |
200 |
< |
fobj = newobject(); |
201 |
< |
if (fobj == OVOID) |
202 |
< |
return(-1); |
203 |
< |
fop = objptr(fobj); |
204 |
< |
fop->omod = OVOID; |
205 |
< |
fop->otype = OBJ_FACE; |
206 |
< |
sprintf(buf, "t%d", fobj); |
207 |
< |
fop->oname = savqstr(buf); |
208 |
< |
fop->oargs.nfargs = 9; |
209 |
< |
fop->oargs.farg = (FLOAT *)malloc(9*sizeof(FLOAT)); |
210 |
< |
if (fop->oargs.farg == NULL) |
211 |
< |
goto nomem; |
199 |
> |
if (fobj == OVOID) { /* create new triangle object */ |
200 |
> |
fobj = newobject(); |
201 |
> |
if (fobj == OVOID) |
202 |
> |
goto nomem; |
203 |
> |
fop = objptr(fobj); |
204 |
> |
fop->omod = mo; |
205 |
> |
fop->otype = OBJ_FACE; |
206 |
> |
sprintf(buf, "t%d", fobj); |
207 |
> |
fop->oname = savqstr(buf); |
208 |
> |
fop->oargs.nfargs = 9; |
209 |
> |
fop->oargs.farg = (RREAL *)malloc(9*sizeof(RREAL)); |
210 |
> |
if (fop->oargs.farg == NULL) |
211 |
> |
goto nomem; |
212 |
> |
} else { /* else reuse failed one */ |
213 |
> |
fop = objptr(fobj); |
214 |
> |
if (fop->otype != OBJ_FACE || fop->oargs.nfargs != 9) |
215 |
> |
error(CONSISTENCY, "code error 1 in cvtri"); |
216 |
> |
} |
217 |
|
for (j = 3; j--; ) { |
218 |
|
fop->oargs.farg[j] = vp1[j]; |
219 |
|
fop->oargs.farg[3+j] = vp2[j]; |
220 |
|
fop->oargs.farg[6+j] = vp3[j]; |
221 |
|
} |
222 |
|
/* create face record */ |
223 |
< |
if ((f = getface(fop)) == NULL || f->area == 0.) { |
224 |
< |
freeobjects(fobj, 1); |
223 |
> |
f = getface(fop); |
224 |
> |
if (f->area == 0.) { |
225 |
> |
free_os(fop); |
226 |
|
return(0); |
227 |
|
} |
228 |
|
if (fop->os != (char *)f) |
229 |
< |
error(CONSISTENCY, "code error in cvtri"); |
229 |
> |
error(CONSISTENCY, "code error 2 in cvtri"); |
230 |
|
/* follow with auxliary data */ |
231 |
|
f = (FACE *)realloc((void *)f, sizeof(FACE)+tdsize(flags)); |
232 |
|
if (f == NULL) |
253 |
|
add2bounds(vp1, vc1); |
254 |
|
add2bounds(vp2, vc2); |
255 |
|
add2bounds(vp3, vc3); |
256 |
+ |
fobj = OVOID; /* we used this one */ |
257 |
|
return(1); |
258 |
|
nomem: |
259 |
|
error(SYSTEM, "out of memory in cvtri"); |
291 |
|
for (i = 3; i--; ) |
292 |
|
for (j = 2; j--; ) |
293 |
|
vert[i].uv[j] = ts->vc[i][j]; |
294 |
< |
ts->obj = addmeshtri(ourmesh, vert); |
294 |
> |
ts->obj = addmeshtri(ourmesh, vert, o->omod); |
295 |
|
if (ts->obj == OVOID) |
296 |
|
error(INTERNAL, "addmeshtri failed"); |
297 |
|
return(ts->obj); |
307 |
|
return; |
308 |
|
/* fix coordinate bounds */ |
309 |
|
for (i = 0; i < 3; i++) { |
310 |
< |
if (meshbounds[0][i] >= meshbounds[1][i]) |
310 |
> |
if (meshbounds[0][i] > meshbounds[1][i]) |
311 |
|
error(USER, "no polygons in mesh"); |
312 |
|
meshbounds[0][i] -= OMARGIN; |
313 |
|
meshbounds[1][i] += OMARGIN; |
323 |
|
ourmesh->uvlim[1][0] = ourmesh->uvlim[1][1] = 0.; |
324 |
|
} else { |
325 |
|
for (i = 0; i < 2; i++) { |
326 |
< |
double marg; |
327 |
< |
marg = 1e-6*(ourmesh->uvlim[1][i] - |
328 |
< |
ourmesh->uvlim[0][i]); |
326 |
> |
double marg; /* expand past endpoints */ |
327 |
> |
marg = (2./(1L<<(8*sizeof(uint16)))) * |
328 |
> |
(ourmesh->uvlim[1][i] - |
329 |
> |
ourmesh->uvlim[0][i]); |
330 |
|
ourmesh->uvlim[0][i] -= marg; |
331 |
|
ourmesh->uvlim[1][i] += marg; |
332 |
|
} |