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 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
|
#include "ray.h" |
13 |
|
|
15 |
– |
#include "octree.h" |
16 |
– |
|
14 |
|
#include "otypes.h" |
15 |
|
|
16 |
|
#include "otspecial.h" |
17 |
|
|
18 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
19 |
|
|
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 |
– |
|
20 |
|
unsigned long raynum = 0; /* next unique ray number */ |
21 |
|
unsigned long nrays = 0; /* number of calls to localhit */ |
22 |
|
|
28 |
|
|
29 |
|
OBJREC Aftplane; /* aft clipping plane object */ |
30 |
|
|
31 |
< |
static int raymove(), checkset(), checkhit(); |
31 |
> |
static int raymove(), checkhit(); |
32 |
> |
static void checkset(); |
33 |
|
|
34 |
< |
#define MAXLOOP 128 /* modifier loop detection */ |
34 |
> |
#ifndef MAXLOOP |
35 |
> |
#define MAXLOOP 0 /* modifier loop detection */ |
36 |
> |
#endif |
37 |
|
|
38 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
39 |
|
|
40 |
|
|
41 |
+ |
int |
42 |
|
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
43 |
|
register RAY *r, *ro; |
44 |
|
int rt; |
45 |
|
double rw; |
46 |
|
{ |
47 |
+ |
double re; |
48 |
+ |
|
49 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
50 |
|
r->rlvl = 0; |
51 |
|
r->rweight = rw; |
74 |
|
copycolor(r->albedo, ro->albedo); |
75 |
|
r->gecc = ro->gecc; |
76 |
|
r->slights = ro->slights; |
85 |
– |
r->rweight = ro->rweight * rw; |
77 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
78 |
|
VCOPY(r->rorg, ro->rop); |
79 |
+ |
r->rweight = ro->rweight * rw; |
80 |
+ |
/* estimate absorption */ |
81 |
+ |
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? |
82 |
+ |
colval(ro->cext,RED) : colval(ro->cext,GRN); |
83 |
+ |
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); |
84 |
+ |
if (re > 0.) |
85 |
+ |
r->rweight *= exp(-re*ro->rot); |
86 |
|
} |
87 |
|
rayclear(r); |
88 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
+ |
void |
93 |
|
rayclear(r) /* clear a ray for (re)evaluation */ |
94 |
|
register RAY *r; |
95 |
|
{ |
97 |
|
r->newcset = r->clipset; |
98 |
|
r->robj = OVOID; |
99 |
|
r->ro = NULL; |
100 |
+ |
r->rox = NULL; |
101 |
|
r->rt = r->rot = FHUGE; |
102 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
103 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
+ |
void |
109 |
|
raytrace(r) /* trace a ray and compute its value */ |
110 |
|
RAY *r; |
111 |
|
{ |
111 |
– |
extern int (*trace)(); |
112 |
– |
|
112 |
|
if (localhit(r, &thescene)) |
113 |
|
raycont(r); /* hit local surface, evaluate */ |
114 |
|
else if (r->ro == &Aftplane) { |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
+ |
void |
128 |
|
raycont(r) /* check for clipped object and continue */ |
129 |
|
register RAY *r; |
130 |
|
{ |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
+ |
void |
138 |
|
raytrans(r) /* transmit ray as is */ |
139 |
|
register RAY *r; |
140 |
|
{ |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
+ |
int |
153 |
|
rayshade(r, mod) /* shade ray r with material mod */ |
154 |
|
register RAY *r; |
155 |
|
int mod; |
156 |
|
{ |
155 |
– |
static int depth = 0; |
157 |
|
int gotmat; |
158 |
|
register OBJREC *m; |
159 |
+ |
#if MAXLOOP |
160 |
+ |
static int depth = 0; |
161 |
|
/* check for infinite loop */ |
162 |
|
if (depth++ >= MAXLOOP) |
163 |
|
objerror(r->ro, USER, "possible modifier loop"); |
164 |
+ |
#endif |
165 |
|
r->rt = r->rot; /* set effective ray length */ |
166 |
|
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
167 |
|
m = objptr(mod); |
174 |
|
/* hack for irradiance calculation */ |
175 |
|
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
176 |
|
if (irr_ignore(m->otype)) { |
177 |
+ |
#if MAXLOOP |
178 |
|
depth--; |
179 |
+ |
#endif |
180 |
|
raytrans(r); |
181 |
|
return(1); |
182 |
|
} |
186 |
|
/* materials call raytexture */ |
187 |
|
gotmat = (*ofun[m->otype].funp)(m, r); |
188 |
|
} |
189 |
+ |
#if MAXLOOP |
190 |
|
depth--; |
191 |
+ |
#endif |
192 |
|
return(gotmat); |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
+ |
void |
197 |
|
rayparticipate(r) /* compute ray medium participation */ |
198 |
|
register RAY *r; |
199 |
|
{ |
229 |
|
RAY *r; |
230 |
|
int mod; |
231 |
|
{ |
223 |
– |
static int depth = 0; |
232 |
|
register OBJREC *m; |
233 |
+ |
#if MAXLOOP |
234 |
+ |
static int depth = 0; |
235 |
|
/* check for infinite loop */ |
236 |
|
if (depth++ >= MAXLOOP) |
237 |
|
objerror(r->ro, USER, "modifier loop"); |
238 |
+ |
#endif |
239 |
|
/* execute textures and patterns */ |
240 |
|
for ( ; mod != OVOID; mod = m->omod) { |
241 |
|
m = objptr(mod); |
251 |
|
objerror(r->ro, USER, errmsg); |
252 |
|
} |
253 |
|
} |
254 |
+ |
#if MAXLOOP |
255 |
|
depth--; /* end here */ |
256 |
+ |
#endif |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
+ |
int |
261 |
|
raymixture(r, fore, back, coef) /* mix modifiers */ |
262 |
|
register RAY *r; |
263 |
|
OBJECT fore, back; |
283 |
|
backmat = rayshade(&br, back); |
284 |
|
/* check for transparency */ |
285 |
|
if (backmat ^ foremat) |
286 |
< |
if (backmat) |
286 |
> |
if (backmat && coef > FTINY) |
287 |
|
raytrans(&fr); |
288 |
< |
else |
288 |
> |
else if (foremat && coef < 1.0-FTINY) |
289 |
|
raytrans(&br); |
290 |
|
/* mix perturbations */ |
291 |
|
for (i = 0; i < 3; i++) |
358 |
|
} |
359 |
|
|
360 |
|
|
361 |
+ |
void |
362 |
|
newrayxf(r) /* get new tranformation matrix for ray */ |
363 |
|
RAY *r; |
364 |
|
{ |
378 |
|
if (rp->rox == &xp->xf) { /* xp in use */ |
379 |
|
xp = xp->next; /* move to next */ |
380 |
|
if (xp == xflast) { /* need new one */ |
381 |
< |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
381 |
> |
xp = (struct xfn *)malloc(sizeof(struct xfn)); |
382 |
|
if (xp == NULL) |
383 |
|
error(SYSTEM, |
384 |
|
"out of memory in newrayxf"); |
395 |
|
} |
396 |
|
|
397 |
|
|
398 |
+ |
void |
399 |
|
flipsurface(r) /* reverse surface orientation */ |
400 |
|
register RAY *r; |
401 |
|
{ |
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
+ |
int |
413 |
|
localhit(r, scene) /* check for hit in the octree */ |
414 |
|
register RAY *r; |
415 |
|
register CUBE *scene; |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
< |
static |
560 |
> |
static int |
561 |
|
checkhit(r, cu, cxs) /* check for hit in full cube */ |
562 |
|
register RAY *r; |
563 |
|
CUBE *cu; |
581 |
|
} |
582 |
|
|
583 |
|
|
584 |
< |
static |
584 |
> |
static void |
585 |
|
checkset(os, cs) /* modify checked set and set to check */ |
586 |
|
register OBJECT *os; /* os' = os - cs */ |
587 |
|
register OBJECT *cs; /* cs' = cs + os */ |