--- ray/src/rt/normal.c 1994/01/12 16:46:45 2.27 +++ ray/src/rt/normal.c 1995/11/22 09:27:55 2.32 @@ -1,4 +1,4 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -23,6 +23,8 @@ static char SCCSid[] = "$SunId$ LBL"; extern double specthresh; /* specular sampling threshold */ extern double specjitter; /* specular sampling jitter */ +extern int backvis; /* back faces visible? */ + static gaussamp(); /* @@ -154,6 +156,9 @@ register RAY *r; { NORMDAT nd; double transtest, transdist; + double mirtest, mirdist; + int hastexture; + double d; COLOR ctmp; register int i; /* easy shadow test */ @@ -162,6 +167,14 @@ register RAY *r; if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) objerror(m, USER, "bad number of arguments"); + /* check for back side */ + if (r->rod < 0.0) { + if (!backvis && m->otype != MAT_TRANS) { + raytrans(r); + return(1); + } + flipsurface(r); /* reorient if backvis */ + } nd.mp = m; nd.rp = r; /* get material color */ @@ -173,46 +186,22 @@ register RAY *r; nd.alpha2 = m->oargs.farg[4]; if ((nd.alpha2 *= nd.alpha2) <= FTINY) nd.specfl |= SP_PURE; - /* reorient if necessary */ - if (r->rod < 0.0) - flipsurface(r); + if (r->ro != NULL && isflat(r->ro->otype)) + nd.specfl |= SP_FLAT; /* get modifiers */ raytexture(r, m->omod); - nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ + if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) + nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ + else { + VCOPY(nd.pnorm, r->ron); + nd.pdot = r->rod; + } if (nd.pdot < .001) nd.pdot = .001; /* non-zero for dirnorm() */ multcolor(nd.mcolor, r->pcol); /* modify material color */ - transtest = 0; - transdist = r->rot; - /* get specular component */ - if ((nd.rspec = m->oargs.farg[3]) > FTINY) { - nd.specfl |= SP_REFL; - /* compute specular color */ - if (m->otype == MAT_METAL) - copycolor(nd.scolor, nd.mcolor); - else - setcolor(nd.scolor, 1.0, 1.0, 1.0); - scalecolor(nd.scolor, nd.rspec); - /* check threshold */ - if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) - nd.specfl |= SP_RBLT; - /* compute reflected ray */ - for (i = 0; i < 3; i++) - nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; - if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ - for (i = 0; i < 3; i++) /* safety measure */ - nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; - - if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { - RAY lr; - if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { - VCOPY(lr.rdir, nd.vrefl); - rayvalue(&lr); - multcolor(lr.rcol, nd.scolor); - addcolor(r->rcol, lr.rcol); - } - } - } + mirtest = transtest = 0; + mirdist = transdist = r->rot; + nd.rspec = m->oargs.farg[3]; /* compute transmission */ if (m->otype == MAT_TRANS) { nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); @@ -224,8 +213,7 @@ register RAY *r; if (!(nd.specfl & SP_PURE) && specthresh >= nd.tspec-FTINY) nd.specfl |= SP_TBLT; - if (r->crtype & SHADOW || - DOT(r->pert,r->pert) <= FTINY*FTINY) { + if (!hastexture || r->crtype & SHADOW) { VCOPY(nd.prdir, r->rdir); transtest = 2; } else { @@ -254,23 +242,55 @@ register RAY *r; } else transtest = 0; - if (r->crtype & SHADOW) /* the rest is shadow */ + if (r->crtype & SHADOW) { /* the rest is shadow */ + r->rt = transdist; return(1); + } + /* get specular reflection */ + if (nd.rspec > FTINY) { + nd.specfl |= SP_REFL; + /* compute specular color */ + if (m->otype == MAT_METAL) + copycolor(nd.scolor, nd.mcolor); + else + setcolor(nd.scolor, 1.0, 1.0, 1.0); + scalecolor(nd.scolor, nd.rspec); + /* check threshold */ + if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) + nd.specfl |= SP_RBLT; + /* compute reflected ray */ + for (i = 0; i < 3; i++) + nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; + /* penetration? */ + if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) + for (i = 0; i < 3; i++) /* safety measure */ + nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; + + if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { + RAY lr; + if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { + VCOPY(lr.rdir, nd.vrefl); + rayvalue(&lr); + multcolor(lr.rcol, nd.scolor); + addcolor(r->rcol, lr.rcol); + if (!hastexture && nd.specfl & SP_FLAT) { + mirtest = 2.*bright(lr.rcol); + mirdist = r->rot + lr.rt; + } + } + } + } /* diffuse reflection */ nd.rdiff = 1.0 - nd.trans - nd.rspec; if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) return(1); /* 100% pure specular */ - if (r->ro != NULL && (r->ro->otype == OBJ_FACE || - r->ro->otype == OBJ_RING)) - nd.specfl |= SP_FLAT; - if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) gaussamp(r, &nd); if (nd.rdiff > FTINY) { /* ambient from this side */ - ambient(ctmp, r); + ambient(ctmp, r, hastexture?nd.pnorm:r->ron); if (nd.specfl & SP_RBLT) scalecolor(ctmp, 1.0-nd.trans); else @@ -280,7 +300,14 @@ register RAY *r; } if (nd.tdiff > FTINY) { /* ambient from other side */ flipsurface(r); - ambient(ctmp, r); + if (hastexture) { + FVECT bnorm; + bnorm[0] = -nd.pnorm[0]; + bnorm[1] = -nd.pnorm[1]; + bnorm[2] = -nd.pnorm[2]; + ambient(ctmp, r, bnorm); + } else + ambient(ctmp, r, r->ron); if (nd.specfl & SP_TBLT) scalecolor(ctmp, nd.trans); else @@ -292,8 +319,11 @@ register RAY *r; /* add direct component */ direct(r, dirnorm, &nd); /* check distance */ - if (transtest > bright(r->rcol)) + d = bright(r->rcol); + if (transtest > d) r->rt = transdist; + else if (mirtest > d) + r->rt = mirdist; return(1); }