| 1 | 
greg | 
2.30 | 
/* Copyright (c) 1995 Regents of the University of California */ | 
| 2 | 
greg | 
1.1 | 
 | 
| 3 | 
  | 
  | 
#ifndef lint | 
| 4 | 
  | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 5 | 
  | 
  | 
#endif | 
| 6 | 
  | 
  | 
 | 
| 7 | 
  | 
  | 
/* | 
| 8 | 
  | 
  | 
 *  normal.c - shading function for normal materials. | 
| 9 | 
  | 
  | 
 * | 
| 10 | 
  | 
  | 
 *     8/19/85 | 
| 11 | 
  | 
  | 
 *     12/19/85 - added stuff for metals. | 
| 12 | 
  | 
  | 
 *     6/26/87 - improved specular model. | 
| 13 | 
  | 
  | 
 *     9/28/87 - added model for translucent materials. | 
| 14 | 
greg | 
2.2 | 
 *     Later changes described in delta comments. | 
| 15 | 
greg | 
1.1 | 
 */ | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
#include  "ray.h" | 
| 18 | 
  | 
  | 
 | 
| 19 | 
  | 
  | 
#include  "otypes.h" | 
| 20 | 
  | 
  | 
 | 
| 21 | 
greg | 
2.2 | 
#include  "random.h" | 
| 22 | 
  | 
  | 
 | 
| 23 | 
greg | 
2.5 | 
extern double  specthresh;              /* specular sampling threshold */ | 
| 24 | 
  | 
  | 
extern double  specjitter;              /* specular sampling jitter */ | 
| 25 | 
  | 
  | 
 | 
| 26 | 
greg | 
2.28 | 
extern int  backvis;                    /* back faces visible? */ | 
| 27 | 
  | 
  | 
 | 
| 28 | 
greg | 
2.24 | 
static  gaussamp(); | 
| 29 | 
  | 
  | 
 | 
| 30 | 
greg | 
1.1 | 
/* | 
| 31 | 
greg | 
2.22 | 
 *      This routine implements the isotropic Gaussian | 
| 32 | 
  | 
  | 
 *  model described by Ward in Siggraph `92 article. | 
| 33 | 
greg | 
1.1 | 
 *      We orient the surface towards the incoming ray, so a single | 
| 34 | 
  | 
  | 
 *  surface can be used to represent an infinitely thin object. | 
| 35 | 
  | 
  | 
 * | 
| 36 | 
  | 
  | 
 *  Arguments for MAT_PLASTIC and MAT_METAL are: | 
| 37 | 
  | 
  | 
 *      red     grn     blu     specular-frac.  facet-slope | 
| 38 | 
  | 
  | 
 * | 
| 39 | 
  | 
  | 
 *  Arguments for MAT_TRANS are: | 
| 40 | 
  | 
  | 
 *      red     grn     blu     rspec   rough   trans   tspec | 
| 41 | 
  | 
  | 
 */ | 
| 42 | 
  | 
  | 
 | 
| 43 | 
greg | 
2.2 | 
                                /* specularity flags */ | 
| 44 | 
  | 
  | 
#define  SP_REFL        01              /* has reflected specular component */ | 
| 45 | 
  | 
  | 
#define  SP_TRAN        02              /* has transmitted specular */ | 
| 46 | 
greg | 
2.11 | 
#define  SP_PURE        04              /* purely specular (zero roughness) */ | 
| 47 | 
  | 
  | 
#define  SP_FLAT        010             /* flat reflecting surface */ | 
| 48 | 
  | 
  | 
#define  SP_RBLT        020             /* reflection below sample threshold */ | 
| 49 | 
  | 
  | 
#define  SP_TBLT        040             /* transmission below threshold */ | 
| 50 | 
greg | 
1.1 | 
 | 
