ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_mirror.c
Revision: 2.24
Committed: Thu May 29 16:42:28 2025 UTC (3 weeks, 1 day ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.23: +18 -7 lines
Log Message:
fix: Updated behavior of "mirror" type to handle indirect transmission, thanks to Jon Sargent

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: m_mirror.c,v 2.23 2023/11/15 18:02:53 greg Exp $";
3 #endif
4 /*
5 * Routines for mirror material supporting virtual light sources
6 */
7
8 #include "copyright.h"
9
10 #include "ray.h"
11 #include "otypes.h"
12 #include "otspecial.h"
13 #include "rtotypes.h"
14 #include "source.h"
15
16 /*
17 * The real arguments for MAT_MIRROR are simply:
18 *
19 * 3 rrefl grefl brefl
20 *
21 * Additionally, the user may specify a single string argument
22 * which is interpreted as the name of the material to use
23 * instead of the mirror if the ray being considered is not
24 * part of the direct calculation.
25 */
26
27 static int mir_proj(MAT4 pm, OBJREC *o, SRCREC *s, int n);
28 static void mirrorproj(MAT4 m, FVECT nv, double offs);
29
30 VSMATERIAL mirror_vs = {mir_proj, 1};
31
32
33 int
34 m_mirror( /* shade mirrored ray */
35 OBJREC *m,
36 RAY *r
37 )
38 {
39 SCOLOR mcolor;
40 RAY nr;
41 int rpure = 1;
42 int i;
43 /* check arguments */
44 if (m->oargs.nfargs != 3 || m->oargs.nsargs > 1)
45 objerror(m, USER, "bad number of arguments");
46 /* check for substitute material */
47 /* but avoid double-counting */
48 if (m->oargs.nsargs > 0 &&
49 (r->rsrc < 0 || source[r->rsrc].so != r->ro)) {
50 int passOK = (r->rod < 0.) |
51 !(r->crtype & (AMBIENT|SPECULAR));
52 if (strcmp(m->oargs.sarg[0], VOIDID)) {
53 OBJECT altmod = lastmod(objndx(m), m->oargs.sarg[0]);
54 OBJREC *altmat;
55 if (passOK) /* no double-count hazard? */
56 return(rayshade(r, altmod));
57 if (altmod == OVOID || /* else check alternate type */
58 (altmat = findmaterial(objptr(altmod))) == NULL)
59 return(0);
60 if (istransp(altmat)) /* pass "transparent" materials */
61 return(rayshade(r, altmod));
62 } else if (passOK) {
63 raytrans(r); /* "safe" void passage */
64 return(1);
65 }
66 }
67 /* check for bad source ray */
68 if (r->rsrc >= 0 && source[r->rsrc].so != r->ro)
69 return(1);
70
71 if (r->rod < 0.) { /* back is black */
72 if (!backvis)
73 raytrans(r); /* unless back visibility is off */
74 return(1);
75 }
76 /* get modifiers */
77 raytexture(r, m->omod);
78 /* assign material color */
79 setscolor(mcolor, m->oargs.farg[0],
80 m->oargs.farg[1],
81 m->oargs.farg[2]);
82 smultscolor(mcolor, r->pcol);
83 /* compute reflected ray */
84 if (r->rsrc >= 0) { /* relayed light source */
85 rayorigin(&nr, REFLECTED, r, mcolor);
86 /* ignore textures */
87 for (i = 0; i < 3; i++)
88 nr.rdir[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
89 /* source we're aiming for next */
90 nr.rsrc = source[r->rsrc].sa.sv.sn;
91 } else { /* ordinary reflection */
92 FVECT pnorm;
93 double pdot;
94
95 if (rayorigin(&nr, REFLECTED, r, mcolor) < 0)
96 return(1);
97 if (!(r->crtype & AMBIENT) &&
98 DOT(r->pert,r->pert) > FTINY*FTINY) {
99 pdot = raynormal(pnorm, r); /* use textures */
100 for (i = 0; i < 3; i++)
101 nr.rdir[i] = r->rdir[i] + 2.*pdot*pnorm[i];
102 rpure = 0;
103 }
104 /* check for penetration */
105 if (rpure || DOT(nr.rdir, r->ron) <= FTINY)
106 for (i = 0; i < 3; i++)
107 nr.rdir[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
108 }
109 checknorm(nr.rdir);
110 rayvalue(&nr);
111 smultscolor(nr.rcol, nr.rcoef);
112 copyscolor(r->mcol, nr.rcol);
113 saddscolor(r->rcol, nr.rcol);
114 r->rmt = r->rot;
115 if (rpure && r->ro != NULL && isflat(r->ro->otype))
116 r->rmt += raydistance(&nr);
117 return(1);
118 }
119
120
121 static int
122 mir_proj( /* compute a mirror's projection */
123 MAT4 pm,
124 OBJREC *o,
125 SRCREC *s,
126 int n
127 )
128 {
129 double corr = 1.;
130 FVECT nv, sc;
131 double od, offs;
132 int i;
133 /* get surface normal and offset */
134 offs = od = getplaneq(nv, o);
135 if (s->sflags & SDISTANT)
136 offs = 0.;
137 /* check for extreme point behind */
138 if (s->sflags & SCIR) {
139 if (s->sflags & (SFLAT|SDISTANT))
140 corr = 1.12837917; /* correct setflatss() */
141 else
142 corr = 1.0/0.7236; /* correct sphsetsrc() */
143 }
144 VCOPY(sc, s->sloc);
145 for (i = s->sflags & SFLAT ? SV : SW; i >= 0; i--)
146 if (DOT(nv, s->ss[i]) > offs)
147 VSUM(sc, sc, s->ss[i], corr);
148 else
149 VSUM(sc, sc, s->ss[i], -corr);
150 if (DOT(sc, nv) <= offs+FTINY)
151 return(0);
152 /* everything OK -- compute projection */
153 mirrorproj(pm, nv, od);
154 return(1);
155 }
156
157
158 static void
159 mirrorproj( /* get mirror projection for surface */
160 MAT4 m,
161 FVECT nv,
162 double offs
163 )
164 {
165 int i, j;
166 /* assign matrix */
167 setident4(m);
168 for (j = 0; j < 3; j++) {
169 for (i = 0; i < 3; i++)
170 m[i][j] -= 2.*nv[i]*nv[j];
171 m[3][j] = 2.*offs*nv[j];
172 }
173 }