14 |
|
|
15 |
|
#include "octree.h" |
16 |
|
|
17 |
– |
#include "source.h" |
18 |
– |
|
17 |
|
#include "otypes.h" |
18 |
|
|
19 |
< |
#include "cone.h" |
19 |
> |
#include "source.h" |
20 |
|
|
23 |
– |
#include "face.h" |
24 |
– |
|
21 |
|
#include "random.h" |
22 |
|
|
23 |
+ |
/* |
24 |
+ |
* Structures used by direct() |
25 |
+ |
*/ |
26 |
|
|
27 |
< |
extern double dstrsrc; /* source distribution amount */ |
28 |
< |
extern double shadthresh; /* relative shadow threshold */ |
29 |
< |
extern double shadcert; /* shadow testing certainty */ |
27 |
> |
typedef struct { |
28 |
> |
FVECT dir; /* source direction */ |
29 |
> |
COLOR coef; /* material coefficient */ |
30 |
> |
COLOR val; /* contribution */ |
31 |
> |
} CONTRIB; /* direct contribution */ |
32 |
|
|
33 |
< |
SRCREC *source = NULL; /* our list of sources */ |
34 |
< |
int nsources = 0; /* the number of sources */ |
33 |
> |
typedef struct { |
34 |
> |
int sno; /* source number */ |
35 |
> |
float brt; /* brightness (for comparison) */ |
36 |
> |
} CNTPTR; /* contribution pointer */ |
37 |
|
|
38 |
|
static CONTRIB *srccnt; /* source contributions in direct() */ |
39 |
|
static CNTPTR *cntord; /* source ordering in direct() */ |
43 |
|
{ |
44 |
|
int i; |
45 |
|
register OBJREC *o, *m; |
46 |
< |
register SRCREC *ns; |
47 |
< |
|
46 |
> |
register int ns; |
47 |
> |
/* initialize dispatch table */ |
48 |
> |
initstypes(); |
49 |
> |
/* find direct sources */ |
50 |
|
for (i = 0; i < nobjects; i++) { |
51 |
|
|
52 |
|
o = objptr(i); |
53 |
|
|
54 |
< |
if (o->omod == OVOID) |
54 |
> |
if (!issurface(o->otype) || o->omod == OVOID) |
55 |
|
continue; |
56 |
|
|
57 |
|
m = objptr(o->omod); |
68 |
|
m->oargs.farg[3] <= FTINY) |
69 |
|
continue; /* don't bother */ |
70 |
|
|
71 |
< |
if ((ns = newsource()) == NULL) |
71 |
> |
if (sfun[o->otype].of == NULL || |
72 |
> |
sfun[o->otype].of->setsrc == NULL) |
73 |
> |
objerror(o, USER, "illegal material"); |
74 |
> |
|
75 |
> |
if ((ns = newsource()) < 0) |
76 |
|
goto memerr; |
77 |
|
|
78 |
< |
setsource(ns, o); |
78 |
> |
setsource(&source[ns], o); |
79 |
|
|
80 |
|
if (m->otype == MAT_GLOW) { |
81 |
< |
ns->sflags |= SPROX; |
82 |
< |
ns->sl.prox = m->oargs.farg[3]; |
81 |
> |
source[ns].sflags |= SPROX; |
82 |
> |
source[ns].sl.prox = m->oargs.farg[3]; |
83 |
|
if (o->otype == OBJ_SOURCE) |
84 |
< |
ns->sflags |= SSKIP; |
84 |
> |
source[ns].sflags |= SSKIP; |
85 |
|
} else if (m->otype == MAT_SPOT) { |
86 |
< |
ns->sflags |= SSPOT; |
87 |
< |
if ((ns->sl.s = makespot(m)) == NULL) |
86 |
> |
source[ns].sflags |= SSPOT; |
87 |
> |
if ((source[ns].sl.s = makespot(m)) == NULL) |
88 |
|
goto memerr; |
89 |
+ |
if (source[ns].sflags & SFLAT && |
90 |
+ |
!checkspot(source[ns].sl.s,source[ns].snorm)) { |
91 |
+ |
objerror(o, WARNING, |
92 |
+ |
"invalid spotlight direction"); |
93 |
+ |
source[ns].sflags |= SSKIP; |
94 |
+ |
} |
95 |
|
} |
96 |
|
} |
97 |
|
if (nsources <= 0) { |
101 |
|
markvirtuals(); /* find and add virtual sources */ |
102 |
|
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
103 |
|
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
104 |
< |
if (srccnt != NULL && cntord != NULL) |
104 |
> |
if (srccnt == NULL || cntord == NULL) |
105 |
|
goto memerr; |
106 |
|
return; |
107 |
|
memerr: |
109 |
|
} |
110 |
|
|
111 |
|
|
97 |
– |
SRCREC * |
98 |
– |
newsource() /* allocate new source in our array */ |
99 |
– |
{ |
100 |
– |
if (nsources == 0) |
101 |
– |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
102 |
– |
else |
103 |
– |
source = (SRCREC *)realloc((char *)source, |
104 |
– |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
105 |
– |
if (source == NULL) |
106 |
– |
return(NULL); |
107 |
– |
source[nsources].sflags = 0; |
108 |
– |
source[nsources].nhits = 1; |
109 |
– |
source[nsources].ntests = 2; /* initial hit probability = 1/2 */ |
110 |
– |
return(&source[nsources++]); |
111 |
– |
} |
112 |
– |
|
113 |
– |
|
114 |
– |
setsource(src, so) /* add a source to the array */ |
115 |
– |
register SRCREC *src; |
116 |
– |
register OBJREC *so; |
117 |
– |
{ |
118 |
– |
double cos(), tan(), sqrt(); |
119 |
– |
double theta; |
120 |
– |
FACE *f; |
121 |
– |
CONE *co; |
122 |
– |
int j; |
123 |
– |
register int i; |
124 |
– |
|
125 |
– |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
126 |
– |
src->so = so; |
127 |
– |
|
128 |
– |
switch (so->otype) { |
129 |
– |
case OBJ_SOURCE: |
130 |
– |
if (so->oargs.nfargs != 4) |
131 |
– |
objerror(so, USER, "bad arguments"); |
132 |
– |
src->sflags |= SDISTANT; |
133 |
– |
VCOPY(src->sloc, so->oargs.farg); |
134 |
– |
if (normalize(src->sloc) == 0.0) |
135 |
– |
objerror(so, USER, "zero direction"); |
136 |
– |
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
137 |
– |
if (theta <= FTINY) |
138 |
– |
objerror(so, USER, "zero size"); |
139 |
– |
src->ss = theta >= PI/4 ? 1.0 : tan(theta); |
140 |
– |
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
141 |
– |
break; |
142 |
– |
case OBJ_SPHERE: |
143 |
– |
VCOPY(src->sloc, so->oargs.farg); |
144 |
– |
src->ss = so->oargs.farg[3]; |
145 |
– |
src->ss2 = PI * src->ss * src->ss; |
146 |
– |
break; |
147 |
– |
case OBJ_FACE: |
148 |
– |
/* get the face */ |
149 |
– |
f = getface(so); |
150 |
– |
/* find the center */ |
151 |
– |
for (j = 0; j < 3; j++) { |
152 |
– |
src->sloc[j] = 0.0; |
153 |
– |
for (i = 0; i < f->nv; i++) |
154 |
– |
src->sloc[j] += VERTEX(f,i)[j]; |
155 |
– |
src->sloc[j] /= (double)f->nv; |
156 |
– |
} |
157 |
– |
if (!inface(src->sloc, f)) |
158 |
– |
objerror(so, USER, "cannot hit center"); |
159 |
– |
src->sflags |= SFLAT; |
160 |
– |
VCOPY(src->snorm, f->norm); |
161 |
– |
src->ss = sqrt(f->area / PI); |
162 |
– |
src->ss2 = f->area; |
163 |
– |
break; |
164 |
– |
case OBJ_RING: |
165 |
– |
/* get the ring */ |
166 |
– |
co = getcone(so, 0); |
167 |
– |
VCOPY(src->sloc, CO_P0(co)); |
168 |
– |
if (CO_R0(co) > 0.0) |
169 |
– |
objerror(so, USER, "cannot hit center"); |
170 |
– |
src->sflags |= SFLAT; |
171 |
– |
VCOPY(src->snorm, co->ad); |
172 |
– |
src->ss = CO_R1(co); |
173 |
– |
src->ss2 = PI * src->ss * src->ss; |
174 |
– |
break; |
175 |
– |
default: |
176 |
– |
objerror(so, USER, "illegal material"); |
177 |
– |
} |
178 |
– |
} |
179 |
– |
|
180 |
– |
|
181 |
– |
SPOT * |
182 |
– |
makespot(m) /* make a spotlight */ |
183 |
– |
register OBJREC *m; |
184 |
– |
{ |
185 |
– |
extern double cos(); |
186 |
– |
register SPOT *ns; |
187 |
– |
|
188 |
– |
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
189 |
– |
return(NULL); |
190 |
– |
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
191 |
– |
VCOPY(ns->aim, m->oargs.farg+4); |
192 |
– |
if ((ns->flen = normalize(ns->aim)) == 0.0) |
193 |
– |
objerror(m, USER, "zero focus vector"); |
194 |
– |
return(ns); |
195 |
– |
} |
196 |
– |
|
197 |
– |
|
112 |
|
double |
113 |
|
srcray(sr, r, sn) /* send a ray to a source, return domega */ |
114 |
|
register RAY *sr; /* returned source ray */ |
128 |
|
sr->rsrc = sn; /* remember source */ |
129 |
|
/* get source direction */ |
130 |
|
if (source[sn].sflags & SDISTANT) { |
217 |
– |
if (source[sn].sflags & SSPOT) { /* check location */ |
218 |
– |
for (i = 0; i < 3; i++) |
219 |
– |
vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i]; |
220 |
– |
d = DOT(source[sn].sloc,vd); |
221 |
– |
d = DOT(vd,vd) - d*d; |
222 |
– |
if (PI*d > source[sn].sl.s->siz) |
223 |
– |
return(0.0); |
224 |
– |
} |
131 |
|
/* constant direction */ |
132 |
|
VCOPY(sr->rdir, source[sn].sloc); |
133 |
|
} else { /* compute direction */ |
144 |
|
for (i = 0; i < 3; i++) { |
145 |
|
dimlist[ndims] = i + 8831; |
146 |
|
vd[i] = dstrsrc * source[sn].ss * |
147 |
< |
(1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx)); |
147 |
> |
(1.0 - 2.0*urand(urind(ilhash(dimlist,ndims+1),samplendx))); |
148 |
|
} |
149 |
|
ndims--; |
150 |
|
if (source[sn].sflags & SFLAT) { /* project offset */ |
154 |
|
} |
155 |
|
for (i = 0; i < 3; i++) /* offset source direction */ |
156 |
|
sr->rdir[i] += vd[i]; |
157 |
+ |
/* normalize */ |
158 |
+ |
d = normalize(sr->rdir); |
159 |
|
|
160 |
< |
} else if (source[sn].sflags & SDISTANT) |
161 |
< |
/* already normalized */ |
162 |
< |
return(source[sn].ss2); |
160 |
> |
} else if (!(source[sn].sflags & SDISTANT)) |
161 |
> |
/* normalize direction */ |
162 |
> |
d = normalize(sr->rdir); |
163 |
|
|
164 |
< |
if ((d = normalize(sr->rdir)) == 0.0) |
165 |
< |
/* at source! */ |
164 |
> |
if (source[sn].sflags & SDISTANT) { |
165 |
> |
if (source[sn].sflags & SSPOT) { /* check location */ |
166 |
> |
for (i = 0; i < 3; i++) |
167 |
> |
vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i]; |
168 |
> |
d = DOT(sr->rdir,vd); |
169 |
> |
d = DOT(vd,vd) - d*d; |
170 |
> |
if (PI*d > source[sn].sl.s->siz) |
171 |
> |
return(0.0); |
172 |
> |
} |
173 |
> |
return(source[sn].ss2); /* domega constant */ |
174 |
> |
} |
175 |
> |
/* check direction */ |
176 |
> |
if (d == 0.0) |
177 |
|
return(0.0); |
259 |
– |
|
260 |
– |
if (source[sn].sflags & SDISTANT) |
261 |
– |
/* domega constant */ |
262 |
– |
return(source[sn].ss2); |
263 |
– |
|
178 |
|
/* check proximity */ |
179 |
|
if (source[sn].sflags & SPROX && |
180 |
|
d > source[sn].sl.prox) |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
< |
sourcehit(r) /* check to see if ray hit distant source */ |
200 |
< |
register RAY *r; |
199 |
> |
srcvalue(r) /* punch ray to source and compute value */ |
200 |
> |
RAY *r; |
201 |
|
{ |
202 |
< |
int first, last; |
289 |
< |
register int i; |
202 |
> |
register SRCREC *sp; |
203 |
|
|
204 |
< |
if (r->rsrc >= 0) { /* check only one if aimed */ |
205 |
< |
first = last = r->rsrc; |
206 |
< |
} else { /* otherwise check all */ |
207 |
< |
first = 0; last = nsources-1; |
204 |
> |
sp = &source[r->rsrc]; |
205 |
> |
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
206 |
> |
/* check intersection */ |
207 |
> |
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
208 |
> |
return; |
209 |
> |
raycont(r); /* compute contribution */ |
210 |
> |
return; |
211 |
|
} |
212 |
< |
for (i = first; i <= last; i++) |
213 |
< |
if (source[i].sflags & SDISTANT) |
214 |
< |
/* |
215 |
< |
* Check to see if ray is within |
216 |
< |
* solid angle of source. |
217 |
< |
*/ |
218 |
< |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
303 |
< |
<= source[i].ss2) { |
304 |
< |
r->ro = source[i].so; |
305 |
< |
if (!(source[i].sflags & SSKIP)) |
306 |
< |
break; |
307 |
< |
} |
308 |
< |
|
309 |
< |
if (r->ro != NULL) { |
310 |
< |
for (i = 0; i < 3; i++) |
311 |
< |
r->ron[i] = -r->rdir[i]; |
312 |
< |
r->rod = 1.0; |
313 |
< |
r->rox = NULL; |
314 |
< |
return(1); |
212 |
> |
/* compute intersection */ |
213 |
> |
if (sp->sflags & SDISTANT ? sourcehit(r) : |
214 |
> |
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
215 |
> |
if (sp->sa.success >= 0) |
216 |
> |
sp->sa.success++; |
217 |
> |
raycont(r); /* compute contribution */ |
218 |
> |
return; |
219 |
|
} |
220 |
< |
return(0); |
220 |
> |
if (sp->sa.success < 0) |
221 |
> |
return; /* bitched already */ |
222 |
> |
sp->sa.success -= AIMREQT; |
223 |
> |
if (sp->sa.success >= 0) |
224 |
> |
return; /* leniency */ |
225 |
> |
sprintf(errmsg, "aiming failure for light source \"%s\"", |
226 |
> |
sp->so->oname); |
227 |
> |
error(WARNING, errmsg); /* issue warning */ |
228 |
|
} |
229 |
|
|
230 |
|
|
271 |
|
cntord[sn].brt = bright(srccnt[sn].coef); |
272 |
|
if (cntord[sn].brt <= 0.0) |
273 |
|
continue; |
274 |
< |
/* compute contribution */ |
275 |
< |
srcvalue(&sr); |
274 |
> |
/* compute potential */ |
275 |
> |
sr.revf = srcvalue; |
276 |
> |
rayvalue(&sr); |
277 |
|
copycolor(srccnt[sn].val, sr.rcol); |
278 |
|
multcolor(srccnt[sn].val, srccnt[sn].coef); |
279 |
|
cntord[sn].brt = bright(srccnt[sn].val); |
341 |
|
(double)source[cntord[sn].sno].ntests; |
342 |
|
scalecolor(srccnt[cntord[sn].sno].val, prob); |
343 |
|
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
432 |
– |
} |
433 |
– |
} |
434 |
– |
|
435 |
– |
|
436 |
– |
srcvalue(r) /* punch ray to source and compute value */ |
437 |
– |
RAY *r; |
438 |
– |
{ |
439 |
– |
register SRCREC *sp; |
440 |
– |
|
441 |
– |
sp = &source[r->rsrc]; |
442 |
– |
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
443 |
– |
RAY nr; |
444 |
– |
/* check intersection */ |
445 |
– |
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
446 |
– |
return; |
447 |
– |
/* relay ray to source */ |
448 |
– |
vsrcrelay(&nr, r); |
449 |
– |
srcvalue(&nr); |
450 |
– |
return; |
451 |
– |
} |
452 |
– |
/* compute intersection */ |
453 |
– |
if (sp->sflags & SDISTANT ? sourcehit(r) : |
454 |
– |
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
455 |
– |
if (sp->sa.success >= 0) |
456 |
– |
sp->sa.success++; |
457 |
– |
raycont(r); /* compute contribution */ |
458 |
– |
return; |
459 |
– |
} |
460 |
– |
if (sp->sa.success < 0) |
461 |
– |
return; /* bitched already */ |
462 |
– |
sp->sa.success -= AIMREQT; |
463 |
– |
if (sp->sa.success >= 0) |
464 |
– |
return; /* leniency */ |
465 |
– |
sprintf(errmsg, "aiming failure for light source \"%s\"", |
466 |
– |
sp->so->oname); |
467 |
– |
error(WARNING, errmsg); /* issue warning */ |
468 |
– |
} |
469 |
– |
|
470 |
– |
|
471 |
– |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
472 |
– |
r->rsrc>=0 && \ |
473 |
– |
source[r->rsrc].so!=r->ro) |
474 |
– |
|
475 |
– |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
476 |
– |
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
477 |
– |
|
478 |
– |
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
479 |
– |
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
480 |
– |
|
481 |
– |
|
482 |
– |
m_light(m, r) /* ray hit a light source */ |
483 |
– |
register OBJREC *m; |
484 |
– |
register RAY *r; |
485 |
– |
{ |
486 |
– |
/* check for over-counting */ |
487 |
– |
if (wrongsource(m, r) || badambient(m, r)) |
488 |
– |
return; |
489 |
– |
/* check for passed illum */ |
490 |
– |
if (passillum(m, r)) { |
491 |
– |
|
492 |
– |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
493 |
– |
raytrans(r); |
494 |
– |
else |
495 |
– |
rayshade(r, modifier(m->oargs.sarg[0])); |
496 |
– |
|
497 |
– |
/* otherwise treat as source */ |
498 |
– |
} else { |
499 |
– |
/* check for behind */ |
500 |
– |
if (r->rod < 0.0) |
501 |
– |
return; |
502 |
– |
/* get distribution pattern */ |
503 |
– |
raytexture(r, m->omod); |
504 |
– |
/* get source color */ |
505 |
– |
setcolor(r->rcol, m->oargs.farg[0], |
506 |
– |
m->oargs.farg[1], |
507 |
– |
m->oargs.farg[2]); |
508 |
– |
/* modify value */ |
509 |
– |
multcolor(r->rcol, r->pcol); |
344 |
|
} |
345 |
|
} |