1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
1.4 |
static const char RCSid[] = "$Id: mgf2rad.c,v 1.3 2003/06/26 00:58:09 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Convert MGF (Materials and Geometry Format) to Radiance |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
|
|
#include <stdlib.h> |
10 |
|
|
#include <math.h> |
11 |
|
|
#include <string.h> |
12 |
|
|
#include "mgflib/parser.h" |
13 |
|
|
#include "color.h" |
14 |
|
|
#include "tmesh.h" |
15 |
|
|
|
16 |
|
|
#define putv(v) printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2]) |
17 |
|
|
|
18 |
|
|
#define invert (xf_context != NULL && xf_context->rev) |
19 |
|
|
|
20 |
|
|
double glowdist = FHUGE; /* glow test distance */ |
21 |
|
|
|
22 |
|
|
double emult = 1.; /* emitter multiplier */ |
23 |
|
|
|
24 |
|
|
FILE *matfp; /* material output file */ |
25 |
|
|
|
26 |
|
|
int r_comment(), r_cone(), r_cyl(), r_face(), r_ies(), r_ring(), r_sph(); |
27 |
|
|
char *material(), *object(), *addarg(); |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
main(argc, argv) /* convert files to stdout */ |
31 |
|
|
int argc; |
32 |
|
|
char *argv[]; |
33 |
|
|
{ |
34 |
|
|
int i; |
35 |
|
|
|
36 |
|
|
matfp = stdout; |
37 |
|
|
/* print out parser version */ |
38 |
|
|
printf("## Translated from MGF Version %d.%d\n", MG_VMAJOR, MG_VMINOR); |
39 |
|
|
/* initialize dispatch table */ |
40 |
|
|
mg_ehand[MG_E_COMMENT] = r_comment; /* we pass comments */ |
41 |
|
|
mg_ehand[MG_E_COLOR] = c_hcolor; /* they get color */ |
42 |
|
|
mg_ehand[MG_E_CONE] = r_cone; /* we do cones */ |
43 |
|
|
mg_ehand[MG_E_CMIX] = c_hcolor; /* they mix colors */ |
44 |
|
|
mg_ehand[MG_E_CSPEC] = c_hcolor; /* they get spectra */ |
45 |
|
|
mg_ehand[MG_E_CXY] = c_hcolor; /* they get chromaticities */ |
46 |
|
|
mg_ehand[MG_E_CCT] = c_hcolor; /* they get color temp's */ |
47 |
|
|
mg_ehand[MG_E_CYL] = r_cyl; /* we do cylinders */ |
48 |
|
|
mg_ehand[MG_E_ED] = c_hmaterial; /* they get emission */ |
49 |
|
|
mg_ehand[MG_E_FACE] = r_face; /* we do faces */ |
50 |
|
|
mg_ehand[MG_E_IES] = r_ies; /* we do IES files */ |
51 |
|
|
mg_ehand[MG_E_IR] = c_hmaterial; /* they get refractive index */ |
52 |
|
|
mg_ehand[MG_E_MATERIAL] = c_hmaterial; /* they get materials */ |
53 |
|
|
mg_ehand[MG_E_NORMAL] = c_hvertex; /* they get normals */ |
54 |
|
|
mg_ehand[MG_E_OBJECT] = obj_handler; /* they track object names */ |
55 |
|
|
mg_ehand[MG_E_POINT] = c_hvertex; /* they get points */ |
56 |
|
|
mg_ehand[MG_E_RD] = c_hmaterial; /* they get diffuse refl. */ |
57 |
|
|
mg_ehand[MG_E_RING] = r_ring; /* we do rings */ |
58 |
|
|
mg_ehand[MG_E_RS] = c_hmaterial; /* they get specular refl. */ |
59 |
|
|
mg_ehand[MG_E_SIDES] = c_hmaterial; /* they get # sides */ |
60 |
|
|
mg_ehand[MG_E_SPH] = r_sph; /* we do spheres */ |
61 |
|
|
mg_ehand[MG_E_TD] = c_hmaterial; /* they get diffuse trans. */ |
62 |
|
|
mg_ehand[MG_E_TS] = c_hmaterial; /* they get specular trans. */ |
63 |
|
|
mg_ehand[MG_E_VERTEX] = c_hvertex; /* they get vertices */ |
64 |
|
|
mg_ehand[MG_E_XF] = xf_handler; /* they track transforms */ |
65 |
|
|
mg_init(); /* initialize the parser */ |
66 |
|
|
/* get our options & print header */ |
67 |
|
|
printf("## %s", argv[0]); |
68 |
|
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
69 |
|
|
printf(" %s", argv[i]); |
70 |
|
|
switch (argv[i][1]) { |
71 |
|
|
case 'g': /* glow distance (meters) */ |
72 |
|
|
if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f")) |
73 |
|
|
goto userr; |
74 |
|
|
glowdist = atof(argv[++i]); |
75 |
|
|
printf(" %s", argv[i]); |
76 |
|
|
break; |
77 |
|
|
case 'e': /* emitter multiplier */ |
78 |
|
|
if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f")) |
79 |
|
|
goto userr; |
80 |
|
|
emult = atof(argv[++i]); |
81 |
|
|
printf(" %s", argv[i]); |
82 |
|
|
break; |
83 |
|
|
case 'm': /* materials file */ |
84 |
|
|
matfp = fopen(argv[++i], "a"); |
85 |
|
|
if (matfp == NULL) { |
86 |
|
|
fprintf(stderr, "%s: cannot append\n", argv[i]); |
87 |
|
|
exit(1); |
88 |
|
|
} |
89 |
|
|
printf(" %s", argv[i]); |
90 |
|
|
break; |
91 |
|
|
default: |
92 |
|
|
goto userr; |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
putchar('\n'); |
96 |
|
|
if (i == argc) { /* convert stdin */ |
97 |
|
|
if (mg_load(NULL) != MG_OK) |
98 |
|
|
exit(1); |
99 |
|
|
if (mg_nunknown) |
100 |
|
|
printf("## %s: %u unknown entities\n", |
101 |
|
|
argv[0], mg_nunknown); |
102 |
|
|
} else /* convert each file */ |
103 |
|
|
for ( ; i < argc; i++) { |
104 |
|
|
printf("## %s %s ##############################\n", |
105 |
|
|
argv[0], argv[i]); |
106 |
|
|
if (mg_load(argv[i]) != MG_OK) |
107 |
|
|
exit(1); |
108 |
|
|
if (mg_nunknown) { |
109 |
|
|
printf("## %s %s: %u unknown entities\n", |
110 |
|
|
argv[0], argv[i], mg_nunknown); |
111 |
|
|
mg_nunknown = 0; |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
exit(0); |
115 |
|
|
userr: |
116 |
|
|
fprintf(stderr, "Usage: %s [-g dist][-e mult][-m matf] [file.mgf] ..\n", |
117 |
|
|
argv[0]); |
118 |
|
|
exit(1); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
|
122 |
|
|
int |
123 |
|
|
r_comment(ac, av) /* repeat a comment verbatim */ |
124 |
|
|
register int ac; |
125 |
|
|
register char **av; |
126 |
|
|
{ |
127 |
|
|
putchar('#'); /* use Radiance comment character */ |
128 |
|
|
while (--ac) { /* pass through verbatim */ |
129 |
|
|
putchar(' '); |
130 |
|
|
fputs(*++av, stdout); |
131 |
|
|
} |
132 |
|
|
putchar('\n'); |
133 |
|
|
return(MG_OK); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
int |
138 |
|
|
r_cone(ac, av) /* put out a cone */ |
139 |
|
|
int ac; |
140 |
|
|
char **av; |
141 |
|
|
{ |
142 |
|
|
static int ncones; |
143 |
|
|
char *mat; |
144 |
|
|
double r1, r2; |
145 |
|
|
C_VERTEX *cv1, *cv2; |
146 |
|
|
FVECT p1, p2; |
147 |
|
|
int inv; |
148 |
|
|
/* check argument count and type */ |
149 |
|
|
if (ac != 5) |
150 |
|
|
return(MG_EARGC); |
151 |
|
|
if (!isflt(av[2]) || !isflt(av[4])) |
152 |
|
|
return(MG_ETYPE); |
153 |
|
|
/* get the endpoint vertices */ |
154 |
|
|
if ((cv1 = c_getvert(av[1])) == NULL || |
155 |
|
|
(cv2 = c_getvert(av[3])) == NULL) |
156 |
|
|
return(MG_EUNDEF); |
157 |
|
|
xf_xfmpoint(p1, cv1->p); /* transform endpoints */ |
158 |
|
|
xf_xfmpoint(p2, cv2->p); |
159 |
|
|
r1 = xf_scale(atof(av[2])); /* scale radii */ |
160 |
|
|
r2 = xf_scale(atof(av[4])); |
161 |
|
|
inv = r1 < 0.; /* check for inverted cone */ |
162 |
|
|
if (r1 == 0.) { /* check for illegal radii */ |
163 |
|
|
if (r2 == 0.) |
164 |
|
|
return(MG_EILL); |
165 |
|
|
inv = r2 < 0.; |
166 |
|
|
} else if (r2 != 0. && inv ^ r2 < 0.) |
167 |
|
|
return(MG_EILL); |
168 |
|
|
if (inv) { |
169 |
|
|
r1 = -r1; |
170 |
|
|
r2 = -r2; |
171 |
|
|
} |
172 |
|
|
if ((mat = material()) == NULL) /* get material */ |
173 |
|
|
return(MG_EBADMAT); |
174 |
|
|
/* spit the sucker out */ |
175 |
|
|
printf("\n%s %s %sc%d\n", mat, inv ? "cup" : "cone", |
176 |
|
|
object(), ++ncones); |
177 |
|
|
printf("0\n0\n8\n"); |
178 |
|
|
putv(p1); |
179 |
|
|
putv(p2); |
180 |
|
|
printf("%18.12g %18.12g\n", r1, r2); |
181 |
|
|
return(MG_OK); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
|
185 |
|
|
int |
186 |
|
|
r_cyl(ac, av) /* put out a cylinder */ |
187 |
|
|
int ac; |
188 |
|
|
char **av; |
189 |
|
|
{ |
190 |
|
|
static int ncyls; |
191 |
|
|
char *mat; |
192 |
|
|
double rad; |
193 |
|
|
C_VERTEX *cv1, *cv2; |
194 |
|
|
FVECT p1, p2; |
195 |
|
|
int inv; |
196 |
|
|
/* check argument count and type */ |
197 |
|
|
if (ac != 4) |
198 |
|
|
return(MG_EARGC); |
199 |
|
|
if (!isflt(av[2])) |
200 |
|
|
return(MG_ETYPE); |
201 |
|
|
/* get the endpoint vertices */ |
202 |
|
|
if ((cv1 = c_getvert(av[1])) == NULL || |
203 |
|
|
(cv2 = c_getvert(av[3])) == NULL) |
204 |
|
|
return(MG_EUNDEF); |
205 |
|
|
xf_xfmpoint(p1, cv1->p); /* transform endpoints */ |
206 |
|
|
xf_xfmpoint(p2, cv2->p); |
207 |
|
|
rad = xf_scale(atof(av[2])); /* scale radius */ |
208 |
|
|
if ((inv = rad < 0.)) /* check for inverted cylinder */ |
209 |
|
|
rad = -rad; |
210 |
|
|
if ((mat = material()) == NULL) /* get material */ |
211 |
|
|
return(MG_EBADMAT); |
212 |
|
|
/* spit out the primitive */ |
213 |
|
|
printf("\n%s %s %scy%d\n", mat, inv ? "tube" : "cylinder", |
214 |
|
|
object(), ++ncyls); |
215 |
|
|
printf("0\n0\n7\n"); |
216 |
|
|
putv(p1); |
217 |
|
|
putv(p2); |
218 |
|
|
printf("%18.12g\n", rad); |
219 |
|
|
return(MG_OK); |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
|
223 |
|
|
int |
224 |
|
|
r_sph(ac, av) /* put out a sphere */ |
225 |
|
|
int ac; |
226 |
|
|
char **av; |
227 |
|
|
{ |
228 |
|
|
static int nsphs; |
229 |
|
|
char *mat; |
230 |
|
|
double rad; |
231 |
|
|
C_VERTEX *cv; |
232 |
|
|
FVECT cent; |
233 |
|
|
int inv; |
234 |
|
|
/* check argument count and type */ |
235 |
|
|
if (ac != 3) |
236 |
|
|
return(MG_EARGC); |
237 |
|
|
if (!isflt(av[2])) |
238 |
|
|
return(MG_ETYPE); |
239 |
|
|
if ((cv = c_getvert(av[1])) == NULL) /* get center vertex */ |
240 |
|
|
return(MG_EUNDEF); |
241 |
|
|
xf_xfmpoint(cent, cv->p); /* transform center */ |
242 |
|
|
rad = xf_scale(atof(av[2])); /* scale radius */ |
243 |
|
|
if ((inv = rad < 0.)) /* check for inversion */ |
244 |
|
|
rad = -rad; |
245 |
|
|
if ((mat = material()) == NULL) /* get material */ |
246 |
|
|
return(MG_EBADMAT); |
247 |
|
|
/* spit out primitive */ |
248 |
|
|
printf("\n%s %s %ss%d\n", mat, inv ? "bubble" : "sphere", |
249 |
|
|
object(), ++nsphs); |
250 |
|
|
printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n", |
251 |
|
|
cent[0], cent[1], cent[2], rad); |
252 |
|
|
return(MG_OK); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
|
256 |
|
|
int |
257 |
|
|
r_ring(ac, av) /* put out a ring */ |
258 |
|
|
int ac; |
259 |
|
|
char **av; |
260 |
|
|
{ |
261 |
|
|
static int nrings; |
262 |
|
|
char *mat; |
263 |
|
|
double r1, r2; |
264 |
|
|
C_VERTEX *cv; |
265 |
|
|
FVECT cent, norm; |
266 |
|
|
/* check argument count and type */ |
267 |
|
|
if (ac != 4) |
268 |
|
|
return(MG_EARGC); |
269 |
|
|
if (!isflt(av[2]) || !isflt(av[3])) |
270 |
|
|
return(MG_ETYPE); |
271 |
|
|
if ((cv = c_getvert(av[1])) == NULL) /* get center vertex */ |
272 |
|
|
return(MG_EUNDEF); |
273 |
|
|
if (is0vect(cv->n)) /* make sure we have normal */ |
274 |
|
|
return(MG_EILL); |
275 |
|
|
xf_xfmpoint(cent, cv->p); /* transform center */ |
276 |
|
|
xf_rotvect(norm, cv->n); /* rotate normal */ |
277 |
|
|
r1 = xf_scale(atof(av[2])); /* scale radii */ |
278 |
|
|
r2 = xf_scale(atof(av[3])); |
279 |
|
|
if (r1 < 0. | r2 <= r1) |
280 |
|
|
return(MG_EILL); |
281 |
|
|
if ((mat = material()) == NULL) /* get material */ |
282 |
|
|
return(MG_EBADMAT); |
283 |
|
|
/* spit out primitive */ |
284 |
|
|
printf("\n%s ring %sr%d\n", mat, object(), ++nrings); |
285 |
|
|
printf("0\n0\n8\n"); |
286 |
|
|
putv(cent); |
287 |
|
|
putv(norm); |
288 |
|
|
printf("%18.12g %18.12g\n", r1, r2); |
289 |
|
|
return(MG_OK); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
|
293 |
|
|
int |
294 |
|
|
r_face(ac, av) /* convert a face */ |
295 |
|
|
int ac; |
296 |
|
|
char **av; |
297 |
|
|
{ |
298 |
|
|
static int nfaces; |
299 |
|
|
int myi = invert; |
300 |
|
|
char *mat; |
301 |
|
|
register int i; |
302 |
|
|
register C_VERTEX *cv; |
303 |
|
|
FVECT v; |
304 |
|
|
int rv; |
305 |
|
|
/* check argument count and type */ |
306 |
|
|
if (ac < 4) |
307 |
|
|
return(MG_EARGC); |
308 |
|
|
if ((mat = material()) == NULL) /* get material */ |
309 |
|
|
return(MG_EBADMAT); |
310 |
|
|
if (ac <= 5) { /* check for smoothing */ |
311 |
|
|
C_VERTEX *cva[5]; |
312 |
|
|
for (i = 1; i < ac; i++) { |
313 |
|
|
if ((cva[i-1] = c_getvert(av[i])) == NULL) |
314 |
|
|
return(MG_EUNDEF); |
315 |
|
|
if (is0vect(cva[i-1]->n)) |
316 |
|
|
break; |
317 |
|
|
} |
318 |
|
|
if (i < ac) |
319 |
|
|
i = ISFLAT; |
320 |
|
|
else |
321 |
|
|
i = flat_tri(cva[0]->p, cva[1]->p, cva[2]->p, |
322 |
|
|
cva[0]->n, cva[1]->n, cva[2]->n); |
323 |
|
|
if (i == DEGEN) |
324 |
|
|
return(MG_OK); /* degenerate (error?) */ |
325 |
|
|
if (i == RVBENT) { |
326 |
|
|
myi = !myi; |
327 |
|
|
i = ISBENT; |
328 |
|
|
} else if (i == RVFLAT) { |
329 |
|
|
myi = !myi; |
330 |
|
|
i = ISFLAT; |
331 |
|
|
} |
332 |
|
|
if (i == ISBENT) { /* smoothed triangles */ |
333 |
|
|
do_tri(mat, cva[0], cva[1], cva[2], myi); |
334 |
|
|
if (ac == 5) |
335 |
|
|
do_tri(mat, cva[2], cva[3], cva[0], myi); |
336 |
|
|
return(MG_OK); |
337 |
|
|
} |
338 |
|
|
} |
339 |
|
|
/* spit out unsmoothed primitive */ |
340 |
|
|
printf("\n%s polygon %sf%d\n", mat, object(), ++nfaces); |
341 |
|
|
printf("0\n0\n%d\n", 3*(ac-1)); |
342 |
|
|
for (i = 1; i < ac; i++) { /* get, transform, print each vertex */ |
343 |
|
|
if ((cv = c_getvert(av[myi ? ac-i : i])) == NULL) |
344 |
|
|
return(MG_EUNDEF); |
345 |
|
|
xf_xfmpoint(v, cv->p); |
346 |
|
|
putv(v); |
347 |
|
|
} |
348 |
|
|
return(MG_OK); |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
|
352 |
|
|
int |
353 |
|
|
r_ies(ac, av) /* convert an IES luminaire file */ |
354 |
|
|
int ac; |
355 |
|
|
char **av; |
356 |
|
|
{ |
357 |
|
|
int xa0 = 2; |
358 |
|
|
char combuf[128]; |
359 |
|
|
char fname[48]; |
360 |
|
|
char *oname; |
361 |
|
|
register char *op; |
362 |
|
|
register int i; |
363 |
|
|
/* check argument count */ |
364 |
|
|
if (ac < 2) |
365 |
|
|
return(MG_EARGC); |
366 |
|
|
/* construct output file name */ |
367 |
|
|
if ((op = strrchr(av[1], '/')) != NULL) |
368 |
|
|
op++; |
369 |
|
|
else |
370 |
|
|
op = av[1]; |
371 |
|
|
(void)strcpy(fname, op); |
372 |
|
|
if ((op = strrchr(fname, '.')) == NULL) |
373 |
|
|
op = fname + strlen(fname); |
374 |
|
|
(void)strcpy(op, ".rad"); |
375 |
|
|
/* see if we need to run ies2rad */ |
376 |
|
|
if (access(fname, 0) == -1) { |
377 |
|
|
(void)strcpy(combuf, "ies2rad");/* build ies2rad command */ |
378 |
|
|
op = combuf + 7; /* get -m option (first) */ |
379 |
|
|
if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) { |
380 |
|
|
if (!isflt(av[xa0+1])) |
381 |
|
|
return(MG_ETYPE); |
382 |
|
|
op = addarg(addarg(op, "-m"), av[xa0+1]); |
383 |
|
|
xa0 += 2; |
384 |
|
|
} |
385 |
|
|
*op++ = ' '; /* build IES filename */ |
386 |
|
|
i = 0; |
387 |
|
|
if (mg_file != NULL && |
388 |
|
|
(oname = strrchr(mg_file->fname,'/')) != NULL) { |
389 |
|
|
i = oname - mg_file->fname + 1; |
390 |
|
|
(void)strcpy(op, mg_file->fname); |
391 |
|
|
} |
392 |
|
|
(void)strcpy(op+i, av[1]); |
393 |
|
|
if (access(op, 0) == -1) /* check for file existence */ |
394 |
|
|
return(MG_ENOFILE); |
395 |
|
|
system(combuf); /* run ies2rad */ |
396 |
|
|
if (access(fname, 0) == -1) /* check success */ |
397 |
|
|
return(MG_EINCL); |
398 |
|
|
} |
399 |
|
|
printf("\n!xform"); /* put out xform command */ |
400 |
|
|
oname = object(); |
401 |
|
|
if (*oname) { |
402 |
|
|
printf(" -n "); |
403 |
|
|
for (op = oname; op[1]; op++) /* remove trailing separator */ |
404 |
|
|
putchar(*op); |
405 |
|
|
} |
406 |
|
|
for (i = xa0; i < ac; i++) |
407 |
|
|
printf(" %s", av[i]); |
408 |
|
|
if (ac > xa0 && xf_argc > 0) |
409 |
|
|
printf(" -i 1"); |
410 |
|
|
for (i = 0; i < xf_argc; i++) |
411 |
|
|
printf(" %s", xf_argv[i]); |
412 |
|
|
printf(" %s\n", fname); |
413 |
|
|
return(MG_OK); |
414 |
|
|
} |
415 |
|
|
|
416 |
|
|
|
417 |
|
|
do_tri(mat, cv1, cv2, cv3, iv) /* put out smoothed triangle */ |
418 |
|
|
char *mat; |
419 |
|
|
C_VERTEX *cv1, *cv2, *cv3; |
420 |
|
|
int iv; |
421 |
|
|
{ |
422 |
|
|
static int ntris; |
423 |
|
|
BARYCCM bvecs; |
424 |
schorsch |
1.3 |
RREAL bcoor[3][3]; |
425 |
greg |
1.1 |
C_VERTEX *cvt; |
426 |
|
|
FVECT v1, v2, v3; |
427 |
|
|
FVECT n1, n2, n3; |
428 |
|
|
register int i; |
429 |
|
|
|
430 |
|
|
if (iv) { /* swap vertex order if inverted */ |
431 |
|
|
cvt = cv1; |
432 |
|
|
cv1 = cv3; |
433 |
|
|
cv3 = cvt; |
434 |
|
|
} |
435 |
|
|
xf_xfmpoint(v1, cv1->p); |
436 |
|
|
xf_xfmpoint(v2, cv2->p); |
437 |
|
|
xf_xfmpoint(v3, cv3->p); |
438 |
|
|
/* compute barycentric coords. */ |
439 |
|
|
if (comp_baryc(&bvecs, v1, v2, v3) < 0) |
440 |
|
|
return; /* degenerate triangle! */ |
441 |
|
|
printf("\n%s texfunc T-nor\n", mat); /* put out texture */ |
442 |
|
|
printf("4 dx dy dz %s\n0\n", TCALNAME); |
443 |
|
|
xf_rotvect(n1, cv1->n); |
444 |
|
|
xf_rotvect(n2, cv2->n); |
445 |
|
|
xf_rotvect(n3, cv3->n); |
446 |
|
|
for (i = 0; i < 3; i++) { |
447 |
|
|
bcoor[i][0] = n1[i]; |
448 |
|
|
bcoor[i][1] = n2[i]; |
449 |
|
|
bcoor[i][2] = n3[i]; |
450 |
|
|
} |
451 |
|
|
put_baryc(&bvecs, bcoor, 3); |
452 |
|
|
/* put out triangle */ |
453 |
|
|
printf("\nT-nor polygon %st%d\n", object(), ++ntris); |
454 |
|
|
printf("0\n0\n9\n"); |
455 |
|
|
putv(v1); |
456 |
|
|
putv(v2); |
457 |
|
|
putv(v3); |
458 |
|
|
} |
459 |
|
|
|
460 |
|
|
|
461 |
|
|
char * |
462 |
|
|
material() /* get (and print) current material */ |
463 |
|
|
{ |
464 |
|
|
char *mname = "mat"; |
465 |
|
|
COLOR radrgb, c2; |
466 |
|
|
double d; |
467 |
|
|
register int i; |
468 |
|
|
|
469 |
|
|
if (c_cmname != NULL) |
470 |
|
|
mname = c_cmname; |
471 |
|
|
if (!c_cmaterial->clock) |
472 |
|
|
return(mname); /* already current */ |
473 |
|
|
/* else update output */ |
474 |
|
|
c_cmaterial->clock = 0; |
475 |
|
|
if (c_cmaterial->ed > .1) { /* emitter */ |
476 |
|
|
cvtcolor(radrgb, &c_cmaterial->ed_c, |
477 |
|
|
emult*c_cmaterial->ed/(PI*WHTEFFICACY)); |
478 |
|
|
if (glowdist < FHUGE) { /* do a glow */ |
479 |
|
|
fprintf(matfp, "\nvoid glow %s\n0\n0\n", mname); |
480 |
|
|
fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED), |
481 |
|
|
colval(radrgb,GRN), |
482 |
|
|
colval(radrgb,BLU), glowdist); |
483 |
|
|
} else { |
484 |
|
|
fprintf(matfp, "\nvoid light %s\n0\n0\n", mname); |
485 |
|
|
fprintf(matfp, "3 %f %f %f\n", colval(radrgb,RED), |
486 |
|
|
colval(radrgb,GRN), |
487 |
|
|
colval(radrgb,BLU)); |
488 |
|
|
} |
489 |
|
|
return(mname); |
490 |
|
|
} |
491 |
|
|
d = c_cmaterial->rd + c_cmaterial->td + |
492 |
|
|
c_cmaterial->rs + c_cmaterial->ts; |
493 |
|
|
if (d < 0. | d > 1.) |
494 |
|
|
return(NULL); |
495 |
|
|
/* check for glass/dielectric */ |
496 |
|
|
if (c_cmaterial->nr > 1.1 && |
497 |
|
|
c_cmaterial->ts > .25 && c_cmaterial->rs <= .125 && |
498 |
|
|
c_cmaterial->td <= .01 && c_cmaterial->rd <= .01 && |
499 |
|
|
c_cmaterial->rs_a <= .01 && c_cmaterial->ts_a <= .01) { |
500 |
|
|
cvtcolor(radrgb, &c_cmaterial->ts_c, |
501 |
|
|
c_cmaterial->ts + c_cmaterial->rs); |
502 |
|
|
if (c_cmaterial->sided) { /* dielectric */ |
503 |
|
|
colval(radrgb,RED) = pow(colval(radrgb,RED), |
504 |
|
|
1./C_1SIDEDTHICK); |
505 |
|
|
colval(radrgb,GRN) = pow(colval(radrgb,GRN), |
506 |
|
|
1./C_1SIDEDTHICK); |
507 |
|
|
colval(radrgb,BLU) = pow(colval(radrgb,BLU), |
508 |
|
|
1./C_1SIDEDTHICK); |
509 |
|
|
fprintf(matfp, "\nvoid dielectric %s\n0\n0\n", mname); |
510 |
|
|
fprintf(matfp, "5 %g %g %g %f 0\n", colval(radrgb,RED), |
511 |
|
|
colval(radrgb,GRN), colval(radrgb,BLU), |
512 |
|
|
c_cmaterial->nr); |
513 |
|
|
return(mname); |
514 |
|
|
} |
515 |
|
|
/* glass */ |
516 |
|
|
fprintf(matfp, "\nvoid glass %s\n0\n0\n", mname); |
517 |
|
|
fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED), |
518 |
|
|
colval(radrgb,GRN), colval(radrgb,BLU), |
519 |
|
|
c_cmaterial->nr); |
520 |
|
|
return(mname); |
521 |
|
|
} |
522 |
|
|
/* check for trans */ |
523 |
|
|
if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) { |
524 |
|
|
double ts, a5, a6; |
525 |
|
|
|
526 |
|
|
if (c_cmaterial->sided) { |
527 |
|
|
ts = sqrt(c_cmaterial->ts); /* approximate */ |
528 |
|
|
a5 = .5; |
529 |
|
|
} else { |
530 |
|
|
ts = c_cmaterial->ts; |
531 |
|
|
a5 = 1.; |
532 |
|
|
} |
533 |
|
|
/* average colors */ |
534 |
|
|
d = c_cmaterial->rd + c_cmaterial->td + ts; |
535 |
|
|
cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d); |
536 |
|
|
cvtcolor(c2, &c_cmaterial->td_c, c_cmaterial->td/d); |
537 |
|
|
addcolor(radrgb, c2); |
538 |
|
|
cvtcolor(c2, &c_cmaterial->ts_c, ts/d); |
539 |
|
|
addcolor(radrgb, c2); |
540 |
|
|
if (c_cmaterial->rs + ts > .0001) |
541 |
|
|
a5 = (c_cmaterial->rs*c_cmaterial->rs_a + |
542 |
|
|
ts*a5*c_cmaterial->ts_a) / |
543 |
|
|
(c_cmaterial->rs + ts); |
544 |
|
|
a6 = (c_cmaterial->td + ts) / |
545 |
|
|
(c_cmaterial->rd + c_cmaterial->td + ts); |
546 |
|
|
if (a6 < .999) |
547 |
|
|
d = c_cmaterial->rd/(1. - c_cmaterial->rs)/(1. - a6); |
548 |
|
|
else |
549 |
|
|
d = c_cmaterial->td + ts; |
550 |
|
|
scalecolor(radrgb, d); |
551 |
|
|
fprintf(matfp, "\nvoid trans %s\n0\n0\n", mname); |
552 |
|
|
fprintf(matfp, "7 %f %f %f\n", colval(radrgb,RED), |
553 |
|
|
colval(radrgb,GRN), colval(radrgb,BLU)); |
554 |
|
|
fprintf(matfp, "\t%f %f %f %f\n", c_cmaterial->rs, a5, a6, |
555 |
|
|
ts/(ts + c_cmaterial->td)); |
556 |
|
|
return(mname); |
557 |
|
|
} |
558 |
|
|
/* check for plastic */ |
559 |
|
|
if (c_cmaterial->rs < .1) { |
560 |
|
|
cvtcolor(radrgb, &c_cmaterial->rd_c, |
561 |
|
|
c_cmaterial->rd/(1.-c_cmaterial->rs)); |
562 |
|
|
fprintf(matfp, "\nvoid plastic %s\n0\n0\n", mname); |
563 |
|
|
fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED), |
564 |
|
|
colval(radrgb,GRN), colval(radrgb,BLU), |
565 |
|
|
c_cmaterial->rs, c_cmaterial->rs_a); |
566 |
|
|
return(mname); |
567 |
|
|
} |
568 |
|
|
/* else it's metal */ |
569 |
|
|
/* average colors */ |
570 |
|
|
cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd); |
571 |
|
|
cvtcolor(c2, &c_cmaterial->rs_c, c_cmaterial->rs); |
572 |
|
|
addcolor(radrgb, c2); |
573 |
|
|
fprintf(matfp, "\nvoid metal %s\n0\n0\n", mname); |
574 |
|
|
fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED), |
575 |
|
|
colval(radrgb,GRN), colval(radrgb,BLU), |
576 |
|
|
c_cmaterial->rs/(c_cmaterial->rd + c_cmaterial->rs), |
577 |
|
|
c_cmaterial->rs_a); |
578 |
|
|
return(mname); |
579 |
|
|
} |
580 |
|
|
|
581 |
|
|
|
582 |
|
|
cvtcolor(radrgb, ciec, intensity) /* convert a CIE XYZ color to RGB */ |
583 |
|
|
COLOR radrgb; |
584 |
|
|
register C_COLOR *ciec; |
585 |
|
|
double intensity; |
586 |
|
|
{ |
587 |
|
|
static COLOR ciexyz; |
588 |
|
|
|
589 |
|
|
c_ccvt(ciec, C_CSXY); /* get xy representation */ |
590 |
|
|
ciexyz[1] = intensity; |
591 |
|
|
ciexyz[0] = ciec->cx/ciec->cy*ciexyz[1]; |
592 |
|
|
ciexyz[2] = ciexyz[1]*(1./ciec->cy - 1.) - ciexyz[0]; |
593 |
|
|
cie_rgb(radrgb, ciexyz); |
594 |
|
|
} |
595 |
|
|
|
596 |
|
|
|
597 |
|
|
char * |
598 |
|
|
object() /* return current object name */ |
599 |
|
|
{ |
600 |
|
|
static char objbuf[64]; |
601 |
|
|
register int i; |
602 |
|
|
register char *cp; |
603 |
|
|
int len; |
604 |
|
|
/* tracked by obj_handler */ |
605 |
|
|
i = obj_nnames - sizeof(objbuf)/16; |
606 |
|
|
if (i < 0) |
607 |
|
|
i = 0; |
608 |
|
|
for (cp = objbuf; i < obj_nnames && |
609 |
|
|
cp + (len=strlen(obj_name[i])) < objbuf+sizeof(objbuf)-1; |
610 |
|
|
i++, *cp++ = '.') { |
611 |
|
|
strcpy(cp, obj_name[i]); |
612 |
|
|
cp += len; |
613 |
|
|
} |
614 |
|
|
*cp = '\0'; |
615 |
|
|
return(objbuf); |
616 |
|
|
} |
617 |
|
|
|
618 |
|
|
|
619 |
|
|
char * |
620 |
|
|
addarg(op, arg) /* add argument and advance pointer */ |
621 |
|
|
register char *op, *arg; |
622 |
|
|
{ |
623 |
|
|
*op = ' '; |
624 |
|
|
while (*++op = *arg++) |
625 |
|
|
; |
626 |
|
|
return(op); |
627 |
|
|
} |