1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Convert Radiance scene description to MGF |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
#include <string.h> |
13 |
#include "fvect.h" |
14 |
#include "object.h" |
15 |
#include "color.h" |
16 |
#include "lookup.h" |
17 |
|
18 |
#define PI 3.14159265358979323846 |
19 |
|
20 |
int o_face(), o_cone(), o_sphere(), o_ring(), o_cylinder(); |
21 |
int o_instance(), o_source(), o_illum(); |
22 |
int o_plastic(), o_metal(), o_glass(), o_mirror(), o_trans(), o_light(); |
23 |
|
24 |
extern void free(); |
25 |
extern char *malloc(); |
26 |
|
27 |
LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */ |
28 |
|
29 |
LUTAB rdispatch = LU_SINIT(NULL,NULL); /* function dispatch table */ |
30 |
|
31 |
char curmat[80]; /* current material */ |
32 |
char curobj[128] = "Untitled"; /* current object name */ |
33 |
|
34 |
double unit_mult = 1.; /* units multiplier */ |
35 |
|
36 |
#define hasmult (unit_mult < .999 || unit_mult > 1.001) |
37 |
|
38 |
/* |
39 |
* Stuff for tracking and reusing vertices: |
40 |
*/ |
41 |
|
42 |
char VKFMT[] = "%+1.9e %+1.9e %+1.9e"; |
43 |
#define VKLEN 64 |
44 |
|
45 |
#define mkvkey(k,v) sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2]) |
46 |
|
47 |
#define NVERTS 256 |
48 |
|
49 |
long clock; /* incremented at each vertex request */ |
50 |
|
51 |
struct vert { |
52 |
long lused; /* when last used (0 if unassigned) */ |
53 |
FVECT p; /* track point position only */ |
54 |
} vert[NVERTS]; /* our vertex cache */ |
55 |
|
56 |
LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */ |
57 |
|
58 |
|
59 |
main(argc, argv) |
60 |
int argc; |
61 |
char **argv; |
62 |
{ |
63 |
int i; |
64 |
|
65 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
66 |
switch (argv[i][1]) { |
67 |
case 'd': /* units */ |
68 |
switch (argv[i][2]) { |
69 |
case 'm': /* meters */ |
70 |
unit_mult = 1.; |
71 |
break; |
72 |
case 'c': /* centimeters */ |
73 |
unit_mult = .01; |
74 |
break; |
75 |
case 'f': /* feet */ |
76 |
unit_mult = 12.*.0254; |
77 |
break; |
78 |
case 'i': /* inches */ |
79 |
unit_mult = .0254; |
80 |
break; |
81 |
default: |
82 |
goto unkopt; |
83 |
} |
84 |
break; |
85 |
default: |
86 |
goto unkopt; |
87 |
} |
88 |
init(); |
89 |
if (i >= argc) |
90 |
rad2mgf(NULL); |
91 |
else |
92 |
for ( ; i < argc; i++) |
93 |
rad2mgf(argv[i]); |
94 |
uninit(); |
95 |
exit(0); |
96 |
unkopt: |
97 |
fprintf(stderr, "Usage: %s [-d{m|c|f|i}] file ..\n", argv[0]); |
98 |
exit(1); |
99 |
} |
100 |
|
101 |
|
102 |
rad2mgf(inp) /* convert a Radiance file to MGF */ |
103 |
char *inp; |
104 |
{ |
105 |
#define mod buf |
106 |
#define typ (buf+128) |
107 |
#define id (buf+256) |
108 |
#define alias (buf+384) |
109 |
char buf[512]; |
110 |
FUNARGS fa; |
111 |
register FILE *fp; |
112 |
register int c; |
113 |
|
114 |
if (inp == NULL) { |
115 |
inp = "standard input"; |
116 |
fp = stdin; |
117 |
} else if (inp[0] == '!') { |
118 |
if ((fp = popen(inp+1, "r")) == NULL) { |
119 |
fputs(inp, stderr); |
120 |
fputs(": cannot execute\n", stderr); |
121 |
exit(1); |
122 |
} |
123 |
} else if ((fp = fopen(inp, "r")) == NULL) { |
124 |
fputs(inp, stderr); |
125 |
fputs(": cannot open\n", stderr); |
126 |
exit(1); |
127 |
} |
128 |
printf("# Begin conversion from: %s\n", inp); |
129 |
while ((c = getc(fp)) != EOF) |
130 |
switch (c) { |
131 |
case ' ': /* white space */ |
132 |
case '\t': |
133 |
case '\n': |
134 |
case '\r': |
135 |
case '\f': |
136 |
break; |
137 |
case '#': /* comment */ |
138 |
if (fgets(buf, sizeof(buf), fp) != NULL) |
139 |
printf("# %s", buf); |
140 |
break; |
141 |
case '!': /* inline command */ |
142 |
ungetc(c, fp); |
143 |
fgetline(buf, sizeof(buf), fp); |
144 |
rad2mgf(buf); |
145 |
break; |
146 |
default: /* Radiance primitive */ |
147 |
ungetc(c, fp); |
148 |
if (fscanf(fp, "%s %s %s", mod, typ, id) != 3) { |
149 |
fputs(inp, stderr); |
150 |
fputs(": unexpected EOF\n", stderr); |
151 |
exit(1); |
152 |
} |
153 |
if (!strcmp(typ, "alias")) { |
154 |
strcpy(alias, "EOF"); |
155 |
fscanf(fp, "%s", alias); |
156 |
newmat(id, alias); |
157 |
} else { |
158 |
if (!readfargs(&fa, fp)) { |
159 |
fprintf(stderr, |
160 |
"%s: bad argument syntax for %s \"%s\"\n", |
161 |
inp, typ, id); |
162 |
exit(1); |
163 |
} |
164 |
cvtprim(inp, mod, typ, id, &fa); |
165 |
freefargs(&fa); |
166 |
} |
167 |
break; |
168 |
} |
169 |
printf("# End conversion from: %s\n", inp); |
170 |
if (inp[0] == '!') |
171 |
pclose(fp); |
172 |
else |
173 |
fclose(fp); |
174 |
#undef mod |
175 |
#undef typ |
176 |
#undef id |
177 |
#undef alias |
178 |
} |
179 |
|
180 |
|
181 |
cvtprim(inp, mod, typ, id, fa) /* process Radiance primitive */ |
182 |
char *inp, *mod, *typ, *id; |
183 |
FUNARGS *fa; |
184 |
{ |
185 |
int (*df)(); |
186 |
|
187 |
df = (int (*)())lu_find(&rdispatch, typ)->data; |
188 |
if (df != NULL) { /* convert */ |
189 |
if ((*df)(mod, typ, id, fa) < 0) { |
190 |
fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id); |
191 |
exit(1); |
192 |
} |
193 |
} else if (lu_find(&rmats, mod)->data != NULL) /* make alias */ |
194 |
newmat(id, mod); |
195 |
} |
196 |
|
197 |
|
198 |
newmat(id, alias) /* add a modifier to the alias list */ |
199 |
char *id; |
200 |
char *alias; |
201 |
{ |
202 |
register LUENT *lp, *lpa; |
203 |
|
204 |
if (alias != NULL) { /* look up alias */ |
205 |
if ((lpa = lu_find(&rmats, alias)) == NULL) |
206 |
goto memerr; |
207 |
if (lpa->data == NULL) |
208 |
alias = NULL; /* doesn't exist! */ |
209 |
} |
210 |
if ((lp = lu_find(&rmats, id)) == NULL) /* look up material */ |
211 |
goto memerr; |
212 |
if (alias != NULL && lp->data == lpa->key) |
213 |
return; /* alias set already */ |
214 |
if (lp->data == NULL) { /* allocate material */ |
215 |
if ((lp->key = (char *)malloc(strlen(id)+1)) == NULL) |
216 |
goto memerr; |
217 |
strcpy(lp->key, id); |
218 |
} |
219 |
if (alias == NULL) { /* set this material */ |
220 |
lp->data = lp->key; |
221 |
printf("m %s =\n", id); |
222 |
} else { /* set this alias */ |
223 |
lp->data = lpa->key; |
224 |
printf("m %s = %s\n", id, alias); |
225 |
} |
226 |
strcpy(curmat, id); |
227 |
return; |
228 |
memerr: |
229 |
fputs("Out of memory in newmat!\n", stderr); |
230 |
exit(1); |
231 |
} |
232 |
|
233 |
|
234 |
setmat(id) /* set material to this one */ |
235 |
char *id; |
236 |
{ |
237 |
if (!strcmp(id, curmat)) /* already set? */ |
238 |
return; |
239 |
if (!strcmp(id, VOIDID)) /* cannot set */ |
240 |
return; |
241 |
printf("m %s\n", id); |
242 |
strcpy(curmat, id); |
243 |
} |
244 |
|
245 |
|
246 |
setobj(id) /* set object name to this one */ |
247 |
char *id; |
248 |
{ |
249 |
register char *cp, *cp2; |
250 |
char *end = NULL; |
251 |
int diff = 0; |
252 |
/* use all but final suffix */ |
253 |
for (cp = id; *cp; cp++) |
254 |
if (*cp == '.') |
255 |
end = cp; |
256 |
if (end == NULL) |
257 |
end = cp; |
258 |
/* copy to current object */ |
259 |
for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++) |
260 |
diff += *cp != *cp2; |
261 |
if (!diff && !*cp2) |
262 |
return; |
263 |
*cp2 = '\0'; |
264 |
fputs("o\no ", stdout); |
265 |
puts(curobj); |
266 |
} |
267 |
|
268 |
|
269 |
init() /* initialize dispatch table and output */ |
270 |
{ |
271 |
lu_init(&vertab, NVERTS); |
272 |
lu_init(&rdispatch, 22); |
273 |
add2dispatch("polygon", o_face); |
274 |
add2dispatch("cone", o_cone); |
275 |
add2dispatch("cup", o_cone); |
276 |
add2dispatch("sphere", o_sphere); |
277 |
add2dispatch("bubble", o_sphere); |
278 |
add2dispatch("cylinder", o_cylinder); |
279 |
add2dispatch("tube", o_cylinder); |
280 |
add2dispatch("ring", o_ring); |
281 |
add2dispatch("instance", o_instance); |
282 |
add2dispatch("plastic", o_plastic); |
283 |
add2dispatch("plastic2", o_plastic); |
284 |
add2dispatch("metal", o_metal); |
285 |
add2dispatch("metal2", o_metal); |
286 |
add2dispatch("glass", o_glass); |
287 |
add2dispatch("trans", o_trans); |
288 |
add2dispatch("trans2", o_trans); |
289 |
add2dispatch("mirror", o_mirror); |
290 |
add2dispatch("light", o_light); |
291 |
add2dispatch("spotlight", o_light); |
292 |
add2dispatch("glow", o_light); |
293 |
add2dispatch("illum", o_illum); |
294 |
puts("# The following was converted from Radiance scene input"); |
295 |
if (hasmult) |
296 |
printf("xf -s %.4e\n", unit_mult); |
297 |
printf("o %s\n", curobj); |
298 |
} |
299 |
|
300 |
|
301 |
uninit() /* mark end of MGF file */ |
302 |
{ |
303 |
puts("o"); |
304 |
if (hasmult) |
305 |
puts("xf"); |
306 |
puts("# End of data converted from Radiance scene input"); |
307 |
lu_done(&rdispatch); |
308 |
lu_done(&rmats); |
309 |
lu_done(&vertab); |
310 |
} |
311 |
|
312 |
|
313 |
clrverts() /* clear vertex table */ |
314 |
{ |
315 |
register int i; |
316 |
|
317 |
lu_done(&vertab); |
318 |
for (i = 0; i < NVERTS; i++) |
319 |
vert[i].lused = 0; |
320 |
lu_init(&vertab, NVERTS); |
321 |
} |
322 |
|
323 |
|
324 |
add2dispatch(name, func) /* add function to dispatch table */ |
325 |
char *name; |
326 |
int (*func)(); |
327 |
{ |
328 |
register LUENT *lp; |
329 |
|
330 |
lp = lu_find(&rdispatch, name); |
331 |
if (lp->key != NULL) { |
332 |
fputs(name, stderr); |
333 |
fputs(": duplicate dispatch entry!\n", stderr); |
334 |
exit(1); |
335 |
} |
336 |
lp->key = name; |
337 |
lp->data = (char *)func; |
338 |
} |
339 |
|
340 |
|
341 |
char * |
342 |
getvertid(vname, vp) /* get/set vertex ID for this point */ |
343 |
char *vname; |
344 |
FVECT vp; |
345 |
{ |
346 |
char vkey[VKLEN]; |
347 |
register LUENT *lp; |
348 |
register int i, vndx; |
349 |
|
350 |
clock++; /* increment counter */ |
351 |
mkvkey(vkey, vp); |
352 |
if ((lp = lu_find(&vertab, vkey)) == NULL) |
353 |
goto memerr; |
354 |
if (lp->data == NULL) { /* allocate new vertex entry */ |
355 |
if (lp->key != NULL) /* reclaim deleted entry */ |
356 |
vertab.ndel--; |
357 |
else { |
358 |
if ((lp->key = (char *)malloc(VKLEN)) == NULL) |
359 |
goto memerr; |
360 |
strcpy(lp->key, vkey); |
361 |
} |
362 |
vndx = 0; /* find oldest vertex */ |
363 |
for (i = 1; i < NVERTS; i++) |
364 |
if (vert[i].lused < vert[vndx].lused) |
365 |
vndx = i; |
366 |
if (vert[vndx].lused) { /* free old entry first */ |
367 |
mkvkey(vkey, vert[vndx].p); |
368 |
lu_delete(&vertab, vkey); |
369 |
} |
370 |
VCOPY(vert[vndx].p, vp); /* assign it */ |
371 |
printf("v v%d =\n\tp %.15g %.15g %.15g\n", /* print it */ |
372 |
vndx, vp[0], vp[1], vp[2]); |
373 |
lp->data = (char *)&vert[vndx]; /* set it */ |
374 |
} else |
375 |
vndx = (struct vert *)lp->data - vert; |
376 |
vert[vndx].lused = clock; /* record this use */ |
377 |
sprintf(vname, "v%d", vndx); |
378 |
return(vname); |
379 |
memerr: |
380 |
fputs("Out of memory in getvertid!\n", stderr); |
381 |
exit(1); |
382 |
} |
383 |
|
384 |
|
385 |
int |
386 |
o_face(mod, typ, id, fa) /* print out a polygon */ |
387 |
char *mod, *typ, *id; |
388 |
FUNARGS *fa; |
389 |
{ |
390 |
char entbuf[512]; |
391 |
register char *cp; |
392 |
register int i; |
393 |
|
394 |
if (fa->nfargs < 9 | fa->nfargs % 3) |
395 |
return(-1); |
396 |
setmat(mod); |
397 |
setobj(id); |
398 |
cp = entbuf; |
399 |
*cp++ = 'f'; |
400 |
for (i = 0; i < fa->nfargs; i += 3) { |
401 |
*cp++ = ' '; |
402 |
getvertid(cp, fa->farg + i); |
403 |
while (*cp) |
404 |
cp++; |
405 |
} |
406 |
puts(entbuf); |
407 |
return(0); |
408 |
} |
409 |
|
410 |
|
411 |
int |
412 |
o_cone(mod, typ, id, fa) /* print out a cone */ |
413 |
char *mod, *typ, *id; |
414 |
register FUNARGS *fa; |
415 |
{ |
416 |
char v1[6], v2[6]; |
417 |
|
418 |
if (fa->nfargs != 8) |
419 |
return(-1); |
420 |
setmat(mod); |
421 |
setobj(id); |
422 |
getvertid(v1, fa->farg); |
423 |
getvertid(v2, fa->farg + 3); |
424 |
if (typ[1] == 'u') /* cup -> inverted cone */ |
425 |
printf("cone %s %.12g %s %.12g\n", |
426 |
v1, -fa->farg[6], v2, -fa->farg[7]); |
427 |
else |
428 |
printf("cone %s %.12g %s %.12g\n", |
429 |
v1, fa->farg[6], v2, fa->farg[7]); |
430 |
return(0); |
431 |
} |
432 |
|
433 |
|
434 |
int |
435 |
o_sphere(mod, typ, id, fa) /* print out a sphere */ |
436 |
char *mod, *typ, *id; |
437 |
register FUNARGS *fa; |
438 |
{ |
439 |
char cent[6]; |
440 |
|
441 |
if (fa->nfargs != 4) |
442 |
return(-1); |
443 |
setmat(mod); |
444 |
setobj(id); |
445 |
printf("sph %s %.12g\n", getvertid(cent, fa->farg), |
446 |
typ[0]=='b' ? -fa->farg[3] : fa->farg[3]); |
447 |
return(0); |
448 |
} |
449 |
|
450 |
|
451 |
int |
452 |
o_cylinder(mod, typ, id, fa) /* print out a cylinder */ |
453 |
char *mod, *typ, *id; |
454 |
register FUNARGS *fa; |
455 |
{ |
456 |
char v1[6], v2[6]; |
457 |
|
458 |
if (fa->nfargs != 7) |
459 |
return(-1); |
460 |
setmat(mod); |
461 |
setobj(id); |
462 |
getvertid(v1, fa->farg); |
463 |
getvertid(v2, fa->farg + 3); |
464 |
printf("cyl %s %.12g %s\n", v1, |
465 |
typ[0]=='t' ? -fa->farg[6] : fa->farg[6], v2); |
466 |
return(0); |
467 |
} |
468 |
|
469 |
|
470 |
int |
471 |
o_ring(mod, typ, id, fa) /* print out a ring */ |
472 |
char *mod, *typ, *id; |
473 |
register FUNARGS *fa; |
474 |
{ |
475 |
if (fa->nfargs != 8) |
476 |
return(-1); |
477 |
setmat(mod); |
478 |
setobj(id); |
479 |
printf("v cent =\n\tp %.12g %.12g %.12g\n", |
480 |
fa->farg[0], fa->farg[1], fa->farg[2]); |
481 |
printf("\tn %.12g %.12g %.12g\n", |
482 |
fa->farg[3], fa->farg[4], fa->farg[5]); |
483 |
if (fa->farg[6] < fa->farg[7]) |
484 |
printf("ring cent %.12g %.12g\n", |
485 |
fa->farg[6], fa->farg[7]); |
486 |
else |
487 |
printf("ring cent %.12g %.12g\n", |
488 |
fa->farg[7], fa->farg[6]); |
489 |
return(0); |
490 |
} |
491 |
|
492 |
|
493 |
int |
494 |
o_instance(mod, typ, id, fa) /* convert an instance */ |
495 |
char *mod, *typ, *id; |
496 |
FUNARGS *fa; |
497 |
{ |
498 |
register int i; |
499 |
register char *cp; |
500 |
char *start = NULL, *end = NULL; |
501 |
/* |
502 |
* We don't really know how to do this, so we just create |
503 |
* a reference to an undefined MGF file and it's the user's |
504 |
* responsibility to create this file and put the appropriate |
505 |
* stuff into it. |
506 |
*/ |
507 |
if (fa->nsargs < 1) |
508 |
return(-1); |
509 |
setmat(mod); /* only works if surfaces are void */ |
510 |
setobj(id); |
511 |
for (cp = fa->sarg[0]; *cp; cp++) /* construct MGF file name */ |
512 |
if (*cp == '/') |
513 |
start = cp+1; |
514 |
else if (*cp == '.') |
515 |
end = cp; |
516 |
if (start == NULL) |
517 |
start = fa->sarg[0]; |
518 |
if (end == NULL || start >= end) |
519 |
end = cp; |
520 |
fputs("i ", stdout); /* print include entity */ |
521 |
for (cp = start; cp < end; cp++) |
522 |
putchar(*cp); |
523 |
fputs(".mgf", stdout); /* add MGF suffix */ |
524 |
for (i = 1; i < fa->nsargs; i++) { /* add transform */ |
525 |
putchar(' '); |
526 |
fputs(fa->sarg[i], stdout); |
527 |
} |
528 |
putchar('\n'); |
529 |
clrverts(); /* vertex id's no longer reliable */ |
530 |
return(0); |
531 |
} |
532 |
|
533 |
|
534 |
int |
535 |
o_source(mod, typ, id, fa) /* convert a source */ |
536 |
char *mod, *typ, *id; |
537 |
FUNARGS *fa; |
538 |
{ |
539 |
return(0); /* there is no MGF equivalent! */ |
540 |
} |
541 |
|
542 |
|
543 |
int |
544 |
o_illum(mod, typ, id, fa) /* convert an illum material */ |
545 |
char *mod, *typ, *id; |
546 |
FUNARGS *fa; |
547 |
{ |
548 |
if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) { |
549 |
newmat(id, fa->sarg[0]); /* just create alias */ |
550 |
return(0); |
551 |
} |
552 |
/* else create invisible material */ |
553 |
newmat(id, NULL); |
554 |
puts("\tts 1 0"); |
555 |
return(0); |
556 |
} |
557 |
|
558 |
|
559 |
int |
560 |
o_plastic(mod, typ, id, fa) /* convert a plastic material */ |
561 |
char *mod, *typ, *id; |
562 |
register FUNARGS *fa; |
563 |
{ |
564 |
COLOR cxyz, rrgb; |
565 |
double d; |
566 |
|
567 |
if (fa->nfargs != (typ[7]=='2' ? 6 : 5)) |
568 |
return(-1); |
569 |
newmat(id, NULL); |
570 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2]; |
571 |
rgb_cie(cxyz, rrgb); |
572 |
puts("\tc"); /* put diffuse component */ |
573 |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
574 |
if (d > FTINY) |
575 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
576 |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])); |
577 |
if (fa->farg[3] > FTINY) { /* put specular component */ |
578 |
puts("\tc"); |
579 |
printf("\trs %.4f %.4f\n", fa->farg[3], |
580 |
typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) : |
581 |
fa->farg[4]); |
582 |
} |
583 |
return(0); |
584 |
} |
585 |
|
586 |
|
587 |
int |
588 |
o_metal(mod, typ, id, fa) /* convert a metal material */ |
589 |
char *mod, *typ, *id; |
590 |
register FUNARGS *fa; |
591 |
{ |
592 |
COLOR cxyz, rrgb; |
593 |
double d; |
594 |
|
595 |
if (fa->nfargs != (typ[5]=='2' ? 6 : 5)) |
596 |
return(-1); |
597 |
newmat(id, NULL); |
598 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2]; |
599 |
rgb_cie(cxyz, rrgb); |
600 |
puts("\tc"); /* put diffuse component */ |
601 |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
602 |
if (d > FTINY) |
603 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
604 |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])); |
605 |
/* put specular component */ |
606 |
printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3], |
607 |
typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) : |
608 |
fa->farg[4]); |
609 |
return(0); |
610 |
} |
611 |
|
612 |
|
613 |
int |
614 |
o_glass(mod, typ, id, fa) /* convert a glass material */ |
615 |
char *mod, *typ, *id; |
616 |
register FUNARGS *fa; |
617 |
{ |
618 |
COLOR cxyz, rrgb, trgb; |
619 |
double nrfr = 1.52, F, d; |
620 |
register int i; |
621 |
|
622 |
if (fa->nfargs != 3 && fa->nfargs != 4) |
623 |
return(-1); |
624 |
newmat(id, NULL); |
625 |
if (fa->nfargs == 4) |
626 |
nrfr = fa->farg[3]; |
627 |
F = (1. - nrfr)/(1. + nrfr); /* use normal incidence */ |
628 |
F *= F; |
629 |
for (i = 0; i < 3; i++) { |
630 |
trgb[i] = fa->farg[i] * (1. - F)*(1. - F) / |
631 |
(1. - F*F*fa->farg[i]*fa->farg[i]); |
632 |
rrgb[i] = F * (1. + (1. - 2.*F)*fa->farg[i]) / |
633 |
(1. - F*F*fa->farg[i]*fa->farg[i]); |
634 |
} |
635 |
rgb_cie(cxyz, rrgb); /* put reflected component */ |
636 |
puts("\tc"); |
637 |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
638 |
if (d > FTINY) |
639 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
640 |
printf("\trs %.4f 0\n", cxyz[1]); |
641 |
rgb_cie(cxyz, trgb); /* put transmitted component */ |
642 |
puts("\tc"); |
643 |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
644 |
if (d > FTINY) |
645 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
646 |
printf("\tts %.4f 0\n", cxyz[1]); |
647 |
return(0); |
648 |
} |
649 |
|
650 |
|
651 |
int |
652 |
o_mirror(mod, typ, id, fa) /* convert a mirror material */ |
653 |
char *mod, *typ, *id; |
654 |
register FUNARGS *fa; |
655 |
{ |
656 |
COLOR cxyz, rrgb; |
657 |
double d; |
658 |
|
659 |
if (fa->nsargs == 1) { /* use alternate material */ |
660 |
newmat(id, fa->sarg[0]); |
661 |
return(0); |
662 |
} |
663 |
if (fa->nfargs != 3) |
664 |
return(-1); |
665 |
newmat(id, NULL); |
666 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2]; |
667 |
rgb_cie(cxyz, rrgb); |
668 |
puts("\tc"); /* put specular component */ |
669 |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
670 |
if (d > FTINY) |
671 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
672 |
printf("\trs %.4f 0\n", cxyz[1]); |
673 |
return(0); |
674 |
} |
675 |
|
676 |
|
677 |
int |
678 |
o_trans(mod, typ, id, fa) /* convert a trans material */ |
679 |
char *mod, *typ, *id; |
680 |
register FUNARGS *fa; |
681 |
{ |
682 |
COLOR cxyz, rrgb; |
683 |
double rough, trans, tspec, d; |
684 |
|
685 |
if (typ[4] == '2') { /* trans2 */ |
686 |
if (fa->nfargs != 8) |
687 |
return(-1); |
688 |
rough = .5*(fa->farg[4] + fa->farg[5]); |
689 |
trans = fa->farg[6]; |
690 |
tspec = fa->farg[7]; |
691 |
} else { /* trans */ |
692 |
if (fa->nfargs != 7) |
693 |
return(-1); |
694 |
rough = fa->farg[4]; |
695 |
trans = fa->farg[5]; |
696 |
tspec = fa->farg[6]; |
697 |
} |
698 |
newmat(id, NULL); |
699 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2]; |
700 |
rgb_cie(cxyz, rrgb); |
701 |
puts("\tc"); /* put transmitted diffuse */ |
702 |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
703 |
if (d > FTINY) |
704 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
705 |
printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec)); |
706 |
/* put transmitted specular */ |
707 |
printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough); |
708 |
/* put reflected diffuse */ |
709 |
printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans)); |
710 |
puts("\tc"); /* put reflected specular */ |
711 |
printf("\trs %.4f %.4f\n", fa->farg[3], rough); |
712 |
return(0); |
713 |
} |
714 |
|
715 |
|
716 |
int |
717 |
o_light(mod, typ, id, fa) /* convert a light type */ |
718 |
char *mod, *typ, *id; |
719 |
register FUNARGS *fa; |
720 |
{ |
721 |
COLOR cxyz, rrgb; |
722 |
double d; |
723 |
|
724 |
if (fa->nfargs < 3) |
725 |
return(-1); |
726 |
newmat(id, NULL); |
727 |
rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2]; |
728 |
rgb_cie(cxyz, rrgb); |
729 |
d = cxyz[0] + cxyz[1] + cxyz[2]; |
730 |
puts("\tc"); |
731 |
if (d > FTINY) |
732 |
printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); |
733 |
printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY)); |
734 |
return(0); |
735 |
} |