ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_direct.c
Revision: 2.6
Committed: Thu Sep 7 12:35:21 1995 UTC (28 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +9 -9 lines
Log Message:
made assignment of DxA, DyA, DzA a little more robust

File Contents

# Content
1 /* Copyright (c) 1994 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Routines for light-redirecting materials and
9 * their associated virtual light sources
10 */
11
12 #include "ray.h"
13
14 #include "otypes.h"
15
16 #include "source.h"
17
18 #include "func.h"
19
20 /*
21 * The arguments for MAT_DIRECT1 are:
22 *
23 * 5+ coef1 dx1 dy1 dz1 funcfile transform..
24 * 0
25 * n A1 A2 .. An
26 *
27 * The arguments for MAT_DIRECT2 are:
28 *
29 * 9+ coef1 dx1 dy1 dz1 coef2 dx2 dy2 dz2 funcfile transform..
30 * 0
31 * n A1 A2 .. An
32 */
33
34
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 m_direct(m, r) /* shade redirected ray */
45 register OBJREC *m;
46 register RAY *r;
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 redirect(m, r, n) /* compute n'th ray redirection */
64 OBJREC *m;
65 RAY *r;
66 int n;
67 {
68 MFUNC *mf;
69 register EPNODE **va;
70 FVECT nsdir;
71 RAY nr;
72 double coef;
73 register int j;
74 /* set up function */
75 mf = getdfunc(m);
76 setfunc(m, r);
77 /* assign direction variable */
78 if (r->rsrc >= 0) {
79 register SRCREC *sp = source + source[r->rsrc].sa.sv.sn;
80
81 if (sp->sflags & SDISTANT)
82 VCOPY(nsdir, sp->sloc);
83 else {
84 for (j = 0; j < 3; j++)
85 nsdir[j] = sp->sloc[j] - r->rop[j];
86 normalize(nsdir);
87 }
88 multv3(nsdir, nsdir, funcxf.xfm);
89 varset("DxA", '=', nsdir[0]/funcxf.sca);
90 varset("DyA", '=', nsdir[1]/funcxf.sca);
91 varset("DzA", '=', nsdir[2]/funcxf.sca);
92 } else {
93 varset("DxA", '=', 0.0);
94 varset("DyA", '=', 0.0);
95 varset("DzA", '=', 0.0);
96 }
97 /* compute coefficient */
98 errno = 0;
99 va = mf->ep + 4*n;
100 coef = evalue(va[0]);
101 if (errno)
102 goto computerr;
103 if (coef <= FTINY || rayorigin(&nr, r, TRANS, coef) < 0)
104 return(0);
105 va++; /* compute direction */
106 for (j = 0; j < 3; j++) {
107 nr.rdir[j] = evalue(va[j]);
108 if (errno)
109 goto computerr;
110 }
111 if (mf->f != &unitxf)
112 multv3(nr.rdir, nr.rdir, mf->f->xfm);
113 if (r->rox != NULL)
114 multv3(nr.rdir, nr.rdir, r->rox->f.xfm);
115 if (normalize(nr.rdir) == 0.0)
116 goto computerr;
117 /* compute value */
118 if (r->rsrc >= 0)
119 nr.rsrc = source[r->rsrc].sa.sv.sn;
120 rayvalue(&nr);
121 scalecolor(nr.rcol, coef);
122 addcolor(r->rcol, nr.rcol);
123 return(1);
124 computerr:
125 objerror(m, WARNING, "compute error");
126 return(-1);
127 }
128
129
130 dir_proj(pm, o, s, n) /* compute a director's projection */
131 MAT4 pm;
132 OBJREC *o;
133 SRCREC *s;
134 int n;
135 {
136 RAY tr;
137 OBJREC *m;
138 MFUNC *mf;
139 EPNODE **va;
140 FVECT cent, newdir, nv, h;
141 double coef, olddot, newdot, od;
142 register int i, j;
143 /* initialize test ray */
144 getmaxdisk(cent, o);
145 if (s->sflags & SDISTANT)
146 for (i = 0; i < 3; i++) {
147 tr.rdir[i] = -s->sloc[i];
148 tr.rorg[i] = cent[i] - tr.rdir[i];
149 }
150 else {
151 for (i = 0; i < 3; i++) {
152 tr.rdir[i] = cent[i] - s->sloc[i];
153 tr.rorg[i] = s->sloc[i];
154 }
155 if (normalize(tr.rdir) == 0.0)
156 return(0); /* at source! */
157 }
158 od = getplaneq(nv, o);
159 olddot = DOT(tr.rdir, nv);
160 if (olddot <= FTINY && olddot >= -FTINY)
161 return(0); /* old dir parallels plane */
162 tr.rmax = 0.0;
163 rayorigin(&tr, NULL, PRIMARY, 1.0);
164 if (!(*ofun[o->otype].funp)(o, &tr))
165 return(0); /* no intersection! */
166 /* compute redirection */
167 m = vsmaterial(o);
168 mf = getdfunc(m);
169 setfunc(m, &tr);
170 varset("DxA", '=', 0.0);
171 varset("DyA", '=', 0.0);
172 varset("DzA", '=', 0.0);
173 errno = 0;
174 va = mf->ep + 4*n;
175 coef = evalue(va[0]);
176 if (errno)
177 goto computerr;
178 if (coef <= FTINY)
179 return(0); /* insignificant */
180 va++;
181 for (i = 0; i < 3; i++) {
182 newdir[i] = evalue(va[i]);
183 if (errno)
184 goto computerr;
185 }
186 if (mf->f != &unitxf)
187 multv3(newdir, newdir, mf->f->xfm);
188 /* normalization unnecessary */
189 newdot = DOT(newdir, nv);
190 if (newdot <= FTINY && newdot >= -FTINY)
191 return(0); /* new dir parallels plane */
192 /* everything OK -- compute shear */
193 for (i = 0; i < 3; i++)
194 h[i] = newdir[i]/newdot - tr.rdir[i]/olddot;
195 setident4(pm);
196 for (j = 0; j < 3; j++) {
197 for (i = 0; i < 3; i++)
198 pm[i][j] += nv[i]*h[j];
199 pm[3][j] = -od*h[j];
200 }
201 if (newdot > 0.0 ^ olddot > 0.0) /* add mirroring */
202 for (j = 0; j < 3; j++) {
203 for (i = 0; i < 3; i++)
204 pm[i][j] -= 2.*nv[i]*nv[j];
205 pm[3][j] += 2.*od*nv[j];
206 }
207 return(1);
208 computerr:
209 objerror(m, WARNING, "projection compute error");
210 return(0);
211 }