--- ray/src/rt/m_brdf.c 1991/05/08 08:27:50 1.7 +++ ray/src/rt/m_brdf.c 1991/08/12 08:20:53 1.16 @@ -122,7 +122,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 */ @@ -191,8 +191,7 @@ register RAY *r; BRDFDAT nd; double transtest, transdist; COLOR ctmp; - double dtmp; - FVECT vec; + double dtmp, tspect, rspecr; register int i; /* check arguments */ switch (m->otype) { @@ -237,7 +236,6 @@ 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 @@ -248,46 +246,39 @@ register RAY *r; break; if (i-3 != nd.dp->nd) objerror(m, USER, "dimension error"); - if (!fundefined(m->oargs.sarg[3])) - loadfunc(m->oargs.sarg[2]); + funcfile(m->oargs.sarg[2]); } else if (m->otype == MAT_BRTDF) { nd.dp = NULL; - if (!fundefined(m->oargs.sarg[7])) - loadfunc(m->oargs.sarg[9]); + funcfile(m->oargs.sarg[9]); } else { nd.dp = NULL; - if (!fundefined(m->oargs.sarg[0])) - loadfunc(m->oargs.sarg[1]); + funcfile(m->oargs.sarg[1]); } /* 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, varvalue(m->oargs.sarg[3]), + varvalue(m->oargs.sarg[4]), + varvalue(m->oargs.sarg[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 +289,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, varvalue(m->oargs.sarg[0]), + varvalue(m->oargs.sarg[1]), + varvalue(m->oargs.sarg[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 +309,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 +328,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); }