1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Routines for mirror material supporting virtual light sources |
9 |
*/ |
10 |
|
11 |
#include "ray.h" |
12 |
|
13 |
#include "otypes.h" |
14 |
|
15 |
#include "source.h" |
16 |
|
17 |
/* |
18 |
* The real arguments for MAT_MIRROR are simply: |
19 |
* |
20 |
* 3 rrefl grefl brefl |
21 |
* |
22 |
* Additionally, the user may specify a single string argument |
23 |
* which is interpreted as the name of the material to use |
24 |
* instead of the mirror if the ray being considered is not |
25 |
* part of the direct calculation. |
26 |
*/ |
27 |
|
28 |
|
29 |
int mir_proj(); |
30 |
VSMATERIAL mirror_vs = {mir_proj, 1}; |
31 |
|
32 |
|
33 |
m_mirror(m, r) /* shade mirrored ray */ |
34 |
register OBJREC *m; |
35 |
register RAY *r; |
36 |
{ |
37 |
COLOR mcolor; |
38 |
RAY nr; |
39 |
register int i; |
40 |
/* check arguments */ |
41 |
if (m->oargs.nfargs != 3 || m->oargs.nsargs > 1) |
42 |
objerror(m, USER, "bad number of arguments"); |
43 |
/* check for substitute material */ |
44 |
if (m->oargs.nsargs > 0 && |
45 |
(r->rsrc < 0 || source[r->rsrc].so != r->ro)) { |
46 |
rayshade(r, modifier(m->oargs.sarg[0])); |
47 |
return; |
48 |
} |
49 |
/* check for bad source ray */ |
50 |
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
51 |
return; |
52 |
|
53 |
if (r->rod < 0.) /* back is black */ |
54 |
return; |
55 |
/* get modifiers */ |
56 |
raytexture(r, m->omod); |
57 |
/* assign material color */ |
58 |
setcolor(mcolor, m->oargs.farg[0], |
59 |
m->oargs.farg[1], |
60 |
m->oargs.farg[2]); |
61 |
multcolor(mcolor, r->pcol); |
62 |
/* compute reflected ray */ |
63 |
if (r->rsrc >= 0) { /* relayed light source */ |
64 |
rayorigin(&nr, r, REFLECTED, 1.); |
65 |
/* ignore textures */ |
66 |
for (i = 0; i < 3; i++) |
67 |
nr.rdir[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
68 |
/* source we're aiming for next */ |
69 |
nr.rsrc = source[r->rsrc].sa.sv.sn; |
70 |
} else { /* ordinary reflection */ |
71 |
FVECT pnorm; |
72 |
double pdot; |
73 |
|
74 |
if (rayorigin(&nr, r, REFLECTED, bright(mcolor)) < 0) |
75 |
return; |
76 |
pdot = raynormal(pnorm, r); /* use textures */ |
77 |
for (i = 0; i < 3; i++) |
78 |
nr.rdir[i] = r->rdir[i] + 2.*pdot*pnorm[i]; |
79 |
/* check for penetration */ |
80 |
if (DOT(nr.rdir, r->ron) <= FTINY) |
81 |
for (i = 0; i < 3; i++) |
82 |
nr.rdir[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
83 |
} |
84 |
rayvalue(&nr); |
85 |
multcolor(nr.rcol, mcolor); |
86 |
addcolor(r->rcol, nr.rcol); |
87 |
} |
88 |
|
89 |
|
90 |
mir_proj(pm, o, s, n) /* compute a mirror's projection */ |
91 |
MAT4 pm; |
92 |
register OBJREC *o; |
93 |
SRCREC *s; |
94 |
int n; |
95 |
{ |
96 |
FVECT nv; |
97 |
double od; |
98 |
/* get surface normal and offset */ |
99 |
od = getplaneq(nv, o); |
100 |
/* check for behind */ |
101 |
if (DOT(s->sloc, nv) <= (s->sflags & SDISTANT ? FTINY : od+FTINY)) |
102 |
return(0); |
103 |
/* everything OK -- compute projection */ |
104 |
mirrorproj(pm, nv, od); |
105 |
return(1); |
106 |
} |
107 |
|
108 |
|
109 |
mirrorproj(m, nv, offs) /* get mirror projection for surface */ |
110 |
register MAT4 m; |
111 |
FVECT nv; |
112 |
double offs; |
113 |
{ |
114 |
register int i, j; |
115 |
/* assign matrix */ |
116 |
setident4(m); |
117 |
for (j = 0; j < 3; j++) { |
118 |
for (i = 0; i < 3; i++) |
119 |
m[i][j] -= 2.*nv[i]*nv[j]; |
120 |
m[3][j] = 2.*offs*nv[j]; |
121 |
} |
122 |
} |