ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
(Generate patch)

Comparing ray/src/rt/normal.c (file contents):
Revision 2.27 by greg, Wed Jan 12 16:46:45 1994 UTC vs.
Revision 2.37 by gwlarson, Wed Dec 16 18:14:58 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
# Line 23 | Line 23 | static char SCCSid[] = "$SunId$ LBL";
23   extern double  specthresh;              /* specular sampling threshold */
24   extern double  specjitter;              /* specular sampling jitter */
25  
26 + extern int  backvis;                    /* back faces visible? */
27 +
28 + #ifndef  MAXITER
29 + #define  MAXITER        10              /* maximum # specular ray attempts */
30 + #endif
31 +
32   static  gaussamp();
33  
34   /*
# Line 154 | Line 160 | register RAY  *r;
160   {
161          NORMDAT  nd;
162          double  transtest, transdist;
163 +        double  mirtest, mirdist;
164 +        int     hastexture;
165 +        double  d;
166          COLOR  ctmp;
167          register int  i;
168                                                  /* easy shadow test */
# Line 162 | Line 171 | register RAY  *r;
171  
172          if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
173                  objerror(m, USER, "bad number of arguments");
174 +                                                /* check for back side */
175 +        if (r->rod < 0.0) {
176 +                if (!backvis && m->otype != MAT_TRANS) {
177 +                        raytrans(r);
178 +                        return(1);
179 +                }
180 +                flipsurface(r);                 /* reorient if backvis */
181 +        }
182          nd.mp = m;
183          nd.rp = r;
184                                                  /* get material color */
# Line 173 | Line 190 | register RAY  *r;
190          nd.alpha2 = m->oargs.farg[4];
191          if ((nd.alpha2 *= nd.alpha2) <= FTINY)
192                  nd.specfl |= SP_PURE;
193 <                                                /* reorient if necessary */
194 <        if (r->rod < 0.0)
178 <                flipsurface(r);
193 >        if (r->ro != NULL && isflat(r->ro->otype))
194 >                nd.specfl |= SP_FLAT;
195                                                  /* get modifiers */
196          raytexture(r, m->omod);
197 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
197 >        if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY)
198 >                nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
199 >        else {
200 >                VCOPY(nd.pnorm, r->ron);
201 >                nd.pdot = r->rod;
202 >        }
203          if (nd.pdot < .001)
204                  nd.pdot = .001;                 /* non-zero for dirnorm() */
205          multcolor(nd.mcolor, r->pcol);          /* modify material color */
206 <        transtest = 0;
207 <        transdist = r->rot;
208 <                                                /* get specular component */
188 <        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
189 <                nd.specfl |= SP_REFL;
190 <                                                /* compute specular color */
191 <                if (m->otype == MAT_METAL)
192 <                        copycolor(nd.scolor, nd.mcolor);
193 <                else
194 <                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
195 <                scalecolor(nd.scolor, nd.rspec);
196 <                                                /* check threshold */
197 <                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
198 <                        nd.specfl |= SP_RBLT;
199 <                                                /* compute reflected ray */
200 <                for (i = 0; i < 3; i++)
201 <                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
202 <                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
203 <                        for (i = 0; i < 3; i++)         /* safety measure */
204 <                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
205 <
206 <                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
207 <                        RAY  lr;
208 <                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
209 <                                VCOPY(lr.rdir, nd.vrefl);
210 <                                rayvalue(&lr);
211 <                                multcolor(lr.rcol, nd.scolor);
212 <                                addcolor(r->rcol, lr.rcol);
213 <                        }
214 <                }
215 <        }
206 >        mirtest = transtest = 0;
207 >        mirdist = transdist = r->rot;
208 >        nd.rspec = m->oargs.farg[3];
209                                                  /* compute transmission */
210          if (m->otype == MAT_TRANS) {
211                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
# Line 224 | Line 217 | register RAY  *r;
217                          if (!(nd.specfl & SP_PURE) &&
218                                          specthresh >= nd.tspec-FTINY)
219                                  nd.specfl |= SP_TBLT;
220 <                        if (r->crtype & SHADOW ||
228 <                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
220 >                        if (!hastexture || r->crtype & SHADOW) {
221                                  VCOPY(nd.prdir, r->rdir);
222                                  transtest = 2;
223                          } else {
# Line 240 | Line 232 | register RAY  *r;
232          } else
233                  nd.tdiff = nd.tspec = nd.trans = 0.0;
234                                                  /* transmitted ray */
235 <        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
235 >        if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
236                  RAY  lr;
237                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
238                          VCOPY(lr.rdir, nd.prdir);
# Line 254 | Line 246 | register RAY  *r;
246          } else
247                  transtest = 0;
248  
249 <        if (r->crtype & SHADOW)                 /* the rest is shadow */
249 >        if (r->crtype & SHADOW) {               /* the rest is shadow */
250 >                r->rt = transdist;
251                  return(1);
252 +        }
253 +                                                /* get specular reflection */
254 +        if (nd.rspec > FTINY) {
255 +                nd.specfl |= SP_REFL;
256 +                                                /* compute specular color */
257 +                if (m->otype == MAT_METAL)
258 +                        copycolor(nd.scolor, nd.mcolor);
259 +                else
260 +                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
261 +                scalecolor(nd.scolor, nd.rspec);
262 +                                                /* check threshold */
263 +                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
264 +                        nd.specfl |= SP_RBLT;
265 +                                                /* compute reflected ray */
266 +                for (i = 0; i < 3; i++)
267 +                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
268 +                                                /* penetration? */
269 +                if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
270 +                        for (i = 0; i < 3; i++)         /* safety measure */
271 +                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
272 +        }
273 +                                                /* reflected ray */
274 +        if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
275 +                RAY  lr;
276 +                if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
277 +                        VCOPY(lr.rdir, nd.vrefl);
278 +                        rayvalue(&lr);
279 +                        multcolor(lr.rcol, nd.scolor);
280 +                        addcolor(r->rcol, lr.rcol);
281 +                        if (!hastexture && nd.specfl & SP_FLAT) {
282 +                                mirtest = 2.*bright(lr.rcol);
283 +                                mirdist = r->rot + lr.rt;
284 +                        }
285 +                }
286 +        }
287                                                  /* diffuse reflection */
288          nd.rdiff = 1.0 - nd.trans - nd.rspec;
289  
290          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
291                  return(1);                      /* 100% pure specular */
292  
293 <        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
294 <                        r->ro->otype == OBJ_RING))
267 <                nd.specfl |= SP_FLAT;
293 >        if (!(nd.specfl & SP_PURE))
294 >                gaussamp(r, &nd);               /* checks *BLT flags */
295  
269        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
270                gaussamp(r, &nd);
271
296          if (nd.rdiff > FTINY) {         /* ambient from this side */
297 <                ambient(ctmp, r);
297 >                ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
298                  if (nd.specfl & SP_RBLT)
299                          scalecolor(ctmp, 1.0-nd.trans);
300                  else
# Line 280 | Line 304 | register RAY  *r;
304          }
305          if (nd.tdiff > FTINY) {         /* ambient from other side */
306                  flipsurface(r);
307 <                ambient(ctmp, r);
307 >                if (hastexture) {
308 >                        FVECT  bnorm;
309 >                        bnorm[0] = -nd.pnorm[0];
310 >                        bnorm[1] = -nd.pnorm[1];
311 >                        bnorm[2] = -nd.pnorm[2];
312 >                        ambient(ctmp, r, bnorm);
313 >                } else
314 >                        ambient(ctmp, r, r->ron);
315                  if (nd.specfl & SP_TBLT)
316                          scalecolor(ctmp, nd.trans);
317                  else
# Line 292 | Line 323 | register RAY  *r;
323                                          /* add direct component */
324          direct(r, dirnorm, &nd);
325                                          /* check distance */
326 <        if (transtest > bright(r->rcol))
326 >        d = bright(r->rcol);
327 >        if (transtest > d)
328                  r->rt = transdist;
329 +        else if (mirtest > d)
330 +                r->rt = mirdist;
331  
332          return(1);
333   }
# Line 308 | Line 342 | register NORMDAT  *np;
342          FVECT  u, v, h;
343          double  rv[2];
344          double  d, sinp, cosp;
345 +        int  niter;
346          register int  i;
347                                          /* quick test */
348          if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
# Line 326 | Line 361 | register NORMDAT  *np;
361          if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
362                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
363                  dimlist[ndims++] = (int)np->mp;
364 <                d = urand(ilhash(dimlist,ndims)+samplendx);
365 <                multisamp(rv, 2, d);
366 <                d = 2.0*PI * rv[0];
367 <                cosp = cos(d);
368 <                sinp = sin(d);
369 <                rv[1] = 1.0 - specjitter*rv[1];
370 <                if (rv[1] <= FTINY)
371 <                        d = 1.0;
372 <                else
373 <                        d = sqrt( np->alpha2 * -log(rv[1]) );
374 <                for (i = 0; i < 3; i++)
375 <                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
376 <                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
377 <                for (i = 0; i < 3; i++)
378 <                        sr.rdir[i] = r->rdir[i] + d*h[i];
379 <                if (DOT(sr.rdir, r->ron) <= FTINY)
380 <                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
381 <                rayvalue(&sr);
382 <                multcolor(sr.rcol, np->scolor);
383 <                addcolor(r->rcol, sr.rcol);
364 >                for (niter = 0; niter < MAXITER; niter++) {
365 >                        if (niter)
366 >                                d = frandom();
367 >                        else
368 >                                d = urand(ilhash(dimlist,ndims)+samplendx);
369 >                        multisamp(rv, 2, d);
370 >                        d = 2.0*PI * rv[0];
371 >                        cosp = tcos(d);
372 >                        sinp = tsin(d);
373 >                        rv[1] = 1.0 - specjitter*rv[1];
374 >                        if (rv[1] <= FTINY)
375 >                                d = 1.0;
376 >                        else
377 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
378 >                        for (i = 0; i < 3; i++)
379 >                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
380 >                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
381 >                        for (i = 0; i < 3; i++)
382 >                                sr.rdir[i] = r->rdir[i] + d*h[i];
383 >                        if (DOT(sr.rdir, r->ron) > FTINY) {
384 >                                rayvalue(&sr);
385 >                                multcolor(sr.rcol, np->scolor);
386 >                                addcolor(r->rcol, sr.rcol);
387 >                                break;
388 >                        }
389 >                }
390                  ndims--;
391          }
392                                          /* compute transmission */
393          if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
394                          rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
395                  dimlist[ndims++] = (int)np->mp;
396 <                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
397 <                multisamp(rv, 2, d);
398 <                d = 2.0*PI * rv[0];
399 <                cosp = cos(d);
400 <                sinp = sin(d);
401 <                rv[1] = 1.0 - specjitter*rv[1];
402 <                if (rv[1] <= FTINY)
403 <                        d = 1.0;
404 <                else
405 <                        d = sqrt( -log(rv[1]) * np->alpha2 );
406 <                for (i = 0; i < 3; i++)
407 <                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
408 <                if (DOT(sr.rdir, r->ron) < -FTINY)
409 <                        normalize(sr.rdir);             /* OK, normalize */
410 <                else
411 <                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
412 <                rayvalue(&sr);
413 <                scalecolor(sr.rcol, np->tspec);
414 <                multcolor(sr.rcol, np->mcolor);         /* modified by color */
415 <                addcolor(r->rcol, sr.rcol);
396 >                for (niter = 0; niter < MAXITER; niter++) {
397 >                        if (niter)
398 >                                d = frandom();
399 >                        else
400 >                                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
401 >                        multisamp(rv, 2, d);
402 >                        d = 2.0*PI * rv[0];
403 >                        cosp = tcos(d);
404 >                        sinp = tsin(d);
405 >                        rv[1] = 1.0 - specjitter*rv[1];
406 >                        if (rv[1] <= FTINY)
407 >                                d = 1.0;
408 >                        else
409 >                                d = sqrt( np->alpha2 * -log(rv[1]) );
410 >                        for (i = 0; i < 3; i++)
411 >                                sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
412 >                        if (DOT(sr.rdir, r->ron) < -FTINY) {
413 >                                normalize(sr.rdir);     /* OK, normalize */
414 >                                rayvalue(&sr);
415 >                                scalecolor(sr.rcol, np->tspec);
416 >                                multcolor(sr.rcol, np->mcolor); /* modified */
417 >                                addcolor(r->rcol, sr.rcol);
418 >                                break;
419 >                        }
420 >                }
421                  ndims--;
422          }
423   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines