1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
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 |
|
|
23 |
< |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
23 |
> |
static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
24 |
|
OBJREC Lamb = { |
25 |
|
OVOID, MAT_PLASTIC, "Lambertian", |
26 |
|
{0, 5, NULL, Lambfa}, NULL, |
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 |
|
|
47 |
– |
#ifndef MAXLOOP |
48 |
– |
#define MAXLOOP 0 /* modifier loop detection */ |
49 |
– |
#endif |
50 |
– |
|
34 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
35 |
|
|
36 |
|
|
37 |
+ |
int |
38 |
|
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
39 |
|
register RAY *r, *ro; |
40 |
|
int rt; |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
+ |
void |
89 |
|
rayclear(r) /* clear a ray for (re)evaluation */ |
90 |
|
register RAY *r; |
91 |
|
{ |
92 |
|
r->rno = raynum++; |
93 |
|
r->newcset = r->clipset; |
94 |
+ |
r->hitf = rayhit; |
95 |
|
r->robj = OVOID; |
96 |
|
r->ro = NULL; |
97 |
+ |
r->rox = NULL; |
98 |
|
r->rt = r->rot = FHUGE; |
99 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
100 |
+ |
r->uv[0] = r->uv[1] = 0.0; |
101 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
102 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
+ |
void |
107 |
|
raytrace(r) /* trace a ray and compute its value */ |
108 |
|
RAY *r; |
109 |
|
{ |
121 |
– |
extern int (*trace)(); |
122 |
– |
|
110 |
|
if (localhit(r, &thescene)) |
111 |
|
raycont(r); /* hit local surface, evaluate */ |
112 |
|
else if (r->ro == &Aftplane) { |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
+ |
void |
126 |
|
raycont(r) /* check for clipped object and continue */ |
127 |
|
register RAY *r; |
128 |
|
{ |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
+ |
void |
136 |
|
raytrans(r) /* transmit ray as is */ |
137 |
|
register RAY *r; |
138 |
|
{ |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
+ |
int |
151 |
|
rayshade(r, mod) /* shade ray r with material mod */ |
152 |
|
register RAY *r; |
153 |
|
int mod; |
154 |
|
{ |
155 |
|
int gotmat; |
156 |
|
register OBJREC *m; |
167 |
– |
#if MAXLOOP |
168 |
– |
static int depth = 0; |
169 |
– |
/* check for infinite loop */ |
170 |
– |
if (depth++ >= MAXLOOP) |
171 |
– |
objerror(r->ro, USER, "possible modifier loop"); |
172 |
– |
#endif |
157 |
|
r->rt = r->rot; /* set effective ray length */ |
158 |
|
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
159 |
|
m = objptr(mod); |
164 |
|
} |
165 |
|
******/ |
166 |
|
/* hack for irradiance calculation */ |
167 |
< |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
167 |
> |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && |
168 |
> |
m->otype != MAT_CLIP && |
169 |
> |
(ofun[m->otype].flags & (T_M|T_X))) { |
170 |
|
if (irr_ignore(m->otype)) { |
185 |
– |
#if MAXLOOP |
186 |
– |
depth--; |
187 |
– |
#endif |
171 |
|
raytrans(r); |
172 |
|
return(1); |
173 |
|
} |
177 |
|
/* materials call raytexture */ |
178 |
|
gotmat = (*ofun[m->otype].funp)(m, r); |
179 |
|
} |
197 |
– |
#if MAXLOOP |
198 |
– |
depth--; |
199 |
– |
#endif |
180 |
|
return(gotmat); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
+ |
void |
185 |
|
rayparticipate(r) /* compute ray medium participation */ |
186 |
|
register RAY *r; |
187 |
|
{ |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
+ |
void |
217 |
|
raytexture(r, mod) /* get material modifiers */ |
218 |
|
RAY *r; |
219 |
< |
int mod; |
219 |
> |
OBJECT mod; |
220 |
|
{ |
221 |
|
register OBJREC *m; |
240 |
– |
#if MAXLOOP |
241 |
– |
static int depth = 0; |
242 |
– |
/* check for infinite loop */ |
243 |
– |
if (depth++ >= MAXLOOP) |
244 |
– |
objerror(r->ro, USER, "modifier loop"); |
245 |
– |
#endif |
222 |
|
/* execute textures and patterns */ |
223 |
|
for ( ; mod != OVOID; mod = m->omod) { |
224 |
|
m = objptr(mod); |
234 |
|
objerror(r->ro, USER, errmsg); |
235 |
|
} |
236 |
|
} |
261 |
– |
#if MAXLOOP |
262 |
– |
depth--; /* end here */ |
263 |
– |
#endif |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
+ |
int |
241 |
|
raymixture(r, fore, back, coef) /* mix modifiers */ |
242 |
|
register RAY *r; |
243 |
|
OBJECT fore, back; |
254 |
|
/* compute foreground and background */ |
255 |
|
foremat = backmat = 0; |
256 |
|
/* foreground */ |
257 |
< |
copystruct(&fr, r); |
257 |
> |
fr = *r; |
258 |
|
if (coef > FTINY) |
259 |
|
foremat = rayshade(&fr, fore); |
260 |
|
/* background */ |
261 |
< |
copystruct(&br, r); |
261 |
> |
br = *r; |
262 |
|
if (coef < 1.0-FTINY) |
263 |
|
backmat = rayshade(&br, back); |
264 |
|
/* check for transparency */ |
265 |
< |
if (backmat ^ foremat) |
265 |
> |
if (backmat ^ foremat) { |
266 |
|
if (backmat && coef > FTINY) |
267 |
|
raytrans(&fr); |
268 |
|
else if (foremat && coef < 1.0-FTINY) |
269 |
|
raytrans(&br); |
270 |
+ |
} |
271 |
|
/* mix perturbations */ |
272 |
|
for (i = 0; i < 3; i++) |
273 |
|
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
+ |
void |
343 |
|
newrayxf(r) /* get new tranformation matrix for ray */ |
344 |
|
RAY *r; |
345 |
|
{ |
359 |
|
if (rp->rox == &xp->xf) { /* xp in use */ |
360 |
|
xp = xp->next; /* move to next */ |
361 |
|
if (xp == xflast) { /* need new one */ |
362 |
< |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
362 |
> |
xp = (struct xfn *)malloc(sizeof(struct xfn)); |
363 |
|
if (xp == NULL) |
364 |
|
error(SYSTEM, |
365 |
|
"out of memory in newrayxf"); |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
+ |
void |
380 |
|
flipsurface(r) /* reverse surface orientation */ |
381 |
|
register RAY *r; |
382 |
|
{ |
390 |
|
} |
391 |
|
|
392 |
|
|
393 |
+ |
void |
394 |
+ |
rayhit(oset, r) /* standard ray hit test */ |
395 |
+ |
OBJECT *oset; |
396 |
+ |
RAY *r; |
397 |
+ |
{ |
398 |
+ |
OBJREC *o; |
399 |
+ |
int i; |
400 |
+ |
|
401 |
+ |
for (i = oset[0]; i > 0; i--) { |
402 |
+ |
o = objptr(oset[i]); |
403 |
+ |
if ((*ofun[o->otype].funp)(o, r)) |
404 |
+ |
r->robj = oset[i]; |
405 |
+ |
} |
406 |
+ |
} |
407 |
+ |
|
408 |
+ |
|
409 |
+ |
int |
410 |
|
localhit(r, scene) /* check for hit in the octree */ |
411 |
|
register RAY *r; |
412 |
|
register CUBE *scene; |
464 |
|
} |
465 |
|
cxset[0] = 0; |
466 |
|
raymove(curpos, cxset, sflags, r, scene); |
467 |
< |
return(r->ro != NULL & r->ro != &Aftplane); |
467 |
> |
return((r->ro != NULL) & (r->ro != &Aftplane)); |
468 |
|
} |
469 |
|
|
470 |
|
|
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
< |
static |
557 |
> |
static int |
558 |
|
checkhit(r, cu, cxs) /* check for hit in full cube */ |
559 |
|
register RAY *r; |
560 |
|
CUBE *cu; |
561 |
|
OBJECT *cxs; |
562 |
|
{ |
563 |
|
OBJECT oset[MAXSET+1]; |
570 |
– |
register OBJREC *o; |
571 |
– |
register int i; |
564 |
|
|
565 |
|
objset(oset, cu->cutree); |
566 |
< |
checkset(oset, cxs); /* eliminate double-checking */ |
567 |
< |
for (i = oset[0]; i > 0; i--) { |
568 |
< |
o = objptr(oset[i]); |
569 |
< |
if ((*ofun[o->otype].funp)(o, r)) |
570 |
< |
r->robj = oset[i]; |
579 |
< |
} |
580 |
< |
if (r->ro == NULL) |
566 |
> |
checkset(oset, cxs); /* avoid double-checking */ |
567 |
> |
|
568 |
> |
(*r->hitf)(oset, r); /* test for hit in set */ |
569 |
> |
|
570 |
> |
if (r->robj == OVOID) |
571 |
|
return(0); /* no scores yet */ |
572 |
|
|
573 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
< |
static |
577 |
> |
static void |
578 |
|
checkset(os, cs) /* modify checked set and set to check */ |
579 |
|
register OBJECT *os; /* os' = os - cs */ |
580 |
|
register OBJECT *cs; /* cs' = cs + os */ |