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 VOIDID "void" /* this is defined in object.h */ |
38 |
+ |
|
39 |
|
#define CALNAME "tmesh.cal" /* the name of our auxiliary file */ |
40 |
< |
#define PATNAME "tpat" /* triangle pattern name (reused) */ |
41 |
< |
#define TEXNAME "tnor" /* triangle texture name (reused) */ |
40 |
> |
#define PATNAME "T-pat" /* triangle pattern name (reused) */ |
41 |
> |
#define TEXNAME "T-nor" /* triangle texture name (reused) */ |
42 |
|
|
43 |
|
#define V_DEFINED 01 /* this vertex is defined */ |
44 |
|
#define V_HASNORM 02 /* vertex has surface normal */ |
56 |
|
|
57 |
|
typedef FLOAT BARYCCM[3][4]; |
58 |
|
|
59 |
< |
#define novert(i) ((i)<0|(i)>=nverts||!(vlist[i].flags&V_DEFINED)) |
59 |
> |
#define novert(i) ((i)<0|(i)>=nverts || !(vlist[i].flags&V_DEFINED)) |
60 |
|
|
61 |
|
#define CHUNKSIZ 128 /* vertex allocation chunk size */ |
62 |
|
|
63 |
|
extern VERTEX *vnew(); /* allocate a vertex (never freed) */ |
64 |
|
|
65 |
+ |
char *defmat = VOIDID; /* default (starting) material name */ |
66 |
+ |
char *defpat = ""; /* default (starting) picture name */ |
67 |
+ |
char *defobj = "T"; /* default (starting) object name */ |
68 |
|
|
69 |
+ |
|
70 |
|
main(argc, argv) /* read in T-mesh files and convert */ |
71 |
|
int argc; |
72 |
|
char *argv[]; |
74 |
|
FILE *fp; |
75 |
|
int i; |
76 |
|
|
77 |
< |
if (argc == 1) |
77 |
> |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
78 |
> |
switch (argv[i][1]) { |
79 |
> |
case 'o': /* object name */ |
80 |
> |
defobj = argv[++i]; |
81 |
> |
break; |
82 |
> |
case 'm': /* default material */ |
83 |
> |
defmat = argv[++i]; |
84 |
> |
break; |
85 |
> |
case 'p': /* default picture */ |
86 |
> |
defpat = argv[++i]; |
87 |
> |
break; |
88 |
> |
default: |
89 |
> |
fprintf(stderr, |
90 |
> |
"Usage: %s [-o obj][-m mat][-p pic] [file ..]\n", |
91 |
> |
argv[0]); |
92 |
> |
exit(1); |
93 |
> |
} |
94 |
> |
if (i >= argc) |
95 |
|
convert("<stdin>", stdin); |
96 |
|
else |
97 |
< |
for (i = 1; i < argc; i++) { |
97 |
> |
for ( ; i < argc; i++) { |
98 |
|
if ((fp = fopen(argv[i], "r")) == NULL) { |
99 |
|
perror(argv[i]); |
100 |
|
exit(1); |
110 |
|
char *fname; |
111 |
|
FILE *fp; |
112 |
|
{ |
113 |
< |
char typ[2]; |
113 |
> |
char typ[4]; |
114 |
|
int id[3]; |
115 |
|
double vec[3]; |
116 |
|
char picfile[128]; |
117 |
|
char matname[64]; |
118 |
< |
char *err; |
119 |
< |
register int c; |
120 |
< |
register VERTEX *lastv = NULL; |
118 |
> |
char objname[64]; |
119 |
> |
register int i; |
120 |
> |
register VERTEX *lastv; |
121 |
> |
/* start fresh */ |
122 |
> |
i = nverts; |
123 |
> |
lastv = vlist; |
124 |
> |
while (i--) |
125 |
> |
(lastv++)->flags = 0; |
126 |
> |
lastv = NULL; |
127 |
> |
strcpy(picfile, defpat); |
128 |
> |
strcpy(matname, defmat); |
129 |
> |
strcpy(objname, defobj); |
130 |
|
|
131 |
< |
picfile[0] = '\0'; |
132 |
< |
strcpy(matname, "void"); |
97 |
< |
|
131 |
> |
printf("\n## T-mesh read from: %s\n", fname); |
132 |
> |
/* scan until EOF */ |
133 |
|
while (fscanf(fp, "%1s", typ) == 1) |
134 |
|
switch (typ[0]) { |
135 |
|
case 'v': /* vertex */ |
143 |
|
syntax(fname, fp, "Bad triangle"); |
144 |
|
if (novert(id[0]) | novert(id[1]) | novert(id[2])) |
145 |
|
syntax(fname, fp, "Undefined triangle vertex"); |
146 |
< |
triangle(picfile, matname, &vlist[id[0]], |
146 |
> |
triangle(picfile, matname, objname, &vlist[id[0]], |
147 |
|
&vlist[id[1]], &vlist[id[2]]); |
148 |
|
break; |
149 |
|
case 'n': /* surface normal */ |
168 |
|
lastv->ndx[1] = vec[1]; |
169 |
|
lastv->flags |= V_HASINDX; |
170 |
|
break; |
171 |
+ |
case 'o': /* object name */ |
172 |
+ |
if (fscanf(fp, "%s", objname) != 1) |
173 |
+ |
syntax(fname, fp, "Bad object name"); |
174 |
+ |
break; |
175 |
|
case 'm': /* material */ |
176 |
|
if (fscanf(fp, "%s", matname) != 1) |
177 |
|
syntax(fname, fp, "Bad material"); |
178 |
|
if (matname[0] == '-' && !matname[1]) |
179 |
< |
strcpy(matname, "void"); |
179 |
> |
strcpy(matname, VOIDID); |
180 |
|
break; |
181 |
|
case 'p': /* picture */ |
182 |
|
if (fscanf(fp, "%s", picfile) != 1) |
185 |
|
picfile[0] = '\0'; |
186 |
|
break; |
187 |
|
case '#': /* comment */ |
188 |
< |
while ((c = getc(fp)) != EOF && c != '\n') |
189 |
< |
; |
188 |
> |
fputs("\n#", stdout); |
189 |
> |
while ((i = getc(fp)) != EOF) { |
190 |
> |
putchar(i); |
191 |
> |
if (i == '\n') |
192 |
> |
break; |
193 |
> |
} |
194 |
|
break; |
195 |
|
default: |
196 |
|
syntax(fname, fp, "Unknown type"); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
< |
triangle(pn, mn, v1, v2, v3) /* put out a triangle */ |
203 |
< |
char *pn, *mn; |
202 |
> |
triangle(pn, mod, obj, v1, v2, v3) /* put out a triangle */ |
203 |
> |
char *pn, *mod, *obj; |
204 |
|
register VERTEX *v1, *v2, *v3; |
205 |
|
{ |
206 |
|
static int ntri = 0; |
164 |
– |
char *mod = mn; |
207 |
|
BARYCCM bvecs; |
208 |
|
/* compute barycentric coordinates */ |
209 |
|
if (v1->flags & v2->flags & v3->flags & (V_HASINDX|V_HASNORM)) |
216 |
|
printf("4 dx dy dz %s\n", CALNAME); |
217 |
|
printf("0\n21\n"); |
218 |
|
put_baryc(bvecs); |
219 |
< |
printf("\t%f %f %f\n", v1->nor[0], v1->nor[1], v1->nor[2]); |
220 |
< |
printf("\t%f %f %f\n", v2->nor[0], v2->nor[1], v2->nor[2]); |
221 |
< |
printf("\t%f %f %f\n", v3->nor[0], v3->nor[1], v3->nor[2]); |
219 |
> |
printf("\t%14.12g %14.12g %14.12g\n", |
220 |
> |
v1->nor[0], v2->nor[0], v3->nor[0]); |
221 |
> |
printf("\t%14.12g %14.12g %14.12g\n", |
222 |
> |
v1->nor[1], v2->nor[1], v3->nor[1]); |
223 |
> |
printf("\t%14.12g %14.12g %14.12g\n", |
224 |
> |
v1->nor[2], v2->nor[2], v3->nor[2]); |
225 |
|
} |
226 |
|
/* put out pattern (if any) */ |
227 |
|
if (*pn && (v1->flags & v2->flags & v3->flags & V_HASINDX)) { |
230 |
|
printf("7 noneg noneg noneg %s %s u v\n", pn, CALNAME); |
231 |
|
printf("0\n18\n"); |
232 |
|
put_baryc(bvecs); |
233 |
< |
printf("\t%f %f\n", v1->ndx[0], v1->ndx[1]); |
234 |
< |
printf("\t%f %f\n", v2->ndx[0], v2->ndx[1]); |
190 |
< |
printf("\t%f %f\n", v3->ndx[0], v3->ndx[1]); |
233 |
> |
printf("\t%f %f %f\n", v1->ndx[0], v2->ndx[0], v3->ndx[0]); |
234 |
> |
printf("\t%f %f %f\n", v1->ndx[1], v2->ndx[1], v3->ndx[1]); |
235 |
|
} |
236 |
|
/* put out triangle */ |
237 |
< |
printf("\n%s polygon t%d\n", mod, ++ntri); |
237 |
> |
printf("\n%s polygon %s.%d\n", mod, obj, ++ntri); |
238 |
|
printf("0\n0\n9\n"); |
239 |
|
printf("%18.12g %18.12g %18.12g\n", v1->pos[0],v1->pos[1],v1->pos[2]); |
240 |
|
printf("%18.12g %18.12g %18.12g\n", v2->pos[0],v2->pos[1],v2->pos[2]); |
245 |
|
int |
246 |
|
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
247 |
|
register BARYCCM bcm; |
248 |
< |
FVECT v1, v2, v3; |
248 |
> |
FLOAT *v1, *v2, *v3; |
249 |
|
{ |
250 |
|
FLOAT *vt; |
251 |
|
FVECT va, vab, vcb; |
312 |
|
"Out of memory while allocating vertex %d\n", id); |
313 |
|
exit(1); |
314 |
|
} |
315 |
< |
while (i < nverts) /* clear them */ |
315 |
> |
while (i < nverts) /* clear what's new */ |
316 |
|
vlist[i++].flags = 0; |
317 |
|
} |
318 |
< |
/* assign it */ |
318 |
> |
/* assign new vertex */ |
319 |
|
vlist[id].pos[0] = x; |
320 |
|
vlist[id].pos[1] = y; |
321 |
|
vlist[id].pos[2] = z; |
336 |
|
int lineno; |
337 |
|
|
338 |
|
if (fp == stdin) |
339 |
< |
fprintf(stderr, "%s: syntax error: %s\n", fn, er); |
339 |
> |
fprintf(stderr, "%s: T-mesh format error: %s\n", fn, er); |
340 |
|
else { |
341 |
|
cpos = ftell(fp); |
342 |
|
fseek(fp, 0L, 0); |
347 |
|
if (c == '\n') |
348 |
|
lineno++; |
349 |
|
} |
350 |
< |
fprintf(stderr, "%s: syntax error at line %d: %s\n", |
350 |
> |
fprintf(stderr, "%s: T-mesh format error at line %d: %s\n", |
351 |
|
fn, lineno, er); |
352 |
|
} |
353 |
|
exit(1); |