--- ray/src/rt/m_brdf.c 1993/11/18 09:43:04 2.9 +++ ray/src/rt/m_brdf.c 2004/03/30 16:13:01 2.21 @@ -1,19 +1,18 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: m_brdf.c,v 2.21 2004/03/30 16:13:01 schorsch Exp $"; #endif - /* * Shading for materials with arbitrary BRDF's */ -#include "ray.h" +#include "copyright.h" +#include "ray.h" +#include "ambient.h" #include "data.h" - +#include "source.h" #include "otypes.h" - +#include "rtotypes.h" #include "func.h" /* @@ -78,20 +77,27 @@ typedef struct { } BRDFDAT; /* BRDF material data */ -dirbrdf(cval, np, ldir, omega) /* compute source contribution */ -COLOR cval; /* returned coefficient */ -register BRDFDAT *np; /* material data */ -FVECT ldir; /* light source direction */ -double omega; /* light source size */ +static srcdirf_t dirbrdf; +static int setbrdfunc(BRDFDAT *np); + + +static void +dirbrdf( /* compute source contribution */ + COLOR cval, /* returned coefficient */ + void *nnp, /* material data */ + FVECT ldir, /* light source direction */ + double omega /* light source size */ +) { + register BRDFDAT *np = nnp; double ldot; double dtmp; COLOR ctmp; FVECT ldx; - double lddx[3], pt[MAXDIM]; - double vldx[4]; + static double vldx[5], pt[MAXDIM]; register char **sa; register int i; +#define lddx (vldx+1) setcolor(cval, 0.0, 0.0, 0.0); @@ -132,35 +138,35 @@ double omega; /* light source size */ multv3(ldx, ldir, funcxf.xfm); for (i = 0; i < 3; i++) lddx[i] = ldx[i]/funcxf.sca; + lddx[3] = omega; /* compute BRTDF */ if (np->mp->otype == MAT_BRTDF) { if (sa[6][0] == '0') /* special case */ colval(ctmp,RED) = 0.0; else - colval(ctmp,RED) = funvalue(sa[6], 3, lddx); + colval(ctmp,RED) = funvalue(sa[6], 4, lddx); if (!strcmp(sa[7],sa[6])) colval(ctmp,GRN) = colval(ctmp,RED); else - colval(ctmp,GRN) = funvalue(sa[7], 3, lddx); + colval(ctmp,GRN) = funvalue(sa[7], 4, lddx); if (!strcmp(sa[8],sa[6])) colval(ctmp,BLU) = colval(ctmp,RED); else if (!strcmp(sa[8],sa[7])) colval(ctmp,BLU) = colval(ctmp,GRN); else - colval(ctmp,BLU) = funvalue(sa[8], 3, lddx); + colval(ctmp,BLU) = funvalue(sa[8], 4, lddx); dtmp = bright(ctmp); } else if (np->dp == NULL) { - dtmp = funvalue(sa[0], 3, lddx); + dtmp = funvalue(sa[0], 4, lddx); setcolor(ctmp, dtmp, dtmp, dtmp); } else { for (i = 0; i < np->dp->nd; i++) - pt[i] = funvalue(sa[3+i], 3, lddx); + pt[i] = funvalue(sa[3+i], 4, lddx); vldx[0] = datavalue(np->dp, pt); - vldx[1] = lddx[0]; vldx[2] = lddx[1]; vldx[3] = lddx[2]; - dtmp = funvalue(sa[0], 4, vldx); + dtmp = funvalue(sa[0], 5, vldx); setcolor(ctmp, dtmp, dtmp, dtmp); } - if (errno) { + if (errno == EDOM || errno == ERANGE) { objerror(np->mp, WARNING, "compute error"); return; } @@ -170,7 +176,7 @@ double omega; /* light source size */ /* * Compute reflected non-diffuse component. */ - if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA) + if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA)) multcolor(ctmp, np->mcolor); dtmp = ldot * omega * np->rspec; scalecolor(ctmp, dtmp); @@ -179,28 +185,33 @@ double omega; /* light source size */ /* * Compute transmitted non-diffuse component. */ - if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA) + if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA)) multcolor(ctmp, np->mcolor); dtmp = -ldot * omega * np->tspec; scalecolor(ctmp, dtmp); addcolor(cval, ctmp); } +#undef lddx } -m_brdf(m, r) /* color a ray which hit a BRDTF material */ -register OBJREC *m; -register RAY *r; +extern int +m_brdf( /* color a ray that hit a BRDTfunc material */ + register OBJREC *m, + register RAY *r +) { + int hitfront = 1; BRDFDAT nd; RAY sr; double transtest, transdist; int hasrefl, hastrans; COLOR ctmp; + FVECT vtmp; register MFUNC *mf; register int i; /* check arguments */ - if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9) + if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9)) objerror(m, USER, "bad # arguments"); nd.mp = m; nd.pr = r; @@ -220,7 +231,7 @@ register RAY *r; setcolor(nd.tdiff, m->oargs.farg[6], m->oargs.farg[7], m->oargs.farg[8]); - /* get modifiers */ + /* get modifiers */ raytexture(r, m->omod); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ if (r->rod < 0.0) { /* orient perturbed values */ @@ -229,6 +240,7 @@ register RAY *r; nd.pnorm[i] = -nd.pnorm[i]; r->pert[i] = -r->pert[i]; } + hitfront = 0; } copycolor(nd.mcolor, r->pcol); /* get pattern color */ multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */ @@ -246,7 +258,7 @@ register RAY *r; setcolor(ctmp, evalue(mf->ep[3]), evalue(mf->ep[4]), evalue(mf->ep[5])); - if (errno) + if (errno == EDOM || errno == ERANGE) objerror(m, WARNING, "compute error"); else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) { if (!(r->crtype & SHADOW) && @@ -268,14 +280,14 @@ register RAY *r; transdist = r->rot + sr.rt; } if (r->crtype & SHADOW) /* the rest is shadow */ - return; + return(1); /* compute reflected ray */ setbrdfunc(&nd); errno = 0; setcolor(ctmp, evalue(mf->ep[0]), evalue(mf->ep[1]), evalue(mf->ep[2])); - if (errno) + if (errno == EDOM || errno == ERANGE) objerror(m, WARNING, "compute error"); else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) { for (i = 0; i < 3; i++) @@ -286,21 +298,24 @@ register RAY *r; } /* compute ambient */ if (hasrefl) { - if (nd.pdot < 0.0) + if (!hitfront) flipsurface(r); - ambient(ctmp, r); + ambient(ctmp, r, nd.pnorm); multcolor(ctmp, nd.rdiff); addcolor(r->rcol, ctmp); /* add to returned color */ - if (nd.pdot < 0.0) + if (!hitfront) flipsurface(r); } if (hastrans) { /* from other side */ - if (nd.pdot > 0.0) + if (hitfront) flipsurface(r); - ambient(ctmp, r); + vtmp[0] = -nd.pnorm[0]; + vtmp[1] = -nd.pnorm[1]; + vtmp[2] = -nd.pnorm[2]; + ambient(ctmp, r, vtmp); multcolor(ctmp, nd.tdiff); addcolor(r->rcol, ctmp); - if (nd.pdot > 0.0) + if (hitfront) flipsurface(r); } if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') @@ -308,24 +323,41 @@ register RAY *r; /* check distance */ if (transtest > bright(r->rcol)) r->rt = transdist; + + return(1); } -m_brdf2(m, r) /* color a ray which hit a BRDF material */ -register OBJREC *m; -register RAY *r; +extern int +m_brdf2( /* color a ray that hit a BRDF material */ + register OBJREC *m, + register RAY *r +) { BRDFDAT nd; COLOR ctmp; + FVECT vtmp; double dtmp; /* always a shadow */ if (r->crtype & SHADOW) - return; + return(1); /* check arguments */ - if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs < - (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4)) + if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < + ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) objerror(m, USER, "bad # arguments"); + /* check for back side */ + if (r->rod < 0.0) { + if (!backvis && m->otype != MAT_TFUNC + && m->otype != MAT_TDATA) { + raytrans(r); + return(1); + } + raytexture(r, m->omod); + flipsurface(r); /* reorient if backvis */ + } else + raytexture(r, m->omod); + nd.mp = m; nd.pr = r; /* get material color */ @@ -335,7 +367,7 @@ register RAY *r; /* get specular component */ nd.rspec = m->oargs.farg[3]; /* compute transmittance */ - if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) { + if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) { nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); nd.tspec = nd.trans * m->oargs.farg[5]; dtmp = nd.trans - nd.tspec; @@ -347,11 +379,6 @@ register RAY *r; /* compute reflectance */ dtmp = 1.0 - nd.trans - nd.rspec; setcolor(nd.rdiff, dtmp, dtmp, dtmp); - /* fix orientation */ - if (r->rod < 0.0) - flipsurface(r); - /* get modifiers */ - raytexture(r, m->omod); nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ multcolor(nd.mcolor, r->pcol); /* modify material color */ multcolor(nd.rdiff, nd.mcolor); @@ -366,14 +393,17 @@ register RAY *r; } /* compute ambient */ if (nd.trans < 1.0-FTINY) { - ambient(ctmp, r); + ambient(ctmp, r, nd.pnorm); scalecolor(ctmp, 1.0-nd.trans); multcolor(ctmp, nd.mcolor); /* modified by material color */ addcolor(r->rcol, ctmp); /* add to returned color */ } if (nd.trans > FTINY) { /* from other side */ flipsurface(r); - ambient(ctmp, r); + vtmp[0] = -nd.pnorm[0]; + vtmp[1] = -nd.pnorm[1]; + vtmp[2] = -nd.pnorm[2]; + ambient(ctmp, r, vtmp); scalecolor(ctmp, nd.trans); multcolor(ctmp, nd.mcolor); addcolor(r->rcol, ctmp); @@ -381,11 +411,15 @@ register RAY *r; } /* add direct component */ direct(r, dirbrdf, &nd); + + return(1); } -setbrdfunc(np) /* set up brdf function and variables */ -register BRDFDAT *np; +static int +setbrdfunc( /* set up brdf function and variables */ + register BRDFDAT *np +) { FVECT vec;