| 51 | 
greg | 
1.3 | 
typedef struct { | 
| 52 | 
  | 
  | 
        OBJREC  *mp;            /* material pointer */ | 
| 53 | 
greg | 
2.16 | 
        RAY  *rp;               /* ray pointer */ | 
| 54 | 
greg | 
2.2 | 
        short  specfl;          /* specularity flags, defined above */ | 
| 55 | 
greg | 
1.1 | 
        COLOR  mcolor;          /* color of this material */ | 
| 56 | 
  | 
  | 
        COLOR  scolor;          /* color of specular component */ | 
| 57 | 
  | 
  | 
        FVECT  vrefl;           /* vector in direction of reflected ray */ | 
| 58 | 
greg | 
1.14 | 
        FVECT  prdir;           /* vector in transmitted direction */ | 
| 59 | 
greg | 
2.2 | 
        double  alpha2;         /* roughness squared */ | 
| 60 | 
greg | 
1.1 | 
        double  rdiff, rspec;   /* reflected specular, diffuse */ | 
| 61 | 
  | 
  | 
        double  trans;          /* transmissivity */ | 
| 62 | 
  | 
  | 
        double  tdiff, tspec;   /* transmitted specular, diffuse */ | 
| 63 | 
  | 
  | 
        FVECT  pnorm;           /* perturbed surface normal */ | 
| 64 | 
  | 
  | 
        double  pdot;           /* perturbed dot product */ | 
| 65 | 
greg | 
1.3 | 
}  NORMDAT;             /* normal material data */ | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
 | 
| 68 | 
  | 
  | 
dirnorm(cval, np, ldir, omega)          /* compute source contribution */ | 
| 69 | 
  | 
  | 
COLOR  cval;                    /* returned coefficient */ | 
| 70 | 
  | 
  | 
register NORMDAT  *np;          /* material data */ | 
| 71 | 
  | 
  | 
FVECT  ldir;                    /* light source direction */ | 
| 72 | 
  | 
  | 
double  omega;                  /* light source size */ | 
| 73 | 
  | 
  | 
{ | 
| 74 | 
greg | 
1.1 | 
        double  ldot; | 
| 75 | 
greg | 
2.16 | 
        double  dtmp, d2; | 
| 76 | 
  | 
  | 
        FVECT  vtmp; | 
| 77 | 
greg | 
1.3 | 
        COLOR  ctmp; | 
| 78 | 
  | 
  | 
 | 
| 79 | 
  | 
  | 
        setcolor(cval, 0.0, 0.0, 0.0); | 
| 80 | 
  | 
  | 
 | 
| 81 | 
  | 
  | 
        ldot = DOT(np->pnorm, ldir); | 
| 82 | 
  | 
  | 
 | 
| 83 | 
  | 
  | 
        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) | 
| 84 | 
  | 
  | 
                return;         /* wrong side */ | 
| 85 | 
  | 
  | 
 | 
| 86 | 
  | 
  | 
        if (ldot > FTINY && np->rdiff > FTINY) { | 
| 87 | 
  | 
  | 
                /* | 
| 88 | 
greg | 
1.4 | 
                 *  Compute and add diffuse reflected component to returned | 
| 89 | 
  | 
  | 
                 *  color.  The diffuse reflected component will always be | 
| 90 | 
  | 
  | 
                 *  modified by the color of the material. | 
| 91 | 
greg | 
1.3 | 
                 */ | 
| 92 | 
  | 
  | 
                copycolor(ctmp, np->mcolor); | 
| 93 | 
  | 
  | 
                dtmp = ldot * omega * np->rdiff / PI; | 
| 94 | 
  | 
  | 
                scalecolor(ctmp, dtmp); | 
| 95 | 
  | 
  | 
                addcolor(cval, ctmp); | 
| 96 | 
  | 
  | 
        } | 
