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