--- ray/src/rt/m_brdf.c 1991/05/08 08:27:50 1.7 +++ ray/src/rt/m_brdf.c 1991/11/25 09:51:03 2.2 @@ -14,6 +14,8 @@ static char SCCSid[] = "$SunId$ LBL"; #include "otypes.h" +#include "func.h" + /* * Arguments to this material include the color and specularity. * String arguments include the reflection function and files. @@ -58,9 +60,6 @@ static char SCCSid[] = "$SunId$ LBL"; * CrP, CgP, CbP - perturbed material color */ -extern double funvalue(), varvalue(); -extern XF funcxf; - typedef struct { OBJREC *mp; /* material pointer */ RAY *pr; /* intersected ray */ @@ -122,7 +121,7 @@ double omega; /* light source size */ if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) return; /* no specular component */ /* set up function */ - setfunc(np->mp, np->pr); + setbrdfunc(np); sa = np->mp->oargs.sarg; errno = 0; /* transform light vector */ @@ -153,8 +152,10 @@ double omega; /* light source size */ dtmp = funvalue(sa[0], 1, &dtmp); setcolor(ctmp, dtmp, dtmp, dtmp); } - if (errno) - goto computerr; + if (errno) { + objerror(np->mp, WARNING, "compute error"); + return; + } if (dtmp <= FTINY) return; if (ldot > 0.0) { @@ -176,10 +177,6 @@ double omega; /* light source size */ scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } - return; -computerr: - objerror(np->mp, WARNING, "compute error"); - return; } @@ -191,8 +188,8 @@ register RAY *r; BRDFDAT nd; double transtest, transdist; COLOR ctmp; - double dtmp; - FVECT vec; + double dtmp, tspect, rspecr; + MFUNC *mf; register int i; /* check arguments */ switch (m->otype) { @@ -237,57 +234,44 @@ register RAY *r; raytexture(r, m->omod); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ multcolor(nd.mcolor, r->pcol); /* modify material color */ - r->rt = r->rot; /* default ray length */ transtest = 0; /* load auxiliary files */ - if (m->otype == MAT_PDATA || m->otype == MAT_MDATA - || m->otype == MAT_TDATA) { + if (hasdata(m->otype)) { nd.dp = getdata(m->oargs.sarg[1]); - for (i = 3; i < m->oargs.nsargs; i++) - if (m->oargs.sarg[i][0] == '-') - break; - if (i-3 != nd.dp->nd) - objerror(m, USER, "dimension error"); - if (!fundefined(m->oargs.sarg[3])) - loadfunc(m->oargs.sarg[2]); + i = (1 << nd.dp->nd) - 1; + mf = getfunc(m, 2, i<<3, 0); } else if (m->otype == MAT_BRTDF) { nd.dp = NULL; - if (!fundefined(m->oargs.sarg[7])) - loadfunc(m->oargs.sarg[9]); + mf = getfunc(m, 9, 0x3f, 0); } else { nd.dp = NULL; - if (!fundefined(m->oargs.sarg[0])) - loadfunc(m->oargs.sarg[1]); + mf = getfunc(m, 1, 0, 0); } /* set special variables */ - setfunc(m, r); - multv3(vec, nd.pnorm, funcxf.xfm); - varset("NxP", '=', vec[0]/funcxf.sca); - varset("NyP", '=', vec[1]/funcxf.sca); - varset("NzP", '=', vec[2]/funcxf.sca); - varset("RdotP", '=', nd.pdot); - varset("CrP", '=', colval(nd.mcolor,RED)); - varset("CgP", '=', colval(nd.mcolor,GRN)); - varset("CbP", '=', colval(nd.mcolor,BLU)); + setbrdfunc(&nd); /* compute transmitted ray */ + tspect = 0.; if (m->otype == MAT_BRTDF && nd.tspec > FTINY) { RAY sr; errno = 0; - setcolor(ctmp, varvalue(m->oargs.sarg[0]), - varvalue(m->oargs.sarg[1]), - varvalue(m->oargs.sarg[2])); - scalecolor(ctmp, nd.tspec); + setcolor(ctmp, evalue(mf->ep[3]), + evalue(mf->ep[4]), + evalue(mf->ep[5])); + scalecolor(ctmp, nd.trans); if (errno) objerror(m, WARNING, "compute error"); - else if ((dtmp = bright(ctmp)) > FTINY && - rayorigin(&sr, r, TRANS, dtmp) == 0) { - if (DOT(r->pert,r->pert) > FTINY*FTINY) { + else if ((tspect = bright(ctmp)) > FTINY && + rayorigin(&sr, r, TRANS, tspect) == 0) { + if (!(r->crtype & SHADOW) && + DOT(r->pert,r->pert) > FTINY*FTINY) { for (i = 0; i < 3; i++) /* perturb direction */ sr.rdir[i] = r->rdir[i] - .75*r->pert[i]; normalize(sr.rdir); - } else + } else { + VCOPY(sr.rdir, r->rdir); transtest = 2; + } rayvalue(&sr); multcolor(sr.rcol, ctmp); addcolor(r->rcol, sr.rcol); @@ -298,17 +282,17 @@ register RAY *r; if (r->crtype & SHADOW) /* the rest is shadow */ return; /* compute reflected ray */ + rspecr = 0.; if (m->otype == MAT_BRTDF && nd.rspec > FTINY) { RAY sr; errno = 0; - setcolor(ctmp, varvalue(m->oargs.sarg[3]), - varvalue(m->oargs.sarg[4]), - varvalue(m->oargs.sarg[5])); - scalecolor(ctmp, nd.rspec); + setcolor(ctmp, evalue(mf->ep[0]), + evalue(mf->ep[1]), + evalue(mf->ep[2])); if (errno) objerror(m, WARNING, "compute error"); - else if ((dtmp = bright(ctmp)) > FTINY && - rayorigin(&sr, r, REFLECTED, dtmp) == 0) { + else if ((rspecr = bright(ctmp)) > FTINY && + rayorigin(&sr, r, REFLECTED, rspecr) == 0) { for (i = 0; i < 3; i++) sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; @@ -318,22 +302,16 @@ register RAY *r; } } /* compute ambient */ - if (nd.rdiff > FTINY) { + if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) { ambient(ctmp, r); - if (m->otype == MAT_BRTDF) - scalecolor(ctmp, nd.rdiff); - else - scalecolor(ctmp, 1.0-nd.trans); + scalecolor(ctmp, dtmp); multcolor(ctmp, nd.mcolor); /* modified by material color */ addcolor(r->rcol, ctmp); /* add to returned color */ } - if (nd.tdiff > FTINY) { /* from other side */ + if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */ flipsurface(r); ambient(ctmp, r); - if (m->otype == MAT_BRTDF) - scalecolor(ctmp, nd.tdiff); - else - scalecolor(ctmp, nd.trans); + scalecolor(ctmp, dtmp); multcolor(ctmp, nd.mcolor); addcolor(r->rcol, ctmp); flipsurface(r); @@ -343,4 +321,25 @@ register RAY *r; /* check distance */ if (transtest > bright(r->rcol)) r->rt = transdist; +} + + +setbrdfunc(np) /* set up brdf function and variables */ +register BRDFDAT *np; +{ + FVECT vec; + + if (setfunc(np->mp, np->pr) == 0) + return(0); /* it's OK, setfunc says we're done */ + /* else (re)assign special variables */ + multv3(vec, np->pnorm, funcxf.xfm); + varset("NxP", '=', vec[0]/funcxf.sca); + varset("NyP", '=', vec[1]/funcxf.sca); + varset("NzP", '=', vec[2]/funcxf.sca); + varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 : + np->pdot >= 1.0 ? 1.0 : np->pdot); + varset("CrP", '=', colval(np->mcolor,RED)); + varset("CgP", '=', colval(np->mcolor,GRN)); + varset("CbP", '=', colval(np->mcolor,BLU)); + return(1); }