| 97 | 
greg | 
2.2 | 
        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { | 
| 98 | 
greg | 
1.3 | 
                /* | 
| 99 | 
  | 
  | 
                 *  Compute specular reflection coefficient using | 
| 100 | 
  | 
  | 
                 *  gaussian distribution model. | 
| 101 | 
  | 
  | 
                 */ | 
| 102 | 
greg | 
2.3 | 
                                                /* roughness */ | 
| 103 | 
greg | 
2.16 | 
                dtmp = np->alpha2; | 
| 104 | 
greg | 
2.3 | 
                                                /* + source if flat */ | 
| 105 | 
  | 
  | 
                if (np->specfl & SP_FLAT) | 
| 106 | 
greg | 
2.16 | 
                        dtmp += omega/(4.0*PI); | 
| 107 | 
greg | 
2.23 | 
                                                /* half vector */ | 
| 108 | 
greg | 
2.18 | 
                vtmp[0] = ldir[0] - np->rp->rdir[0]; | 
| 109 | 
  | 
  | 
                vtmp[1] = ldir[1] - np->rp->rdir[1]; | 
| 110 | 
  | 
  | 
                vtmp[2] = ldir[2] - np->rp->rdir[2]; | 
| 111 | 
greg | 
2.16 | 
                d2 = DOT(vtmp, np->pnorm); | 
| 112 | 
greg | 
2.23 | 
                d2 *= d2; | 
| 113 | 
  | 
  | 
                d2 = (DOT(vtmp,vtmp) - d2) / d2; | 
| 114 | 
greg | 
1.3 | 
                                                /* gaussian */ | 
| 115 | 
greg | 
2.16 | 
                dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); | 
| 116 | 
greg | 
1.3 | 
                                                /* worth using? */ | 
| 117 | 
  | 
  | 
                if (dtmp > FTINY) { | 
| 118 | 
  | 
  | 
                        copycolor(ctmp, np->scolor); | 
| 119 | 
greg | 
2.14 | 
                        dtmp *= omega * sqrt(ldot/np->pdot); | 
| 120 | 
greg | 
1.3 | 
                        scalecolor(ctmp, dtmp); | 
| 121 | 
  | 
  | 
                        addcolor(cval, ctmp); | 
| 122 | 
  | 
  | 
                } | 
| 123 | 
  | 
  | 
        } | 
| 124 | 
  | 
  | 
        if (ldot < -FTINY && np->tdiff > FTINY) { | 
| 125 | 
  | 
  | 
                /* | 
| 126 | 
  | 
  | 
                 *  Compute diffuse transmission. | 
| 127 | 
  | 
  | 
                 */ | 
| 128 | 
  | 
  | 
                copycolor(ctmp, np->mcolor); | 
| 129 | 
  | 
  | 
                dtmp = -ldot * omega * np->tdiff / PI; | 
| 130 | 
  | 
  | 
                scalecolor(ctmp, dtmp); | 
| 131 | 
  | 
  | 
                addcolor(cval, ctmp); | 
| 132 | 
  | 
  | 
        } | 
| 133 | 
greg | 
2.2 | 
        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { | 
| 134 | 
greg | 
1.3 | 
                /* | 
| 135 | 
greg | 
1.4 | 
                 *  Compute specular transmission.  Specular transmission | 
| 136 | 
greg | 
1.13 | 
                 *  is always modified by material color. | 
| 137 | 
greg | 
1.3 | 
                 */ | 
| 138 | 
  | 
  | 
                                                /* roughness + source */ | 
| 139 | 
greg | 
2.19 | 
                dtmp = np->alpha2 + omega/PI; | 
| 140 | 
greg | 
1.3 | 
                                                /* gaussian */ | 
| 141 | 
greg | 
2.21 | 
                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); | 
| 142 | 
greg | 
1.3 | 
                                                /* worth using? */ | 
| 143 | 
  | 
  | 
                if (dtmp > FTINY) { | 
| 144 | 
greg | 
1.13 | 
                        copycolor(ctmp, np->mcolor); | 
| 145 | 
greg | 
2.18 | 
                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); | 
| 146 | 
greg | 
1.13 | 
                        scalecolor(ctmp, dtmp); | 
| 147 | 
greg | 
1.3 | 
                        addcolor(cval, ctmp); | 
| 148 | 
  | 
  | 
                } | 
| 149 | 
  | 
  | 
        } | 
| 150 | 
  | 
  | 
} | 
| 151 | 
  | 
  | 
 | 
| 152 | 
  | 
  | 
 | 
| 153 | 
greg | 
2.2 | 
m_normal(m, r)                  /* color a ray that hit something normal */ | 
| 154 | 
greg | 
1.3 | 
register OBJREC  *m; | 
| 155 | 
  | 
  | 
