42 |
|
static C_COLOR cie_zf = C_CIEZ; |
43 |
|
|
44 |
|
static int setspectrum(); |
45 |
+ |
static int setbbtemp(); |
46 |
|
static void mixcolors(); |
47 |
|
|
48 |
|
|
106 |
|
case MG_E_CXY: /* assign CIE XY value */ |
107 |
|
if (ac != 3) |
108 |
|
return(MG_EARGC); |
109 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
109 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
110 |
|
return(MG_ETYPE); |
111 |
|
c_ccolor->cx = atof(av[1]); |
112 |
|
c_ccolor->cy = atof(av[2]); |
119 |
|
case MG_E_CSPEC: /* assign spectral values */ |
120 |
|
if (ac < 5) |
121 |
|
return(MG_EARGC); |
122 |
< |
if (!isint(av[1]) || !isint(av[2])) |
122 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
123 |
|
return(MG_ETYPE); |
124 |
< |
return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]), |
124 |
> |
return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]), |
125 |
|
ac-3, av+3)); |
126 |
+ |
case MG_E_CCT: /* assign black body spectrum */ |
127 |
+ |
if (ac != 2) |
128 |
+ |
return(MG_EARGC); |
129 |
+ |
if (!isflt(av[1])) |
130 |
+ |
return(MG_ETYPE); |
131 |
+ |
return(setbbtemp(c_ccolor, atof(av[1]))); |
132 |
|
case MG_E_CMIX: /* mix colors */ |
133 |
|
if (ac < 5 || (ac-1)%2) |
134 |
|
return(MG_EARGC); |
255 |
|
case MG_E_RS: /* set specular reflectance */ |
256 |
|
if (ac != 3) |
257 |
|
return(MG_EARGC); |
258 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
258 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
259 |
|
return(MG_ETYPE); |
260 |
|
c_cmaterial->rs = atof(av[1]); |
261 |
|
c_cmaterial->rs_a = atof(av[2]); |
268 |
|
case MG_E_TS: /* set specular transmittance */ |
269 |
|
if (ac != 3) |
270 |
|
return(MG_EARGC); |
271 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
271 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
272 |
|
return(MG_ETYPE); |
273 |
|
c_cmaterial->ts = atof(av[1]); |
274 |
|
c_cmaterial->ts_a = atof(av[2]); |
355 |
|
case MG_E_POINT: /* set point */ |
356 |
|
if (ac != 4) |
357 |
|
return(MG_EARGC); |
358 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
358 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
359 |
|
return(MG_ETYPE); |
360 |
|
c_cvertex->p[0] = atof(av[1]); |
361 |
|
c_cvertex->p[1] = atof(av[2]); |
365 |
|
case MG_E_NORMAL: /* set normal */ |
366 |
|
if (ac != 4) |
367 |
|
return(MG_EARGC); |
368 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
368 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
369 |
|
return(MG_ETYPE); |
370 |
|
c_cvertex->n[0] = atof(av[1]); |
371 |
|
c_cvertex->n[1] = atof(av[2]); |
452 |
|
double x, y, z; |
453 |
|
register int i; |
454 |
|
|
455 |
< |
if (clr->flags & fl) /* already done */ |
455 |
> |
fl &= ~clr->flags; /* ignore what's done */ |
456 |
> |
if (!fl) /* everything's done! */ |
457 |
|
return; |
458 |
|
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
459 |
|
*clr = c_dfcolor; |
460 |
< |
else if (fl & C_CSXY) { /* cspec -> cxy */ |
460 |
> |
if (fl & C_CSXY) { /* cspec -> cxy */ |
461 |
|
x = y = z = 0.; |
462 |
|
for (i = 0; i < C_CNSS; i++) { |
463 |
|
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
464 |
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
465 |
|
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
466 |
|
} |
467 |
+ |
x /= (double)cie_xf.ssum; |
468 |
+ |
y /= (double)cie_yf.ssum; |
469 |
+ |
z /= (double)cie_zf.ssum; |
470 |
|
z += x + y; |
471 |
|
clr->cx = x / z; |
472 |
|
clr->cy = y / z; |
473 |
|
clr->flags |= C_CSXY; |
474 |
< |
} else { /* cxy -> cspec */ |
474 |
> |
} else if (fl & C_CSSPEC) { /* cxy -> cspec */ |
475 |
|
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
476 |
|
x = clr->cx * z / cie_xf.ssum; |
477 |
|
y = clr->cy * z / cie_yf.ssum; |
481 |
|
clr->ssum += clr->ssamp[i] = |
482 |
|
x * cie_xf.ssamp[i] + |
483 |
|
y * cie_yf.ssamp[i] + |
484 |
< |
z * cie_zf.ssamp[i] ; |
484 |
> |
z * cie_zf.ssamp[i] + .5; |
485 |
|
clr->flags |= C_CSSPEC; |
486 |
|
} |
487 |
+ |
if (fl & C_CSEFF) { /* compute efficacy */ |
488 |
+ |
if (clr->flags & C_CDSPEC) { /* from spectrum */ |
489 |
+ |
y = 0.; |
490 |
+ |
for (i = 0; i < C_CNSS; i++) |
491 |
+ |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
492 |
+ |
clr->eff = C_CLPWM * y / clr->ssum; |
493 |
+ |
} else /* clr->flags & C_CDXY */ { /* from (x,y) */ |
494 |
+ |
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff + |
495 |
+ |
(1. - clr->cx - clr->cy)*cie_zf.eff; |
496 |
+ |
} |
497 |
+ |
clr->flags |= C_CSEFF; |
498 |
+ |
} |
499 |
|
} |
500 |
|
|
501 |
|
|
502 |
|
static int |
503 |
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
504 |
|
register C_COLOR *clr; |
505 |
< |
int wlmin, wlmax; |
505 |
> |
double wlmin, wlmax; |
506 |
|
int ac; |
507 |
|
char **av; |
508 |
|
{ |
509 |
|
double scale; |
510 |
< |
float *va; |
511 |
< |
register int i; |
512 |
< |
int wl, pos; |
510 |
> |
float va[C_CNSS]; |
511 |
> |
register int i, pos; |
512 |
> |
int n, imax; |
513 |
> |
int wl; |
514 |
|
double wl0, wlstep; |
515 |
< |
|
516 |
< |
if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL) |
515 |
> |
/* check bounds */ |
516 |
> |
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
517 |
|
return(MG_EILL); |
518 |
< |
if ((va = (float *)malloc(ac*sizeof(float))) == NULL) |
519 |
< |
return(MG_EMEM); |
518 |
> |
wlstep = (wlmax - wlmin)/(ac-1); |
519 |
> |
while (wlmin < C_CMINWL) { |
520 |
> |
wlmin += wlstep; |
521 |
> |
ac--; av++; |
522 |
> |
} |
523 |
> |
while (wlmax > C_CMAXWL) { |
524 |
> |
wlmax -= wlstep; |
525 |
> |
ac--; |
526 |
> |
} |
527 |
> |
if (ac < 2) |
528 |
> |
return(MG_EILL); |
529 |
> |
imax = ac; /* box filter if necessary */ |
530 |
> |
if (wlstep < C_CWLI) { |
531 |
> |
wlstep = C_CWLI; |
532 |
> |
imax = (wlmax - wlmin)/wlstep; |
533 |
> |
} |
534 |
|
scale = 0.; /* get values and maximum */ |
535 |
< |
for (i = 0; i < ac; i++) { |
536 |
< |
if (!isflt(av[i])) |
537 |
< |
return(MG_ETYPE); |
538 |
< |
va[i] = atof(av[i]); |
535 |
> |
pos = 0; |
536 |
> |
for (i = 0; i < imax; i++) { |
537 |
> |
va[i] = 0.; n = 0; |
538 |
> |
while (pos < (i+.5)*ac/imax) { |
539 |
> |
if (!isflt(av[pos])) |
540 |
> |
return(MG_ETYPE); |
541 |
> |
va[i] += atof(av[pos++]); |
542 |
> |
n++; |
543 |
> |
} |
544 |
> |
if (n > 1) |
545 |
> |
va[i] /= (double)n; |
546 |
|
if (va[i] < 0.) |
547 |
|
return(MG_EILL); |
548 |
|
if (va[i] > scale) |
553 |
|
scale = C_CMAXV / scale; |
554 |
|
clr->ssum = 0; /* convert to our spacing */ |
555 |
|
wl0 = wlmin; |
511 |
– |
wlstep = (double)(wlmax - wlmin)/(ac-1); |
556 |
|
pos = 0; |
557 |
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
558 |
< |
if (wl < wlmin || wl > wlmax) |
558 |
> |
if (wl < wlmin | wl > wlmax) |
559 |
|
clr->ssamp[i] = 0; |
560 |
|
else { |
561 |
|
while (wl0 + wlstep < wl+FTINY) { |
562 |
|
wl0 += wlstep; |
563 |
|
pos++; |
564 |
|
} |
565 |
< |
if (wl+FTINY >= wl0 && wl-FTINY <= wl0) |
566 |
< |
clr->ssamp[i] = scale*va[pos]; |
565 |
> |
if (wl+FTINY >= wl0 & wl-FTINY <= wl0) |
566 |
> |
clr->ssamp[i] = scale*va[pos] + .5; |
567 |
|
else /* interpolate if necessary */ |
568 |
< |
clr->ssamp[i] = scale / wlstep * |
568 |
> |
clr->ssamp[i] = .5 + scale / wlstep * |
569 |
|
( va[pos]*(wl0+wlstep - wl) + |
570 |
|
va[pos+1]*(wl - wl0) ); |
571 |
|
clr->ssum += clr->ssamp[i]; |
572 |
|
} |
573 |
|
clr->flags = C_CDSPEC|C_CSSPEC; |
574 |
|
clr->clock++; |
531 |
– |
free((MEM_PTR)va); |
575 |
|
return(MG_OK); |
576 |
|
} |
577 |
|
|
586 |
|
register int i; |
587 |
|
|
588 |
|
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
589 |
< |
c_ccvt(c1, C_CSSPEC); |
590 |
< |
c_ccvt(c2, C_CSSPEC); |
591 |
< |
w1 /= (double)c1->ssum; |
592 |
< |
w2 /= (double)c2->ssum; |
589 |
> |
c_ccvt(c1, C_CSSPEC|C_CSEFF); |
590 |
> |
c_ccvt(c2, C_CSSPEC|C_CSEFF); |
591 |
> |
w1 /= c1->eff*c1->ssum; |
592 |
> |
w2 /= c2->eff*c2->ssum; |
593 |
|
scale = 0.; |
594 |
|
for (i = 0; i < C_CNSS; i++) { |
595 |
|
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
610 |
|
cres->flags = C_CDXY|C_CSXY; |
611 |
|
} |
612 |
|
} |
613 |
+ |
|
614 |
+ |
|
615 |
+ |
#define C1 3.741832e-16 /* W-m^2 */ |
616 |
+ |
#define C2 1.4388e-2 /* m-K */ |
617 |
+ |
|
618 |
+ |
#define bbsp(l,t) (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.))) |
619 |
+ |
#define bblm(t) (C2/5./(t)) |
620 |
+ |
|
621 |
+ |
static int |
622 |
+ |
setbbtemp(clr, tk) /* set black body spectrum */ |
623 |
+ |
register C_COLOR *clr; |
624 |
+ |
double tk; |
625 |
+ |
{ |
626 |
+ |
double sf, wl; |
627 |
+ |
register int i; |
628 |
+ |
|
629 |
+ |
if (tk < 1000) |
630 |
+ |
return(MG_EILL); |
631 |
+ |
wl = bblm(tk); /* scalefactor based on peak */ |
632 |
+ |
if (wl < C_CMINWL*1e-9) |
633 |
+ |
wl = C_CMINWL*1e-9; |
634 |
+ |
else if (wl > C_CMAXWL*1e-9) |
635 |
+ |
wl = C_CMAXWL*1e-9; |
636 |
+ |
sf = C_CMAXV/bbsp(wl,tk); |
637 |
+ |
clr->ssum = 0; |
638 |
+ |
for (i = 0; i < C_CNSS; i++) { |
639 |
+ |
wl = (C_CMINWL + i*C_CWLI)*1e-9; |
640 |
+ |
clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5; |
641 |
+ |
} |
642 |
+ |
clr->flags = C_CDSPEC|C_CSSPEC; |
643 |
+ |
clr->clock++; |
644 |
+ |
return(MG_OK); |
645 |
+ |
} |
646 |
+ |
|
647 |
+ |
#undef C1 |
648 |
+ |
#undef C2 |
649 |
+ |
#undef bbsp |
650 |
+ |
#undef bblm |