5 |
|
#endif |
6 |
|
|
7 |
|
/* |
8 |
< |
* Convert a triangle mesh into a Radiance description. |
8 |
> |
* Convert a trianglular mesh into a Radiance description. |
9 |
|
* |
10 |
|
* Unlike most other converters, we have defined a file |
11 |
< |
* format for the mesh ourselves. It contains eight types, |
11 |
> |
* format for the input ourselves. The format contains eight types, |
12 |
|
* each of which is identified by a single letter. These are: |
13 |
|
* |
14 |
|
* # comment = a comment. Continues until end of line. |
15 |
|
* v id Px Py Pz = a vertex. The id must be an integer. |
16 |
|
* n Nx Ny Nz = a normal. Corresponds to most recent vertex. |
17 |
|
* i Iu Iv = an index. Corresponds to most recent vertex. |
18 |
< |
* p picture = a picture. Used as a pattern what follows. |
18 |
> |
* p picture = a picture. Used as a pattern for following. |
19 |
|
* m material = a material name. Used for what follows. |
20 |
+ |
* o object = an object name. Used for what follows. |
21 |
|
* t id1 id2 id3 = a triangle. |
22 |
|
* |
23 |
|
* Only the 't' type results in any output. The others merely set values |
27 |
|
* only makes sense for a mesh which is to be put into an octree for |
28 |
|
* instancing.) Using a pattern requires that each vertex have an |
29 |
|
* associated index value for generating the colorpict primitive. |
30 |
+ |
* Likewise, an interpolated surface normal also requires that each |
31 |
+ |
* vertex of the triangle have an associated normal vector. |
32 |
+ |
* It is not necessary for the normal vectors to have unit length. |
33 |
|
*/ |
34 |
|
|
35 |
|
#include "standard.h" |
36 |
|
|
37 |
+ |
#define ABS(x) ((x)>=0 ? (x) : -(x)) |
38 |
+ |
|
39 |
+ |
#define VOIDID "void" /* this is defined in object.h */ |
40 |
+ |
|
41 |
|
#define CALNAME "tmesh.cal" /* the name of our auxiliary file */ |
42 |
< |
#define PATNAME "tpat" /* triangle pattern name (reused) */ |
43 |
< |
#define TEXNAME "tnor" /* triangle texture name (reused) */ |
42 |
> |
#define PATNAME "T-pat" /* triangle pattern name (reused) */ |
43 |
> |
#define TEXNAME "T-nor" /* triangle texture name (reused) */ |
44 |
|
|
45 |
|
#define V_DEFINED 01 /* this vertex is defined */ |
46 |
|
#define V_HASNORM 02 /* vertex has surface normal */ |
56 |
|
VERTEX *vlist = NULL; /* our vertex list */ |
57 |
|
int nverts = 0; /* number of vertices in our list */ |
58 |
|
|
59 |
< |
typedef FLOAT BARYCCM[3][4]; |
59 |
> |
typedef struct { |
60 |
> |
int ax; /* major axis */ |
61 |
> |
FLOAT tm[2][3]; /* transformation */ |
62 |
> |
} BARYCCM; |
63 |
|
|
64 |
< |
#define novert(i) ((i)<0|(i)>=nverts||!(vlist[i].flags&V_DEFINED)) |
64 |
> |
#define novert(i) ((i)<0|(i)>=nverts || !(vlist[i].flags&V_DEFINED)) |
65 |
|
|
66 |
|
#define CHUNKSIZ 128 /* vertex allocation chunk size */ |
67 |
|
|
68 |
|
extern VERTEX *vnew(); /* allocate a vertex (never freed) */ |
69 |
|
|
70 |
+ |
char *defmat = VOIDID; /* default (starting) material name */ |
71 |
+ |
char *defpat = ""; /* default (starting) picture name */ |
72 |
+ |
char *defobj = "T"; /* default (starting) object name */ |
73 |
|
|
74 |
+ |
|
75 |
|
main(argc, argv) /* read in T-mesh files and convert */ |
76 |
|
int argc; |
77 |
|
char *argv[]; |
79 |
|
FILE *fp; |
80 |
|
int i; |
81 |
|
|
82 |
< |
if (argc == 1) |
82 |
> |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
83 |
> |
switch (argv[i][1]) { |
84 |
> |
case 'o': /* object name */ |
85 |
> |
defobj = argv[++i]; |
86 |
> |
break; |
87 |
> |
case 'm': /* default material */ |
88 |
> |
defmat = argv[++i]; |
89 |
> |
break; |
90 |
> |
case 'p': /* default picture */ |
91 |
> |
defpat = argv[++i]; |
92 |
> |
break; |
93 |
> |
default: |
94 |
> |
fprintf(stderr, |
95 |
> |
"Usage: %s [-o obj][-m mat][-p pic] [file ..]\n", |
96 |
> |
argv[0]); |
97 |
> |
exit(1); |
98 |
> |
} |
99 |
> |
if (i >= argc) |
100 |
|
convert("<stdin>", stdin); |
101 |
|
else |
102 |
< |
for (i = 1; i < argc; i++) { |
102 |
> |
for ( ; i < argc; i++) { |
103 |
|
if ((fp = fopen(argv[i], "r")) == NULL) { |
104 |
|
perror(argv[i]); |
105 |
|
exit(1); |
115 |
|
char *fname; |
116 |
|
FILE *fp; |
117 |
|
{ |
118 |
< |
char typ[2]; |
118 |
> |
char typ[4]; |
119 |
|
int id[3]; |
120 |
|
double vec[3]; |
121 |
|
char picfile[128]; |
122 |
|
char matname[64]; |
123 |
< |
char *err; |
124 |
< |
register int c; |
125 |
< |
register VERTEX *lastv = NULL; |
123 |
> |
char objname[64]; |
124 |
> |
register int i; |
125 |
> |
register VERTEX *lastv; |
126 |
> |
/* start fresh */ |
127 |
> |
i = nverts; |
128 |
> |
lastv = vlist; |
129 |
> |
while (i--) |
130 |
> |
(lastv++)->flags = 0; |
131 |
> |
lastv = NULL; |
132 |
> |
strcpy(picfile, defpat); |
133 |
> |
strcpy(matname, defmat); |
134 |
> |
strcpy(objname, defobj); |
135 |
|
|
136 |
< |
printf("# T-mesh from: %s\n", fname); |
137 |
< |
|
97 |
< |
picfile[0] = '\0'; |
98 |
< |
strcpy(matname, "void"); |
99 |
< |
|
136 |
> |
printf("\n## T-mesh read from: %s\n", fname); |
137 |
> |
/* scan until EOF */ |
138 |
|
while (fscanf(fp, "%1s", typ) == 1) |
139 |
|
switch (typ[0]) { |
140 |
|
case 'v': /* vertex */ |
148 |
|
syntax(fname, fp, "Bad triangle"); |
149 |
|
if (novert(id[0]) | novert(id[1]) | novert(id[2])) |
150 |
|
syntax(fname, fp, "Undefined triangle vertex"); |
151 |
< |
triangle(picfile, matname, &vlist[id[0]], |
151 |
> |
triangle(picfile, matname, objname, &vlist[id[0]], |
152 |
|
&vlist[id[1]], &vlist[id[2]]); |
153 |
|
break; |
154 |
|
case 'n': /* surface normal */ |
173 |
|
lastv->ndx[1] = vec[1]; |
174 |
|
lastv->flags |= V_HASINDX; |
175 |
|
break; |
176 |
+ |
case 'o': /* object name */ |
177 |
+ |
if (fscanf(fp, "%s", objname) != 1) |
178 |
+ |
syntax(fname, fp, "Bad object name"); |
179 |
+ |
break; |
180 |
|
case 'm': /* material */ |
181 |
|
if (fscanf(fp, "%s", matname) != 1) |
182 |
|
syntax(fname, fp, "Bad material"); |
183 |
|
if (matname[0] == '-' && !matname[1]) |
184 |
< |
strcpy(matname, "void"); |
184 |
> |
strcpy(matname, VOIDID); |
185 |
|
break; |
186 |
|
case 'p': /* picture */ |
187 |
|
if (fscanf(fp, "%s", picfile) != 1) |
190 |
|
picfile[0] = '\0'; |
191 |
|
break; |
192 |
|
case '#': /* comment */ |
193 |
< |
while ((c = getc(fp)) != EOF && c != '\n') |
194 |
< |
; |
193 |
> |
fputs("\n#", stdout); |
194 |
> |
while ((i = getc(fp)) != EOF) { |
195 |
> |
putchar(i); |
196 |
> |
if (i == '\n') |
197 |
> |
break; |
198 |
> |
} |
199 |
|
break; |
200 |
|
default: |
201 |
|
syntax(fname, fp, "Unknown type"); |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
< |
triangle(pn, mn, v1, v2, v3) /* put out a triangle */ |
208 |
< |
char *pn, *mn; |
207 |
> |
triangle(pn, mod, obj, v1, v2, v3) /* put out a triangle */ |
208 |
> |
char *pn, *mod, *obj; |
209 |
|
register VERTEX *v1, *v2, *v3; |
210 |
|
{ |
211 |
|
static int ntri = 0; |
166 |
– |
char *mod = mn; |
212 |
|
BARYCCM bvecs; |
213 |
|
/* compute barycentric coordinates */ |
214 |
|
if (v1->flags & v2->flags & v3->flags & (V_HASINDX|V_HASNORM)) |
215 |
< |
if (comp_baryc(bvecs, v1->pos, v2->pos, v3->pos) < 0) |
215 |
> |
if (comp_baryc(&bvecs, v1->pos, v2->pos, v3->pos) < 0) |
216 |
|
return; |
217 |
|
/* put out texture (if any) */ |
218 |
|
if (v1->flags & v2->flags & v3->flags & V_HASNORM) { |
219 |
|
printf("\n%s texfunc %s\n", mod, TEXNAME); |
220 |
|
mod = TEXNAME; |
221 |
|
printf("4 dx dy dz %s\n", CALNAME); |
222 |
< |
printf("0\n21\n"); |
223 |
< |
put_baryc(bvecs); |
222 |
> |
printf("0\n16 "); |
223 |
> |
put_baryc(&bvecs); |
224 |
|
printf("\t%14.12g %14.12g %14.12g\n", |
225 |
|
v1->nor[0], v2->nor[0], v3->nor[0]); |
226 |
|
printf("\t%14.12g %14.12g %14.12g\n", |
233 |
|
printf("\n%s colorpict %s\n", mod, PATNAME); |
234 |
|
mod = PATNAME; |
235 |
|
printf("7 noneg noneg noneg %s %s u v\n", pn, CALNAME); |
236 |
< |
printf("0\n18\n"); |
237 |
< |
put_baryc(bvecs); |
236 |
> |
printf("0\n13 "); |
237 |
> |
put_baryc(&bvecs); |
238 |
|
printf("\t%f %f %f\n", v1->ndx[0], v2->ndx[0], v3->ndx[0]); |
239 |
|
printf("\t%f %f %f\n", v1->ndx[1], v2->ndx[1], v3->ndx[1]); |
240 |
|
} |
241 |
|
/* put out triangle */ |
242 |
< |
printf("\n%s polygon t%d\n", mod, ++ntri); |
242 |
> |
printf("\n%s polygon %s.%d\n", mod, obj, ++ntri); |
243 |
|
printf("0\n0\n9\n"); |
244 |
|
printf("%18.12g %18.12g %18.12g\n", v1->pos[0],v1->pos[1],v1->pos[2]); |
245 |
|
printf("%18.12g %18.12g %18.12g\n", v2->pos[0],v2->pos[1],v2->pos[2]); |
249 |
|
|
250 |
|
int |
251 |
|
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
252 |
< |
register BARYCCM bcm; |
253 |
< |
FVECT v1, v2, v3; |
252 |
> |
register BARYCCM *bcm; |
253 |
> |
FLOAT *v1, *v2, *v3; |
254 |
|
{ |
255 |
|
FLOAT *vt; |
256 |
|
FVECT va, vab, vcb; |
257 |
|
double d; |
258 |
+ |
int ax0, ax1; |
259 |
|
register int i, j; |
260 |
< |
|
261 |
< |
for (j = 0; j < 3; j++) { |
262 |
< |
for (i = 0; i < 3; i++) { |
263 |
< |
vab[i] = v1[i] - v2[i]; |
264 |
< |
vcb[i] = v3[i] - v2[i]; |
265 |
< |
} |
266 |
< |
d = DOT(vcb,vcb); |
260 |
> |
/* compute major axis */ |
261 |
> |
for (i = 0; i < 3; i++) { |
262 |
> |
vab[i] = v1[i] - v2[i]; |
263 |
> |
vcb[i] = v3[i] - v2[i]; |
264 |
> |
} |
265 |
> |
fcross(va, vab, vcb); |
266 |
> |
bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1; |
267 |
> |
bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2; |
268 |
> |
ax0 = (bcm->ax + 1) % 3; |
269 |
> |
ax1 = (bcm->ax + 2) % 3; |
270 |
> |
for (j = 0; j < 2; j++) { |
271 |
> |
vab[0] = v1[ax0] - v2[ax0]; |
272 |
> |
vcb[0] = v3[ax0] - v2[ax0]; |
273 |
> |
vab[1] = v1[ax1] - v2[ax1]; |
274 |
> |
vcb[1] = v3[ax1] - v2[ax1]; |
275 |
> |
d = vcb[0]*vcb[0] + vcb[1]*vcb[1]; |
276 |
|
if (d <= FTINY) |
277 |
|
return(-1); |
278 |
< |
d = DOT(vcb,vab)/d; |
279 |
< |
for (i = 0; i < 3; i++) |
280 |
< |
va[i] = vab[i] - vcb[i]*d; |
281 |
< |
d = DOT(va,va); |
278 |
> |
d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d; |
279 |
> |
va[0] = vab[0] - vcb[0]*d; |
280 |
> |
va[1] = vab[1] - vcb[1]*d; |
281 |
> |
d = va[0]*va[0] + va[1]*va[1]; |
282 |
|
if (d <= FTINY) |
283 |
|
return(-1); |
284 |
< |
for (i = 0; i < 3; i++) { |
285 |
< |
va[i] /= d; |
286 |
< |
bcm[j][i] = va[i]; |
232 |
< |
} |
233 |
< |
bcm[j][3] = -DOT(v2,va); |
284 |
> |
bcm->tm[j][0] = va[0] /= d; |
285 |
> |
bcm->tm[j][1] = va[1] /= d; |
286 |
> |
bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]); |
287 |
|
/* rotate vertices */ |
288 |
|
vt = v1; |
289 |
|
v1 = v2; |
295 |
|
|
296 |
|
|
297 |
|
put_baryc(bcm) /* put barycentric coord. vectors */ |
298 |
< |
register BARYCCM bcm; |
298 |
> |
register BARYCCM *bcm; |
299 |
|
{ |
300 |
< |
register int i; |
301 |
< |
|
302 |
< |
for (i = 0; i < 3; i++) |
303 |
< |
printf("%14.8f %14.8f %14.8f %14.8f\n", |
304 |
< |
bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]); |
300 |
> |
printf("\t%d\n", bcm->ax); |
301 |
> |
printf("%14.8f %14.8f %14.8f\n", |
302 |
> |
bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]); |
303 |
> |
printf("%14.8f %14.8f %14.8f\n", |
304 |
> |
bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]); |
305 |
|
} |
306 |
|
|
307 |
|
|
312 |
|
{ |
313 |
|
register int i; |
314 |
|
|
315 |
< |
if (id > nverts) { /* get some more */ |
315 |
> |
if (id >= nverts) { /* get some more */ |
316 |
|
i = nverts; |
317 |
< |
nverts = CHUNKSIZ*((id%CHUNKSIZ)+1); |
317 |
> |
nverts = CHUNKSIZ*((id/CHUNKSIZ)+1); |
318 |
|
if (vlist == NULL) |
319 |
|
vlist = (VERTEX *)malloc(nverts*sizeof(VERTEX)); |
320 |
|
else |
325 |
|
"Out of memory while allocating vertex %d\n", id); |
326 |
|
exit(1); |
327 |
|
} |
328 |
< |
while (i < nverts) /* clear them */ |
328 |
> |
while (i < nverts) /* clear what's new */ |
329 |
|
vlist[i++].flags = 0; |
330 |
|
} |
331 |
< |
/* assign it */ |
331 |
> |
/* assign new vertex */ |
332 |
|
vlist[id].pos[0] = x; |
333 |
|
vlist[id].pos[1] = y; |
334 |
|
vlist[id].pos[2] = z; |
349 |
|
int lineno; |
350 |
|
|
351 |
|
if (fp == stdin) |
352 |
< |
fprintf(stderr, "%s: syntax error: %s\n", fn, er); |
352 |
> |
fprintf(stderr, "%s: T-mesh format error: %s\n", fn, er); |
353 |
|
else { |
354 |
|
cpos = ftell(fp); |
355 |
|
fseek(fp, 0L, 0); |
360 |
|
if (c == '\n') |
361 |
|
lineno++; |
362 |
|
} |
363 |
< |
fprintf(stderr, "%s: syntax error at line %d: %s\n", |
363 |
> |
fprintf(stderr, "%s: T-mesh format error at line %d: %s\n", |
364 |
|
fn, lineno, er); |
365 |
|
} |
366 |
|
exit(1); |