register RAY  *r; | 
| 156 | 
  | 
  | 
{ | 
| 157 | 
  | 
  | 
        NORMDAT  nd; | 
| 158 | 
greg | 
1.9 | 
        double  transtest, transdist; | 
| 159 | 
greg | 
2.29 | 
        double  mirtest, mirdist; | 
| 160 | 
  | 
  | 
        int     hastexture; | 
| 161 | 
  | 
  | 
        double  d; | 
| 162 | 
greg | 
1.1 | 
        COLOR  ctmp; | 
| 163 | 
  | 
  | 
        register int  i; | 
| 164 | 
  | 
  | 
                                                /* easy shadow test */ | 
| 165 | 
  | 
  | 
        if (r->crtype & SHADOW && m->otype != MAT_TRANS) | 
| 166 | 
greg | 
2.27 | 
                return(1); | 
| 167 | 
greg | 
2.2 | 
 | 
| 168 | 
  | 
  | 
        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) | 
| 169 | 
  | 
  | 
                objerror(m, USER, "bad number of arguments"); | 
| 170 | 
greg | 
2.29 | 
                                                /* check for back side */ | 
| 171 | 
  | 
  | 
        if (r->rod < 0.0) { | 
| 172 | 
  | 
  | 
                if (!backvis && m->otype != MAT_TRANS) { | 
| 173 | 
  | 
  | 
                        raytrans(r); | 
| 174 | 
  | 
  | 
                        return(1); | 
| 175 | 
  | 
  | 
                } | 
| 176 | 
  | 
  | 
                flipsurface(r);                 /* reorient if backvis */ | 
| 177 | 
  | 
  | 
        } | 
| 178 | 
greg | 
1.3 | 
        nd.mp = m; | 
| 179 | 
greg | 
2.16 | 
        nd.rp = r; | 
| 180 | 
greg | 
1.1 | 
                                                /* get material color */ | 
| 181 | 
greg | 
1.3 | 
        setcolor(nd.mcolor, m->oargs.farg[0], | 
| 182 | 
greg | 
1.1 | 
                           m->oargs.farg[1], | 
| 183 | 
  | 
  | 
                           m->oargs.farg[2]); | 
| 184 | 
  | 
  | 
                                                /* get roughness */ | 
| 185 | 
greg | 
2.2 | 
        nd.specfl = 0; | 
| 186 | 
greg | 
1.3 | 
        nd.alpha2 = m->oargs.farg[4]; | 
| 187 | 
greg | 
2.2 | 
        if ((nd.alpha2 *= nd.alpha2) <= FTINY) | 
| 188 | 
  | 
  | 
                nd.specfl |= SP_PURE; | 
| 189 | 
greg | 
2.29 | 
        if (r->ro != NULL && isflat(r->ro->otype)) | 
| 190 | 
  | 
  | 
                nd.specfl |= SP_FLAT; | 
| 191 | 
greg | 
1.1 | 
                                                /* get modifiers */ | 
| 192 | 
  | 
  | 
        raytexture(r, m->omod); | 
| 193 | 
greg | 
2.29 | 
        if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) | 
| 194 | 
  | 
  | 
                nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */ | 
| 195 | 
  | 
  | 
        else { | 
| 196 | 
  | 
  | 
                VCOPY(nd.pnorm, r->ron); | 
| 197 | 
  | 
  | 
                nd.pdot = r->rod; | 
| 198 | 
  | 
  | 
        } | 
| 199 | 
greg | 
1.13 | 
        if (nd.pdot < .001) | 
| 200 | 
  | 
  | 
                nd.pdot = .001;                 /* non-zero for dirnorm() */ | 
| 201 | 
greg | 
1.3 | 
        multcolor(nd.mcolor, r->pcol);          /* modify material color */ | 
| 202 | 
greg | 
2.29 | 
        mirtest = transtest = 0; | 
| 203 | 
  | 
  | 
        mirdist = transdist = r->rot; | 
| 204 | 
greg | 
2.30 | 
        nd.rspec = m->oargs.farg[3]; | 
