| 1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* raytrace.c - routines for tracing and shading rays. |
| 6 |
|
* |
| 7 |
< |
* 8/7/85 |
| 7 |
> |
* External symbols declared in ray.h |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
+ |
/* ==================================================================== |
| 11 |
+ |
* The Radiance Software License, Version 1.0 |
| 12 |
+ |
* |
| 13 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
| 14 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
| 15 |
+ |
* |
| 16 |
+ |
* Redistribution and use in source and binary forms, with or without |
| 17 |
+ |
* modification, are permitted provided that the following conditions |
| 18 |
+ |
* are met: |
| 19 |
+ |
* |
| 20 |
+ |
* 1. Redistributions of source code must retain the above copyright |
| 21 |
+ |
* notice, this list of conditions and the following disclaimer. |
| 22 |
+ |
* |
| 23 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
| 24 |
+ |
* notice, this list of conditions and the following disclaimer in |
| 25 |
+ |
* the documentation and/or other materials provided with the |
| 26 |
+ |
* distribution. |
| 27 |
+ |
* |
| 28 |
+ |
* 3. The end-user documentation included with the redistribution, |
| 29 |
+ |
* if any, must include the following acknowledgment: |
| 30 |
+ |
* "This product includes Radiance software |
| 31 |
+ |
* (http://radsite.lbl.gov/) |
| 32 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
| 33 |
+ |
* (http://www.lbl.gov/)." |
| 34 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
| 35 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
| 36 |
+ |
* |
| 37 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
| 38 |
+ |
* and "The Regents of the University of California" must |
| 39 |
+ |
* not be used to endorse or promote products derived from this |
| 40 |
+ |
* software without prior written permission. For written |
| 41 |
+ |
* permission, please contact [email protected]. |
| 42 |
+ |
* |
| 43 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
| 44 |
+ |
* nor may "Radiance" appear in their name, without prior written |
| 45 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
| 46 |
+ |
* |
| 47 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 48 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 49 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 50 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
| 51 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 52 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 53 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 54 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 55 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 56 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 57 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 58 |
+ |
* SUCH DAMAGE. |
| 59 |
+ |
* ==================================================================== |
| 60 |
+ |
* |
| 61 |
+ |
* This software consists of voluntary contributions made by many |
| 62 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
| 63 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
| 64 |
+ |
* <http://www.lbl.gov/>. |
| 65 |
+ |
*/ |
| 66 |
+ |
|
| 67 |
|
#include "ray.h" |
| 68 |
|
|
| 15 |
– |
#include "octree.h" |
| 16 |
– |
|
| 69 |
|
#include "otypes.h" |
| 70 |
|
|
| 71 |
|
#include "otspecial.h" |
| 72 |
|
|
| 73 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
| 74 |
|
|
| 23 |
– |
extern CUBE thescene; /* our scene */ |
| 24 |
– |
extern int maxdepth; /* maximum recursion depth */ |
| 25 |
– |
extern double minweight; /* minimum ray weight */ |
| 26 |
– |
extern int do_irrad; /* compute irradiance? */ |
| 27 |
– |
extern COLOR ambval; /* ambient value */ |
| 28 |
– |
|
| 29 |
– |
extern COLOR cextinction; /* global extinction coefficient */ |
| 30 |
– |
extern COLOR salbedo; /* global scattering albedo */ |
| 31 |
– |
extern double seccg; /* global scattering eccentricity */ |
| 32 |
– |
extern double ssampdist; /* scatter sampling distance */ |
| 33 |
– |
|
| 75 |
|
unsigned long raynum = 0; /* next unique ray number */ |
| 76 |
|
unsigned long nrays = 0; /* number of calls to localhit */ |
| 77 |
|
|
| 83 |
|
|
| 84 |
|
OBJREC Aftplane; /* aft clipping plane object */ |
| 85 |
|
|
| 86 |
< |
static int raymove(), checkset(), checkhit(); |
| 86 |
> |
static int raymove(), checkhit(); |
| 87 |
> |
static void checkset(); |
| 88 |
|
|
| 89 |
< |
#define MAXLOOP 128 /* modifier loop detection */ |
| 89 |
> |
#ifndef MAXLOOP |
| 90 |
> |
#define MAXLOOP 0 /* modifier loop detection */ |
| 91 |
> |
#endif |
| 92 |
|
|
| 93 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
| 94 |
|
|
| 95 |
|
|
| 96 |
+ |
int |
| 97 |
|
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
| 98 |
|
register RAY *r, *ro; |
| 99 |
|
int rt; |
| 100 |
|
double rw; |
| 101 |
|
{ |
| 102 |
+ |
double re; |
| 103 |
+ |
|
| 104 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
| 105 |
|
r->rlvl = 0; |
| 106 |
|
r->rweight = rw; |
| 129 |
|
copycolor(r->albedo, ro->albedo); |
| 130 |
|
r->gecc = ro->gecc; |
| 131 |
|
r->slights = ro->slights; |
| 85 |
– |
r->rweight = ro->rweight * rw; |
| 132 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
| 133 |
|
VCOPY(r->rorg, ro->rop); |
| 134 |
+ |
r->rweight = ro->rweight * rw; |
| 135 |
+ |
/* estimate absorption */ |
| 136 |
+ |
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? |
| 137 |
+ |
colval(ro->cext,RED) : colval(ro->cext,GRN); |
| 138 |
+ |
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); |
| 139 |
+ |
if (re > 0.) |
| 140 |
+ |
r->rweight *= exp(-re*ro->rot); |
| 141 |
|
} |
| 142 |
|
rayclear(r); |
| 143 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
|
| 147 |
+ |
void |
| 148 |
|
rayclear(r) /* clear a ray for (re)evaluation */ |
| 149 |
|
register RAY *r; |
| 150 |
|
{ |
| 151 |
|
r->rno = raynum++; |
| 152 |
|
r->newcset = r->clipset; |
| 153 |
+ |
r->robj = OVOID; |
| 154 |
|
r->ro = NULL; |
| 155 |
< |
r->rot = FHUGE; |
| 155 |
> |
r->rox = NULL; |
| 156 |
> |
r->rt = r->rot = FHUGE; |
| 157 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
| 158 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
| 159 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
| 104 |
– |
r->rt = 0.0; |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
+ |
void |
| 164 |
|
raytrace(r) /* trace a ray and compute its value */ |
| 165 |
|
RAY *r; |
| 166 |
|
{ |
| 111 |
– |
extern int (*trace)(); |
| 112 |
– |
|
| 167 |
|
if (localhit(r, &thescene)) |
| 168 |
|
raycont(r); /* hit local surface, evaluate */ |
| 169 |
|
else if (r->ro == &Aftplane) { |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
+ |
void |
| 183 |
|
raycont(r) /* check for clipped object and continue */ |
| 184 |
|
register RAY *r; |
| 185 |
|
{ |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
|
| 192 |
+ |
void |
| 193 |
|
raytrans(r) /* transmit ray as is */ |
| 194 |
|
register RAY *r; |
| 195 |
|
{ |
| 204 |
|
} |
| 205 |
|
|
| 206 |
|
|
| 207 |
+ |
int |
| 208 |
|
rayshade(r, mod) /* shade ray r with material mod */ |
| 209 |
|
register RAY *r; |
| 210 |
|
int mod; |
| 211 |
|
{ |
| 155 |
– |
static int depth = 0; |
| 212 |
|
int gotmat; |
| 213 |
|
register OBJREC *m; |
| 214 |
+ |
#if MAXLOOP |
| 215 |
+ |
static int depth = 0; |
| 216 |
|
/* check for infinite loop */ |
| 217 |
|
if (depth++ >= MAXLOOP) |
| 218 |
|
objerror(r->ro, USER, "possible modifier loop"); |
| 219 |
+ |
#endif |
| 220 |
|
r->rt = r->rot; /* set effective ray length */ |
| 221 |
|
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
| 222 |
|
m = objptr(mod); |
| 229 |
|
/* hack for irradiance calculation */ |
| 230 |
|
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
| 231 |
|
if (irr_ignore(m->otype)) { |
| 232 |
+ |
#if MAXLOOP |
| 233 |
|
depth--; |
| 234 |
+ |
#endif |
| 235 |
|
raytrans(r); |
| 236 |
|
return(1); |
| 237 |
|
} |
| 241 |
|
/* materials call raytexture */ |
| 242 |
|
gotmat = (*ofun[m->otype].funp)(m, r); |
| 243 |
|
} |
| 244 |
+ |
#if MAXLOOP |
| 245 |
|
depth--; |
| 246 |
+ |
#endif |
| 247 |
|
return(gotmat); |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
|
| 251 |
+ |
void |
| 252 |
|
rayparticipate(r) /* compute ray medium participation */ |
| 253 |
|
register RAY *r; |
| 254 |
|
{ |
| 284 |
|
RAY *r; |
| 285 |
|
int mod; |
| 286 |
|
{ |
| 223 |
– |
static int depth = 0; |
| 287 |
|
register OBJREC *m; |
| 288 |
+ |
#if MAXLOOP |
| 289 |
+ |
static int depth = 0; |
| 290 |
|
/* check for infinite loop */ |
| 291 |
|
if (depth++ >= MAXLOOP) |
| 292 |
|
objerror(r->ro, USER, "modifier loop"); |
| 293 |
+ |
#endif |
| 294 |
|
/* execute textures and patterns */ |
| 295 |
|
for ( ; mod != OVOID; mod = m->omod) { |
| 296 |
|
m = objptr(mod); |
| 306 |
|
objerror(r->ro, USER, errmsg); |
| 307 |
|
} |
| 308 |
|
} |
| 309 |
+ |
#if MAXLOOP |
| 310 |
|
depth--; /* end here */ |
| 311 |
+ |
#endif |
| 312 |
|
} |
| 313 |
|
|
| 314 |
|
|
| 315 |
+ |
int |
| 316 |
|
raymixture(r, fore, back, coef) /* mix modifiers */ |
| 317 |
|
register RAY *r; |
| 318 |
|
OBJECT fore, back; |
| 338 |
|
backmat = rayshade(&br, back); |
| 339 |
|
/* check for transparency */ |
| 340 |
|
if (backmat ^ foremat) |
| 341 |
< |
if (backmat) |
| 341 |
> |
if (backmat && coef > FTINY) |
| 342 |
|
raytrans(&fr); |
| 343 |
< |
else |
| 343 |
> |
else if (foremat && coef < 1.0-FTINY) |
| 344 |
|
raytrans(&br); |
| 345 |
|
/* mix perturbations */ |
| 346 |
|
for (i = 0; i < 3; i++) |
| 413 |
|
} |
| 414 |
|
|
| 415 |
|
|
| 416 |
+ |
void |
| 417 |
|
newrayxf(r) /* get new tranformation matrix for ray */ |
| 418 |
|
RAY *r; |
| 419 |
|
{ |
| 433 |
|
if (rp->rox == &xp->xf) { /* xp in use */ |
| 434 |
|
xp = xp->next; /* move to next */ |
| 435 |
|
if (xp == xflast) { /* need new one */ |
| 436 |
< |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
| 436 |
> |
xp = (struct xfn *)malloc(sizeof(struct xfn)); |
| 437 |
|
if (xp == NULL) |
| 438 |
|
error(SYSTEM, |
| 439 |
|
"out of memory in newrayxf"); |
| 450 |
|
} |
| 451 |
|
|
| 452 |
|
|
| 453 |
+ |
void |
| 454 |
|
flipsurface(r) /* reverse surface orientation */ |
| 455 |
|
register RAY *r; |
| 456 |
|
{ |
| 464 |
|
} |
| 465 |
|
|
| 466 |
|
|
| 467 |
+ |
int |
| 468 |
|
localhit(r, scene) /* check for hit in the octree */ |
| 469 |
|
register RAY *r; |
| 470 |
|
register CUBE *scene; |
| 612 |
|
} |
| 613 |
|
|
| 614 |
|
|
| 615 |
< |
static |
| 615 |
> |
static int |
| 616 |
|
checkhit(r, cu, cxs) /* check for hit in full cube */ |
| 617 |
|
register RAY *r; |
| 618 |
|
CUBE *cu; |
| 626 |
|
checkset(oset, cxs); /* eliminate double-checking */ |
| 627 |
|
for (i = oset[0]; i > 0; i--) { |
| 628 |
|
o = objptr(oset[i]); |
| 629 |
< |
(*ofun[o->otype].funp)(o, r); |
| 629 |
> |
if ((*ofun[o->otype].funp)(o, r)) |
| 630 |
> |
r->robj = oset[i]; |
| 631 |
|
} |
| 632 |
|
if (r->ro == NULL) |
| 633 |
|
return(0); /* no scores yet */ |
| 636 |
|
} |
| 637 |
|
|
| 638 |
|
|
| 639 |
< |
static |
| 639 |
> |
static void |
| 640 |
|
checkset(os, cs) /* modify checked set and set to check */ |
| 641 |
|
register OBJECT *os; /* os' = os - cs */ |
| 642 |
|
register OBJECT *cs; /* cs' = cs + os */ |