ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_direct.c
Revision: 2.12
Committed: Tue Mar 30 16:13:01 2004 UTC (20 years ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 2.11: +21 -20 lines
Log Message:
Continued ANSIfication. There are only bits and pieces left now.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: m_direct.c,v 2.11 2003/07/27 22:12:03 schorsch Exp $";
3 #endif
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 #include "otypes.h"
13 #include "rtotypes.h"
14 #include "source.h"
15 #include "func.h"
16
17 /*
18 * The arguments for MAT_DIRECT1 are:
19 *
20 * 5+ coef1 dx1 dy1 dz1 funcfile transform..
21 * 0
22 * n A1 A2 .. An
23 *
24 * The arguments for MAT_DIRECT2 are:
25 *
26 * 9+ coef1 dx1 dy1 dz1 coef2 dx2 dy2 dz2 funcfile transform..
27 * 0
28 * n A1 A2 .. An
29 */
30
31 static int redirect(OBJREC *m, RAY *r, int n);
32 static int dir_proj(MAT4 pm, OBJREC *o, SRCREC *s, int n);
33
34 VSMATERIAL direct1_vs = {dir_proj, 1};
35 VSMATERIAL direct2_vs = {dir_proj, 2};
36
37 #define getdfunc(m) ( (m)->otype == MAT_DIRECT1 ? \
38 getfunc(m, 4, 0xf, 1) : \
39 getfunc(m, 8, 0xff, 1) )
40
41
42 extern int
43 m_direct( /* shade redirected ray */
44 register OBJREC *m,
45 register RAY *r
46 )
47 {
48 /* check if source ray */
49 if (r->rsrc >= 0 && source[r->rsrc].so != r->ro)
50 return(1); /* got the wrong guy */
51 /* compute first projection */
52 if (m->otype == MAT_DIRECT1 ||
53 (r->rsrc < 0 || source[r->rsrc].sa.sv.pn == 0))
54 redirect(m, r, 0);
55 /* compute second projection */
56 if (m->otype == MAT_DIRECT2 &&
57 (r->rsrc < 0 || source[r->rsrc].sa.sv.pn == 1))
58 redirect(m, r, 1);
59 return(1);
60 }
61
62
63 static int
64 redirect( /* compute n'th ray redirection */
65 OBJREC *m,
66 RAY *r,
67 int n
68 )
69 {
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 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 va = mf->ep + 4*n;
102 coef = evalue(va[0]);
103 if (errno == EDOM || errno == ERANGE)
104 goto computerr;
105 if (coef <= FTINY || rayorigin(&nr, r, TRANS, coef) < 0)
106 return(0);
107 va++; /* compute direction */
108 for (j = 0; j < 3; j++) {
109 nr.rdir[j] = evalue(va[j]);
110 if (errno == EDOM || errno == ERANGE)
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)
118 goto computerr;
119 /* compute value */
120 if (r->rsrc >= 0)
121 nr.rsrc = source[r->rsrc].sa.sv.sn;
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");
130 return(-1);
131 }
132
133
134 static int
135 dir_proj( /* compute a director's projection */
136 MAT4 pm,
137 OBJREC *o,
138 SRCREC *s,
139 int n
140 )
141 {
142 RAY tr;
143 OBJREC *m;
144 MFUNC *mf;
145 EPNODE **va;
146 FVECT cent, newdir, nv, h;
147 double coef, olddot, newdot, od;
148 register int i, j;
149 /* initialize test ray */
150 getmaxdisk(cent, o);
151 if (s->sflags & SDISTANT)
152 for (i = 0; i < 3; i++) {
153 tr.rdir[i] = -s->sloc[i];
154 tr.rorg[i] = cent[i] - tr.rdir[i];
155 }
156 else {
157 for (i = 0; i < 3; i++) {
158 tr.rdir[i] = cent[i] - s->sloc[i];
159 tr.rorg[i] = s->sloc[i];
160 }
161 if (normalize(tr.rdir) == 0.0)
162 return(0); /* at source! */
163 }
164 od = getplaneq(nv, o);
165 olddot = DOT(tr.rdir, nv);
166 if (olddot <= FTINY && olddot >= -FTINY)
167 return(0); /* old dir parallels plane */
168 tr.rmax = 0.0;
169 rayorigin(&tr, NULL, PRIMARY, 1.0);
170 if (!(*ofun[o->otype].funp)(o, &tr))
171 return(0); /* no intersection! */
172 /* compute redirection */
173 m = vsmaterial(o);
174 mf = getdfunc(m);
175 setfunc(m, &tr);
176 varset("DxA", '=', 0.0);
177 varset("DyA", '=', 0.0);
178 varset("DzA", '=', 0.0);
179 errno = 0;
180 va = mf->ep + 4*n;
181 coef = evalue(va[0]);
182 if (errno == EDOM || errno == ERANGE)
183 goto computerr;
184 if (coef <= FTINY)
185 return(0); /* insignificant */
186 va++;
187 for (i = 0; i < 3; i++) {
188 newdir[i] = evalue(va[i]);
189 if (errno == EDOM || errno == ERANGE)
190 goto computerr;
191 }
192 if (mf->f != &unitxf)
193 multv3(newdir, newdir, mf->f->xfm);
194 /* normalization unnecessary */
195 newdot = DOT(newdir, nv);
196 if (newdot <= FTINY && newdot >= -FTINY)
197 return(0); /* new dir parallels plane */
198 /* everything OK -- compute shear */
199 for (i = 0; i < 3; i++)
200 h[i] = newdir[i]/newdot - tr.rdir[i]/olddot;
201 setident4(pm);
202 for (j = 0; j < 3; j++) {
203 for (i = 0; i < 3; i++)
204 pm[i][j] += nv[i]*h[j];
205 pm[3][j] = -od*h[j];
206 }
207 if ((newdot > 0.0) ^ (olddot > 0.0)) /* add mirroring */
208 for (j = 0; j < 3; j++) {
209 for (i = 0; i < 3; i++)
210 pm[i][j] -= 2.*nv[i]*nv[j];
211 pm[3][j] += 2.*od*nv[j];
212 }
213 return(1);
214 computerr:
215 objerror(m, WARNING, "projection compute error");
216 return(0);
217 }