| 205 | 
greg | 
1.3 | 
                                                /* compute transmission */ | 
| 206 | 
greg | 
1.1 | 
        if (m->otype == MAT_TRANS) { | 
| 207 | 
greg | 
1.3 | 
                nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); | 
| 208 | 
  | 
  | 
                nd.tspec = nd.trans * m->oargs.farg[6]; | 
| 209 | 
  | 
  | 
                nd.tdiff = nd.trans - nd.tspec; | 
| 210 | 
greg | 
2.2 | 
                if (nd.tspec > FTINY) { | 
| 211 | 
  | 
  | 
                        nd.specfl |= SP_TRAN; | 
| 212 | 
greg | 
2.5 | 
                                                        /* check threshold */ | 
| 213 | 
greg | 
2.25 | 
                        if (!(nd.specfl & SP_PURE) && | 
| 214 | 
  | 
  | 
                                        specthresh >= nd.tspec-FTINY) | 
| 215 | 
greg | 
2.5 | 
                                nd.specfl |= SP_TBLT; | 
| 216 | 
greg | 
2.29 | 
                        if (!hastexture || r->crtype & SHADOW) { | 
| 217 | 
greg | 
2.2 | 
                                VCOPY(nd.prdir, r->rdir); | 
| 218 | 
  | 
  | 
                                transtest = 2; | 
| 219 | 
  | 
  | 
                        } else { | 
| 220 | 
  | 
  | 
                                for (i = 0; i < 3; i++)         /* perturb */ | 
| 221 | 
greg | 
2.19 | 
                                        nd.prdir[i] = r->rdir[i] - r->pert[i]; | 
| 222 | 
greg | 
2.7 | 
                                if (DOT(nd.prdir, r->ron) < -FTINY) | 
| 223 | 
  | 
  | 
                                        normalize(nd.prdir);    /* OK */ | 
| 224 | 
  | 
  | 
                                else | 
| 225 | 
  | 
  | 
                                        VCOPY(nd.prdir, r->rdir); | 
| 226 | 
greg | 
2.2 | 
                        } | 
| 227 | 
greg | 
1.14 | 
                } | 
| 228 | 
greg | 
1.1 | 
        } else | 
| 229 | 
greg | 
1.3 | 
                nd.tdiff = nd.tspec = nd.trans = 0.0; | 
| 230 | 
greg | 
1.1 | 
                                                /* transmitted ray */ | 
| 231 | 
greg | 
2.2 | 
        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { | 
| 232 | 
greg | 
1.3 | 
                RAY  lr; | 
| 233 | 
  | 
  | 
                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { | 
| 234 | 
greg | 
1.14 | 
                        VCOPY(lr.rdir, nd.prdir); | 
| 235 | 
greg | 
1.1 | 
                        rayvalue(&lr); | 
| 236 | 
greg | 
1.3 | 
                        scalecolor(lr.rcol, nd.tspec); | 
| 237 | 
greg | 
1.8 | 
                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */ | 
| 238 | 
greg | 
1.1 | 
                        addcolor(r->rcol, lr.rcol); | 
| 239 | 
greg | 
1.9 | 
                        transtest *= bright(lr.rcol); | 
| 240 | 
  | 
  | 
                        transdist = r->rot + lr.rt; | 
| 241 | 
greg | 
1.1 | 
                } | 
| 242 | 
greg | 
2.11 | 
        } else | 
| 243 | 
  | 
  | 
                transtest = 0; | 
| 244 | 
greg | 
2.2 | 
 | 
| 245 | 
greg | 
2.29 | 
        if (r->crtype & SHADOW) {               /* the rest is shadow */ | 
| 246 | 
  | 
  | 
                r->rt = transdist; | 
| 247 | 
greg | 
2.27 | 
                return(1); | 
| 248 | 
greg | 
2.30 | 
        } | 
| 249 | 
  | 
  | 
                                                /* get specular reflection */ | 
