1 |
– |
/* Copyright (c) 1991 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 |
|
* Routines for light-redirecting materials and |
6 |
|
* their associated virtual light sources |
7 |
|
*/ |
8 |
|
|
9 |
+ |
#include "copyright.h" |
10 |
+ |
|
11 |
|
#include "ray.h" |
12 |
|
|
13 |
|
#include "otypes.h" |
14 |
|
|
15 |
|
#include "source.h" |
16 |
|
|
17 |
+ |
#include "func.h" |
18 |
+ |
|
19 |
|
/* |
20 |
|
* The arguments for MAT_DIRECT1 are: |
21 |
|
* |
31 |
|
*/ |
32 |
|
|
33 |
|
|
34 |
< |
extern double varvalue(); |
34 |
> |
static int dir_proj(); |
35 |
|
|
35 |
– |
int dir_proj(); |
36 |
|
VSMATERIAL direct1_vs = {dir_proj, 1}; |
37 |
|
VSMATERIAL direct2_vs = {dir_proj, 2}; |
38 |
|
|
39 |
+ |
#define getdfunc(m) ( (m)->otype == MAT_DIRECT1 ? \ |
40 |
+ |
getfunc(m, 4, 0xf, 1) : \ |
41 |
+ |
getfunc(m, 8, 0xff, 1) ) |
42 |
|
|
43 |
+ |
|
44 |
+ |
int |
45 |
|
m_direct(m, r) /* shade redirected ray */ |
46 |
|
register OBJREC *m; |
47 |
|
register RAY *r; |
48 |
|
{ |
49 |
|
/* check if source ray */ |
50 |
|
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
51 |
< |
return; /* got the wrong guy */ |
47 |
< |
dir_check(m); |
51 |
> |
return(1); /* got the wrong guy */ |
52 |
|
/* compute first projection */ |
53 |
|
if (m->otype == MAT_DIRECT1 || |
54 |
|
(r->rsrc < 0 || source[r->rsrc].sa.sv.pn == 0)) |
57 |
|
if (m->otype == MAT_DIRECT2 && |
58 |
|
(r->rsrc < 0 || source[r->rsrc].sa.sv.pn == 1)) |
59 |
|
redirect(m, r, 1); |
60 |
+ |
return(1); |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
+ |
int |
65 |
|
redirect(m, r, n) /* compute n'th ray redirection */ |
66 |
|
OBJREC *m; |
67 |
|
RAY *r; |
68 |
|
int n; |
69 |
|
{ |
70 |
< |
register char **sa; |
70 |
> |
MFUNC *mf; |
71 |
> |
register EPNODE **va; |
72 |
> |
FVECT nsdir; |
73 |
|
RAY nr; |
74 |
|
double coef; |
75 |
|
register int j; |
76 |
|
/* set up function */ |
77 |
< |
setmap(m, r, &((FULLXF *)m->os)->b); |
78 |
< |
sa = m->oargs.sarg + 4*n; |
77 |
> |
mf = getdfunc(m); |
78 |
> |
setfunc(m, r); |
79 |
> |
/* assign direction variable */ |
80 |
> |
if (r->rsrc >= 0) { |
81 |
> |
register SRCREC *sp = source + source[r->rsrc].sa.sv.sn; |
82 |
> |
|
83 |
> |
if (sp->sflags & SDISTANT) |
84 |
> |
VCOPY(nsdir, sp->sloc); |
85 |
> |
else { |
86 |
> |
for (j = 0; j < 3; j++) |
87 |
> |
nsdir[j] = sp->sloc[j] - r->rop[j]; |
88 |
> |
normalize(nsdir); |
89 |
> |
} |
90 |
> |
multv3(nsdir, nsdir, funcxf.xfm); |
91 |
> |
varset("DxA", '=', nsdir[0]/funcxf.sca); |
92 |
> |
varset("DyA", '=', nsdir[1]/funcxf.sca); |
93 |
> |
varset("DzA", '=', nsdir[2]/funcxf.sca); |
94 |
> |
} else { |
95 |
> |
varset("DxA", '=', 0.0); |
96 |
> |
varset("DyA", '=', 0.0); |
97 |
> |
varset("DzA", '=', 0.0); |
98 |
> |
} |
99 |
|
/* compute coefficient */ |
100 |
|
errno = 0; |
101 |
< |
coef = varvalue(sa[0]); |
101 |
> |
va = mf->ep + 4*n; |
102 |
> |
coef = evalue(va[0]); |
103 |
|
if (errno) |
104 |
|
goto computerr; |
105 |
|
if (coef <= FTINY || rayorigin(&nr, r, TRANS, coef) < 0) |
106 |
|
return(0); |
107 |
< |
/* compute direction */ |
108 |
< |
errno = 0; |
109 |
< |
for (j = 0; j < 3; j++) |
110 |
< |
nr.rdir[j] = varvalue(sa[j+1]); |
111 |
< |
if (errno) |
112 |
< |
goto computerr; |
113 |
< |
multv3(nr.rdir, nr.rdir, ((FULLXF *)m->os)->f.xfm); |
107 |
> |
va++; /* compute direction */ |
108 |
> |
for (j = 0; j < 3; j++) { |
109 |
> |
nr.rdir[j] = evalue(va[j]); |
110 |
> |
if (errno) |
111 |
> |
goto computerr; |
112 |
> |
} |
113 |
> |
if (mf->f != &unitxf) |
114 |
> |
multv3(nr.rdir, nr.rdir, mf->f->xfm); |
115 |
|
if (r->rox != NULL) |
116 |
|
multv3(nr.rdir, nr.rdir, r->rox->f.xfm); |
117 |
|
if (normalize(nr.rdir) == 0.0) |
122 |
|
rayvalue(&nr); |
123 |
|
scalecolor(nr.rcol, coef); |
124 |
|
addcolor(r->rcol, nr.rcol); |
125 |
+ |
if (r->ro != NULL && isflat(r->ro->otype)) |
126 |
+ |
r->rt = r->rot + nr.rt; |
127 |
|
return(1); |
128 |
|
computerr: |
129 |
|
objerror(m, WARNING, "compute error"); |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
+ |
static int |
135 |
|
dir_proj(pm, o, s, n) /* compute a director's projection */ |
136 |
|
MAT4 pm; |
137 |
|
OBJREC *o; |
139 |
|
int n; |
140 |
|
{ |
141 |
|
RAY tr; |
142 |
< |
register OBJREC *m; |
143 |
< |
char **sa; |
142 |
> |
OBJREC *m; |
143 |
> |
MFUNC *mf; |
144 |
> |
EPNODE **va; |
145 |
|
FVECT cent, newdir, nv, h; |
146 |
< |
double olddot, newdot, od; |
146 |
> |
double coef, olddot, newdot, od; |
147 |
|
register int i, j; |
114 |
– |
/* get material arguments */ |
115 |
– |
m = objptr(o->omod); |
116 |
– |
dir_check(m); |
117 |
– |
sa = m->oargs.sarg + 4*n; |
148 |
|
/* initialize test ray */ |
149 |
|
getmaxdisk(cent, o); |
150 |
|
if (s->sflags & SDISTANT) |
164 |
|
olddot = DOT(tr.rdir, nv); |
165 |
|
if (olddot <= FTINY && olddot >= -FTINY) |
166 |
|
return(0); /* old dir parallels plane */ |
167 |
+ |
tr.rmax = 0.0; |
168 |
|
rayorigin(&tr, NULL, PRIMARY, 1.0); |
169 |
|
if (!(*ofun[o->otype].funp)(o, &tr)) |
170 |
|
return(0); /* no intersection! */ |
171 |
|
/* compute redirection */ |
172 |
< |
setmap(m, &tr, &((FULLXF *)m->os)->b); |
172 |
> |
m = vsmaterial(o); |
173 |
> |
mf = getdfunc(m); |
174 |
> |
setfunc(m, &tr); |
175 |
> |
varset("DxA", '=', 0.0); |
176 |
> |
varset("DyA", '=', 0.0); |
177 |
> |
varset("DzA", '=', 0.0); |
178 |
|
errno = 0; |
179 |
< |
if (varvalue(sa[0]) <= FTINY) |
180 |
< |
return(0); /* insignificant */ |
179 |
> |
va = mf->ep + 4*n; |
180 |
> |
coef = evalue(va[0]); |
181 |
|
if (errno) |
182 |
|
goto computerr; |
183 |
< |
for (i = 0; i < 3; i++) |
184 |
< |
newdir[i] = varvalue(sa[i+1]); |
185 |
< |
if (errno) |
186 |
< |
goto computerr; |
187 |
< |
multv3(newdir, newdir, ((FULLXF *)m->os)->f.xfm); |
183 |
> |
if (coef <= FTINY) |
184 |
> |
return(0); /* insignificant */ |
185 |
> |
va++; |
186 |
> |
for (i = 0; i < 3; i++) { |
187 |
> |
newdir[i] = evalue(va[i]); |
188 |
> |
if (errno) |
189 |
> |
goto computerr; |
190 |
> |
} |
191 |
> |
if (mf->f != &unitxf) |
192 |
> |
multv3(newdir, newdir, mf->f->xfm); |
193 |
|
/* normalization unnecessary */ |
194 |
|
newdot = DOT(newdir, nv); |
195 |
|
if (newdot <= FTINY && newdot >= -FTINY) |
213 |
|
computerr: |
214 |
|
objerror(m, WARNING, "projection compute error"); |
215 |
|
return(0); |
175 |
– |
} |
176 |
– |
|
177 |
– |
|
178 |
– |
static |
179 |
– |
dir_check(m) /* check arguments and load function file */ |
180 |
– |
register OBJREC *m; |
181 |
– |
{ |
182 |
– |
register FULLXF *mxf; |
183 |
– |
register int ff; |
184 |
– |
|
185 |
– |
ff = m->otype == MAT_DIRECT1 ? 5 : 9; |
186 |
– |
if (ff > m->oargs.nsargs) |
187 |
– |
objerror(m, USER, "too few arguments"); |
188 |
– |
if (!vardefined(m->oargs.sarg[0])) |
189 |
– |
loadfunc(m->oargs.sarg[ff-1]); |
190 |
– |
if (m->os == NULL) { |
191 |
– |
mxf = (FULLXF *)malloc(sizeof(FULLXF)); |
192 |
– |
if (mxf == NULL) |
193 |
– |
error(SYSTEM, "out of memory in dir_check"); |
194 |
– |
if (fullxf(mxf, m->oargs.nsargs-ff, m->oargs.sarg+ff) != |
195 |
– |
m->oargs.nsargs-ff) |
196 |
– |
objerror(m, USER, "bad transform"); |
197 |
– |
if (mxf->f.sca < 0.0) |
198 |
– |
mxf->f.sca = -mxf->f.sca; |
199 |
– |
if (mxf->b.sca < 0.0) |
200 |
– |
mxf->b.sca = -mxf->b.sca; |
201 |
– |
m->os = (char *)mxf; |
202 |
– |
} |
216 |
|
} |