18 |
|
|
19 |
|
#include "face.h" |
20 |
|
|
21 |
+ |
#define SRCINC 4 /* realloc increment for array */ |
22 |
|
|
23 |
|
SRCREC *source = NULL; /* our list of sources */ |
24 |
|
int nsources = 0; /* the number of sources */ |
29 |
|
initstypes() /* initialize source dispatch table */ |
30 |
|
{ |
31 |
|
extern VSMATERIAL mirror_vs, direct1_vs, direct2_vs; |
32 |
< |
extern int fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc(); |
32 |
> |
extern int fsetsrc(), ssetsrc(), sphsetsrc(), cylsetsrc(), rsetsrc(); |
33 |
> |
extern int nopart(), flatpart(), cylpart(); |
34 |
|
extern double fgetplaneq(), rgetplaneq(); |
35 |
|
extern double fgetmaxdisk(), rgetmaxdisk(); |
36 |
< |
static SOBJECT fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk}; |
37 |
< |
static SOBJECT ssobj = {ssetsrc}; |
38 |
< |
static SOBJECT sphsobj = {sphsetsrc}; |
39 |
< |
static SOBJECT rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk}; |
36 |
> |
static SOBJECT fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk}; |
37 |
> |
static SOBJECT ssobj = {ssetsrc, nopart}; |
38 |
> |
static SOBJECT sphsobj = {sphsetsrc, nopart}; |
39 |
> |
static SOBJECT cylsobj = {cylsetsrc, cylpart}; |
40 |
> |
static SOBJECT rsobj = {rsetsrc, flatpart, rgetplaneq, rgetmaxdisk}; |
41 |
|
|
42 |
|
sfun[MAT_MIRROR].mf = &mirror_vs; |
43 |
|
sfun[MAT_DIRECT1].mf = &direct1_vs; |
45 |
|
sfun[OBJ_FACE].of = &fsobj; |
46 |
|
sfun[OBJ_SOURCE].of = &ssobj; |
47 |
|
sfun[OBJ_SPHERE].of = &sphsobj; |
48 |
+ |
sfun[OBJ_CYLINDER].of = &cylsobj; |
49 |
|
sfun[OBJ_RING].of = &rsobj; |
50 |
|
} |
51 |
|
|
54 |
|
newsource() /* allocate new source in our array */ |
55 |
|
{ |
56 |
|
if (nsources == 0) |
57 |
< |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
58 |
< |
else |
57 |
> |
source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC)); |
58 |
> |
else if (nsources%SRCINC == 0) |
59 |
|
source = (SRCREC *)realloc((char *)source, |
60 |
< |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
60 |
> |
(unsigned)(nsources+SRCINC)*sizeof(SRCREC)); |
61 |
|
if (source == NULL) |
62 |
|
return(-1); |
63 |
|
source[nsources].sflags = 0; |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
+ |
setflatss(src) /* set sampling for a flat source */ |
71 |
+ |
register SRCREC *src; |
72 |
+ |
{ |
73 |
+ |
double mult; |
74 |
+ |
register int i; |
75 |
+ |
|
76 |
+ |
src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0; |
77 |
+ |
for (i = 0; i < 3; i++) |
78 |
+ |
if (src->snorm[i] < 0.6 && src->snorm[i] > -0.6) |
79 |
+ |
break; |
80 |
+ |
src->ss[SV][i] = 1.0; |
81 |
+ |
fcross(src->ss[SU], src->ss[SV], src->snorm); |
82 |
+ |
mult = .5 * sqrt( src->ss2 / DOT(src->ss[SU],src->ss[SU]) ); |
83 |
+ |
for (i = 0; i < 3; i++) |
84 |
+ |
src->ss[SU][i] *= mult; |
85 |
+ |
fcross(src->ss[SV], src->snorm, src->ss[SU]); |
86 |
+ |
} |
87 |
+ |
|
88 |
+ |
|
89 |
|
fsetsrc(src, so) /* set a face as a source */ |
90 |
|
register SRCREC *src; |
91 |
|
OBJREC *so; |
92 |
|
{ |
93 |
|
register FACE *f; |
94 |
|
register int i, j; |
95 |
+ |
double d; |
96 |
|
|
97 |
|
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
98 |
|
src->so = so; |
109 |
|
objerror(so, USER, "cannot hit center"); |
110 |
|
src->sflags |= SFLAT; |
111 |
|
VCOPY(src->snorm, f->norm); |
88 |
– |
src->ss = sqrt(f->area / PI); |
112 |
|
src->ss2 = f->area; |
113 |
+ |
/* find maximum radius */ |
114 |
+ |
src->srad = 0.; |
115 |
+ |
for (i = 0; i < f->nv; i++) { |
116 |
+ |
d = dist2(VERTEX(f,i), src->sloc); |
117 |
+ |
if (d > src->srad) |
118 |
+ |
src->srad = d; |
119 |
+ |
} |
120 |
+ |
src->srad = sqrt(src->srad); |
121 |
+ |
/* compute size vectors */ |
122 |
+ |
if (f->nv == 4 || (f->nv == 5 && /* parallelogram case */ |
123 |
+ |
dist2(VERTEX(f,0),VERTEX(f,4)) <= FTINY*FTINY)) |
124 |
+ |
for (j = 0; j < 3; j++) { |
125 |
+ |
src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]); |
126 |
+ |
src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]); |
127 |
+ |
} |
128 |
+ |
else |
129 |
+ |
setflatss(src); |
130 |
|
} |
131 |
|
|
132 |
|
|
147 |
|
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
148 |
|
if (theta <= FTINY) |
149 |
|
objerror(so, USER, "zero size"); |
110 |
– |
src->ss = theta >= PI/4.0 ? 1.0 : tan(theta); |
150 |
|
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
151 |
+ |
/* the following is approximate */ |
152 |
+ |
src->srad = sqrt(src->ss2/PI); |
153 |
+ |
VCOPY(src->snorm, src->sloc); |
154 |
+ |
setflatss(src); /* hey, whatever works */ |
155 |
+ |
src->ss[SW][0] = src->ss[SW][1] = src->ss[SW][2] = 0.0; |
156 |
|
} |
157 |
|
|
158 |
|
|
160 |
|
register SRCREC *src; |
161 |
|
register OBJREC *so; |
162 |
|
{ |
163 |
+ |
register int i; |
164 |
+ |
|
165 |
|
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
166 |
|
src->so = so; |
167 |
|
if (so->oargs.nfargs != 4) |
169 |
|
if (so->oargs.farg[3] <= FTINY) |
170 |
|
objerror(so, USER, "illegal radius"); |
171 |
|
VCOPY(src->sloc, so->oargs.farg); |
172 |
< |
src->ss = so->oargs.farg[3]; |
173 |
< |
src->ss2 = PI * src->ss * src->ss; |
172 |
> |
src->srad = so->oargs.farg[3]; |
173 |
> |
src->ss2 = PI * src->srad * src->srad; |
174 |
> |
for (i = 0; i < 3; i++) |
175 |
> |
src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0; |
176 |
> |
for (i = 0; i < 3; i++) |
177 |
> |
src->ss[i][i] = .7236 * so->oargs.farg[3]; |
178 |
|
} |
179 |
|
|
180 |
|
|
193 |
|
objerror(so, USER, "cannot hit center"); |
194 |
|
src->sflags |= SFLAT; |
195 |
|
VCOPY(src->snorm, co->ad); |
196 |
< |
src->ss = CO_R1(co); |
197 |
< |
src->ss2 = PI * src->ss * src->ss; |
196 |
> |
src->srad = CO_R1(co); |
197 |
> |
src->ss2 = PI * src->srad * src->srad; |
198 |
> |
setflatss(src); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
+ |
cylsetsrc(src, so) /* set a cylinder as a source */ |
203 |
+ |
register SRCREC *src; |
204 |
+ |
OBJREC *so; |
205 |
+ |
{ |
206 |
+ |
register CONE *co; |
207 |
+ |
register int i; |
208 |
+ |
|
209 |
+ |
src->sa.success = 4*AIMREQT-1; /* bitch on fourth failure */ |
210 |
+ |
src->so = so; |
211 |
+ |
/* get the cylinder */ |
212 |
+ |
co = getcone(so, 0); |
213 |
+ |
if (CO_R0(co) > .2*co->al) /* heuristic constraint */ |
214 |
+ |
objerror(so, WARNING, "source aspect too small"); |
215 |
+ |
src->sflags |= SCYL; |
216 |
+ |
for (i = 0; i < 3; i++) |
217 |
+ |
src->sloc[i] = .5 * (CO_P1(co)[i] + CO_P0(co)[i]); |
218 |
+ |
src->srad = .5*co->al; |
219 |
+ |
src->ss2 = 2.*CO_R0(co)*co->al; |
220 |
+ |
/* set sampling vectors */ |
221 |
+ |
for (i = 0; i < 3; i++) |
222 |
+ |
src->ss[SU][i] = .5 * co->al * co->ad[i]; |
223 |
+ |
src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0; |
224 |
+ |
for (i = 0; i < 3; i++) |
225 |
+ |
if (co->ad[i] < 0.6 && co->ad[i] > -0.6) |
226 |
+ |
break; |
227 |
+ |
src->ss[SV][i] = 1.0; |
228 |
+ |
fcross(src->ss[SW], src->ss[SV], co->ad); |
229 |
+ |
normalize(src->ss[SW]); |
230 |
+ |
for (i = 0; i < 3; i++) |
231 |
+ |
src->ss[SW][i] *= .8559 * CO_R0(co); |
232 |
+ |
fcross(src->ss[SV], src->ss[SW], co->ad); |
233 |
+ |
} |
234 |
+ |
|
235 |
+ |
|
236 |
|
SPOT * |
237 |
|
makespot(m) /* make a spotlight */ |
238 |
|
register OBJREC *m; |
477 |
|
first = 0; last = nsources-1; |
478 |
|
} |
479 |
|
for (i = first; i <= last; i++) |
480 |
< |
if (source[i].sflags & SDISTANT) |
480 |
> |
if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT) |
481 |
|
/* |
482 |
|
* Check to see if ray is within |
483 |
|
* solid angle of source. |
500 |
|
} |
501 |
|
|
502 |
|
|
503 |
< |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
504 |
< |
r->rsrc>=0 && \ |
505 |
< |
source[r->rsrc].so!=r->ro) |
503 |
> |
/**************************************************************** |
504 |
> |
* The following macros were separated from the m_light() routine |
505 |
> |
* because they are very nasty and difficult to understand. |
506 |
> |
*/ |
507 |
|
|
508 |
+ |
/* wrongillum * |
509 |
+ |
* |
510 |
+ |
* We cannot allow an illum to pass to another illum, because that |
511 |
+ |
* would almost certainly constitute overcounting. |
512 |
+ |
* However, we do allow an illum to pass to another illum |
513 |
+ |
* that is actually going to relay to a virtual light source. |
514 |
+ |
*/ |
515 |
+ |
|
516 |
+ |
#define wrongillum(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
517 |
+ |
objptr(source[r->rsrc].so->omod)->otype==MAT_ILLUM) |
518 |
+ |
|
519 |
+ |
/* wrongsource * |
520 |
+ |
* |
521 |
+ |
* This source is the wrong source (ie. overcounted) if we are |
522 |
+ |
* aimed to a different source than the one we hit and the one |
523 |
+ |
* we hit is not an illum which should be passed. |
524 |
+ |
*/ |
525 |
+ |
|
526 |
+ |
#define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \ |
527 |
+ |
(m->otype!=MAT_ILLUM || wrongillum(m,r))) |
528 |
+ |
|
529 |
+ |
/* distglow * |
530 |
+ |
* |
531 |
+ |
* A distant glow is an object that sometimes acts as a light source, |
532 |
+ |
* but is too far away from the test point to be one in this case. |
533 |
+ |
*/ |
534 |
+ |
|
535 |
+ |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
536 |
+ |
r->rot > m->oargs.farg[3]) |
537 |
+ |
|
538 |
+ |
/* badambient * |
539 |
+ |
* |
540 |
+ |
* We must avoid including counting light sources in the ambient calculation, |
541 |
+ |
* since the direct component is handled separately. Therefore, any |
542 |
+ |
* ambient ray which hits an active light source must be discarded. |
543 |
+ |
*/ |
544 |
+ |
|
545 |
|
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
546 |
< |
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
546 |
> |
!distglow(m, r)) |
547 |
|
|
548 |
+ |
/* passillum * |
549 |
+ |
* |
550 |
+ |
* An illum passes to another material type when we didn't hit it |
551 |
+ |
* on purpose (as part of a direct calculation), or it is relaying |
552 |
+ |
* a virtual light source. |
553 |
+ |
*/ |
554 |
+ |
|
555 |
|
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
556 |
< |
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
556 |
> |
(r->rsrc<0 || source[r->rsrc].so!=r->ro || \ |
557 |
> |
source[r->rsrc].sflags&SVIRTUAL)) |
558 |
|
|
559 |
+ |
/* srcignore * |
560 |
+ |
* |
561 |
+ |
* The -di flag renders light sources invisible, and here is the test. |
562 |
+ |
*/ |
563 |
|
|
564 |
+ |
#define srcignore(m, r) (directinvis && !(r->crtype&SHADOW) && \ |
565 |
+ |
!distglow(m, r)) |
566 |
+ |
|
567 |
+ |
|
568 |
|
m_light(m, r) /* ray hit a light source */ |
569 |
|
register OBJREC *m; |
570 |
|
register RAY *r; |
574 |
|
return; |
575 |
|
/* check for passed illum */ |
576 |
|
if (passillum(m, r)) { |
438 |
– |
|
577 |
|
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
578 |
|
raytrans(r); |
579 |
|
else |
580 |
|
rayshade(r, modifier(m->oargs.sarg[0])); |
581 |
< |
|
582 |
< |
/* otherwise treat as source */ |
583 |
< |
} else { |
581 |
> |
return; |
582 |
> |
} |
583 |
> |
/* otherwise treat as source */ |
584 |
|
/* check for behind */ |
585 |
< |
if (r->rod < 0.0) |
586 |
< |
return; |
585 |
> |
if (r->rod < 0.0) |
586 |
> |
return; |
587 |
> |
/* check for invisibility */ |
588 |
> |
if (srcignore(m, r)) |
589 |
> |
return; |
590 |
|
/* get distribution pattern */ |
591 |
< |
raytexture(r, m->omod); |
591 |
> |
raytexture(r, m->omod); |
592 |
|
/* get source color */ |
593 |
< |
setcolor(r->rcol, m->oargs.farg[0], |
594 |
< |
m->oargs.farg[1], |
595 |
< |
m->oargs.farg[2]); |
593 |
> |
setcolor(r->rcol, m->oargs.farg[0], |
594 |
> |
m->oargs.farg[1], |
595 |
> |
m->oargs.farg[2]); |
596 |
|
/* modify value */ |
597 |
< |
multcolor(r->rcol, r->pcol); |
457 |
< |
} |
597 |
> |
multcolor(r->rcol, r->pcol); |
598 |
|
} |