| 250 | 
  | 
  | 
        if (nd.rspec > FTINY) { | 
| 251 | 
  | 
  | 
                nd.specfl |= SP_REFL; | 
| 252 | 
  | 
  | 
                                                /* compute specular color */ | 
| 253 | 
  | 
  | 
                if (m->otype == MAT_METAL) | 
| 254 | 
  | 
  | 
                        copycolor(nd.scolor, nd.mcolor); | 
| 255 | 
  | 
  | 
                else | 
| 256 | 
  | 
  | 
                        setcolor(nd.scolor, 1.0, 1.0, 1.0); | 
| 257 | 
  | 
  | 
                scalecolor(nd.scolor, nd.rspec); | 
| 258 | 
  | 
  | 
                                                /* check threshold */ | 
| 259 | 
  | 
  | 
                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) | 
| 260 | 
  | 
  | 
                        nd.specfl |= SP_RBLT; | 
| 261 | 
  | 
  | 
                                                /* compute reflected ray */ | 
| 262 | 
  | 
  | 
                for (i = 0; i < 3; i++) | 
| 263 | 
  | 
  | 
                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; | 
| 264 | 
  | 
  | 
                                                /* penetration? */ | 
| 265 | 
  | 
  | 
                if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) | 
| 266 | 
  | 
  | 
                        for (i = 0; i < 3; i++)         /* safety measure */ | 
| 267 | 
  | 
  | 
                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; | 
| 268 | 
  | 
  | 
 | 
| 269 | 
  | 
  | 
                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { | 
| 270 | 
  | 
  | 
                        RAY  lr; | 
| 271 | 
  | 
  | 
                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { | 
| 272 | 
  | 
  | 
                                VCOPY(lr.rdir, nd.vrefl); | 
| 273 | 
  | 
  | 
                                rayvalue(&lr); | 
| 274 | 
  | 
  | 
                                multcolor(lr.rcol, nd.scolor); | 
| 275 | 
  | 
  | 
                                addcolor(r->rcol, lr.rcol); | 
| 276 | 
  | 
  | 
                                if (!hastexture && nd.specfl & SP_FLAT) { | 
| 277 | 
  | 
  | 
                                        mirtest = 2.*bright(lr.rcol); | 
| 278 | 
  | 
  | 
                                        mirdist = r->rot + lr.rt; | 
| 279 | 
  | 
  | 
                                } | 
| 280 | 
  | 
  | 
                        } | 
| 281 | 
  | 
  | 
                } | 
| 282 | 
greg | 
2.29 | 
        } | 
| 283 | 
greg | 
1.1 | 
                                                /* diffuse reflection */ | 
| 284 | 
greg | 
1.3 | 
        nd.rdiff = 1.0 - nd.trans - nd.rspec; | 
| 285 | 
greg | 
1.1 | 
 | 
| 286 | 
greg | 
2.2 | 
        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) | 
| 287 | 
greg | 
2.27 | 
                return(1);                      /* 100% pure specular */ | 
| 288 | 
greg | 
2.3 | 
 | 
| 289 | 
greg | 
2.2 | 
        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) | 
| 290 | 
  | 
  | 
                gaussamp(r, &nd); | 
| 291 | 
  | 
  | 
 | 
| 292 | 
greg | 
1.3 | 
        if (nd.rdiff > FTINY) {         /* ambient from this side */ | 
| 293 | 
greg | 
2.31 | 
                ambient(ctmp, r, hastexture?nd.pnorm:r->ron); | 
| 294 | 
greg | 
2.5 | 
                if (nd.specfl & SP_RBLT) | 
| 295 | 
  | 
  | 
                        scalecolor(ctmp, 1.0-nd.trans); | 
| 296 | 
  | 
  | 
                else | 
| 297 | 
  | 
  | 
                        scalecolor(ctmp, nd.rdiff); | 
| 298 | 
greg | 
1.3 | 
                multcolor(ctmp, nd.mcolor);     /* modified by material color */ | 
| 299 | 
greg | 
1.2 | 
                addcolor(r->rcol, ctmp);        /* add to returned color */ | 
| 300 | 
  | 
  | 
        } | 
| 301 | 
greg | 
1.3 | 
        if (nd.tdiff > FTINY) {         /* ambient from other side */ | 
| 302 | 
greg | 
1.1 | 
                flipsurface(r); | 
| 303 | 
greg | 
2.32 | 
                if (hastexture) { | 
| 304 | 
  | 
  | 
                        FVECT  bnorm; | 
| 305 | 
  | 
  | 
                        bnorm[0] = -nd.pnorm[0]; | 
| 306 | 
  | 
  | 
                        bnorm[1] = -nd.pnorm[1]; | 
| 307 | 
  | 
  | 
                        bnorm[2] = -nd.pnorm[2]; | 
| 308 | 
  | 
  | 
                        ambient(ctmp, r, bnorm); | 
| 309 | 
  | 
  | 
                } else | 
| 310 | 
  | 
  | 
                        ambient(ctmp, r, r->ron); | 
| 311 | 
greg | 
2.5 | 
                if (nd.specfl & SP_TBLT) | 
| 312 | 
  | 
  | 
                        scalecolor(ctmp, nd.trans); | 
| 313 | 
  | 
  | 
                else | 
| 314 | 
  | 
  | 
                        scalecolor(ctmp, nd.tdiff); | 
| 315 | 
greg | 
1.13 | 
                multcolor(ctmp, nd.mcolor);     /* modified by color */ | 
| 316 | 
greg | 
1.1 | 
                addcolor(r->rcol, ctmp); | 
| 317 | 
  | 
  | 
                flipsurface(r); | 
| 318 | 
  | 
  | 
        } | 
| 319 | 
greg | 
1.3 | 
                                        /* add direct component */ | 
| 320 | 
  | 
  | 
        direct(r, dirnorm, &nd); | 
| 321 | 
greg | 
1.9 | 
                                        /* check distance */ | 
| 322 | 
greg | 
2.29 | 
        d = bright(r->rcol); | 
| 323 | 
  | 
  | 
        if (transtest > d) | 
| 324 | 
greg | 
1.9 | 
                r->rt = transdist; | 
| 325 | 
greg | 
2.29 | 
        else if (mirtest > d) | 
| 326 | 
  | 
  | 
                r->rt = mirdist; | 
| 327 | 
greg | 
2.27 | 
 | 
| 328 | 
  | 
  | 
        return(1); | 
| 329 | 
greg | 
2.2 | 
} | 
| 330 | 
  | 
  | 
 | 
| 331 | 
  | 
  | 
 | 
| 332 | 
  | 
  | 
static | 
| 333 | 
  | 
  | 
gaussamp(r, np)                 /* sample gaussian specular */ | 
| 334 | 
  | 
  | 
RAY  *r; | 
| 335 | 
  | 
  | 
register NORMDAT  *np; | 
| 336 | 
  | 
  | 
{ | 
| 337 | 
  | 
  | 
        RAY  sr; | 
| 338 | 
  | 
  | 
        FVECT  u, v, h; | 
| 339 | 
  | 
  | 
        double  rv[2]; | 
| 340 | 
  | 
  | 
        double  d, sinp, cosp; | 
| 341 | 
  | 
  | 
        register int  i; | 
| 342 | 
greg | 
2.13 | 
                                        /* quick test */ | 
| 343 | 
  | 
  | 
        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && | 
| 344 | 
  | 
  | 
                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) | 
| 345 | 
  | 
  | 
                return; | 
| 346 | 
greg | 
2.2 | 
                                        /* set up sample coordinates */ | 
| 347 | 
  | 
  | 
        v[0] = v[1] = v[2] = 0.0; | 
| 348 | 
  | 
  | 
        for (i = 0; i < 3; i++) | 
| 349 | 
  | 
  | 
                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) | 
| 350 | 
  | 
  | 
                        break; | 
| 351 | 
  | 
  | 
        v[i] = 1.0; | 
| 352 | 
  | 
  | 
        fcross(u, v, np->pnorm); | 
| 353 | 
  | 
  | 
        normalize(u); | 
| 354 | 
  | 
  | 
        fcross(v, np->pnorm, u); | 
| 355 | 
  | 
  | 
                                        /* compute reflection */ | 
| 356 | 
greg | 
2.5 | 
        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && | 
| 357 | 
greg | 
2.2 | 
                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { | 
| 358 | 
  | 
  | 
                dimlist[ndims++] = (int)np->mp; | 
| 359 | 
greg | 
2.7 | 
                d = urand(ilhash(dimlist,ndims)+samplendx); | 
| 360 | 
  | 
  | 
                multisamp(rv, 2, d); | 
| 361 | 
  | 
  | 
                d = 2.0*PI * rv[0]; | 
| 362 | 
  | 
  | 
                cosp = cos(d); | 
| 363 | 
  | 
  | 
                sinp = sin(d); | 
| 364 | 
  | 
  | 
                rv[1] = 1.0 - specjitter*rv[1]; | 
| 365 | 
  | 
  | 
                if (rv[1] <= FTINY) | 
| 366 | 
  | 
  | 
                        d = 1.0; | 
| 367 | 
  | 
  | 
                else | 
| 368 | 
  | 
  | 
                        d = sqrt( np->alpha2 * -log(rv[1]) ); | 
| 369 | 
  | 
  | 
                for (i = 0; i < 3; i++) | 
| 370 | 
  | 
  | 
                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); | 
| 371 | 
  | 
  | 
                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); | 
| 372 | 
  | 
  | 
                for (i = 0; i < 3; i++) | 
| 373 | 
  | 
  | 
                        sr.rdir[i] = r->rdir[i] + d*h[i]; | 
| 374 | 
  | 
  | 
                if (DOT(sr.rdir, r->ron) <= FTINY) | 
| 375 | 
  | 
  | 
                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */ | 
| 376 | 
  | 
  | 
                rayvalue(&sr); | 
| 377 | 
  | 
  | 
                multcolor(sr.rcol, np->scolor); | 
| 378 | 
  | 
  | 
                addcolor(r->rcol, sr.rcol); | 
| 379 | 
greg | 
2.2 | 
                ndims--; | 
| 380 | 
  | 
  | 
        } | 
| 381 | 
  | 
  | 
                                        /* compute transmission */ | 
| 382 | 
greg | 
2.8 | 
        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && | 
| 383 | 
  | 
  | 
                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { | 
| 384 | 
  | 
  | 
                dimlist[ndims++] = (int)np->mp; | 
| 385 | 
  | 
  | 
                d = urand(ilhash(dimlist,ndims)+1823+samplendx); | 
| 386 | 
  | 
  | 
                multisamp(rv, 2, d); | 
| 387 | 
  | 
  | 
                d = 2.0*PI * rv[0]; | 
| 388 | 
  | 
  | 
                cosp = cos(d); | 
| 389 | 
  | 
  | 
                sinp = sin(d); | 
| 390 | 
  | 
  | 
                rv[1] = 1.0 - specjitter*rv[1]; | 
| 391 | 
  | 
  | 
                if (rv[1] <= FTINY) | 
| 392 | 
  | 
  | 
                        d = 1.0; | 
| 393 | 
  | 
  | 
                else | 
| 394 | 
greg | 
2.20 | 
                        d = sqrt( -log(rv[1]) * np->alpha2 ); | 
| 395 | 
greg | 
2.8 | 
                for (i = 0; i < 3; i++) | 
| 396 | 
  | 
  | 
                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); | 
| 397 | 
  | 
  | 
                if (DOT(sr.rdir, r->ron) < -FTINY) | 
| 398 | 
  | 
  | 
                        normalize(sr.rdir);             /* OK, normalize */ | 
| 399 | 
  | 
  | 
                else | 
| 400 | 
  | 
  | 
                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */ | 
| 401 | 
  | 
  | 
                rayvalue(&sr); | 
| 402 | 
greg | 
2.11 | 
                scalecolor(sr.rcol, np->tspec); | 
| 403 | 
  | 
  | 
                multcolor(sr.rcol, np->mcolor);         /* modified by color */ | 
| 404 | 
greg | 
2.8 | 
                addcolor(r->rcol, sr.rcol); | 
| 405 | 
  | 
  | 
                ndims--; | 
| 406 | 
  | 
  | 
        } | 
| 407 | 
greg | 
1.1 | 
} |